mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2025-06-15 16:46:06 +00:00
Break out mobile and dummy adapter
This commit is contained in:
143
src/mobile/mobile.go
Normal file
143
src/mobile/mobile.go
Normal file
@ -0,0 +1,143 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gologme/log"
|
||||
|
||||
hjson "github.com/hjson/hjson-go"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/dummy"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
||||
)
|
||||
|
||||
// Yggdrasil's mobile package 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.
|
||||
type Yggdrasil struct {
|
||||
core *yggdrasil.Core
|
||||
multicast *multicast.Multicast
|
||||
dummy.DummyAdapter
|
||||
}
|
||||
|
||||
func (m *Yggdrasil) addStaticPeers(cfg *config.NodeConfig) {
|
||||
if len(cfg.Peers) == 0 && len(cfg.InterfacePeers) == 0 {
|
||||
return
|
||||
}
|
||||
for {
|
||||
for _, peer := range cfg.Peers {
|
||||
m.core.AddPeer(peer, "")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
for intf, intfpeers := range cfg.InterfacePeers {
|
||||
for _, peer := range intfpeers {
|
||||
m.core.AddPeer(peer, intf)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
// StartAutoconfigure starts a node with a randomly generated config
|
||||
func (m *Yggdrasil) StartAutoconfigure() error {
|
||||
m.core = &yggdrasil.Core{}
|
||||
//m.Adapter = dummy.DummyAdapter{}
|
||||
mobilelog := MobileLogger{}
|
||||
logger := log.New(mobilelog, "", 0)
|
||||
nc := config.GenerateConfig()
|
||||
nc.IfName = "dummy"
|
||||
nc.AdminListen = "tcp://localhost:9001"
|
||||
nc.Peers = []string{}
|
||||
if hostname, err := os.Hostname(); err == nil {
|
||||
nc.NodeInfo = map[string]interface{}{"name": hostname}
|
||||
}
|
||||
m.core.SetRouterAdapter(&m)
|
||||
state, err := m.core.Start(nc, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Start the multicast interface
|
||||
m.multicast.Init(m.core, state, logger, nil)
|
||||
if err := m.multicast.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting multicast:", err)
|
||||
}
|
||||
go m.addStaticPeers(nc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartJSON starts a node with the given JSON config. You can get JSON config
|
||||
// (rather than HJSON) by using the GenerateConfigJSON() function
|
||||
func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
||||
m.core = &yggdrasil.Core{}
|
||||
//m.Adapter = dummy.DummyAdapter{}
|
||||
mobilelog := MobileLogger{}
|
||||
logger := log.New(mobilelog, "", 0)
|
||||
nc := config.GenerateConfig()
|
||||
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"
|
||||
m.core.SetRouterAdapter(&m)
|
||||
state, err := m.core.Start(nc, logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Start the multicast interface
|
||||
m.multicast.Init(m.core, state, logger, nil)
|
||||
if err := m.multicast.Start(); err != nil {
|
||||
logger.Errorln("An error occurred starting multicast:", err)
|
||||
}
|
||||
go m.addStaticPeers(nc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stops the mobile Yggdrasil instance
|
||||
func (m *Yggdrasil) Stop() error {
|
||||
m.core.Stop()
|
||||
if err := m.Stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateConfigJSON generates mobile-friendly configuration in JSON format
|
||||
func GenerateConfigJSON() []byte {
|
||||
nc := config.GenerateConfig()
|
||||
nc.IfName = "dummy"
|
||||
if json, err := json.Marshal(nc); err == nil {
|
||||
return json
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetAddressString gets the node's IPv6 address
|
||||
func (m *Yggdrasil) GetAddressString() string {
|
||||
return m.core.Address().String()
|
||||
}
|
||||
|
||||
// GetSubnetString gets the node's IPv6 subnet in CIDR notation
|
||||
func (m *Yggdrasil) GetSubnetString() string {
|
||||
return m.core.Subnet().String()
|
||||
}
|
||||
|
||||
// GetBoxPubKeyString gets the node's public encryption key
|
||||
func (m *Yggdrasil) GetBoxPubKeyString() string {
|
||||
return m.core.BoxPubKey()
|
||||
}
|
||||
|
||||
// GetSigPubKeyString gets the node's public signing key
|
||||
func (m *Yggdrasil) GetSigPubKeyString() string {
|
||||
return m.core.SigPubKey()
|
||||
}
|
12
src/mobile/mobile_android.go
Normal file
12
src/mobile/mobile_android.go
Normal file
@ -0,0 +1,12 @@
|
||||
// +build android
|
||||
|
||||
package mobile
|
||||
|
||||
import "log"
|
||||
|
||||
type MobileLogger struct{}
|
||||
|
||||
func (nsl MobileLogger) Write(p []byte) (n int, err error) {
|
||||
log.Println(string(p))
|
||||
return len(p), nil
|
||||
}
|
61
src/mobile/mobile_ios.go
Normal file
61
src/mobile/mobile_ios.go
Normal file
@ -0,0 +1,61 @@
|
||||
// +build darwin
|
||||
|
||||
package mobile
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c
|
||||
#cgo LDFLAGS: -framework Foundation
|
||||
#import <Foundation/Foundation.h>
|
||||
void Log(const char *text) {
|
||||
NSString *nss = [NSString stringWithUTF8String:text];
|
||||
NSLog(@"%@", nss);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type MobileLogger struct {
|
||||
}
|
||||
|
||||
func (nsl MobileLogger) Write(p []byte) (n int, err error) {
|
||||
p = append(p, 0)
|
||||
cstr := (*C.char)(unsafe.Pointer(&p[0]))
|
||||
C.Log(cstr)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (c *Core) AWDLCreateInterface(name, local, remote string, incoming bool) error {
|
||||
if intf, err := c.link.awdl.create(name, local, remote, incoming); err != nil || intf == nil {
|
||||
c.log.Println("c.link.awdl.create:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Core) AWDLShutdownInterface(name string) error {
|
||||
return c.link.awdl.shutdown(name)
|
||||
}
|
||||
|
||||
func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
|
||||
if intf := c.link.awdl.getInterface(identity); intf != nil {
|
||||
read, ok := <-intf.rwc.toAWDL
|
||||
if !ok {
|
||||
return nil, errors.New("AWDLRecvPacket: channel closed")
|
||||
}
|
||||
return read, nil
|
||||
}
|
||||
return nil, errors.New("AWDLRecvPacket identity not known: " + identity)
|
||||
}
|
||||
|
||||
func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
|
||||
packet := append(util.GetBytes(), buf[:]...)
|
||||
if intf := c.link.awdl.getInterface(identity); intf != nil {
|
||||
intf.rwc.fromAWDL <- packet
|
||||
return nil
|
||||
}
|
||||
return errors.New("AWDLSendPacket identity not known: " + identity)
|
||||
}
|
||||
*/
|
Reference in New Issue
Block a user