Merge pull request #769 from micro/advertise

allow setting advertise address
This commit is contained in:
Asim Aslam 2019-09-18 19:06:24 +01:00 committed by GitHub
commit ee74e26582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -86,11 +86,17 @@ func newNetwork(opts ...Option) Network {
tun.WithTunnel(options.Tunnel),
)
// set the address to advertise
address := options.Address
if len(options.Advertise) > 0 {
address = options.Advertise
}
// server is network server
server := server.NewServer(
server.Id(options.Id),
server.Address(options.Id),
server.Advertise(options.Address),
server.Advertise(address),
server.Name(options.Name),
server.Transport(tunTransport),
)
@ -108,7 +114,7 @@ func newNetwork(opts ...Option) Network {
network := &network{
node: &node{
id: options.Id,
address: options.Address,
address: address,
peers: make(map[string]*node),
},
options: options,
@ -710,7 +716,11 @@ func (n *network) Connect() error {
}
// set our internal node address
// if advertise address is not set
if len(n.options.Advertise) == 0 {
n.node.address = n.Tunnel.Address()
n.server.Init(server.Advertise(n.Tunnel.Address()))
}
// initialize the tunnel to resolved nodes
n.Tunnel.Init(

View File

@ -20,6 +20,8 @@ type Options struct {
Name string
// Address to bind to
Address string
// Advertise sets the address to advertise
Advertise string
// Nodes is a list of seed nodes
Nodes []string
// Tunnel is network tunnel
@ -53,6 +55,13 @@ func Address(a string) Option {
}
}
// Advertise sets the address to advertise
func Advertise(a string) Option {
return func(o *Options) {
o.Advertise = a
}
}
// Nodes is a list of seed nodes used along
// with resolved node
func Nodes(n ...string) Option {