mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
Merge branch 'develop' into mobile-listen
This commit is contained in:
commit
e75a576686
53 changed files with 2412 additions and 1851 deletions
|
@ -3,7 +3,6 @@ package mobile
|
|||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
|
||||
|
@ -12,7 +11,6 @@ import (
|
|||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
||||
|
@ -46,20 +44,16 @@ func (m *Yggdrasil) StartAutoconfigure() error {
|
|||
func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
||||
setMemLimitIfPossible()
|
||||
|
||||
m.logger = log.New(m.log, "", 0)
|
||||
m.logger.EnableLevel("error")
|
||||
m.logger.EnableLevel("warn")
|
||||
m.logger.EnableLevel("info")
|
||||
m.config = defaults.GenerateConfig()
|
||||
if err := json.Unmarshal(configjson, &m.config); err != nil {
|
||||
logger := log.New(m.log, "", 0)
|
||||
logger.EnableLevel("error")
|
||||
logger.EnableLevel("warn")
|
||||
logger.EnableLevel("info")
|
||||
m.config = config.GenerateConfig()
|
||||
if err := m.config.UnmarshalHJSON(configjson); err != nil {
|
||||
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})
|
||||
|
@ -79,7 +73,8 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
|||
for _, lAddr := range m.config.Listen {
|
||||
options = append(options, core.ListenAddress(lAddr))
|
||||
}
|
||||
m.core, err = core.New(sk[:], m.logger, options...)
|
||||
var err error
|
||||
m.core, err = core.New(m.config.Certificate, logger, options...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -96,6 +91,7 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
|||
Listen: intf.Listen,
|
||||
Port: intf.Port,
|
||||
Priority: uint8(intf.Priority),
|
||||
Password: intf.Password,
|
||||
})
|
||||
}
|
||||
m.multicast, err = multicast.New(m.core, m.logger, options...)
|
||||
|
@ -179,7 +175,7 @@ func (m *Yggdrasil) RetryPeersNow() {
|
|||
|
||||
// GenerateConfigJSON generates mobile-friendly configuration in JSON format
|
||||
func GenerateConfigJSON() []byte {
|
||||
nc := defaults.GenerateConfig()
|
||||
nc := config.GenerateConfig()
|
||||
nc.IfName = "none"
|
||||
if json, err := json.Marshal(nc); err == nil {
|
||||
return json
|
||||
|
@ -204,9 +200,9 @@ func (m *Yggdrasil) GetPublicKeyString() string {
|
|||
return hex.EncodeToString(m.core.GetSelf().Key)
|
||||
}
|
||||
|
||||
// GetCoordsString gets the node's coordinates
|
||||
func (m *Yggdrasil) GetCoordsString() string {
|
||||
return fmt.Sprintf("%v", m.core.GetSelf().Coords)
|
||||
// GetRoutingEntries gets the number of entries in the routing table
|
||||
func (m *Yggdrasil) GetRoutingEntries() int {
|
||||
return int(m.core.GetSelf().RoutingEntries)
|
||||
}
|
||||
|
||||
func (m *Yggdrasil) GetPeersJSON() (result string) {
|
||||
|
@ -215,8 +211,11 @@ func (m *Yggdrasil) GetPeersJSON() (result string) {
|
|||
IP string
|
||||
}{}
|
||||
for _, v := range m.core.GetPeers() {
|
||||
a := address.AddrForKey(v.Key)
|
||||
ip := net.IP(a[:]).String()
|
||||
var ip string
|
||||
if v.Key != nil {
|
||||
a := address.AddrForKey(v.Key)
|
||||
ip = net.IP(a[:]).String()
|
||||
}
|
||||
peers = append(peers, struct {
|
||||
core.PeerInfo
|
||||
IP string
|
||||
|
@ -232,8 +231,16 @@ func (m *Yggdrasil) GetPeersJSON() (result string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (m *Yggdrasil) GetDHTJSON() (result string) {
|
||||
if res, err := json.Marshal(m.core.GetDHT()); err == nil {
|
||||
func (m *Yggdrasil) GetPathsJSON() (result string) {
|
||||
if res, err := json.Marshal(m.core.GetPaths()); err == nil {
|
||||
return string(res)
|
||||
} else {
|
||||
return "{}"
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Yggdrasil) GetTreeJSON() (result string) {
|
||||
if res, err := json.Marshal(m.core.GetTree()); err == nil {
|
||||
return string(res)
|
||||
} else {
|
||||
return "{}"
|
||||
|
|
|
@ -1,15 +1,27 @@
|
|||
package mobile
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/gologme/log"
|
||||
)
|
||||
|
||||
func TestStartYggdrasil(t *testing.T) {
|
||||
ygg := &Yggdrasil{}
|
||||
logger := log.New(os.Stdout, "", 0)
|
||||
logger.EnableLevel("error")
|
||||
logger.EnableLevel("warn")
|
||||
logger.EnableLevel("info")
|
||||
|
||||
ygg := &Yggdrasil{
|
||||
logger: logger,
|
||||
}
|
||||
if err := ygg.StartAutoconfigure(); err != nil {
|
||||
t.Fatalf("Failed to start Yggdrasil: %s", err)
|
||||
}
|
||||
t.Log("Address:", ygg.GetAddressString())
|
||||
t.Log("Subnet:", ygg.GetSubnetString())
|
||||
t.Log("Coords:", ygg.GetCoordsString())
|
||||
t.Log("Routing entries:", ygg.GetRoutingEntries())
|
||||
if err := ygg.Stop(); err != nil {
|
||||
t.Fatalf("Failed to stop Yggdrasil: %s", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue