This commit is contained in:
asolana 2022-10-07 11:36:39 +02:00
parent 74910f96da
commit 82dd30ee5c
61 changed files with 2197 additions and 2030 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"regexp"
"github.com/gologme/log"
@ -25,10 +26,10 @@ import (
// functions. Note that in the case of iOS we handle reading/writing to/from TUN
// in Swift therefore we use the "dummy" TUN interface instead.
type Yggdrasil struct {
core core.Core
core *core.Core
iprwc *ipv6rwc.ReadWriteCloser
config *config.NodeConfig
multicast multicast.Multicast
multicast *multicast.Multicast
log MobileLogger
}
@ -48,27 +49,58 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
if err := json.Unmarshal(configjson, &m.config); err != nil {
return err
}
m.config.IfName = "none"
if err := m.core.Start(m.config, logger); err != nil {
logger.Errorln("An error occured starting Yggdrasil:", err)
return err
// Setup the Yggdrasil node itself.
{
sk, err := hex.DecodeString(m.config.PrivateKey)
if err != nil {
panic(err)
}
options := []core.SetupOption{}
for _, peer := range m.config.Peers {
options = append(options, core.Peer{URI: peer})
}
for intf, peers := range m.config.InterfacePeers {
for _, peer := range peers {
options = append(options, core.Peer{URI: peer, SourceInterface: intf})
}
}
for _, allowed := range m.config.AllowedPublicKeys {
k, err := hex.DecodeString(allowed)
if err != nil {
panic(err)
}
options = append(options, core.AllowedPublicKey(k[:]))
}
m.core, err = core.New(sk[:], logger, options...)
if err != nil {
panic(err)
}
}
// Setup the multicast module.
if len(m.config.MulticastInterfaces) > 0 {
var err error
options := []multicast.SetupOption{}
for _, intf := range m.config.MulticastInterfaces {
options = append(options, multicast.MulticastInterface{
Regex: regexp.MustCompile(intf.Regex),
Beacon: intf.Beacon,
Listen: intf.Listen,
Port: intf.Port,
})
}
m.multicast, err = multicast.New(m.core, logger, options...)
if err != nil {
logger.Errorln("An error occurred starting multicast:", err)
}
}
mtu := m.config.IfMTU
m.iprwc = ipv6rwc.NewReadWriteCloser(&m.core)
m.iprwc = ipv6rwc.NewReadWriteCloser(m.core)
if m.iprwc.MaxMTU() < mtu {
mtu = m.iprwc.MaxMTU()
}
m.iprwc.SetMTU(mtu)
if len(m.config.MulticastInterfaces) > 0 {
if err := m.multicast.Init(&m.core, m.config, logger, nil); err != nil {
logger.Errorln("An error occurred initialising multicast:", err)
return err
}
if err := m.multicast.Start(); err != nil {
logger.Errorln("An error occurred starting multicast:", err)
return err
}
}
return nil
}
@ -139,18 +171,18 @@ func (m *Yggdrasil) GetCoordsString() string {
func (m *Yggdrasil) GetPeersJSON() (result string) {
peers := []struct {
core.Peer
core.PeerInfo
IP string
}{}
for _, v := range m.core.GetPeers() {
a := address.AddrForKey(v.Key)
ip := net.IP(a[:]).String()
peers = append(peers, struct {
core.Peer
core.PeerInfo
IP string
}{
Peer: v,
IP: ip,
PeerInfo: v,
IP: ip,
})
}
if res, err := json.Marshal(peers); err == nil {