diff --git a/build/gobind/monolith.go b/build/gobind/monolith.go index bf4c020ba..b73e47f86 100644 --- a/build/gobind/monolith.go +++ b/build/gobind/monolith.go @@ -39,7 +39,23 @@ func (m *DendriteMonolith) PeerCount() int { return m.YggdrasilNode.PeerCount() } -func (m *DendriteMonolith) Start() { +func (m *DendriteMonolith) SetMulticastEnabled(enabled bool) { + m.YggdrasilNode.SetMulticastEnabled(enabled) +} + +func (m *DendriteMonolith) SetStaticPeer(uri string) error { + return m.YggdrasilNode.SetStaticPeer(uri) +} + +func (m *DendriteMonolith) DisconnectNonMulticastPeers() { + m.YggdrasilNode.DisconnectNonMulticastPeers() +} + +func (m *DendriteMonolith) DisconnectMulticastPeers() { + m.YggdrasilNode.DisconnectMulticastPeers() +} + +func (m *DendriteMonolith) Start(staticPeer string, enableMulticast bool) { logger := logrus.Logger{ Out: BindLogger{}, } @@ -51,7 +67,7 @@ func (m *DendriteMonolith) Start() { panic(err) } - ygg, err := yggconn.Setup("dendrite", "", m.StorageDirectory) + ygg, err := yggconn.Setup("dendrite", staticPeer, m.StorageDirectory, enableMulticast) if err != nil { panic(err) } diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index d3e9e5493..9c8c5055e 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -54,7 +54,7 @@ func main() { flag.Parse() internal.SetupPprof() - ygg, err := yggconn.Setup(*instanceName, *instancePeer, ".") + ygg, err := yggconn.Setup(*instanceName, *instancePeer, ".", true) if err != nil { panic(err) } diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/node.go b/cmd/dendrite-demo-yggdrasil/yggconn/node.go index da184fd5b..4b08f1f38 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/node.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/node.go @@ -80,7 +80,7 @@ func (n *Node) DialerContext(ctx context.Context, network, address string) (net. } // nolint:gocyclo -func Setup(instanceName, instancePeer, storageDirectory string) (*Node, error) { +func Setup(instanceName, instancePeer, storageDirectory string, enableMulticast bool) (*Node, error) { n := &Node{ core: &yggdrasil.Core{}, config: yggdrasilconfig.GenerateConfig(), @@ -100,22 +100,30 @@ func Setup(instanceName, instancePeer, storageDirectory string) (*Node, error) { if err := json.Unmarshal([]byte(yggconf), &n.config); err != nil { panic(err) } - } else { - n.config.AdminListen = "none" // fmt.Sprintf("unix://%s/%s-yggdrasil.sock", storageDirectory, instanceName) - n.config.MulticastInterfaces = []string{".*"} - n.config.EncryptionPrivateKey = hex.EncodeToString(n.EncryptionPrivateKey()) - n.config.EncryptionPublicKey = hex.EncodeToString(n.EncryptionPublicKey()) - - j, err := json.MarshalIndent(n.config, "", " ") - if err != nil { - panic(err) - } - if e := ioutil.WriteFile(yggfile, j, 0600); e != nil { - n.log.Printf("Couldn't write private key to file '%s': %s\n", yggfile, e) - } } - var err error + if instancePeer != "" { + n.config.Peers = []string{instancePeer} + } else { + n.config.Peers = []string{} + } + n.config.AdminListen = "none" + if enableMulticast { + n.config.MulticastInterfaces = []string{".*"} + } else { + n.config.MulticastInterfaces = []string{} + } + n.config.EncryptionPrivateKey = hex.EncodeToString(n.EncryptionPrivateKey()) + n.config.EncryptionPublicKey = hex.EncodeToString(n.EncryptionPublicKey()) + + j, err := json.MarshalIndent(n.config, "", " ") + if err != nil { + panic(err) + } + if e := ioutil.WriteFile(yggfile, j, 0600); e != nil { + n.log.Printf("Couldn't write private key to file '%s': %s\n", yggfile, e) + } + n.log.EnableLevel("error") n.log.EnableLevel("warn") n.log.EnableLevel("info") @@ -123,11 +131,6 @@ func Setup(instanceName, instancePeer, storageDirectory string) (*Node, error) { if err != nil { panic(err) } - if instancePeer != "" { - if err = n.core.AddPeer(instancePeer, ""); err != nil { - panic(err) - } - } if err = n.multicast.Init(n.core, n.state, n.log, nil); err != nil { panic(err) } @@ -210,3 +213,51 @@ func (n *Node) KnownNodes() []gomatrixserverlib.ServerName { } return nodes } + +func (n *Node) SetMulticastEnabled(enabled bool) { + if enabled { + n.config.MulticastInterfaces = []string{".*"} + } else { + n.config.MulticastInterfaces = []string{} + } + n.multicast.UpdateConfig(n.config) + if !enabled { + n.DisconnectMulticastPeers() + } +} + +func (n *Node) DisconnectMulticastPeers() { + for _, sp := range n.core.GetSwitchPeers() { + if !strings.HasPrefix(sp.Endpoint, "fe80") { + continue + } + if err := n.core.DisconnectPeer(sp.Port); err != nil { + n.log.Printf("Failed to disconnect port %d: %s", sp.Port, err) + } + } +} + +func (n *Node) DisconnectNonMulticastPeers() { + for _, sp := range n.core.GetSwitchPeers() { + if strings.HasPrefix(sp.Endpoint, "fe80") { + continue + } + if err := n.core.DisconnectPeer(sp.Port); err != nil { + n.log.Printf("Failed to disconnect port %d: %s", sp.Port, err) + } + } +} + +func (n *Node) SetStaticPeer(uri string) error { + n.config.Peers = []string{} + n.core.UpdateConfig(n.config) + n.DisconnectNonMulticastPeers() + if uri != "" { + n.log.Infoln("Adding static peer", uri) + if err := n.core.AddPeer(uri, ""); err != nil { + n.log.Infoln("Adding static peer failed:", err) + return err + } + } + return nil +} diff --git a/go.mod b/go.mod index ab8b016aa..4fec1bc9a 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/uber-go/atomic v1.3.0 // indirect github.com/uber/jaeger-client-go v2.15.0+incompatible github.com/uber/jaeger-lib v1.5.0 - github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200703125141-dbe5c1b1c190 + github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200706132141-82b2af2963bf go.uber.org/atomic v1.4.0 golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 gopkg.in/h2non/bimg.v1 v1.0.18 diff --git a/go.sum b/go.sum index 966d32d4d..e89b93dfe 100644 --- a/go.sum +++ b/go.sum @@ -652,8 +652,8 @@ github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhe github.com/xdg/stringprep v1.0.0/go.mod h1:Jhud4/sHMO4oL310DaZAKk9ZaJ08SJfe+sJh0HrGL1Y= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yggdrasil-network/yggdrasil-extras v0.0.0-20200525205615-6c8a4a2e8855/go.mod h1:xQdsh08Io6nV4WRnOVTe6gI8/2iTvfLDQ0CYa5aMt+I= -github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200703125141-dbe5c1b1c190 h1:xfCiZ7C6nDRkJaZa/V7C14prMBU2ZrrNVp/P+upaHm8= -github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200703125141-dbe5c1b1c190/go.mod h1:d+Nz6SPeG6kmeSPFL0cvfWfgwEql75fUnZiAONgvyBE= +github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200706132141-82b2af2963bf h1:80RhF740jE7Rry2tg0UGnye95vBTZ/G6RNCBM7oiivE= +github.com/yggdrasil-network/yggdrasil-go v0.3.15-0.20200706132141-82b2af2963bf/go.mod h1:d+Nz6SPeG6kmeSPFL0cvfWfgwEql75fUnZiAONgvyBE= go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA=