Skip to content

Commit

Permalink
basic ranged auto listener (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Nov 6, 2024
1 parent 99a2e66 commit fe08a19
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
27 changes: 17 additions & 10 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import (
)

type Agent struct {
cfg *AgentConfig
root env_core.Root
agentSocket string
shares map[string]*share
addShare chan *share
rmShare chan *share
accesses map[string]*access
addAccess chan *access
rmAccess chan *access
cfg *AgentConfig
httpEndpoint string
root env_core.Root
agentSocket string
shares map[string]*share
addShare chan *share
rmShare chan *share
accesses map[string]*access
addAccess chan *access
rmAccess chan *access
}

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

if err := http.ListenAndServe(cfg.ConsoleEndpoint, agentUi.Middleware(mux)); err != nil {
listener, err := AutoListener(cfg.ConsoleAddress, cfg.ConsoleStartPort, cfg.ConsoleEndPort)
if err != nil {
logrus.Fatalf("unable to create a listener: %v", err)
}
a.httpEndpoint = listener.Addr().String()

if err := http.Serve(listener, agentUi.Middleware(mux)); err != nil {
logrus.Error(err)
}
}
Expand Down
17 changes: 17 additions & 0 deletions agent/autoListener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package agent

import (
"fmt"
"net"
)

func AutoListener(address string, startPort, endPort uint16) (net.Listener, error) {
for i := startPort; i <= endPort; i++ {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, i))
if err != nil {
continue
}
return l, nil
}
return nil, fmt.Errorf("no listener found in range")
}
10 changes: 7 additions & 3 deletions agent/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package agent

type AgentConfig struct {
ConsoleEndpoint string
ConsoleAddress string
ConsoleStartPort uint16
ConsoleEndPort uint16
}

func DefaultAgentConfig() *AgentConfig {
func DefaultConfig() *AgentConfig {
return &AgentConfig{
ConsoleEndpoint: "127.0.0.1:8888",
ConsoleAddress: "127.0.0.1",
ConsoleStartPort: 8080,
ConsoleEndPort: 8181,
}
}
2 changes: 1 addition & 1 deletion agent/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func (i *agentGrpcImpl) Version(_ context.Context, _ *agentGrpc.VersionRequest)
logrus.Debugf("responding to version inquiry with '%v'", v)
return &agentGrpc.VersionResponse{
V: v,
ConsoleEndpoint: i.agent.Config().ConsoleEndpoint,
ConsoleEndpoint: i.agent.httpEndpoint,
}, nil
}
16 changes: 11 additions & 5 deletions cmd/zrok/agentStart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func init() {
}

type agentStartCommand struct {
cmd *cobra.Command
consoleEndpoint string
cmd *cobra.Command
consoleAddress string
consoleStartPort uint16
consoleEndPort uint16
}

func newAgentStartCommand() *agentStartCommand {
Expand All @@ -27,7 +29,9 @@ func newAgentStartCommand() *agentStartCommand {
}
command := &agentStartCommand{cmd: cmd}
cmd.Run = command.run
cmd.Flags().StringVar(&command.consoleEndpoint, "console-endpoint", "127.0.0.1:8888", "gRPC gateway endpoint")
cmd.Flags().StringVar(&command.consoleAddress, "console-endpoint", "127.0.0.1", "gRPC gateway address")
cmd.Flags().Uint16Var(&command.consoleStartPort, "console-start-port", 8080, "gRPC gateway starting port")
cmd.Flags().Uint16Var(&command.consoleEndPort, "console-end-port", 8181, "gRPC gateway ending port")
return command
}

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

cfg := agent.DefaultAgentConfig()
cfg.ConsoleEndpoint = cmd.consoleEndpoint
cfg := agent.DefaultConfig()
cfg.ConsoleAddress = cmd.consoleAddress
cfg.ConsoleStartPort = cmd.consoleStartPort
cfg.ConsoleEndPort = cmd.consoleEndPort
a, err := agent.NewAgent(cfg, root)
if err != nil {
tui.Error("error creating agent", err)
Expand Down

0 comments on commit fe08a19

Please sign in to comment.