From 034c06d46d18be2c68933b4592a514caf0f6a4da Mon Sep 17 00:00:00 2001 From: Adam <37312852+Aptimex@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:22:27 -0700 Subject: [PATCH] Status improvements (#70) * add localhost IP to status * concurrent status API requests --- src/cmd/serve.go | 2 +- src/cmd/status.go | 91 +++++++++++++++++++++++++++++----------------- src/peer/config.go | 9 ++++- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/cmd/serve.go b/src/cmd/serve.go index 35f4c46..d8bc20b 100644 --- a/src/cmd/serve.go +++ b/src/cmd/serve.go @@ -342,6 +342,7 @@ func (c serveCmdConfig) Run() { }, }, Addresses: relayAddresses, + LocalhostIP: viper.GetString("Relay.Interface.LocalhostIP"), } configRelay, err := peer.GetConfig(configRelayArgs) @@ -598,7 +599,6 @@ func configureLocalhostForwarding(localhostAddr netip.Addr, s *stack.Stack) { // Adds a rule to the start of a table chain. func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook) (stack.Table) { insertIndex := int(table.BuiltinChains[chain]) - fmt.Printf("Inserting rule into index %d\n", insertIndex) table.Rules = slices.Insert(table.Rules, insertIndex, newRule) // Increment the later chain and underflow index pointers to account for the rule added to the Rules slice diff --git a/src/cmd/status.go b/src/cmd/status.go index 7415ad3..160c231 100644 --- a/src/cmd/status.go +++ b/src/cmd/status.go @@ -19,6 +19,16 @@ type statusCmdConfig struct { configFileE2EE string } +// Represents one Server or Client in tree +type Node struct { + peerConfig peer.PeerConfig + relayConfig peer.Config + e2eeConfig peer.Config + children []*Node + interfaces []api.HostInterface + error string +} + // Defaults for status command. // See root command for shared defaults. var statusCmd = statusCmdConfig{ @@ -50,16 +60,6 @@ func init() { // Run attempts to parse config files into a network diagram. func (cc statusCmdConfig) Run() { - // Start building tree. - type Node struct { - peerConfig peer.PeerConfig - relayConfig peer.Config - e2eeConfig peer.Config - children []*Node - interfaces []api.HostInterface - error string - } - var err error // Parse the relay and e2ee config files @@ -81,32 +81,21 @@ func (cc statusCmdConfig) Run() { nodes := make(map[string]Node) var errorNodes []Node e2ee_peer_list := client.e2eeConfig.GetPeers() + nodeChannel := make(chan Node) for _, ep := range e2ee_peer_list { - relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort))) - if err != nil { - errorNodes = append(errorNodes, Node{ - peerConfig: ep, - error: err.Error(), - }) + // Make all the API requests concurrently to speed things up + go cc.makeAPIRequests(nodeChannel, ep) + } - } else { - var interfaces []api.HostInterface - if cc.networkInfo { - interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort))) - if err != nil { - interfaces = append(interfaces, api.HostInterface{ - Name: "ERROR: " + err.Error(), - }) - } - } + // Don't need to do anything with values, just need to loop the same number of times + for range e2ee_peer_list { + responseNode := <- nodeChannel - nodes[relayConfig.GetPublicKey()] = Node{ - peerConfig: ep, - relayConfig: relayConfig, - e2eeConfig: e2eeConfig, - interfaces: interfaces, - } - } + if responseNode.error == "" { + nodes[responseNode.relayConfig.GetPublicKey()] = responseNode + } else { + errorNodes = append(errorNodes, responseNode) + } } // Build tree by adding each relay node as a child. @@ -167,6 +156,10 @@ func (cc statusCmdConfig) Run() { api, strings.Join(ips, ","), ) + + if c.relayConfig.GetLocalhostIP() != "" { + nodeString += "\n lhost IP: " + c.relayConfig.GetLocalhostIP() + } if cc.networkInfo { nodeString += ` @@ -175,7 +168,7 @@ Network Interfaces: ------------------- ` for _, ifx := range c.interfaces { - nodeString += fmt.Sprintf("%v:\n", ifx.Name) + nodeString += ifx.Name + "\n" for _, a := range ifx.Addrs { nodeString += strings.Repeat(" ", 2) + a.String() + "\n" } @@ -233,6 +226,36 @@ Network Interfaces: } } +func (cc statusCmdConfig) makeAPIRequests(ch chan<- Node, ep peer.PeerConfig) { + relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort))) + if err != nil { + ch <- Node{ + peerConfig: ep, + error: err.Error(), + } + return + + } else { + var interfaces []api.HostInterface + if cc.networkInfo { + interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort))) + if err != nil { + interfaces = append(interfaces, api.HostInterface{ + Name: "ERROR: " + err.Error(), + }) + } + } + + ch <- Node{ + peerConfig: ep, + relayConfig: relayConfig, + e2eeConfig: e2eeConfig, + interfaces: interfaces, + } + return + } +} + func errorWrap(text string, lineWidth int) string { words := strings.Fields(strings.TrimSpace(text)) if len(words) == 0 { diff --git a/src/peer/config.go b/src/peer/config.go index 3f3b1a6..f0973ac 100644 --- a/src/peer/config.go +++ b/src/peer/config.go @@ -101,6 +101,13 @@ func GetConfig(args ConfigArgs) (Config, error) { } } + if args.LocalhostIP != "" { + err = c.SetLocalhostIP(args.LocalhostIP) + if err != nil { + return Config{}, err + } + } + return c, nil } @@ -157,7 +164,6 @@ func ParseConfig(filename string) (c Config, err error) { err = c.SetMTU(mtu) case "localhostip": err = c.SetLocalhostIP(value) - fmt.Println("LocalhostIP value parsed") } if err != nil { return c, err @@ -241,6 +247,7 @@ func (c *Config) UnmarshalJSON(b []byte) error { c.config = tmp.Config c.peers = tmp.Peers c.addresses = tmp.Addresses + c.localhostIP = tmp.LocalhostIP return nil }