yggdrasil-go/src/yggdrasil/tcp.go

359 lines
9.1 KiB
Go
Raw Normal View History

2017-12-29 07:16:20 +03:00
package yggdrasil
// This sends packets to peers using TCP as a transport
// It's generally better tested than the UDP implementation
// Using it regularly is insane, but I find TCP easier to test/debug with it
// Updating and optimizing the UDP version is a higher priority
// TODO:
// Something needs to make sure we're getting *valid* packets
// Could be used to DoS (connect, give someone else's keys, spew garbage)
// I guess the "peer" part should watch for link packets, disconnect?
// TCP connections start with a metadata exchange.
// It involves exchanging version numbers and crypto keys
// See version.go for version metadata format
2018-06-13 01:50:08 +03:00
import (
2019-01-13 21:08:41 +03:00
"context"
2018-06-13 01:50:08 +03:00
"fmt"
2018-06-21 18:39:43 +03:00
"math/rand"
2018-06-13 01:50:08 +03:00
"net"
"strings"
2018-06-13 01:50:08 +03:00
"sync"
"time"
"golang.org/x/net/proxy"
"github.com/yggdrasil-network/yggdrasil-go/src/util"
2018-06-13 01:50:08 +03:00
)
2017-12-29 07:16:20 +03:00
const default_timeout = 6 * time.Second
const tcp_ping_interval = (default_timeout * 2 / 3)
2017-12-29 07:16:20 +03:00
// The TCP listener and information about active TCP connections, to avoid duplication.
2019-03-04 20:52:57 +03:00
type tcp struct {
link *link
reconfigure chan chan error
mutex sync.Mutex // Protecting the below
listeners map[string]*TcpListener
calls map[string]struct{}
conns map[linkInfo](chan struct{})
}
2019-03-29 21:18:31 +03:00
// TcpListener is a stoppable TCP listener interface. These are typically
// returned from calls to the ListenTCP() function and are also used internally
// to represent listeners created by the "Listen" configuration option and for
// multicast interfaces.
type TcpListener struct {
Listener net.Listener
Stop chan bool
2017-12-29 07:16:20 +03:00
}
// Wrapper function to set additional options for specific connection types.
2019-03-04 20:52:57 +03:00
func (t *tcp) setExtraOptions(c net.Conn) {
switch sock := c.(type) {
case *net.TCPConn:
sock.SetNoDelay(true)
// TODO something for socks5
default:
}
}
// Returns the address of the listener.
2019-03-04 20:52:57 +03:00
func (t *tcp) getAddr() *net.TCPAddr {
2019-03-04 21:47:40 +03:00
// TODO: Fix this, because this will currently only give a single address
// to multicast.go, which obviously is not great, but right now multicast.go
// doesn't have the ability to send more than one address in a packet either
t.mutex.Lock()
defer t.mutex.Unlock()
for _, l := range t.listeners {
return l.Listener.Addr().(*net.TCPAddr)
2019-03-04 20:52:57 +03:00
}
return nil
2018-05-28 00:13:37 +03:00
}
// Initializes the struct.
2019-03-04 20:52:57 +03:00
func (t *tcp) init(l *link) error {
t.link = l
t.reconfigure = make(chan chan error, 1)
2019-03-04 21:47:40 +03:00
t.mutex.Lock()
t.calls = make(map[string]struct{})
t.conns = make(map[linkInfo](chan struct{}))
t.listeners = make(map[string]*TcpListener)
2019-03-04 21:47:40 +03:00
t.mutex.Unlock()
2019-03-04 20:52:57 +03:00
2018-12-30 18:21:09 +03:00
go func() {
for {
2019-03-04 20:52:57 +03:00
e := <-t.reconfigure
t.link.core.config.Mutex.RLock()
added := util.Difference(t.link.core.config.Current.Listen, t.link.core.config.Previous.Listen)
deleted := util.Difference(t.link.core.config.Previous.Listen, t.link.core.config.Current.Listen)
t.link.core.config.Mutex.RUnlock()
2019-03-04 21:47:40 +03:00
if len(added) > 0 || len(deleted) > 0 {
for _, a := range added {
if a[:6] != "tcp://" {
continue
}
if _, err := t.listen(a[6:]); err != nil {
e <- err
continue
}
}
for _, d := range deleted {
if d[:6] != "tcp://" {
continue
}
t.mutex.Lock()
if listener, ok := t.listeners[d[6:]]; ok {
t.mutex.Unlock()
listener.Stop <- true
} else {
t.mutex.Unlock()
}
}
e <- nil
2019-01-15 11:51:19 +03:00
} else {
e <- nil
2018-12-30 18:21:09 +03:00
}
}
}()
t.link.core.config.Mutex.RLock()
defer t.link.core.config.Mutex.RUnlock()
for _, listenaddr := range t.link.core.config.Current.Listen {
2019-03-04 20:52:57 +03:00
if listenaddr[:6] != "tcp://" {
continue
}
if _, err := t.listen(listenaddr[6:]); err != nil {
2019-03-04 20:52:57 +03:00
return err
}
}
return nil
2018-12-30 18:21:09 +03:00
}
func (t *tcp) listen(listenaddr string) (*TcpListener, error) {
2018-12-30 18:21:09 +03:00
var err error
2019-01-13 21:08:41 +03:00
ctx := context.Background()
lc := net.ListenConfig{
2019-03-04 20:52:57 +03:00
Control: t.tcpContext,
2019-01-13 21:08:41 +03:00
}
2019-03-04 20:52:57 +03:00
listener, err := lc.Listen(ctx, "tcp", listenaddr)
2018-04-19 17:30:40 +03:00
if err == nil {
l := TcpListener{
Listener: listener,
Stop: make(chan bool),
}
go t.listener(&l, listenaddr)
return &l, nil
2018-01-05 01:37:51 +03:00
}
2018-05-28 00:13:37 +03:00
return nil, err
2017-12-29 07:16:20 +03:00
}
// Runs the listener, which spawns off goroutines for incoming connections.
func (t *tcp) listener(l *TcpListener, listenaddr string) {
if l == nil {
2019-03-04 20:52:57 +03:00
return
}
// Track the listener so that we can find it again in future
t.mutex.Lock()
if _, isIn := t.listeners[listenaddr]; isIn {
t.mutex.Unlock()
l.Listener.Close()
return
} else {
t.listeners[listenaddr] = l
t.mutex.Unlock()
}
// And here we go!
accepted := make(chan bool)
2019-03-08 13:26:46 +03:00
defer func() {
t.link.core.log.Infoln("Stopping TCP listener on:", l.Listener.Addr().String())
l.Listener.Close()
2019-03-08 13:26:46 +03:00
t.mutex.Lock()
delete(t.listeners, listenaddr)
t.mutex.Unlock()
}()
t.link.core.log.Infoln("Listening for TCP on:", l.Listener.Addr().String())
2018-01-05 01:37:51 +03:00
for {
var sock net.Conn
var err error
// Listen in a separate goroutine, as that way it does not block us from
// receiving "stop" events
go func() {
sock, err = l.Listener.Accept()
accepted <- true
}()
// Wait for either an accepted connection, or a message telling us to stop
// the TCP listener
2018-12-30 18:21:09 +03:00
select {
case <-accepted:
if err != nil {
t.link.core.log.Errorln("Failed to accept connection:", err)
return
}
2019-03-08 13:26:46 +03:00
go t.handler(sock, true, nil)
case <-l.Stop:
2018-12-30 18:21:09 +03:00
return
2018-01-05 01:37:51 +03:00
}
}
2017-12-29 07:16:20 +03:00
}
// Checks if we already are calling this address
2019-03-04 20:52:57 +03:00
func (t *tcp) isAlreadyCalling(saddr string) bool {
t.mutex.Lock()
defer t.mutex.Unlock()
_, isIn := t.calls[saddr]
return isIn
}
// Checks if a connection already exists.
// If not, it adds it to the list of active outgoing calls (to block future attempts) and dials the address.
// If the dial is successful, it launches the handler.
// When finished, it removes the outgoing call, so reconnection attempts can be made later.
// This all happens in a separate goroutine that it spawns.
func (t *tcp) call(saddr string, options interface{}, sintf string) {
2018-01-05 01:37:51 +03:00
go func() {
callname := saddr
if sintf != "" {
callname = fmt.Sprintf("%s/%s", saddr, sintf)
}
2019-03-04 20:52:57 +03:00
if t.isAlreadyCalling(callname) {
2018-06-14 17:11:34 +03:00
return
}
2019-03-04 20:52:57 +03:00
t.mutex.Lock()
t.calls[callname] = struct{}{}
t.mutex.Unlock()
defer func() {
// Block new calls for a little while, to mitigate livelock scenarios
time.Sleep(default_timeout)
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
2019-03-04 20:52:57 +03:00
t.mutex.Lock()
delete(t.calls, callname)
t.mutex.Unlock()
}()
2018-06-14 17:11:34 +03:00
var conn net.Conn
var err error
socksaddr, issocks := options.(string)
if issocks {
2018-09-25 21:46:06 +03:00
if sintf != "" {
return
}
2019-03-08 13:26:46 +03:00
dialerdst, er := net.ResolveTCPAddr("tcp", socksaddr)
if er != nil {
return
}
var dialer proxy.Dialer
2019-03-08 13:26:46 +03:00
dialer, err = proxy.SOCKS5("tcp", dialerdst.String(), nil, proxy.Direct)
if err != nil {
return
}
2018-06-14 17:11:34 +03:00
conn, err = dialer.Dial("tcp", saddr)
if err != nil {
return
}
conn = &wrappedConn{
c: conn,
raddr: &wrappedAddr{
network: "tcp",
addr: saddr,
},
}
2019-03-08 13:26:46 +03:00
t.handler(conn, false, dialerdst.String())
2018-06-14 17:11:34 +03:00
} else {
dst, err := net.ResolveTCPAddr("tcp", saddr)
if err != nil {
return
}
if dst.IP.IsLinkLocalUnicast() {
dst.Zone = sintf
if dst.Zone == "" {
return
}
}
2019-01-13 21:08:41 +03:00
dialer := net.Dialer{
2019-03-04 20:52:57 +03:00
Control: t.tcpContext,
2019-01-13 21:08:41 +03:00
}
if sintf != "" {
ief, err := net.InterfaceByName(sintf)
if err != nil {
return
}
if ief.Flags&net.FlagUp == 0 {
return
}
addrs, err := ief.Addrs()
if err == nil {
for addrindex, addr := range addrs {
src, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
2019-01-18 02:06:59 +03:00
if src.Equal(dst.IP) {
continue
}
if !src.IsGlobalUnicast() && !src.IsLinkLocalUnicast() {
continue
}
bothglobal := src.IsGlobalUnicast() == dst.IP.IsGlobalUnicast()
bothlinklocal := src.IsLinkLocalUnicast() == dst.IP.IsLinkLocalUnicast()
if !bothglobal && !bothlinklocal {
continue
}
if (src.To4() != nil) != (dst.IP.To4() != nil) {
continue
}
if bothglobal || bothlinklocal || addrindex == len(addrs)-1 {
dialer.LocalAddr = &net.TCPAddr{
IP: src,
Port: 0,
Zone: sintf,
}
2019-01-18 02:06:59 +03:00
break
}
}
if dialer.LocalAddr == nil {
return
}
}
}
conn, err = dialer.Dial("tcp", dst.String())
2018-01-05 01:37:51 +03:00
if err != nil {
t.link.core.log.Debugln("Failed to dial TCP:", err)
2018-01-05 01:37:51 +03:00
return
}
2019-03-08 13:26:46 +03:00
t.handler(conn, false, nil)
2018-01-05 01:37:51 +03:00
}
}()
2017-12-29 07:16:20 +03:00
}
2019-03-08 13:26:46 +03:00
func (t *tcp) handler(sock net.Conn, incoming bool, options interface{}) {
defer sock.Close()
2019-03-04 20:52:57 +03:00
t.setExtraOptions(sock)
stream := stream{}
2019-01-23 18:16:22 +03:00
stream.init(sock)
local, _, _ := net.SplitHostPort(sock.LocalAddr().String())
remote, _, _ := net.SplitHostPort(sock.RemoteAddr().String())
force := net.ParseIP(strings.Split(remote, "%")[0]).IsLinkLocalUnicast()
2019-03-08 13:26:46 +03:00
var name string
var proto string
if socksaddr, issocks := options.(string); issocks {
name = "socks://" + socksaddr + "/" + sock.RemoteAddr().String()
proto = "socks"
} else {
name = "tcp://" + sock.RemoteAddr().String()
proto = "tcp"
}
link, err := t.link.core.link.create(&stream, name, proto, local, remote, incoming, force)
if err != nil {
2019-03-04 20:52:57 +03:00
t.link.core.log.Println(err)
panic(err)
}
2019-03-04 20:52:57 +03:00
t.link.core.log.Debugln("DEBUG: starting handler for", name)
2019-01-23 06:48:43 +03:00
err = link.handler()
2019-03-04 20:52:57 +03:00
t.link.core.log.Debugln("DEBUG: stopped handler for", name, err)
}