Skip to content

Commit fe08a19

Browse files
basic ranged auto listener (#779)
1 parent 99a2e66 commit fe08a19

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

agent/agent.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ import (
1818
)
1919

2020
type Agent struct {
21-
cfg *AgentConfig
22-
root env_core.Root
23-
agentSocket string
24-
shares map[string]*share
25-
addShare chan *share
26-
rmShare chan *share
27-
accesses map[string]*access
28-
addAccess chan *access
29-
rmAccess chan *access
21+
cfg *AgentConfig
22+
httpEndpoint string
23+
root env_core.Root
24+
agentSocket string
25+
shares map[string]*share
26+
addShare chan *share
27+
rmShare chan *share
28+
accesses map[string]*access
29+
addAccess chan *access
30+
rmAccess chan *access
3031
}
3132

3233
func NewAgent(cfg *AgentConfig, root env_core.Root) (*Agent, error) {
@@ -109,7 +110,13 @@ func (a *Agent) gateway(cfg *AgentConfig) {
109110
logrus.Fatalf("unable to register gateway: %v", err)
110111
}
111112

112-
if err := http.ListenAndServe(cfg.ConsoleEndpoint, agentUi.Middleware(mux)); err != nil {
113+
listener, err := AutoListener(cfg.ConsoleAddress, cfg.ConsoleStartPort, cfg.ConsoleEndPort)
114+
if err != nil {
115+
logrus.Fatalf("unable to create a listener: %v", err)
116+
}
117+
a.httpEndpoint = listener.Addr().String()
118+
119+
if err := http.Serve(listener, agentUi.Middleware(mux)); err != nil {
113120
logrus.Error(err)
114121
}
115122
}

agent/autoListener.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package agent
2+
3+
import (
4+
"fmt"
5+
"net"
6+
)
7+
8+
func AutoListener(address string, startPort, endPort uint16) (net.Listener, error) {
9+
for i := startPort; i <= endPort; i++ {
10+
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, i))
11+
if err != nil {
12+
continue
13+
}
14+
return l, nil
15+
}
16+
return nil, fmt.Errorf("no listener found in range")
17+
}

agent/config.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package agent
22

33
type AgentConfig struct {
4-
ConsoleEndpoint string
4+
ConsoleAddress string
5+
ConsoleStartPort uint16
6+
ConsoleEndPort uint16
57
}
68

7-
func DefaultAgentConfig() *AgentConfig {
9+
func DefaultConfig() *AgentConfig {
810
return &AgentConfig{
9-
ConsoleEndpoint: "127.0.0.1:8888",
11+
ConsoleAddress: "127.0.0.1",
12+
ConsoleStartPort: 8080,
13+
ConsoleEndPort: 8181,
1014
}
1115
}

agent/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func (i *agentGrpcImpl) Version(_ context.Context, _ *agentGrpc.VersionRequest)
1212
logrus.Debugf("responding to version inquiry with '%v'", v)
1313
return &agentGrpc.VersionResponse{
1414
V: v,
15-
ConsoleEndpoint: i.agent.Config().ConsoleEndpoint,
15+
ConsoleEndpoint: i.agent.httpEndpoint,
1616
}, nil
1717
}

cmd/zrok/agentStart.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ func init() {
1515
}
1616

1717
type agentStartCommand struct {
18-
cmd *cobra.Command
19-
consoleEndpoint string
18+
cmd *cobra.Command
19+
consoleAddress string
20+
consoleStartPort uint16
21+
consoleEndPort uint16
2022
}
2123

2224
func newAgentStartCommand() *agentStartCommand {
@@ -27,7 +29,9 @@ func newAgentStartCommand() *agentStartCommand {
2729
}
2830
command := &agentStartCommand{cmd: cmd}
2931
cmd.Run = command.run
30-
cmd.Flags().StringVar(&command.consoleEndpoint, "console-endpoint", "127.0.0.1:8888", "gRPC gateway endpoint")
32+
cmd.Flags().StringVar(&command.consoleAddress, "console-endpoint", "127.0.0.1", "gRPC gateway address")
33+
cmd.Flags().Uint16Var(&command.consoleStartPort, "console-start-port", 8080, "gRPC gateway starting port")
34+
cmd.Flags().Uint16Var(&command.consoleEndPort, "console-end-port", 8181, "gRPC gateway ending port")
3135
return command
3236
}
3337

@@ -41,8 +45,10 @@ func (cmd *agentStartCommand) run(_ *cobra.Command, _ []string) {
4145
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
4246
}
4347

44-
cfg := agent.DefaultAgentConfig()
45-
cfg.ConsoleEndpoint = cmd.consoleEndpoint
48+
cfg := agent.DefaultConfig()
49+
cfg.ConsoleAddress = cmd.consoleAddress
50+
cfg.ConsoleStartPort = cmd.consoleStartPort
51+
cfg.ConsoleEndPort = cmd.consoleEndPort
4652
a, err := agent.NewAgent(cfg, root)
4753
if err != nil {
4854
tui.Error("error creating agent", err)

0 commit comments

Comments
 (0)