yggdrasil-go/src/yggdrasil/mobile.go

133 lines
3.6 KiB
Go
Raw Normal View History

2019-01-02 21:05:54 +03:00
// +build mobile
package yggdrasil
import (
2019-01-04 01:50:08 +03:00
"encoding/json"
2019-01-02 21:05:54 +03:00
"log"
"os"
"regexp"
"time"
2019-01-02 21:05:54 +03:00
2019-01-03 02:15:36 +03:00
hjson "github.com/hjson/hjson-go"
"github.com/mitchellh/mapstructure"
2019-01-02 21:05:54 +03:00
"github.com/yggdrasil-network/yggdrasil-go/src/config"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
)
// This file is meant to "plug the gap" for mobile support, as Gomobile will
// not create headers for Swift/Obj-C etc if they have complex (non-native)
// types. Therefore for iOS we will expose some nice simple 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.
func (c *Core) addStaticPeers(cfg *config.NodeConfig) {
if len(cfg.Peers) == 0 && len(cfg.InterfacePeers) == 0 {
return
}
for {
for _, peer := range cfg.Peers {
c.AddPeer(peer, "")
time.Sleep(time.Second)
}
for intf, intfpeers := range cfg.InterfacePeers {
for _, peer := range intfpeers {
c.AddPeer(peer, intf)
time.Sleep(time.Second)
}
}
time.Sleep(time.Minute)
}
}
// Starts a node with a randomly generated config.
2019-01-02 21:05:54 +03:00
func (c *Core) StartAutoconfigure() error {
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
2019-01-02 21:05:54 +03:00
nc := config.GenerateConfig(true)
nc.IfName = "dummy"
2019-01-04 01:50:08 +03:00
nc.AdminListen = "tcp://localhost:9001"
2019-01-02 21:05:54 +03:00
nc.Peers = []string{}
if hostname, err := os.Hostname(); err == nil {
nc.NodeInfo = map[string]interface{}{"name": hostname}
}
ifceExpr, err := regexp.Compile(".*")
if err == nil {
c.ifceExpr = append(c.ifceExpr, ifceExpr)
}
if err := c.Start(nc, logger); err != nil {
return err
}
go c.addStaticPeers(nc)
2019-01-02 21:05:54 +03:00
return nil
}
// Starts a node with the given JSON config. You can get JSON config (rather
// than HJSON) by using the GenerateConfigJSON() function.
2019-01-03 02:15:36 +03:00
func (c *Core) StartJSON(configjson []byte) error {
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
2019-01-03 02:15:36 +03:00
nc := config.GenerateConfig(false)
var dat map[string]interface{}
if err := hjson.Unmarshal(configjson, &dat); err != nil {
return err
}
if err := mapstructure.Decode(dat, &nc); err != nil {
return err
}
nc.IfName = "dummy"
//c.log.Println(nc.MulticastInterfaces)
2019-01-04 01:50:08 +03:00
for _, ll := range nc.MulticastInterfaces {
//c.log.Println("Processing MC", ll)
2019-01-04 01:50:08 +03:00
ifceExpr, err := regexp.Compile(ll)
if err != nil {
panic(err)
}
c.AddMulticastInterfaceExpr(ifceExpr)
}
2019-01-03 02:15:36 +03:00
if err := c.Start(nc, logger); err != nil {
return err
}
go c.addStaticPeers(nc)
2019-01-03 02:15:36 +03:00
return nil
}
// Generates mobile-friendly configuration in JSON format.
2019-01-04 01:50:08 +03:00
func GenerateConfigJSON() []byte {
nc := config.GenerateConfig(false)
nc.IfName = "dummy"
if json, err := json.Marshal(nc); err == nil {
return json
} else {
return nil
}
}
// Gets the node's IPv6 address.
2019-01-02 21:05:54 +03:00
func (c *Core) GetAddressString() string {
return c.GetAddress().String()
}
// Gets the node's IPv6 subnet in CIDR notation.
2019-01-02 21:05:54 +03:00
func (c *Core) GetSubnetString() string {
return c.GetSubnet().String()
}
// Wait for a packet from the router. You will use this when implementing a
// dummy adapter in place of real TUN - when this call returns a packet, you
// will probably want to give it to the OS to write to TUN.
2019-01-02 21:05:54 +03:00
func (c *Core) RouterRecvPacket() ([]byte, error) {
packet := <-c.router.tun.recv
return packet, nil
}
// Send a packet to the router. You will use this when implementing a
// dummy adapter in place of real TUN - when the operating system tells you
// that a new packet is available from TUN, call this function to give it to
// Yggdrasil.
2019-01-02 21:05:54 +03:00
func (c *Core) RouterSendPacket(buf []byte) error {
packet := append(util.GetBytes(), buf[:]...)
c.router.tun.send <- packet
return nil
}