Skip to content

Commit c0ff7f6

Browse files
committed
feat: implemented disconnect command
1 parent b7fd330 commit c0ff7f6

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ _testmain.go
3030
dist/
3131
.vproxy.conf
3232
t/
33+
*.pprof

bin/vproxy/flags.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ vproxy connect hello.local:8888 -- vproxy hello
9898
Name: "connect",
9999
Aliases: []string{"client", "c", "add"},
100100
Usage: "Add a new vhost",
101-
Action: startClient,
101+
Action: connectVhost,
102102
Before: loadClientConfig,
103103
Flags: []cli.Flag{
104104
&cli.StringFlag{
@@ -121,6 +121,30 @@ vproxy connect hello.local:8888 -- vproxy hello
121121
},
122122
},
123123
},
124+
{
125+
Name: "disconnect",
126+
Aliases: []string{"remove"},
127+
Usage: "Remove vhost",
128+
Action: disconnectVhost,
129+
Before: loadClientConfig,
130+
UsageText: `vproxy disconnect [command options] <hostname>`,
131+
Flags: []cli.Flag{
132+
&cli.StringFlag{
133+
Name: "host",
134+
Value: "127.0.0.1",
135+
Usage: "Daemon host IP",
136+
},
137+
&cli.IntFlag{
138+
Name: "http",
139+
Value: 80,
140+
Usage: "Daemon HTTP port",
141+
},
142+
&cli.BoolFlag{
143+
Name: "all",
144+
Usage: "Remove all vhosts",
145+
},
146+
},
147+
},
124148
{
125149
Name: "tail",
126150
Aliases: []string{"stream", "attach"},

bin/vproxy/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func printVersion(c *cli.Context) error {
3737
return nil
3838
}
3939

40-
func startClient(c *cli.Context) error {
40+
func connectVhost(c *cli.Context) error {
4141

4242
// collect and validate binds
4343
args := c.Args().Slice()
@@ -74,6 +74,13 @@ func startClient(c *cli.Context) error {
7474
return nil
7575
}
7676

77+
func disconnectVhost(c *cli.Context) error {
78+
hostname := c.Args().First()
79+
client := createClient(c)
80+
client.RemoveVhost(hostname, c.Bool("all"))
81+
return nil
82+
}
83+
7784
func createClient(c *cli.Context) *vproxy.Client {
7885
host := c.String("host")
7986
httpPort := c.Int("http")

client.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"os/signal"
13+
"strconv"
1314
"strings"
1415
"sync"
1516
)
@@ -71,7 +72,7 @@ func (c *Client) addBinding(bind string, detach bool) {
7172

7273
s := strings.Split(bind, ":")
7374
if len(s) >= 2 {
74-
fmt.Printf("[*] registering vhost: %s -> https://%s\n", bind, s[0])
75+
fmt.Printf("[*] registering vhost: https://%s -> %s\n", s[0], bind)
7576
} else {
7677
fmt.Println("[*] registering vhost:", bind)
7778
}
@@ -94,8 +95,7 @@ func (c *Client) Tail(hostname string, follow bool) {
9495
data.Add("host", hostname)
9596
res, err := http.DefaultClient.PostForm(c.uri("/clients/stream"), data)
9697
if err != nil {
97-
stopCommand(c.cmd)
98-
log.Fatalf("error registering client: %s\n", err)
98+
log.Fatalf("error: %s\n", err)
9999
}
100100
fmt.Printf("[*] streaming logs for %s\n", hostname)
101101
streamLogs(res, follow)
@@ -126,6 +126,20 @@ func streamLogs(res *http.Response, follow bool) {
126126
}
127127
}
128128

129+
func (c *Client) RemoveVhost(hostname string, all bool) {
130+
data := url.Values{}
131+
data.Add("host", hostname)
132+
data.Add("all", strconv.FormatBool(all))
133+
res, err := http.DefaultClient.PostForm(c.uri("/clients/remove"), data)
134+
if err != nil {
135+
log.Fatalf("error: %s\n", err)
136+
}
137+
defer res.Body.Close()
138+
r := bufio.NewReader(res.Body)
139+
b, _ := ioutil.ReadAll(r)
140+
fmt.Println(string(b))
141+
}
142+
129143
// IsDaemonRunning tries to check if a vproxy daemon is already running on the given addr
130144
func (c *Client) IsDaemonRunning() bool {
131145
res, err := http.DefaultClient.Get(c.uri("/hello"))

daemon.go

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (d *Daemon) Run() {
110110
d.loggedHandler.HandleFunc("/_vproxy/clients", d.listClients)
111111
d.loggedHandler.HandleFunc("/_vproxy/clients/add", d.registerVhost)
112112
d.loggedHandler.HandleFunc("/_vproxy/clients/stream", d.streamLogs)
113+
d.loggedHandler.HandleFunc("/_vproxy/clients/remove", d.removeVhost)
113114
d.wg.Add(1) // ensure we don't exit immediately
114115

115116
if d.enableHTTP() {
@@ -237,6 +238,20 @@ func (d *Daemon) relayLogsUntilClose(vhost *Vhost, w http.ResponseWriter, reqCtx
237238
}
238239
}
239240

241+
func (d *Daemon) removeVhost(w http.ResponseWriter, r *http.Request) {
242+
hostname := r.PostFormValue("host")
243+
vhost := d.loggedHandler.GetVhost(hostname)
244+
if vhost == nil {
245+
fmt.Fprintf(w, "error: host '%s' not found", hostname)
246+
return
247+
}
248+
249+
fmt.Printf("[*] removing vhost: %s -> %d\n", vhost.Host, vhost.Port)
250+
fmt.Fprintf(w, "removing vhost: %s -> %d", vhost.Host, vhost.Port)
251+
vhost.Close()
252+
d.loggedHandler.RemoveVhost(vhost.Host)
253+
}
254+
240255
// addVhost for the given binding to the LoggedHandler
241256
func (d *Daemon) addVhost(binding string, w http.ResponseWriter) *Vhost {
242257
vhost, err := CreateVhost(binding, d.enableTLS())

0 commit comments

Comments
 (0)