5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 07:30:27 +00:00

Merge pull request #320 from neilalexander/link

link.go: Connect/disconnect logging, check AllowedEncryptionKeys
This commit is contained in:
Neil Alexander 2019-02-01 07:57:27 +00:00 committed by GitHub
commit 6d83d970bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 14 deletions

View File

@ -54,14 +54,14 @@ func (a *awdl) init(l *link) error {
return nil return nil
} }
func (a *awdl) create(name, local, remote string) (*awdlInterface, error) { func (a *awdl) create(name, local, remote string, incoming bool) (*awdlInterface, error) {
rwc := awdlReadWriteCloser{ rwc := awdlReadWriteCloser{
fromAWDL: make(chan []byte, 1), fromAWDL: make(chan []byte, 1),
toAWDL: make(chan []byte, 1), toAWDL: make(chan []byte, 1),
} }
s := stream{} s := stream{}
s.init(rwc) s.init(rwc)
linkif, err := a.link.create(&s, name, "awdl", local, remote) linkif, err := a.link.create(&s, name, "awdl", local, remote, incoming, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,12 +1,16 @@
package yggdrasil package yggdrasil
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"net"
"strings"
"sync" "sync"
//"sync/atomic" //"sync/atomic"
"time" "time"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/util" "github.com/yggdrasil-network/yggdrasil-go/src/util"
) )
@ -41,6 +45,8 @@ type linkInterface struct {
peer *peer peer *peer
msgIO linkInterfaceMsgIO msgIO linkInterfaceMsgIO
info linkInfo info linkInfo
incoming bool
force bool
closed chan struct{} closed chan struct{}
} }
@ -58,7 +64,7 @@ func (l *link) init(c *Core) error {
return nil return nil
} }
func (l *link) create(msgIO linkInterfaceMsgIO, name, linkType, local, remote string) (*linkInterface, error) { func (l *link) create(msgIO linkInterfaceMsgIO, name, linkType, local, remote string, incoming, force bool) (*linkInterface, error) {
// Technically anything unique would work for names, but lets pick something human readable, just for debugging // Technically anything unique would work for names, but lets pick something human readable, just for debugging
intf := linkInterface{ intf := linkInterface{
name: name, name: name,
@ -69,6 +75,8 @@ func (l *link) create(msgIO linkInterfaceMsgIO, name, linkType, local, remote st
local: local, local: local,
remote: remote, remote: remote,
}, },
incoming: incoming,
force: force,
} }
//l.interfaces[intf.name] = &intf //l.interfaces[intf.name] = &intf
//go intf.start() //go intf.start()
@ -101,6 +109,13 @@ func (intf *linkInterface) handler() error {
intf.link.core.log.Errorln("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer)) intf.link.core.log.Errorln("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
return errors.New("failed to connect: wrong version") return errors.New("failed to connect: wrong version")
} }
// Check if we're authorized to connect to this key / IP
if !intf.force && !intf.link.core.peers.isAllowedEncryptionPublicKey(&meta.box) {
intf.link.core.log.Debugf("%s connection to %s forbidden: AllowedEncryptionPublicKeys does not contain key %s",
strings.ToUpper(intf.info.linkType), intf.info.remote, hex.EncodeToString(meta.box[:]))
intf.msgIO.close()
return nil
}
// Check if we already have a link to this node // Check if we already have a link to this node
intf.info.box = meta.box intf.info.box = meta.box
intf.info.sig = meta.sig intf.info.sig = meta.sig
@ -143,7 +158,17 @@ func (intf *linkInterface) handler() error {
out <- msg out <- msg
} }
intf.peer.linkOut = make(chan []byte, 1) intf.peer.linkOut = make(chan []byte, 1)
intf.peer.close = func() { intf.msgIO.close() } themAddr := address.AddrForNodeID(crypto.GetNodeID(&intf.info.box))
themAddrString := net.IP(themAddr[:]).String()
themString := fmt.Sprintf("%s@%s", themAddrString, intf.info.remote)
intf.peer.close = func() {
intf.msgIO.close()
intf.link.core.log.Infof("Disconnected %s: %s, source %s",
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
}
intf.link.core.log.Infof("Connected %s: %s, source %s",
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
// Start the link loop
go intf.peer.linkLoop() go intf.peer.linkLoop()
// Start the writer // Start the writer
signalReady := make(chan struct{}, 1) signalReady := make(chan struct{}, 1)

View File

@ -5,10 +5,11 @@ package yggdrasil
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"log"
"os" "os"
"time" "time"
"github.com/gologme/log"
hjson "github.com/hjson/hjson-go" hjson "github.com/hjson/hjson-go"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/config"

View File

@ -29,8 +29,8 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
return len(p), nil return len(p), nil
} }
func (c *Core) AWDLCreateInterface(name, local, remote string) error { func (c *Core) AWDLCreateInterface(name, local, remote string, incoming bool) error {
if intf, err := c.link.awdl.create(name, local, remote); err != nil || intf == nil { if intf, err := c.link.awdl.create(name, local, remote, incoming); err != nil || intf == nil {
c.log.Println("c.link.awdl.create:", err) c.log.Println("c.link.awdl.create:", err)
return err return err
} }

View File

@ -284,8 +284,9 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
stream.init(sock) stream.init(sock)
local, _, _ := net.SplitHostPort(sock.LocalAddr().String()) local, _, _ := net.SplitHostPort(sock.LocalAddr().String())
remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String()) remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String())
remotelinklocal := net.ParseIP(remote).IsLinkLocalUnicast()
name := "tcp://" + sock.RemoteAddr().String() name := "tcp://" + sock.RemoteAddr().String()
link, err := iface.core.link.create(&stream, name, "tcp", local, remote) link, err := iface.core.link.create(&stream, name, "tcp", local, remote, incoming, remotelinklocal)
if err != nil { if err != nil {
iface.core.log.Println(err) iface.core.log.Println(err)
panic(err) panic(err)