Skip to content

Commit

Permalink
feat(cli): add drop relay conns command
Browse files Browse the repository at this point in the history
  • Loading branch information
go-compile committed Jul 22, 2023
1 parent f362bb2 commit a749fa3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/localrelay/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func help() {
fmt.Println(" localrelay ips")
fmt.Println(" localrelay drop")
fmt.Println(" localrelay dropip <ip>")
fmt.Println(" localrelay droprelay <relay>")
fmt.Println(" localrelay stop")
fmt.Println(" localrelay stop <relay>")
fmt.Println(" localrelay restart")
Expand Down
23 changes: 23 additions & 0 deletions cmd/localrelay/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
23 changes: 23 additions & 0 deletions cmd/localrelay/ipcClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 17 additions & 2 deletions cmd/localrelay/ipcServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}
5 changes: 5 additions & 0 deletions cmd/localrelay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a749fa3

Please sign in to comment.