|
| 1 | +package mobile |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/gologme/log" |
| 10 | + |
| 11 | + "github.com/yggdrasil-network/yggdrasil-go/src/address" |
| 12 | + "github.com/yggdrasil-network/yggdrasil-go/src/config" |
| 13 | + "github.com/yggdrasil-network/yggdrasil-go/src/core" |
| 14 | + "github.com/yggdrasil-network/yggdrasil-go/src/defaults" |
| 15 | + "github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc" |
| 16 | + "github.com/yggdrasil-network/yggdrasil-go/src/multicast" |
| 17 | + "github.com/yggdrasil-network/yggdrasil-go/src/version" |
| 18 | + |
| 19 | + _ "golang.org/x/mobile/bind" |
| 20 | +) |
| 21 | + |
| 22 | +// Yggdrasil mobile package is meant to "plug the gap" for mobile support, as |
| 23 | +// Gomobile will not create headers for Swift/Obj-C etc if they have complex |
| 24 | +// (non-native) types. Therefore for iOS we will expose some nice simple |
| 25 | +// functions. Note that in the case of iOS we handle reading/writing to/from TUN |
| 26 | +// in Swift therefore we use the "dummy" TUN interface instead. |
| 27 | +type Yggdrasil struct { |
| 28 | + core core.Core |
| 29 | + iprwc *ipv6rwc.ReadWriteCloser |
| 30 | + config *config.NodeConfig |
| 31 | + multicast multicast.Multicast |
| 32 | + log MobileLogger |
| 33 | +} |
| 34 | + |
| 35 | +// StartAutoconfigure starts a node with a randomly generated config |
| 36 | +func (m *Yggdrasil) StartAutoconfigure() error { |
| 37 | + return m.StartJSON([]byte("{}")) |
| 38 | +} |
| 39 | + |
| 40 | +// StartJSON starts a node with the given JSON config. You can get JSON config |
| 41 | +// (rather than HJSON) by using the GenerateConfigJSON() function |
| 42 | +func (m *Yggdrasil) StartJSON(configjson []byte) error { |
| 43 | + logger := log.New(m.log, "", 0) |
| 44 | + logger.EnableLevel("error") |
| 45 | + logger.EnableLevel("warn") |
| 46 | + logger.EnableLevel("info") |
| 47 | + m.config = defaults.GenerateConfig() |
| 48 | + if err := json.Unmarshal(configjson, &m.config); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + m.config.IfName = "none" |
| 52 | + if err := m.core.Start(m.config, logger); err != nil { |
| 53 | + logger.Errorln("An error occured starting Yggdrasil:", err) |
| 54 | + return err |
| 55 | + } |
| 56 | + mtu := m.config.IfMTU |
| 57 | + m.iprwc = ipv6rwc.NewReadWriteCloser(&m.core) |
| 58 | + if m.iprwc.MaxMTU() < mtu { |
| 59 | + mtu = m.iprwc.MaxMTU() |
| 60 | + } |
| 61 | + m.iprwc.SetMTU(mtu) |
| 62 | + if len(m.config.MulticastInterfaces) > 0 { |
| 63 | + if err := m.multicast.Init(&m.core, m.config, logger, nil); err != nil { |
| 64 | + logger.Errorln("An error occurred initialising multicast:", err) |
| 65 | + return err |
| 66 | + } |
| 67 | + if err := m.multicast.Start(); err != nil { |
| 68 | + logger.Errorln("An error occurred starting multicast:", err) |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// Send sends a packet to Yggdrasil. It should be a fully formed |
| 76 | +// IPv6 packet |
| 77 | +func (m *Yggdrasil) Send(p []byte) error { |
| 78 | + if m.iprwc == nil { |
| 79 | + return nil |
| 80 | + } |
| 81 | + _, _ = m.iprwc.Write(p) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// Recv waits for and reads a packet coming from Yggdrasil. It |
| 86 | +// will be a fully formed IPv6 packet |
| 87 | +func (m *Yggdrasil) Recv() ([]byte, error) { |
| 88 | + if m.iprwc == nil { |
| 89 | + return nil, nil |
| 90 | + } |
| 91 | + var buf [65535]byte |
| 92 | + n, _ := m.iprwc.Read(buf[:]) |
| 93 | + return buf[:n], nil |
| 94 | +} |
| 95 | + |
| 96 | +// Stop the mobile Yggdrasil instance |
| 97 | +func (m *Yggdrasil) Stop() error { |
| 98 | + logger := log.New(m.log, "", 0) |
| 99 | + logger.EnableLevel("info") |
| 100 | + logger.Infof("Stop the mobile Yggdrasil instance %s", "") |
| 101 | + if err := m.multicast.Stop(); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + m.core.Stop() |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// GenerateConfigJSON generates mobile-friendly configuration in JSON format |
| 109 | +func GenerateConfigJSON() []byte { |
| 110 | + nc := defaults.GenerateConfig() |
| 111 | + nc.IfName = "none" |
| 112 | + if json, err := json.Marshal(nc); err == nil { |
| 113 | + return json |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +// GetAddressString gets the node's IPv6 address |
| 119 | +func (m *Yggdrasil) GetAddressString() string { |
| 120 | + ip := m.core.Address() |
| 121 | + return ip.String() |
| 122 | +} |
| 123 | + |
| 124 | +// GetSubnetString gets the node's IPv6 subnet in CIDR notation |
| 125 | +func (m *Yggdrasil) GetSubnetString() string { |
| 126 | + subnet := m.core.Subnet() |
| 127 | + return subnet.String() |
| 128 | +} |
| 129 | + |
| 130 | +// GetPublicKeyString gets the node's public key in hex form |
| 131 | +func (m *Yggdrasil) GetPublicKeyString() string { |
| 132 | + return hex.EncodeToString(m.core.GetSelf().Key) |
| 133 | +} |
| 134 | + |
| 135 | +// GetCoordsString gets the node's coordinates |
| 136 | +func (m *Yggdrasil) GetCoordsString() string { |
| 137 | + return fmt.Sprintf("%v", m.core.GetSelf().Coords) |
| 138 | +} |
| 139 | + |
| 140 | +func (m *Yggdrasil) GetPeersJSON() (result string) { |
| 141 | + peers := []struct { |
| 142 | + core.Peer |
| 143 | + IP string |
| 144 | + }{} |
| 145 | + for _, v := range m.core.GetPeers() { |
| 146 | + a := address.AddrForKey(v.Key) |
| 147 | + ip := net.IP(a[:]).String() |
| 148 | + peers = append(peers, struct { |
| 149 | + core.Peer |
| 150 | + IP string |
| 151 | + }{ |
| 152 | + Peer: v, |
| 153 | + IP: ip, |
| 154 | + }) |
| 155 | + } |
| 156 | + if res, err := json.Marshal(peers); err == nil { |
| 157 | + return string(res) |
| 158 | + } else { |
| 159 | + return "{}" |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func (m *Yggdrasil) GetDHTJSON() (result string) { |
| 164 | + if res, err := json.Marshal(m.core.GetDHT()); err == nil { |
| 165 | + return string(res) |
| 166 | + } else { |
| 167 | + return "{}" |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// GetMTU returns the configured node MTU. This must be called AFTER Start. |
| 172 | +func (m *Yggdrasil) GetMTU() int { |
| 173 | + return int(m.core.MTU()) |
| 174 | +} |
| 175 | + |
| 176 | +func GetVersion() string { |
| 177 | + return version.BuildVersion() |
| 178 | +} |
0 commit comments