mirror of
https://github.com/openziti/zrok.git
synced 2025-08-17 03:11:17 +02:00
basic ranged auto listener (#779)
This commit is contained in:
@ -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
17
agent/autoListener.go
Normal 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")
|
||||
}
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user