From fd482d6510aa8297c2732c1769c90e6f2239a8b1 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sun, 13 Aug 2023 21:22:58 +0100 Subject: [PATCH] Report MPTCP state through admin socket --- src/admin/getpeers.go | 18 ++++++++++-------- src/core/api.go | 2 ++ src/core/link_tcp_mptcp_go121.go | 18 +++++++++++++++++- src/core/link_tcp_mptcp_other.go | 4 ++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/admin/getpeers.go b/src/admin/getpeers.go index c6cd5be48..b6893f794 100644 --- a/src/admin/getpeers.go +++ b/src/admin/getpeers.go @@ -24,6 +24,7 @@ type PeerEntry struct { PublicKey string `json:"key"` Port uint64 `json:"port"` Priority uint64 `json:"priority"` + Multipath bool `json:"multipath"` RXBytes DataUnit `json:"bytes_recvd,omitempty"` TXBytes DataUnit `json:"bytes_sent,omitempty"` Uptime float64 `json:"uptime,omitempty"` @@ -36,14 +37,15 @@ func (a *AdminSocket) getPeersHandler(req *GetPeersRequest, res *GetPeersRespons res.Peers = make([]PeerEntry, 0, len(peers)) for _, p := range peers { peer := PeerEntry{ - Port: p.Port, - Up: p.Up, - Inbound: p.Inbound, - Priority: uint64(p.Priority), // can't be uint8 thanks to gobind - URI: p.URI, - RXBytes: DataUnit(p.RXBytes), - TXBytes: DataUnit(p.TXBytes), - Uptime: p.Uptime.Seconds(), + Port: p.Port, + Up: p.Up, + Inbound: p.Inbound, + Priority: uint64(p.Priority), // can't be uint8 thanks to gobind + Multipath: p.Multipath, + URI: p.URI, + RXBytes: DataUnit(p.RXBytes), + TXBytes: DataUnit(p.TXBytes), + Uptime: p.Uptime.Seconds(), } if addr := address.AddrForKey(p.Key); addr != nil { peer.PublicKey = hex.EncodeToString(p.Key) diff --git a/src/core/api.go b/src/core/api.go index ebc818f53..204368dfc 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -31,6 +31,7 @@ type PeerInfo struct { Coords []uint64 Port uint64 Priority uint8 + Multipath bool RXBytes uint64 TXBytes uint64 Uptime time.Duration @@ -87,6 +88,7 @@ func (c *Core) GetPeers() []PeerInfo { peerinfo.RXBytes = atomic.LoadUint64(&c.rx) peerinfo.TXBytes = atomic.LoadUint64(&c.tx) peerinfo.Uptime = time.Since(c.up) + peerinfo.Multipath = isMPTCP(c) } if p, ok := conns[conn]; ok { peerinfo.Key = p.Key diff --git a/src/core/link_tcp_mptcp_go121.go b/src/core/link_tcp_mptcp_go121.go index 545273625..ccb928fb2 100644 --- a/src/core/link_tcp_mptcp_go121.go +++ b/src/core/link_tcp_mptcp_go121.go @@ -3,7 +3,10 @@ package core -import "net" +import ( + "crypto/tls" + "net" +) func setMPTCPForDialer(d *net.Dialer) { d.SetMultipathTCP(true) @@ -12,3 +15,16 @@ func setMPTCPForDialer(d *net.Dialer) { func setMPTCPForListener(lc *net.ListenConfig) { lc.SetMultipathTCP(true) } + +func isMPTCP(c net.Conn) bool { + switch tc := c.(type) { + case *net.TCPConn: + mp, _ := tc.MultipathTCP() + return mp + case *tls.Conn: + mp, _ := tc.NetConn().(*net.TCPConn).MultipathTCP() + return mp + default: + return false + } +} diff --git a/src/core/link_tcp_mptcp_other.go b/src/core/link_tcp_mptcp_other.go index 28bc46c58..600558d96 100644 --- a/src/core/link_tcp_mptcp_other.go +++ b/src/core/link_tcp_mptcp_other.go @@ -12,3 +12,7 @@ func setMPTCPForDialer(d *net.Dialer) { func setMPTCPForListener(lc *net.ListenConfig) { // Not supported on versions under Go 1.21 } + +func isMPTCP(c net.Conn) bool { + return false +}