Skip to content

Commit 034c06d

Browse files
authored
Status improvements (#70)
* add localhost IP to status * concurrent status API requests
1 parent ae063c0 commit 034c06d

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

src/cmd/serve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func (c serveCmdConfig) Run() {
342342
},
343343
},
344344
Addresses: relayAddresses,
345+
LocalhostIP: viper.GetString("Relay.Interface.LocalhostIP"),
345346
}
346347

347348
configRelay, err := peer.GetConfig(configRelayArgs)
@@ -598,7 +599,6 @@ func configureLocalhostForwarding(localhostAddr netip.Addr, s *stack.Stack) {
598599
// Adds a rule to the start of a table chain.
599600
func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook) (stack.Table) {
600601
insertIndex := int(table.BuiltinChains[chain])
601-
fmt.Printf("Inserting rule into index %d\n", insertIndex)
602602
table.Rules = slices.Insert(table.Rules, insertIndex, newRule)
603603

604604
// Increment the later chain and underflow index pointers to account for the rule added to the Rules slice

src/cmd/status.go

+57-34
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ type statusCmdConfig struct {
1919
configFileE2EE string
2020
}
2121

22+
// Represents one Server or Client in tree
23+
type Node struct {
24+
peerConfig peer.PeerConfig
25+
relayConfig peer.Config
26+
e2eeConfig peer.Config
27+
children []*Node
28+
interfaces []api.HostInterface
29+
error string
30+
}
31+
2232
// Defaults for status command.
2333
// See root command for shared defaults.
2434
var statusCmd = statusCmdConfig{
@@ -50,16 +60,6 @@ func init() {
5060

5161
// Run attempts to parse config files into a network diagram.
5262
func (cc statusCmdConfig) Run() {
53-
// Start building tree.
54-
type Node struct {
55-
peerConfig peer.PeerConfig
56-
relayConfig peer.Config
57-
e2eeConfig peer.Config
58-
children []*Node
59-
interfaces []api.HostInterface
60-
error string
61-
}
62-
6363
var err error
6464

6565
// Parse the relay and e2ee config files
@@ -81,32 +81,21 @@ func (cc statusCmdConfig) Run() {
8181
nodes := make(map[string]Node)
8282
var errorNodes []Node
8383
e2ee_peer_list := client.e2eeConfig.GetPeers()
84+
nodeChannel := make(chan Node)
8485
for _, ep := range e2ee_peer_list {
85-
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
86-
if err != nil {
87-
errorNodes = append(errorNodes, Node{
88-
peerConfig: ep,
89-
error: err.Error(),
90-
})
86+
// Make all the API requests concurrently to speed things up
87+
go cc.makeAPIRequests(nodeChannel, ep)
88+
}
9189

92-
} else {
93-
var interfaces []api.HostInterface
94-
if cc.networkInfo {
95-
interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
96-
if err != nil {
97-
interfaces = append(interfaces, api.HostInterface{
98-
Name: "ERROR: " + err.Error(),
99-
})
100-
}
101-
}
90+
// Don't need to do anything with values, just need to loop the same number of times
91+
for range e2ee_peer_list {
92+
responseNode := <- nodeChannel
10293

103-
nodes[relayConfig.GetPublicKey()] = Node{
104-
peerConfig: ep,
105-
relayConfig: relayConfig,
106-
e2eeConfig: e2eeConfig,
107-
interfaces: interfaces,
108-
}
109-
}
94+
if responseNode.error == "" {
95+
nodes[responseNode.relayConfig.GetPublicKey()] = responseNode
96+
} else {
97+
errorNodes = append(errorNodes, responseNode)
98+
}
11099
}
111100

112101
// Build tree by adding each relay node as a child.
@@ -167,6 +156,10 @@ func (cc statusCmdConfig) Run() {
167156
api,
168157
strings.Join(ips, ","),
169158
)
159+
160+
if c.relayConfig.GetLocalhostIP() != "" {
161+
nodeString += "\n lhost IP: " + c.relayConfig.GetLocalhostIP()
162+
}
170163

171164
if cc.networkInfo {
172165
nodeString += `
@@ -175,7 +168,7 @@ Network Interfaces:
175168
-------------------
176169
`
177170
for _, ifx := range c.interfaces {
178-
nodeString += fmt.Sprintf("%v:\n", ifx.Name)
171+
nodeString += ifx.Name + "\n"
179172
for _, a := range ifx.Addrs {
180173
nodeString += strings.Repeat(" ", 2) + a.String() + "\n"
181174
}
@@ -233,6 +226,36 @@ Network Interfaces:
233226
}
234227
}
235228

229+
func (cc statusCmdConfig) makeAPIRequests(ch chan<- Node, ep peer.PeerConfig) {
230+
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
231+
if err != nil {
232+
ch <- Node{
233+
peerConfig: ep,
234+
error: err.Error(),
235+
}
236+
return
237+
238+
} else {
239+
var interfaces []api.HostInterface
240+
if cc.networkInfo {
241+
interfaces, err = api.ServerInterfaces(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
242+
if err != nil {
243+
interfaces = append(interfaces, api.HostInterface{
244+
Name: "ERROR: " + err.Error(),
245+
})
246+
}
247+
}
248+
249+
ch <- Node{
250+
peerConfig: ep,
251+
relayConfig: relayConfig,
252+
e2eeConfig: e2eeConfig,
253+
interfaces: interfaces,
254+
}
255+
return
256+
}
257+
}
258+
236259
func errorWrap(text string, lineWidth int) string {
237260
words := strings.Fields(strings.TrimSpace(text))
238261
if len(words) == 0 {

src/peer/config.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ func GetConfig(args ConfigArgs) (Config, error) {
101101
}
102102
}
103103

104+
if args.LocalhostIP != "" {
105+
err = c.SetLocalhostIP(args.LocalhostIP)
106+
if err != nil {
107+
return Config{}, err
108+
}
109+
}
110+
104111
return c, nil
105112
}
106113

@@ -157,7 +164,6 @@ func ParseConfig(filename string) (c Config, err error) {
157164
err = c.SetMTU(mtu)
158165
case "localhostip":
159166
err = c.SetLocalhostIP(value)
160-
fmt.Println("LocalhostIP value parsed")
161167
}
162168
if err != nil {
163169
return c, err
@@ -241,6 +247,7 @@ func (c *Config) UnmarshalJSON(b []byte) error {
241247
c.config = tmp.Config
242248
c.peers = tmp.Peers
243249
c.addresses = tmp.Addresses
250+
c.localhostIP = tmp.LocalhostIP
244251

245252
return nil
246253
}

0 commit comments

Comments
 (0)