Skip to content

Commit

Permalink
refactor: ipc status command
Browse files Browse the repository at this point in the history
  • Loading branch information
go-compile committed Jul 22, 2023
1 parent e23a633 commit c98f83c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
39 changes: 19 additions & 20 deletions cmd/localrelay/ipcClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
"net/url"
"strconv"

"github.com/pkg/errors"
)

// commandDataIPC will send a command with a data section
Expand Down Expand Up @@ -68,31 +70,28 @@ func serviceRun(relays []string) error {
}

func serviceStatus() (*status, error) {
// conn, err := IPCConnect()
// if err != nil {
// return nil, err
// }

// defer conn.Close()
client, conn, err := IPCConnect()
if err != nil {
return nil, err
}

// _, err = conn.Write([]byte{0, 3, daemonStatus, 0, 0})
// if err != nil {
// return nil, err
// }
defer conn.Close()

// payload, err := readCommand(conn)
// if err != nil {
// return nil, err
// }
resp, err := client.Get("http://lr/status")
if err != nil {
return nil, err
}

// var s status
// if err := json.Unmarshal(payload, &s); err != nil {
// return nil, err
// }
if resp.StatusCode != 200 {
return nil, errors.New("failed to fetch status")
}

// return &s, nil
var status status
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return nil, err
}

return nil, nil
return &status, nil
}

func stopRelay(relayName string) error {
Expand Down
28 changes: 28 additions & 0 deletions cmd/localrelay/ipcServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func assignIPCRoutes(r *router.Router) {
r.GET("/", ipcRouteRoot)
r.GET("/stop/{relay}", ipcRouteStop)
r.POST("/run", ipcRouteRun)
r.GET("/status", ipcRouteStatus)
}

func ipcHeadersMiddleware(handler fasthttp.RequestHandler) fasthttp.RequestHandler {
Expand Down Expand Up @@ -141,3 +142,30 @@ func ipcRouteRun(ctx *fasthttp.RequestCtx) {
ctx.Write([]byte(`{"message":"Relay successfully launched."}`))
return
}

func ipcRouteStatus(ctx *fasthttp.RequestCtx) {
relayMetrics := make(map[string]metrics)

relays := runningRelaysCopy()
for _, r := range relays {
active, total := r.Metrics.Connections()
relayMetrics[r.Name] = metrics{
In: r.Metrics.Download(),
Out: r.Metrics.Upload(),
Active: active,
DialAvg: r.DialerAvg(),
TotalConns: total,
TotalRequests: r.Metrics.Requests(),
}
}

ctx.SetStatusCode(200)
json.NewEncoder(ctx).Encode(&status{
Relays: relays,
Pid: os.Getpid(),
Version: VERSION,
Started: daemonStarted.Unix(),

Metrics: relayMetrics,
})
}
1 change: 0 additions & 1 deletion cmd/localrelay/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func relayStatus() error {
// carriage returns
s, err := serviceStatus()
if err != nil {

fmt.Printf("Daemon: \x1b[31m [OFFLINE] \x1b[0m\r\n")
fmt.Println(err)

Expand Down

0 comments on commit c98f83c

Please sign in to comment.