Skip to content

Commit

Permalink
feat: implemented disconect --all flag
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Nov 4, 2021
1 parent c0ff7f6 commit 164637b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 7 additions & 1 deletion bin/vproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ func connectVhost(c *cli.Context) error {

func disconnectVhost(c *cli.Context) error {
hostname := c.Args().First()
all := c.Bool("all")

if !all && hostname == "" {
return fmt.Errorf("missing hostname or --all")
}

client := createClient(c)
client.RemoveVhost(hostname, c.Bool("all"))
client.RemoveVhost(hostname, all)
return nil
}

Expand Down
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func (c *Client) RemoveVhost(hostname string, all bool) {
defer res.Body.Close()
r := bufio.NewReader(res.Body)
b, _ := ioutil.ReadAll(r)
fmt.Println(string(b))
s := strings.TrimSpace(string(b))
if s != "" {
fmt.Println(s)
}
}

// IsDaemonRunning tries to check if a vproxy daemon is already running on the given addr
Expand Down
26 changes: 22 additions & 4 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -240,14 +241,31 @@ func (d *Daemon) relayLogsUntilClose(vhost *Vhost, w http.ResponseWriter, reqCtx

func (d *Daemon) removeVhost(w http.ResponseWriter, r *http.Request) {
hostname := r.PostFormValue("host")
vhost := d.loggedHandler.GetVhost(hostname)
if vhost == nil {
fmt.Fprintf(w, "error: host '%s' not found", hostname)
all, _ := strconv.ParseBool(r.PostFormValue("all"))

if all {
for _, vhost := range d.loggedHandler.vhostMux.Servers {
d.doRemoveVhost(vhost, w)
}

} else if hostname != "" {
vhost := d.loggedHandler.GetVhost(hostname)
if vhost == nil {
fmt.Fprintf(w, "error: host '%s' not found", hostname)
return
}
d.doRemoveVhost(vhost, w)

} else {
fmt.Fprint(w, "error: missing hostname")
return
}

}

func (d *Daemon) doRemoveVhost(vhost *Vhost, w http.ResponseWriter) {
fmt.Printf("[*] removing vhost: %s -> %d\n", vhost.Host, vhost.Port)
fmt.Fprintf(w, "removing vhost: %s -> %d", vhost.Host, vhost.Port)
fmt.Fprintf(w, "removing vhost: %s -> %d\n", vhost.Host, vhost.Port)
vhost.Close()
d.loggedHandler.RemoveVhost(vhost.Host)
}
Expand Down

0 comments on commit 164637b

Please sign in to comment.