add initial advertise(Addr|Port) support

(cherry picked from commit 2a0b4f335eb54caa19ad908cdc441d73b57d68ca)
This commit is contained in:
Steve Fan 2020-07-27 21:02:11 +08:00 committed by Killian Kemps
parent c025351b36
commit c9afa9f5ef
3 changed files with 9 additions and 3 deletions

View File

@ -31,7 +31,7 @@ type Cluster struct {
// New is used to create a new Cluster instance // New is used to create a new Cluster instance
// The returned instance is ready to be updated with the local node settings then joined // The returned instance is ready to be updated with the local node settings then joined
func New(name string, init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool) (*Cluster, error) { func New(name string, init bool, clusterKey []byte, bindAddr string, bindPort int, advertiseAddr string, advertisePort int, useIPAsName bool) (*Cluster, error) {
state := &state{} state := &state{}
if !init { if !init {
loadState(state, name) loadState(state, name)
@ -47,7 +47,8 @@ func New(name string, init bool, clusterKey []byte, bindAddr string, bindPort in
mlConfig.SecretKey = clusterKey mlConfig.SecretKey = clusterKey
mlConfig.BindAddr = bindAddr mlConfig.BindAddr = bindAddr
mlConfig.BindPort = bindPort mlConfig.BindPort = bindPort
mlConfig.AdvertisePort = bindPort mlConfig.AdvertiseAddr = advertiseAddr
mlConfig.AdvertisePort = advertisePort
if useIPAsName && bindAddr != "0.0.0.0" { if useIPAsName && bindAddr != "0.0.0.0" {
mlConfig.Name = bindAddr mlConfig.Name = bindAddr
} }

View File

@ -17,6 +17,7 @@ type config struct {
Init bool `desc:"whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten"` Init bool `desc:"whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten"`
BindAddr string `id:"bind-addr" desc:"IP address to bind to for cluster membership traffic (cannot be used with --bind-iface)"` BindAddr string `id:"bind-addr" desc:"IP address to bind to for cluster membership traffic (cannot be used with --bind-iface)"`
BindIface string `id:"bind-iface" desc:"Interface to bind to for cluster membership traffic (cannot be used with --bind-addr)"` BindIface string `id:"bind-iface" desc:"Interface to bind to for cluster membership traffic (cannot be used with --bind-addr)"`
AdvertiseAddr string `id:"advertise-addr" desc:"IP address to advertise to other nodes for NAT traversal"`
ClusterPort int `id:"cluster-port" desc:"port used for membership gossip traffic (both TCP and UDP); must be the same across cluster" default:"7946"` ClusterPort int `id:"cluster-port" desc:"port used for membership gossip traffic (both TCP and UDP); must be the same across cluster" default:"7946"`
WireguardPort int `id:"wireguard-port" desc:"port used for wireguard traffic (UDP); must be the same across cluster" default:"51820"` WireguardPort int `id:"wireguard-port" desc:"port used for wireguard traffic (UDP); must be the same across cluster" default:"51820"`
BaseMtu int `id:"mtu" desc:"MTU of the underlying network, taking intermediary hops into account" default:"1500"` BaseMtu int `id:"mtu" desc:"MTU of the underlying network, taking intermediary hops into account" default:"1500"`
@ -80,6 +81,10 @@ func loadConfig() (*config, error) {
} }
} }
if config.AdvertiseAddr == "" {
config.AdvertiseAddr = config.BindAddr
}
return &config, nil return &config, nil
} }

View File

@ -35,7 +35,7 @@ func main() {
logrus.SetLevel(logLevel) logrus.SetLevel(logLevel)
// Create the wireguard and cluster configuration // Create the wireguard and cluster configuration
cluster, err := cluster.New(config.Interface, config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.UseIPAsName) cluster, err := cluster.New(config.Interface, config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.AdvertiseAddr, config.ClusterPort, config.UseIPAsName)
if err != nil { if err != nil {
logrus.WithError(err).Fatal("could not create cluster") logrus.WithError(err).Fatal("could not create cluster")
} }