Skip to content

Commit

Permalink
Returning version and peers on status
Browse files Browse the repository at this point in the history
  • Loading branch information
daonb committed Jun 3, 2024
1 parent c9bd573 commit a1a74cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
17 changes: 6 additions & 11 deletions sock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type CandidatePairValues struct {
RemoteProtocol string `json:"remote_proto"`
RemoteType string `json:"remote_type"`
}
type StatusMessage struct {
Version string `json:"version"`
Peers []CandidatePairValues `json:"peers,omitempty"`
}

const socketFileName = "webexec.sock"

Expand Down Expand Up @@ -209,12 +213,7 @@ func (s *sockServer) handleStatus(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if len(peers.Peers) == 0 {
fmt.Println("No peers connected")
return
}
var ret []CandidatePairValues
fmt.Println("Connected peers:")
ret := StatusMessage{Version: version}
for _, peer := range peers.Peers {
if peer.PC == nil {
continue
Expand All @@ -239,7 +238,7 @@ func (s *sockServer) handleStatus(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Failed to get remote candidate", http.StatusInternalServerError)
return
}
ret = append(ret, CandidatePairValues{
ret.Peers = append(ret.Peers, CandidatePairValues{
FP: peer.FP,
LocalAddr: fmt.Sprintf("%s:%d", local.IP, local.Port),
LocalProtocol: local.Protocol,
Expand All @@ -251,10 +250,6 @@ func (s *sockServer) handleStatus(w http.ResponseWriter, r *http.Request) {
break
}
}
if ret == nil {
// no peers are connected, return an empty response
ret = []CandidatePairValues{}
}
b, err := json.Marshal(ret)
if err != nil {
http.Error(w, "Failed to marshal response", http.StatusInternalServerError)
Expand Down
16 changes: 12 additions & 4 deletions webexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func statusCMD(c *cli.Context) error {
if err != nil {
return fmt.Errorf("Failed to get the agent's status: %v", err)
}
var pairs []CandidatePairValues
var stats StatusMessage
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Failed to read the agent's status: %v", err)
Expand All @@ -579,7 +579,7 @@ func statusCMD(c *cli.Context) error {
fmt.Println("Running agent is of an older version, please `webexec restart`")
return nil
}
err = json.Unmarshal(body, &pairs)
err = json.Unmarshal(body, &stats)
if err != nil {
if err == io.EOF {
fmt.Println("No connected peers")
Expand All @@ -589,11 +589,19 @@ func statusCMD(c *cli.Context) error {
fmt.Printf("Failed to decode the agent's status:\n%s\n", body)
return fmt.Errorf("Failed to decode the agent's status: %s", err)
}
label("Agent version")
fmt.Print(": ")
value("%s\n", stats.Version)
label("Connected peers")
if len(stats.Peers) == 0 {
fmt.Print(": ")
header(os.Stdout, "None\n")
return nil
}
fmt.Println(":")
w := tabwriter.NewWriter(os.Stdout, 0, 3, 1, ' ', 0)
header(w, " FP\tADDRESS\tPROTO\tTYPE\t |\tADDRESS\tPROT\tTYPE\n")
for _, pair := range pairs {
header(w, " FP\tADDRESS\tPROTO\tTYPE\t ||\tADDRESS\tPROT\tTYPE\n")
for _, pair := range stats.Peers {
pair.Write(w)
}
w.Flush()
Expand Down

0 comments on commit a1a74cf

Please sign in to comment.