diff --git a/cmd/localrelay/args.go b/cmd/localrelay/args.go index 881d6e0..0abe3bb 100644 --- a/cmd/localrelay/args.go +++ b/cmd/localrelay/args.go @@ -246,6 +246,7 @@ func help() { fmt.Println(" localrelay ips") fmt.Println(" localrelay drop") fmt.Println(" localrelay dropip ") + fmt.Println(" localrelay droprelay ") fmt.Println(" localrelay stop") fmt.Println(" localrelay stop ") fmt.Println(" localrelay restart") diff --git a/cmd/localrelay/conns.go b/cmd/localrelay/conns.go index 4aebe80..97b8140 100644 --- a/cmd/localrelay/conns.go +++ b/cmd/localrelay/conns.go @@ -122,3 +122,26 @@ func dropConnsIP(opt *options) error { return dropIP(opt.commands[1]) } + +func dropConnsRelay(opt *options) error { + // make terminal raw to allow the use of colour on windows terminals + current, _ := console.ConsoleFromFile(os.Stdout) + // NOTE: Docker healthchecks will panic "provided file is not a console" + + if current != nil { + defer current.Reset() + } + + if current != nil { + if err := current.SetRaw(); err != nil { + log.Println(err) + } + } + + if len(opt.commands) < 2 { + fmt.Println("Provide a relay name.") + return nil + } + + return dropRelay(opt.commands[1]) +} diff --git a/cmd/localrelay/ipcClient.go b/cmd/localrelay/ipcClient.go index deec0a9..b195ed5 100644 --- a/cmd/localrelay/ipcClient.go +++ b/cmd/localrelay/ipcClient.go @@ -160,3 +160,26 @@ func dropIP(ip string) error { return nil } + +func dropRelay(relay string) error { + client, conn, err := IPCConnect() + if err != nil { + return err + } + + defer conn.Close() + + resp, err := client.Get("http://lr/drop/relay/" + url.PathEscape(relay)) + if err != nil { + return err + } + + switch resp.StatusCode { + case 200: + fmt.Printf("All connections from %q have been dropped.\r\n", relay) + default: + fmt.Printf("Failed to drop connections. Status code: %d.\n", resp.StatusCode) + } + + return nil +} diff --git a/cmd/localrelay/ipcServer.go b/cmd/localrelay/ipcServer.go index 95d8067..703ed19 100644 --- a/cmd/localrelay/ipcServer.go +++ b/cmd/localrelay/ipcServer.go @@ -39,8 +39,7 @@ func assignIPCRoutes(r *router.Router) { r.GET("/connections", ipcRouteConns) r.GET("/drop", ipcRouteDropAll) r.GET("/drop/ip/{ip}", ipcRouteDropIP) - // r.GET("/drop/relay/{}", ipcRouteDropRelay) - // r.GET("/drop/addr/{}", ipcRouteDropAddr) + r.GET("/drop/relay/{relay}", ipcRouteDropRelay) } func ipcHeadersMiddleware(handler fasthttp.RequestHandler) fasthttp.RequestHandler { @@ -230,3 +229,19 @@ func ipcRouteDropIP(ctx *fasthttp.RequestCtx) { } } } + +func ipcRouteDropRelay(ctx *fasthttp.RequestCtx) { + relay := ctx.UserValue("relay").(string) + + relays := runningRelaysCopy() + // iterate through all relays and close every connection + for _, r := range relays { + if r.Name != relay { + continue + } + + for _, conn := range r.GetConns() { + go conn.Conn.Close() + } + } +} diff --git a/cmd/localrelay/main.go b/cmd/localrelay/main.go index 45f737e..3f0eef1 100644 --- a/cmd/localrelay/main.go +++ b/cmd/localrelay/main.go @@ -170,6 +170,11 @@ func main() { fmt.Println(err) } return + case "droprelay": + if err := dropConnsRelay(opt); err != nil { + fmt.Println(err) + } + return default: fmt.Printf("Unrecognised command %q\n", opt.commands[i]) return