2021-05-23 19:42:26 +00:00
|
|
|
package core
|
2019-05-18 16:21:02 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-15 19:50:56 +00:00
|
|
|
"crypto/ed25519"
|
2021-06-13 10:43:03 +00:00
|
|
|
"encoding/json"
|
2019-05-18 16:21:02 +00:00
|
|
|
"net"
|
2021-05-24 01:34:13 +00:00
|
|
|
"net/url"
|
2023-06-18 08:40:40 +00:00
|
|
|
"sync/atomic"
|
2022-10-02 12:20:39 +00:00
|
|
|
"time"
|
2019-05-18 16:21:02 +00:00
|
|
|
|
2023-06-18 08:40:40 +00:00
|
|
|
"github.com/Arceliar/phony"
|
|
|
|
|
2023-04-06 20:45:49 +00:00
|
|
|
"github.com/Arceliar/ironwood/network"
|
2021-05-08 13:35:58 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-05-18 16:21:02 +00:00
|
|
|
)
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
type SelfInfo struct {
|
2023-03-19 10:33:07 +00:00
|
|
|
Key ed25519.PublicKey
|
|
|
|
RoutingEntries uint64
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
type PeerInfo struct {
|
2023-04-06 20:45:49 +00:00
|
|
|
URI string
|
|
|
|
Up bool
|
|
|
|
Inbound bool
|
|
|
|
LastError error
|
|
|
|
LastErrorTime time.Time
|
|
|
|
Key ed25519.PublicKey
|
|
|
|
Root ed25519.PublicKey
|
|
|
|
Coords []uint64
|
|
|
|
Port uint64
|
|
|
|
Priority uint8
|
|
|
|
RXBytes uint64
|
|
|
|
TXBytes uint64
|
|
|
|
Uptime time.Duration
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 19:44:38 +00:00
|
|
|
type TreeEntryInfo struct {
|
2023-03-26 21:49:40 +00:00
|
|
|
Key ed25519.PublicKey
|
|
|
|
Parent ed25519.PublicKey
|
|
|
|
Sequence uint64
|
|
|
|
//Port uint64
|
|
|
|
//Rest uint64
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
type PathEntryInfo struct {
|
2023-03-19 10:33:07 +00:00
|
|
|
Key ed25519.PublicKey
|
2023-05-13 19:44:38 +00:00
|
|
|
Path []uint64
|
2023-03-19 10:33:07 +00:00
|
|
|
Sequence uint64
|
2019-05-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
type SessionInfo struct {
|
2022-09-03 15:55:57 +00:00
|
|
|
Key ed25519.PublicKey
|
|
|
|
RXBytes uint64
|
|
|
|
TXBytes uint64
|
|
|
|
Uptime time.Duration
|
2019-05-19 15:29:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
func (c *Core) GetSelf() SelfInfo {
|
|
|
|
var self SelfInfo
|
2021-07-05 18:14:12 +00:00
|
|
|
s := c.PacketConn.PacketConn.Debug.GetSelf()
|
2021-05-15 19:50:56 +00:00
|
|
|
self.Key = s.Key
|
2023-03-19 10:33:07 +00:00
|
|
|
self.RoutingEntries = s.RoutingEntries
|
2021-05-15 19:50:56 +00:00
|
|
|
return self
|
2019-05-19 15:29:04 +00:00
|
|
|
}
|
2019-05-18 16:21:02 +00:00
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
func (c *Core) GetPeers() []PeerInfo {
|
2023-03-19 10:33:07 +00:00
|
|
|
peers := []PeerInfo{}
|
2023-04-06 20:45:49 +00:00
|
|
|
conns := map[net.Conn]network.DebugPeerInfo{}
|
|
|
|
iwpeers := c.PacketConn.PacketConn.Debug.GetPeers()
|
|
|
|
for _, p := range iwpeers {
|
|
|
|
conns[p.Conn] = p
|
|
|
|
}
|
|
|
|
|
2023-06-18 08:40:40 +00:00
|
|
|
phony.Block(&c.links, func() {
|
|
|
|
for info, state := range c.links._links {
|
|
|
|
var peerinfo PeerInfo
|
|
|
|
var conn net.Conn
|
|
|
|
peerinfo.URI = info.uri
|
|
|
|
peerinfo.LastError = state._err
|
|
|
|
peerinfo.LastErrorTime = state._errtime
|
|
|
|
if c := state._conn; c != nil {
|
|
|
|
conn = c
|
|
|
|
peerinfo.Up = true
|
|
|
|
peerinfo.Inbound = state.linkType == linkTypeIncoming
|
|
|
|
peerinfo.RXBytes = atomic.LoadUint64(&c.rx)
|
|
|
|
peerinfo.TXBytes = atomic.LoadUint64(&c.tx)
|
|
|
|
peerinfo.Uptime = time.Since(c.up)
|
|
|
|
}
|
|
|
|
if p, ok := conns[conn]; ok {
|
|
|
|
peerinfo.Key = p.Key
|
|
|
|
peerinfo.Root = p.Root
|
|
|
|
peerinfo.Port = p.Port
|
|
|
|
peerinfo.Priority = p.Priority
|
|
|
|
}
|
|
|
|
peers = append(peers, peerinfo)
|
2022-09-17 19:07:00 +00:00
|
|
|
}
|
2023-06-18 08:40:40 +00:00
|
|
|
})
|
2023-04-06 20:45:49 +00:00
|
|
|
|
2019-05-18 16:21:02 +00:00
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:44:38 +00:00
|
|
|
func (c *Core) GetTree() []TreeEntryInfo {
|
|
|
|
var trees []TreeEntryInfo
|
|
|
|
ts := c.PacketConn.PacketConn.Debug.GetTree()
|
|
|
|
for _, t := range ts {
|
|
|
|
var info TreeEntryInfo
|
|
|
|
info.Key = t.Key
|
|
|
|
info.Parent = t.Parent
|
|
|
|
info.Sequence = t.Sequence
|
2023-03-26 21:49:40 +00:00
|
|
|
//info.Port = d.Port
|
2023-03-18 12:14:32 +00:00
|
|
|
//info.Rest = d.Rest
|
2023-05-13 19:44:38 +00:00
|
|
|
trees = append(trees, info)
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
2023-05-13 19:44:38 +00:00
|
|
|
return trees
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
func (c *Core) GetPaths() []PathEntryInfo {
|
|
|
|
var paths []PathEntryInfo
|
2021-07-05 18:14:12 +00:00
|
|
|
ps := c.PacketConn.PacketConn.Debug.GetPaths()
|
2021-05-15 19:50:56 +00:00
|
|
|
for _, p := range ps {
|
2022-08-06 14:05:12 +00:00
|
|
|
var info PathEntryInfo
|
2021-05-15 19:50:56 +00:00
|
|
|
info.Key = p.Key
|
2023-03-19 10:33:07 +00:00
|
|
|
info.Sequence = p.Sequence
|
2023-05-13 19:44:38 +00:00
|
|
|
info.Path = p.Path
|
2021-05-15 19:50:56 +00:00
|
|
|
paths = append(paths, info)
|
2019-05-19 15:29:04 +00:00
|
|
|
}
|
2021-05-15 19:50:56 +00:00
|
|
|
return paths
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 14:05:12 +00:00
|
|
|
func (c *Core) GetSessions() []SessionInfo {
|
|
|
|
var sessions []SessionInfo
|
2021-07-05 18:14:12 +00:00
|
|
|
ss := c.PacketConn.Debug.GetSessions()
|
2021-05-15 19:50:56 +00:00
|
|
|
for _, s := range ss {
|
2022-08-06 14:05:12 +00:00
|
|
|
var info SessionInfo
|
2021-05-15 19:50:56 +00:00
|
|
|
info.Key = s.Key
|
2022-09-03 15:55:57 +00:00
|
|
|
info.RXBytes = s.RX
|
|
|
|
info.TXBytes = s.TX
|
|
|
|
info.Uptime = s.Uptime
|
2021-05-15 19:50:56 +00:00
|
|
|
sessions = append(sessions, info)
|
2019-05-19 15:29:04 +00:00
|
|
|
}
|
|
|
|
return sessions
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 02:51:09 +00:00
|
|
|
// Listen starts a new listener (either TCP or TLS). The input should be a url.URL
|
|
|
|
// parsed from a string of the form e.g. "tcp://a.b.c.d:e". In the case of a
|
|
|
|
// link-local address, the interface should be provided as the second argument.
|
2022-09-17 19:07:00 +00:00
|
|
|
func (c *Core) Listen(u *url.URL, sintf string) (*Listener, error) {
|
2023-04-06 20:45:49 +00:00
|
|
|
return c.links.listen(u, sintf)
|
2021-05-24 02:47:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 16:21:02 +00:00
|
|
|
// Address gets the IPv6 address of the Yggdrasil node. This is always a /128
|
2019-09-01 22:47:47 +00:00
|
|
|
// address. The IPv6 address is only relevant when the node is operating as an
|
|
|
|
// IP router and often is meaningless when embedded into an application, unless
|
|
|
|
// that application also implements either VPN functionality or deals with IP
|
|
|
|
// packets specifically.
|
2019-07-28 10:30:24 +00:00
|
|
|
func (c *Core) Address() net.IP {
|
2021-05-08 13:35:58 +00:00
|
|
|
addr := net.IP(address.AddrForKey(c.public)[:])
|
|
|
|
return addr
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subnet gets the routed IPv6 subnet of the Yggdrasil node. This is always a
|
2019-09-01 22:47:47 +00:00
|
|
|
// /64 subnet. The IPv6 subnet is only relevant when the node is operating as an
|
|
|
|
// IP router and often is meaningless when embedded into an application, unless
|
|
|
|
// that application also implements either VPN functionality or deals with IP
|
|
|
|
// packets specifically.
|
2019-07-28 10:30:24 +00:00
|
|
|
func (c *Core) Subnet() net.IPNet {
|
2021-05-08 13:35:58 +00:00
|
|
|
subnet := address.SubnetForKey(c.public)[:]
|
|
|
|
subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0)
|
|
|
|
return net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger sets the output logger of the Yggdrasil node after startup. This
|
2019-09-01 22:47:47 +00:00
|
|
|
// may be useful if you want to redirect the output later. Note that this
|
|
|
|
// expects a Logger from the github.com/gologme/log package and not from Go's
|
|
|
|
// built-in log package.
|
2022-10-02 12:20:39 +00:00
|
|
|
func (c *Core) SetLogger(log Logger) {
|
2019-05-18 16:21:02 +00:00
|
|
|
c.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeer adds a peer. This should be specified in the peer URI format, e.g.:
|
2022-10-02 11:35:43 +00:00
|
|
|
//
|
|
|
|
// tcp://a.b.c.d:e
|
|
|
|
// socks://a.b.c.d:e/f.g.h.i:j
|
|
|
|
//
|
2019-05-18 16:21:02 +00:00
|
|
|
// This adds the peer to the peer list, so that they will be called again if the
|
|
|
|
// connection drops.
|
2023-04-06 20:45:49 +00:00
|
|
|
func (c *Core) AddPeer(u *url.URL, sintf string) error {
|
|
|
|
return c.links.add(u, sintf, linkTypePersistent)
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:35:43 +00:00
|
|
|
// RemovePeer removes a peer. The peer should be specified in URI format, see AddPeer.
|
|
|
|
// The peer is not disconnected immediately.
|
2023-10-22 09:27:41 +00:00
|
|
|
func (c *Core) RemovePeer(u *url.URL, sintf string) error {
|
|
|
|
return c.links.remove(u, sintf, linkTypePersistent)
|
2019-05-20 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 16:21:02 +00:00
|
|
|
// CallPeer calls a peer once. This should be specified in the peer URI format,
|
|
|
|
// e.g.:
|
2022-09-03 10:42:05 +00:00
|
|
|
//
|
|
|
|
// tcp://a.b.c.d:e
|
|
|
|
// socks://a.b.c.d:e/f.g.h.i:j
|
|
|
|
//
|
2019-05-18 16:21:02 +00:00
|
|
|
// This does not add the peer to the peer list, so if the connection drops, the
|
|
|
|
// peer will not be called again automatically.
|
2021-05-24 02:47:12 +00:00
|
|
|
func (c *Core) CallPeer(u *url.URL, sintf string) error {
|
2023-04-06 20:45:49 +00:00
|
|
|
return c.links.add(u, sintf, linkTypeEphemeral)
|
2019-05-18 16:21:02 +00:00
|
|
|
}
|
2021-06-13 09:54:06 +00:00
|
|
|
|
|
|
|
func (c *Core) PublicKey() ed25519.PublicKey {
|
|
|
|
return c.public
|
|
|
|
}
|
|
|
|
|
2021-06-13 10:43:03 +00:00
|
|
|
// Hack to get the admin stuff working, TODO something cleaner
|
|
|
|
|
|
|
|
type AddHandler interface {
|
2022-09-24 11:22:38 +00:00
|
|
|
AddHandler(name, desc string, args []string, handlerfunc AddHandlerFunc) error
|
2021-06-13 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 01:16:57 +00:00
|
|
|
type AddHandlerFunc func(json.RawMessage) (interface{}, error)
|
|
|
|
|
2021-06-13 10:43:03 +00:00
|
|
|
// SetAdmin must be called after Init and before Start.
|
|
|
|
// It sets the admin handler for NodeInfo and the Debug admin functions.
|
|
|
|
func (c *Core) SetAdmin(a AddHandler) error {
|
2022-09-24 11:22:38 +00:00
|
|
|
if err := a.AddHandler(
|
|
|
|
"getNodeInfo", "Request nodeinfo from a remote node by its public key", []string{"key"},
|
|
|
|
c.proto.nodeinfo.nodeInfoAdminHandler,
|
|
|
|
); err != nil {
|
2021-06-13 10:43:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-24 11:22:38 +00:00
|
|
|
if err := a.AddHandler(
|
|
|
|
"debug_remoteGetSelf", "Debug use only", []string{"key"},
|
|
|
|
c.proto.getSelfHandler,
|
|
|
|
); err != nil {
|
2021-06-13 10:43:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-24 11:22:38 +00:00
|
|
|
if err := a.AddHandler(
|
|
|
|
"debug_remoteGetPeers", "Debug use only", []string{"key"},
|
|
|
|
c.proto.getPeersHandler,
|
|
|
|
); err != nil {
|
2021-06-13 10:43:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-24 11:22:38 +00:00
|
|
|
if err := a.AddHandler(
|
2023-05-13 19:44:38 +00:00
|
|
|
"debug_remoteGetTree", "Debug use only", []string{"key"},
|
|
|
|
c.proto.getTreeHandler,
|
2022-09-24 11:22:38 +00:00
|
|
|
); err != nil {
|
2021-06-13 10:43:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|