basic ranged auto listener (#779)

This commit is contained in:
Michael Quigley 2024-11-06 17:18:20 -05:00
parent 99a2e665a6
commit fe08a19272
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 53 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import (
type Agent struct {
cfg *AgentConfig
httpEndpoint string
root env_core.Root
agentSocket string
shares map[string]*share
@ -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)
}
}

17
agent/autoListener.go Normal file
View File

@ -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")
}

View File

@ -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,
}
}

View File

@ -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
}

View File

@ -16,7 +16,9 @@ func init() {
type agentStartCommand struct {
cmd *cobra.Command
consoleEndpoint string
consoleAddress string
consoleStartPort uint16
consoleEndPort uint16
}
func newAgentStartCommand() *agentStartCommand {
@ -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
}
@ -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)