5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 00:11:36 +00:00

get the tcp version compiling again

This commit is contained in:
Arceliar 2018-02-03 12:25:02 -06:00
parent 4430ba8520
commit 6dfceca433

View File

@ -10,6 +10,7 @@ import "net"
import "os" import "os"
import "os/signal" import "os/signal"
import "time" import "time"
import "regexp"
import _ "net/http/pprof" import _ "net/http/pprof"
import "net/http" import "net/http"
@ -25,16 +26,19 @@ import . "yggdrasil"
* It can generate a new config (--genconf) * It can generate a new config (--genconf)
* It can read a config from stdin (--useconf) * It can read a config from stdin (--useconf)
* It can run with an automatic config (--autoconf) * It can run with an automatic config (--autoconf)
*/ */
type nodeConfig struct { type nodeConfig struct {
Listen string Listen string
AdminListen string
Peers []string Peers []string
BoxPub string BoxPub string
BoxPriv string BoxPriv string
SigPub string SigPub string
SigPriv string SigPriv string
Multicast bool Multicast bool
LinkLocal string
IfName string
} }
type node struct { type node struct {
@ -44,25 +48,44 @@ type node struct {
func (n *node) init(cfg *nodeConfig, logger *log.Logger) { func (n *node) init(cfg *nodeConfig, logger *log.Logger) {
boxPub, err := hex.DecodeString(cfg.BoxPub) boxPub, err := hex.DecodeString(cfg.BoxPub)
if err != nil { panic(err) } if err != nil {
panic(err)
}
boxPriv, err := hex.DecodeString(cfg.BoxPriv) boxPriv, err := hex.DecodeString(cfg.BoxPriv)
if err != nil { panic(err) } if err != nil {
panic(err)
}
sigPub, err := hex.DecodeString(cfg.SigPub) sigPub, err := hex.DecodeString(cfg.SigPub)
if err != nil { panic(err) } if err != nil {
panic(err)
}
sigPriv, err := hex.DecodeString(cfg.SigPriv) sigPriv, err := hex.DecodeString(cfg.SigPriv)
if err != nil { panic(err) } if err != nil {
panic(err)
}
n.core.DEBUG_init(boxPub, boxPriv, sigPub, sigPriv) n.core.DEBUG_init(boxPub, boxPriv, sigPub, sigPriv)
n.core.DEBUG_setLogger(logger) n.core.DEBUG_setLogger(logger)
ifceExpr, err := regexp.Compile(cfg.LinkLocal)
if err != nil {
panic(err)
}
n.core.DEBUG_setIfceExpr(ifceExpr)
logger.Println("Starting interface...") logger.Println("Starting interface...")
n.core.DEBUG_setupAndStartGlobalTCPInterface(cfg.Listen) n.core.DEBUG_setupAndStartGlobalTCPInterface(cfg.Listen)
logger.Println("Started interface") logger.Println("Started interface")
go func () { logger.Println("Starting admin socket...")
if len(cfg.Peers) == 0 { return } n.core.DEBUG_setupAndStartAdminInterface(cfg.AdminListen)
logger.Println("Started admin socket")
go func() {
if len(cfg.Peers) == 0 {
return
}
for { for {
for _, p := range cfg.Peers { for _, p := range cfg.Peers {
n.core.DEBUG_addTCPConn(p) n.core.DEBUG_addTCPConn(p)
time.Sleep(time.Second) time.Sleep(time.Second)
} }
time.Sleep(time.Minute)
} }
}() }()
} }
@ -73,41 +96,64 @@ func generateConfig() *nodeConfig {
spub, spriv := core.DEBUG_newSigKeys() spub, spriv := core.DEBUG_newSigKeys()
cfg := nodeConfig{} cfg := nodeConfig{}
cfg.Listen = "[::]:0" cfg.Listen = "[::]:0"
cfg.AdminListen = "localhost:9001"
cfg.BoxPub = hex.EncodeToString(bpub[:]) cfg.BoxPub = hex.EncodeToString(bpub[:])
cfg.BoxPriv = hex.EncodeToString(bpriv[:]) cfg.BoxPriv = hex.EncodeToString(bpriv[:])
cfg.SigPub = hex.EncodeToString(spub[:]) cfg.SigPub = hex.EncodeToString(spub[:])
cfg.SigPriv = hex.EncodeToString(spriv[:]) cfg.SigPriv = hex.EncodeToString(spriv[:])
cfg.Peers = []string{} cfg.Peers = []string{}
cfg.Multicast = true cfg.Multicast = true
cfg.LinkLocal = ""
cfg.IfName = "auto"
return &cfg return &cfg
} }
func doGenconf() string { func doGenconf() string {
cfg := generateConfig() cfg := generateConfig()
bs, err := json.MarshalIndent(cfg, "", " ") bs, err := json.MarshalIndent(cfg, "", " ")
if err != nil { panic(err) } if err != nil {
panic(err)
}
return string(bs) return string(bs)
} }
var multicastAddr = "[ff02::114]:9001" var multicastAddr = "[ff02::114]:9001"
func (n *node) listen() { func (n *node) listen() {
groupAddr, err := net.ResolveUDPAddr("udp", multicastAddr) groupAddr, err := net.ResolveUDPAddr("udp6", multicastAddr)
if err != nil { panic(err) } if err != nil {
panic(err)
}
bs := make([]byte, 2048) bs := make([]byte, 2048)
for { for {
nBytes, rcm, fromAddr, err := n.sock.ReadFrom(bs) nBytes, rcm, fromAddr, err := n.sock.ReadFrom(bs)
if err != nil { panic(err) } if err != nil {
panic(err)
}
//if rcm == nil { continue } // wat //if rcm == nil { continue } // wat
//fmt.Println("DEBUG:", "packet from:", fromAddr.String()) //fmt.Println("DEBUG:", "packet from:", fromAddr.String())
if !rcm.Dst.IsLinkLocalMulticast() { continue } if rcm != nil {
if !rcm.Dst.Equal(groupAddr.IP) { continue } // Windows can't set the flag needed to return a non-nil value here
// So only make these checks if we get something useful back
// TODO? Skip them always, I'm not sure if they're really needed...
if !rcm.Dst.IsLinkLocalMulticast() {
continue
}
if !rcm.Dst.Equal(groupAddr.IP) {
continue
}
}
anAddr := string(bs[:nBytes]) anAddr := string(bs[:nBytes])
addr, err := net.ResolveTCPAddr("tcp", anAddr) addr, err := net.ResolveTCPAddr("tcp6", anAddr)
if err != nil { panic(err) ; continue } // Panic for testing, remove later if err != nil {
panic(err)
continue
} // Panic for testing, remove later
from := fromAddr.(*net.UDPAddr) from := fromAddr.(*net.UDPAddr)
//fmt.Println("DEBUG:", "heard:", addr.IP.String(), "from:", from.IP.String()) //fmt.Println("DEBUG:", "heard:", addr.IP.String(), "from:", from.IP.String())
if addr.IP.String() != from.IP.String() { continue } if addr.IP.String() != from.IP.String() {
continue
}
addr.Zone = from.Zone addr.Zone = from.Zone
saddr := addr.String() saddr := addr.String()
//if _, isIn := n.peers[saddr]; isIn { continue } //if _, isIn := n.peers[saddr]; isIn { continue }
@ -118,26 +164,38 @@ func (n *node) listen() {
} }
func (n *node) announce() { func (n *node) announce() {
groupAddr, err := net.ResolveUDPAddr("udp", multicastAddr) groupAddr, err := net.ResolveUDPAddr("udp6", multicastAddr)
if err != nil { panic(err) } if err != nil {
tcpaddr := n.core.DEBUG_getGlobalTCPAddr() panic(err)
anAddr, err := net.ResolveTCPAddr("tcp", tcpaddr.String()) }
if err != nil { panic(err) } var anAddr net.TCPAddr
tcpAddr := n.core.DEBUG_getGlobalTCPAddr()
anAddr.Port = tcpAddr.Port
destAddr, err := net.ResolveUDPAddr("udp6", multicastAddr) destAddr, err := net.ResolveUDPAddr("udp6", multicastAddr)
if err != nil { panic(err) } if err != nil {
panic(err)
}
for { for {
ifaces, err := net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { panic(err) } if err != nil {
panic(err)
}
for _, iface := range ifaces { for _, iface := range ifaces {
n.sock.JoinGroup(&iface, groupAddr) n.sock.JoinGroup(&iface, groupAddr)
//err := n.sock.JoinGroup(&iface, groupAddr) //err := n.sock.JoinGroup(&iface, groupAddr)
//if err != nil { panic(err) } //if err != nil { panic(err) }
addrs, err := iface.Addrs() addrs, err := iface.Addrs()
if err != nil { panic(err) } if err != nil {
panic(err)
}
for _, addr := range addrs { for _, addr := range addrs {
addrIP, _, _ := net.ParseCIDR(addr.String()) addrIP, _, _ := net.ParseCIDR(addr.String())
if addrIP.To4() != nil { continue } // IPv6 only if addrIP.To4() != nil {
if !addrIP.IsLinkLocalUnicast() { continue } continue
} // IPv6 only
if !addrIP.IsLinkLocalUnicast() {
continue
}
anAddr.IP = addrIP anAddr.IP = addrIP
anAddr.Zone = iface.Name anAddr.Zone = iface.Name
destAddr.Zone = iface.Name destAddr.Zone = iface.Name
@ -160,17 +218,27 @@ func main() {
flag.Parse() flag.Parse()
var cfg *nodeConfig var cfg *nodeConfig
switch { switch {
case *autoconf: cfg = generateConfig() case *autoconf:
cfg = generateConfig()
case *useconf: case *useconf:
config, err := ioutil.ReadAll(os.Stdin) config, err := ioutil.ReadAll(os.Stdin)
if err != nil { panic(err) } if err != nil {
decoder := json.NewDecoder(bytes.NewReader(config)) panic(err)
err = decoder.Decode(&cfg) }
if err != nil { panic(err) } decoder := json.NewDecoder(bytes.NewReader(config))
case *genconf: fmt.Println(doGenconf()) cfg = generateConfig()
default: flag.PrintDefaults() err = decoder.Decode(cfg)
if err != nil {
panic(err)
}
case *genconf:
fmt.Println(doGenconf())
default:
flag.PrintDefaults()
}
if cfg == nil {
return
} }
if cfg == nil { return }
logger := log.New(os.Stdout, "", log.Flags()) logger := log.New(os.Stdout, "", log.Flags())
if *pprof { if *pprof {
runtime.SetBlockProfileRate(1) runtime.SetBlockProfileRate(1)
@ -181,8 +249,8 @@ func main() {
n := node{} n := node{}
n.init(cfg, logger) n.init(cfg, logger)
logger.Println("Starting tun...") logger.Println("Starting tun...")
//n.core.DEBUG_startTun() // 1280, the smallest supported MTU n.core.DEBUG_startTun(cfg.IfName) // 1280, the smallest supported MTU
n.core.DEBUG_startTunWithMTU(65535) // Largest supported MTU //n.core.DEBUG_startTunWithMTU(cfg.IfName, 65535) // Largest supported MTU
defer func() { defer func() {
logger.Println("Closing...") logger.Println("Closing...")
n.core.DEBUG_stopTun() n.core.DEBUG_stopTun()
@ -190,20 +258,26 @@ func main() {
logger.Println("Started...") logger.Println("Started...")
if cfg.Multicast { if cfg.Multicast {
addr, err := net.ResolveUDPAddr("udp", multicastAddr) addr, err := net.ResolveUDPAddr("udp", multicastAddr)
if err != nil { panic(err) } if err != nil {
panic(err)
}
listenString := fmt.Sprintf("[::]:%v", addr.Port) listenString := fmt.Sprintf("[::]:%v", addr.Port)
conn, err := net.ListenPacket("udp6", listenString) conn, err := net.ListenPacket("udp6", listenString)
if err != nil { panic(err) } if err != nil {
panic(err)
}
//defer conn.Close() // Let it close on its own when the application exits //defer conn.Close() // Let it close on its own when the application exits
n.sock = ipv6.NewPacketConn(conn) n.sock = ipv6.NewPacketConn(conn)
if err = n.sock.SetControlMessage(ipv6.FlagDst, true) ; err != nil { panic(err) } if err = n.sock.SetControlMessage(ipv6.FlagDst, true); err != nil {
// Windows can't set this flag, so we need to handle it in other ways
//panic(err)
}
go n.listen() go n.listen()
go n.announce() go n.announce()
} }
// Catch interrupt to exit gracefully // Catch interrupt to exit gracefully
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt, os.Kill)
<-c <-c
logger.Println("Stopping...") logger.Println("Stopping...")
} }