mirror of
https://github.com/openziti/zrok.git
synced 2025-04-24 19:28:57 +02:00
basic ranged auto listener (#779)
This commit is contained in:
parent
99a2e665a6
commit
fe08a19272
@ -19,6 +19,7 @@ import (
|
|||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
cfg *AgentConfig
|
cfg *AgentConfig
|
||||||
|
httpEndpoint string
|
||||||
root env_core.Root
|
root env_core.Root
|
||||||
agentSocket string
|
agentSocket string
|
||||||
shares map[string]*share
|
shares map[string]*share
|
||||||
@ -109,7 +110,13 @@ func (a *Agent) gateway(cfg *AgentConfig) {
|
|||||||
logrus.Fatalf("unable to register gateway: %v", err)
|
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)
|
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
|
package agent
|
||||||
|
|
||||||
type AgentConfig struct {
|
type AgentConfig struct {
|
||||||
ConsoleEndpoint string
|
ConsoleAddress string
|
||||||
|
ConsoleStartPort uint16
|
||||||
|
ConsoleEndPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func DefaultAgentConfig() *AgentConfig {
|
func DefaultConfig() *AgentConfig {
|
||||||
return &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)
|
logrus.Debugf("responding to version inquiry with '%v'", v)
|
||||||
return &agentGrpc.VersionResponse{
|
return &agentGrpc.VersionResponse{
|
||||||
V: v,
|
V: v,
|
||||||
ConsoleEndpoint: i.agent.Config().ConsoleEndpoint,
|
ConsoleEndpoint: i.agent.httpEndpoint,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,9 @@ func init() {
|
|||||||
|
|
||||||
type agentStartCommand struct {
|
type agentStartCommand struct {
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
consoleEndpoint string
|
consoleAddress string
|
||||||
|
consoleStartPort uint16
|
||||||
|
consoleEndPort uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAgentStartCommand() *agentStartCommand {
|
func newAgentStartCommand() *agentStartCommand {
|
||||||
@ -27,7 +29,9 @@ func newAgentStartCommand() *agentStartCommand {
|
|||||||
}
|
}
|
||||||
command := &agentStartCommand{cmd: cmd}
|
command := &agentStartCommand{cmd: cmd}
|
||||||
cmd.Run = command.run
|
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
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,8 +45,10 @@ func (cmd *agentStartCommand) run(_ *cobra.Command, _ []string) {
|
|||||||
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
|
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := agent.DefaultAgentConfig()
|
cfg := agent.DefaultConfig()
|
||||||
cfg.ConsoleEndpoint = cmd.consoleEndpoint
|
cfg.ConsoleAddress = cmd.consoleAddress
|
||||||
|
cfg.ConsoleStartPort = cmd.consoleStartPort
|
||||||
|
cfg.ConsoleEndPort = cmd.consoleEndPort
|
||||||
a, err := agent.NewAgent(cfg, root)
|
a, err := agent.NewAgent(cfg, root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tui.Error("error creating agent", err)
|
tui.Error("error creating agent", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user