basic ranged auto listener (#779)

This commit is contained in:
Michael Quigley
2024-11-06 17:18:20 -05:00
parent 99a2e665a6
commit fe08a19272
5 changed files with 53 additions and 19 deletions

View File

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