1 //go:build !noyggdrasil
5 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
6 Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, version 3 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 iwt "github.com/Arceliar/ironwood/types"
31 gologme "github.com/gologme/log"
32 "github.com/neilalexander/utp"
33 ycfg "github.com/yggdrasil-network/yggdrasil-go/src/config"
34 ycore "github.com/yggdrasil-network/yggdrasil-go/src/core"
35 "golang.org/x/crypto/ed25519"
38 var glog *gologme.Logger
41 glog = gologme.New(log.Writer(), "yggdrasil: ", gologme.Lmsgprefix)
42 glog.EnableLevel("warn")
43 glog.EnableLevel("error")
44 glog.EnableLevel("info")
47 func NewYggdrasilConn(aliases map[string]string, in string) (ConnDeadlined, error) {
48 // pub;prv;peer[, ...]
49 cols := strings.Split(in, ";")
51 return nil, errors.New("invalid yggdrasil: address format")
53 pubHex, prvHex, peersRaw := cols[0], cols[1], cols[2]
54 if v, ok := aliases[pubHex]; ok {
57 if v, ok := aliases[prvHex]; ok {
60 if v, ok := aliases[peersRaw]; ok {
63 peers := strings.Split(peersRaw, ",")
64 for i, peer := range peers {
65 if v, ok := aliases[peer]; ok {
69 addrRaw, err := hex.DecodeString(pubHex)
73 cfg := ycfg.NodeConfig{
76 NodeInfo: map[string]interface{}{"name": "NNCP " + Version},
77 NodeInfoPrivacy: true,
80 if err := core.Start(&cfg, glog); err != nil {
83 utpSock, err := utp.NewSocketFromPacketConn(core)
87 addr := make(iwt.Addr, ed25519.PublicKeySize)
89 return utpSock.DialAddr(addr)
92 func NewYggdrasilListener(aliases map[string]string, in string) (net.Listener, error) {
93 // prv;bind[, ...];[pub, ...];[peer, ...]
94 cols := strings.Split(in, ";")
96 return nil, errors.New("invalid -yggdrasil address format")
98 prvHex, bindsRaw, pubsRaw, peersRaw := cols[0], cols[1], cols[2], cols[3]
99 if v, ok := aliases[prvHex]; ok {
102 if v, ok := aliases[bindsRaw]; ok {
105 binds := strings.Split(bindsRaw, ",")
106 for i, bind := range binds {
107 if v, ok := aliases[bind]; ok {
111 if v, ok := aliases[pubsRaw]; ok {
114 pubs := strings.Split(pubsRaw, ",")
115 if len(pubs) == 1 && pubs[0] == "" {
118 for i, pub := range pubs {
119 if v, ok := aliases[pub]; ok {
123 if v, ok := aliases[peersRaw]; ok {
126 peers := strings.Split(peersRaw, ",")
127 if len(peers) == 1 && peers[0] == "" {
130 for i, peer := range peers {
131 if v, ok := aliases[peer]; ok {
135 cfg := ycfg.NodeConfig{
138 AllowedPublicKeys: pubs,
140 NodeInfo: map[string]interface{}{"name": "NNCP " + Version},
141 NodeInfoPrivacy: true,
143 core := &ycore.Core{}
144 if err := core.Start(&cfg, glog); err != nil {
147 utpSock, err := utp.NewSocketFromPacketConn(core)