Skip to content

Commit 8e1dbef

Browse files
committed
Upgrading the status cmd
1 parent c62f318 commit 8e1dbef

File tree

4 files changed

+113
-21
lines changed

4 files changed

+113
-21
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
git.rootprojects.org/root/go-gitver/v2 v2.0.2
77
github.com/coreos/go-semver v0.3.1
88
github.com/creack/pty v1.1.20
9-
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5
109
github.com/google/uuid v1.3.1
1110
github.com/gorilla/websocket v1.4.2
1211
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
@@ -30,6 +29,7 @@ require (
3029
github.com/benbjohnson/clock v1.3.0 // indirect
3130
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
3231
github.com/davecgh/go-spew v1.1.1 // indirect
32+
github.com/dchest/uniuri v1.2.0 // indirect
3333
github.com/go-ole/go-ole v1.2.6 // indirect
3434
github.com/google/go-cmp v0.5.9 // indirect
3535
github.com/kr/pretty v0.3.1 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/creack/pty v1.1.20/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
1717
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1818
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1919
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
20-
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 h1:RAV05c0xOkJ3dZGS0JFybxFKZ2WMLabgx3uXnd7rpGs=
21-
github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4=
20+
github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g=
21+
github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY=
2222
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2323
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
2424
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=

sock.go

+72-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"runtime"
1515
"strings"
1616
"sync"
17+
"text/tabwriter"
1718
"time"
1819

1920
"github.com/dchest/uniuri"
@@ -41,6 +42,15 @@ type LiveOffer struct {
4142
p *peers.Peer
4243
id string
4344
}
45+
type CandidatePairValues struct {
46+
FP string `json:"fp"`
47+
LocalAddr string `json:"local_addr"` // IP:Port
48+
LocalProtocol string `json:"local_proto"`
49+
LocalType string `json:"local_type"`
50+
RemoteAddr string `json:"remote_addr"`
51+
RemoteProtocol string `json:"remote_proto"`
52+
RemoteType string `json:"remote_type"`
53+
}
4454

4555
const socketFileName = "webexec.sock"
4656

@@ -194,19 +204,63 @@ func StartSocketServer(lc fx.Lifecycle, s *sockServer, params SocketStartParams)
194204
}
195205

