mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	Remove mobile module, since it can now be moved into another repository
This commit is contained in:
		
							parent
							
								
									c9554f82be
								
							
						
					
					
						commit
						cbc8711dd3
					
				
					 4 changed files with 0 additions and 328 deletions
				
			
		| 
						 | 
					@ -1,108 +0,0 @@
 | 
				
			||||||
package mobile
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"io"
 | 
					 | 
				
			||||||
	"sync"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type awdl struct {
 | 
					 | 
				
			||||||
	link        *link
 | 
					 | 
				
			||||||
	reconfigure chan chan error
 | 
					 | 
				
			||||||
	mutex       sync.RWMutex // protects interfaces below
 | 
					 | 
				
			||||||
	interfaces  map[string]*awdlInterface
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type awdlInterface struct {
 | 
					 | 
				
			||||||
	linkif *linkInterface
 | 
					 | 
				
			||||||
	rwc    awdlReadWriteCloser
 | 
					 | 
				
			||||||
	peer   *peer
 | 
					 | 
				
			||||||
	stream stream
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type awdlReadWriteCloser struct {
 | 
					 | 
				
			||||||
	fromAWDL chan []byte
 | 
					 | 
				
			||||||
	toAWDL   chan []byte
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) {
 | 
					 | 
				
			||||||
	if packet, ok := <-c.fromAWDL; ok {
 | 
					 | 
				
			||||||
		n = copy(p, packet)
 | 
					 | 
				
			||||||
		return n, nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return 0, io.EOF
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c awdlReadWriteCloser) Write(p []byte) (n int, err error) {
 | 
					 | 
				
			||||||
	var pc []byte
 | 
					 | 
				
			||||||
	pc = append(pc, p...)
 | 
					 | 
				
			||||||
	c.toAWDL <- pc
 | 
					 | 
				
			||||||
	return len(pc), nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c awdlReadWriteCloser) Close() error {
 | 
					 | 
				
			||||||
	close(c.fromAWDL)
 | 
					 | 
				
			||||||
	close(c.toAWDL)
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (a *awdl) init(l *link) error {
 | 
					 | 
				
			||||||
	a.link = l
 | 
					 | 
				
			||||||
	a.mutex.Lock()
 | 
					 | 
				
			||||||
	a.interfaces = make(map[string]*awdlInterface)
 | 
					 | 
				
			||||||
	a.reconfigure = make(chan chan error, 1)
 | 
					 | 
				
			||||||
	a.mutex.Unlock()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	go func() {
 | 
					 | 
				
			||||||
		for e := range a.reconfigure {
 | 
					 | 
				
			||||||
			e <- nil
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (a *awdl) create(name, local, remote string, incoming bool) (*awdlInterface, error) {
 | 
					 | 
				
			||||||
	rwc := awdlReadWriteCloser{
 | 
					 | 
				
			||||||
		fromAWDL: make(chan []byte, 1),
 | 
					 | 
				
			||||||
		toAWDL:   make(chan []byte, 1),
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	s := stream{}
 | 
					 | 
				
			||||||
	s.init(rwc)
 | 
					 | 
				
			||||||
	linkif, err := a.link.create(&s, name, "awdl", local, remote, incoming, true)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	intf := awdlInterface{
 | 
					 | 
				
			||||||
		linkif: linkif,
 | 
					 | 
				
			||||||
		rwc:    rwc,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	a.mutex.Lock()
 | 
					 | 
				
			||||||
	a.interfaces[name] = &intf
 | 
					 | 
				
			||||||
	a.mutex.Unlock()
 | 
					 | 
				
			||||||
	go intf.linkif.handler()
 | 
					 | 
				
			||||||
	return &intf, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (a *awdl) getInterface(identity string) *awdlInterface {
 | 
					 | 
				
			||||||
	a.mutex.RLock()
 | 
					 | 
				
			||||||
	defer a.mutex.RUnlock()
 | 
					 | 
				
			||||||
	if intf, ok := a.interfaces[identity]; ok {
 | 
					 | 
				
			||||||
		return intf
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (a *awdl) shutdown(identity string) error {
 | 
					 | 
				
			||||||
	if intf, ok := a.interfaces[identity]; ok {
 | 
					 | 
				
			||||||
		close(intf.linkif.closed)
 | 
					 | 
				
			||||||
		intf.rwc.Close()
 | 
					 | 
				
			||||||
		a.mutex.Lock()
 | 
					 | 
				
			||||||
		delete(a.interfaces, identity)
 | 
					 | 
				
			||||||
		a.mutex.Unlock()
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return errors.New("Interface not found or already closed")
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,147 +0,0 @@
 | 
				
			||||||
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/multicast"
 | 
					 | 
				
			||||||
	"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Yggdrasil 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
 | 
					 | 
				
			||||||
	log       MobileLogger
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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 {
 | 
					 | 
				
			||||||
	logger := log.New(m.log, "", 0)
 | 
					 | 
				
			||||||
	logger.EnableLevel("error")
 | 
					 | 
				
			||||||
	logger.EnableLevel("warn")
 | 
					 | 
				
			||||||
	logger.EnableLevel("info")
 | 
					 | 
				
			||||||
	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}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	/*if err := m.core.SetRouterAdapter(m); err != nil {
 | 
					 | 
				
			||||||
		logger.Errorln("An error occured setting router adapter:", err)
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}*/
 | 
					 | 
				
			||||||
	state, err := m.core.Start(nc, logger)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Errorln("An error occured starting Yggdrasil:", err)
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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 {
 | 
					 | 
				
			||||||
	logger := log.New(m.log, "", 0)
 | 
					 | 
				
			||||||
	logger.EnableLevel("error")
 | 
					 | 
				
			||||||
	logger.EnableLevel("warn")
 | 
					 | 
				
			||||||
	logger.EnableLevel("info")
 | 
					 | 
				
			||||||
	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"
 | 
					 | 
				
			||||||
	/*if err := m.core.SetRouterAdapter(m); err != nil {
 | 
					 | 
				
			||||||
		logger.Errorln("An error occured setting router adapter:", err)
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}*/
 | 
					 | 
				
			||||||
	state, err := m.core.Start(nc, logger)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		logger.Errorln("An error occured starting Yggdrasil:", err)
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Stop 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
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,12 +0,0 @@
 | 
				
			||||||
// +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
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,61 +0,0 @@
 | 
				
			||||||
// +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)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue