Updated functionality for updating Yggdrasil config in iOS demo

This commit is contained in:
Neil Alexander 2020-07-06 14:51:59 +01:00
parent ea9df46c70
commit 6c4eabbe92
5 changed files with 93 additions and 26 deletions

View File

@ -39,7 +39,23 @@ func (m *DendriteMonolith) PeerCount() int {
return m.YggdrasilNode.PeerCount() 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{ logger := logrus.Logger{
Out: BindLogger{}, Out: BindLogger{},
} }
@ -51,7 +67,7 @@ func (m *DendriteMonolith) Start() {
panic(err) panic(err)
} }
ygg, err := yggconn.Setup("dendrite", "", m.StorageDirectory) ygg, err := yggconn.Setup("dendrite", staticPeer, m.StorageDirectory, enableMulticast)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -54,7 +54,7 @@ func main() {
flag.Parse() flag.Parse()
internal.SetupPprof() internal.SetupPprof()
ygg, err := yggconn.Setup(*instanceName, *instancePeer, ".") ygg, err := yggconn.Setup(*instanceName, *instancePeer, ".", true)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -80,7 +80,7 @@ func (n *Node) DialerContext(ctx context.Context, network, address string) (net.
} }
// nolint:gocyclo // nolint:gocyclo
func Setup(instanceName, instancePeer, storageDirectory string) (*Node, error) { func Setup(instanceName, instancePeer, storageDirectory string, enableMulticast bool) (*Node, error) {
n := &Node{ n := &Node{
core: &yggdrasil.Core{}, core: &yggdrasil.Core{},
config: yggdrasilconfig.GenerateConfig(), 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 { if err := json.Unmarshal([]byte(yggconf), &n.config); err != nil {
panic(err) 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("error")
n.log.EnableLevel("warn") n.log.EnableLevel("warn")
n.log.EnableLevel("info") n.log.EnableLevel("info")
@ -123,11 +131,6 @@ func Setup(instanceName, instancePeer, storageDirectory string) (*Node, error) {
if err != nil { if err != nil {
panic(err) 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 { if err = n.multicast.Init(n.core, n.state, n.log, nil); err != nil {
panic(err) panic(err)
} }
@ -210,3 +213,51 @@ func (n *Node) KnownNodes() []gomatrixserverlib.ServerName {
} }
return nodes 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
}

2
go.mod
View File

@ -36,7 +36,7 @@ require (
github.com/uber-go/atomic v1.3.0 // indirect github.com/uber-go/atomic v1.3.0 // indirect
github.com/uber/jaeger-client-go v2.15.0+incompatible github.com/uber/jaeger-client-go v2.15.0+incompatible
github.com/uber/jaeger-lib v1.5.0 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 go.uber.org/atomic v1.4.0
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5
gopkg.in/h2non/bimg.v1 v1.0.18 gopkg.in/h2non/bimg.v1 v1.0.18

4
go.sum
View File

@ -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/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/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-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.20200706132141-82b2af2963bf h1:80RhF740jE7Rry2tg0UGnye95vBTZ/G6RNCBM7oiivE=
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/go.mod h1:d+Nz6SPeG6kmeSPFL0cvfWfgwEql75fUnZiAONgvyBE=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA= 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.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA= go.opencensus.io v0.22.1/go.mod h1:Ap50jQcDJrx6rB6VgeeFPtuPIf3wMRvRfrfYDO6+BmA=