196206
func (s *sockServer) handleStatus(w http.ResponseWriter, r *http.Request) {
197-
if r.Method == "GET" {
198-
w.Write([]byte("READY"))
207+
Logger.Info("Got status request")
208+
if r.Method != "GET" {
209+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
210+
return
199211
}
200-
/* TODO: return status of all connected peers
201212
if len(peers.Peers) == 0 {
202213
fmt.Println("No peers connected")
203-
} else {
204-
fmt.Println("Connected peers:")
205-
for _, peer := range peers.Peers {
206-
fmt.Printf(" %s", peer.FP)
214+
return
215+
}
216+
var ret []CandidatePairValues
217+
fmt.Println("Connected peers:")
218+
for _, peer := range peers.Peers {
219+
if peer.PC == nil {
220+
continue
207221
}
222+
stats := peer.PC.GetStats()
223+
for _, report := range stats {
224+
pairStats, ok := report.(webrtc.ICECandidatePairStats)
225+
if !ok || pairStats.Type != webrtc.StatsTypeCandidatePair {
226+
continue
227+
}
228+
// check if it is selected
229+
if pairStats.State != webrtc.StatsICECandidatePairStateSucceeded {
230+
continue
231+
}
232+
local, ok := stats[pairStats.LocalCandidateID].(webrtc.ICECandidateStats)
233+
if !ok {
234+
http.Error(w, "Failed to get local candidate", http.StatusInternalServerError)
235+
return
236+
}
237+
remote, ok := stats[pairStats.RemoteCandidateID].(webrtc.ICECandidateStats)
238+
if !ok {
239+
http.Error(w, "Failed to get remote candidate", http.StatusInternalServerError)
240+
return
241+
}
242+
ret = append(ret, CandidatePairValues{
243+
FP: peer.FP,
244+
LocalAddr: fmt.Sprintf("%s:%d", local.IP, local.Port),
245+
LocalProtocol: local.Protocol,
246+
LocalType: local.CandidateType.String(),
247+
RemoteAddr: fmt.Sprintf("%s:%d", remote.IP, remote.Port),
248+
RemoteProtocol: remote.Protocol,
249+
RemoteType: remote.CandidateType.String(),
250+
})
251+
break
252+
}
253+
}
254+
if ret == nil {
255+
// no peers are connected, return an empty response
256+
ret = []CandidatePairValues{}
257+
}
258+
b, err := json.Marshal(ret)
259+
if err != nil {
260+
http.Error(w, "Failed to marshal response", http.StatusInternalServerError)
261+
return
208262
}
209-
*/
263+
w.Write(b)
210264
}
211265
func (s *sockServer) handleLayout(w http.ResponseWriter, r *http.Request) {
212266
if r.Method == "GET" {
@@ -401,3 +455,13 @@ func writeClipboard(data []byte, mimeType string) error {
401455
}
402456
return cmd.Wait()
403457
}
458+
459+
// const ELLIPSIS = "\uf141"
460+
func writeICEPairsHeader(w *tabwriter.Writer) {
461+
fmt.Fprintln(w, "Finger\uf141\tAddr\tProtocol\tType\t\uf141\tAddr\tProtocol\tType")
462+
}
463+
func (p *CandidatePairValues) Write(w *tabwriter.Writer) {
464+
fp := fmt.Sprintf("%s\uf141", string([]rune(p.FP)[:6]))
465+
fmt.Fprintln(w, strings.Join([]string{fp, p.LocalAddr, p.LocalProtocol,
466+
p.LocalType, "->", p.RemoteAddr, p.RemoteProtocol, p.RemoteType}, "\t"))
467+
}

webexec.go

+38-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919
"sync"
2020
"syscall"
21+
"text/tabwriter"
2122
"time"
2223

2324
"github.com/coreos/go-semver/semver"
@@ -345,6 +346,17 @@ func restart(c *cli.Context) error {
345346
return start(c)
346347
}
347348

349+
// newSocketClient creates a new http client for the unix socket
350+
func newSocketClient() http.Client {
351+
return http.Client{
352+
Transport: &http.Transport{
353+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
354+
return net.Dial("unix", GetSockFP())
355+
},
356+
},
357+
}
358+
}
359+
348360
// accept function accepts offers to connect
349361
func accept(c *cli.Context) error {
350362
certs, err := GetCerts()
@@ -355,7 +367,7 @@ func accept(c *cli.Context) error {
355367
if err != nil {
356368
return err
357369
}
358-
fp := GetSockFP()
370+
359371
pid, err := getAgentPid()
360372
if err != nil {
361373
return err
@@ -373,13 +385,7 @@ func accept(c *cli.Context) error {
373385
return fmt.Errorf("Failed to fork agent: %s", err)
374386
}
375387
}
376-
httpc := http.Client{
377-
Transport: &http.Transport{
378-
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
379-
return net.Dial("unix", fp)
380-
},
381-
},
382-
}
388+
httpc := newSocketClient()
383389
// First get the agent's status and print to the clist
384390
var msg string
385391
var r *http.Response
@@ -523,7 +529,7 @@ func getAgentPid() (int, error) {
523529
}
524530
return pidf.Read()
525531
}
526-
func status(c *cli.Context) error {
532+
func statusCMD(c *cli.Context) error {
527533
pid, err := getAgentPid()
528534
if err != nil {
529535
return err
@@ -540,6 +546,28 @@ func status(c *cli.Context) error {
540546
} else {
541547
fmt.Printf("Fingerprint: %s\n", fp)
542548
}
549+
httpc := newSocketClient()
550+
resp, err := httpc.Get("http://unix/status")
551+
if err != nil {
552+
return fmt.Errorf("Failed to get the agent's status: %s", err)
553+
}
554+
var pairs []CandidatePairValues
555+
// read the response into pair usin json.NewDecoder
556+
err = json.NewDecoder(resp.Body).Decode(&pairs)
557+
if err != nil {
558+
if err == io.EOF {
559+
fmt.Println("No connected peers")
560+
return nil
561+
}
562+
return fmt.Errorf("Failed to decode the agent's status: %s", err)
563+
}
564+
fmt.Println("\nConnected peers:")
565+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
566+
writeICEPairsHeader(w)
567+
for _, pair := range pairs {
568+
pair.Write(w)
569+
}
570+
w.Flush()
543571
return nil
544572
}
545573
func initCMD(c *cli.Context) error {
@@ -767,7 +795,7 @@ func main() {
767795
}, {
768796
Name: "status",
769797
Usage: "webexec agent's status",
770-
Action: status,
798+
Action: statusCMD,
771799
}, {
772800
Name: "stop",
773801
Usage: "stop the user's agent",

0 commit comments

Comments
 (0)