mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-22 10:40:27 +00:00
get the tcp version compiling again
This commit is contained in:
parent
4430ba8520
commit
6dfceca433
@ -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,130 +26,187 @@ 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
|
||||||
Peers []string
|
AdminListen string
|
||||||
BoxPub string
|
Peers []string
|
||||||
BoxPriv string
|
BoxPub string
|
||||||
SigPub string
|
BoxPriv string
|
||||||
SigPriv string
|
SigPub string
|
||||||
Multicast bool
|
SigPriv string
|
||||||
|
Multicast bool
|
||||||
|
LinkLocal string
|
||||||
|
IfName string
|
||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
type node struct {
|
||||||
core Core
|
core Core
|
||||||
sock *ipv6.PacketConn
|
sock *ipv6.PacketConn
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
boxPriv, err := hex.DecodeString(cfg.BoxPriv)
|
panic(err)
|
||||||
if err != nil { panic(err) }
|
}
|
||||||
sigPub, err := hex.DecodeString(cfg.SigPub)
|
boxPriv, err := hex.DecodeString(cfg.BoxPriv)
|
||||||
if err != nil { panic(err) }
|
if err != nil {
|
||||||
sigPriv, err := hex.DecodeString(cfg.SigPriv)
|
panic(err)
|
||||||
if err != nil { panic(err) }
|
}
|
||||||
n.core.DEBUG_init(boxPub, boxPriv, sigPub, sigPriv)
|
sigPub, err := hex.DecodeString(cfg.SigPub)
|
||||||
n.core.DEBUG_setLogger(logger)
|
if err != nil {
|
||||||
logger.Println("Starting interface...")
|
panic(err)
|
||||||
n.core.DEBUG_setupAndStartGlobalTCPInterface(cfg.Listen)
|
}
|
||||||
logger.Println("Started interface")
|
sigPriv, err := hex.DecodeString(cfg.SigPriv)
|
||||||
go func () {
|
if err != nil {
|
||||||
if len(cfg.Peers) == 0 { return }
|
panic(err)
|
||||||
for {
|
}
|
||||||
for _, p := range cfg.Peers {
|
n.core.DEBUG_init(boxPub, boxPriv, sigPub, sigPriv)
|
||||||
n.core.DEBUG_addTCPConn(p)
|
n.core.DEBUG_setLogger(logger)
|
||||||
time.Sleep(time.Second)
|
ifceExpr, err := regexp.Compile(cfg.LinkLocal)
|
||||||
}
|
if err != nil {
|
||||||
}
|
panic(err)
|
||||||
}()
|
}
|
||||||
|
n.core.DEBUG_setIfceExpr(ifceExpr)
|
||||||
|
logger.Println("Starting interface...")
|
||||||
|
n.core.DEBUG_setupAndStartGlobalTCPInterface(cfg.Listen)
|
||||||
|
logger.Println("Started interface")
|
||||||
|
logger.Println("Starting admin socket...")
|
||||||
|
n.core.DEBUG_setupAndStartAdminInterface(cfg.AdminListen)
|
||||||
|
logger.Println("Started admin socket")
|
||||||
|
go func() {
|
||||||
|
if len(cfg.Peers) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
for _, p := range cfg.Peers {
|
||||||
|
n.core.DEBUG_addTCPConn(p)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateConfig() *nodeConfig {
|
func generateConfig() *nodeConfig {
|
||||||
core := Core{}
|
core := Core{}
|
||||||
bpub, bpriv := core.DEBUG_newBoxKeys()
|
bpub, bpriv := core.DEBUG_newBoxKeys()
|
||||||
spub, spriv := core.DEBUG_newSigKeys()
|
spub, spriv := core.DEBUG_newSigKeys()
|
||||||
cfg := nodeConfig{}
|
cfg := nodeConfig{}
|
||||||
cfg.Listen = "[::]:0"
|
cfg.Listen = "[::]:0"
|
||||||
cfg.BoxPub = hex.EncodeToString(bpub[:])
|
cfg.AdminListen = "localhost:9001"
|
||||||
cfg.BoxPriv = hex.EncodeToString(bpriv[:])
|
cfg.BoxPub = hex.EncodeToString(bpub[:])
|
||||||
cfg.SigPub = hex.EncodeToString(spub[:])
|
cfg.BoxPriv = hex.EncodeToString(bpriv[:])
|
||||||
cfg.SigPriv = hex.EncodeToString(spriv[:])
|
cfg.SigPub = hex.EncodeToString(spub[:])
|
||||||
cfg.Peers = []string{}
|
cfg.SigPriv = hex.EncodeToString(spriv[:])
|
||||||
cfg.Multicast = true
|
cfg.Peers = []string{}
|
||||||
return &cfg
|
cfg.Multicast = true
|
||||||
|
cfg.LinkLocal = ""
|
||||||
|
cfg.IfName = "auto"
|
||||||
|
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 {
|
||||||
return string(bs)
|
panic(err)
|
||||||
|
}
|
||||||
|
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 {
|
||||||
bs := make([]byte, 2048)
|
panic(err)
|
||||||
for {
|
}
|
||||||
nBytes, rcm, fromAddr, err := n.sock.ReadFrom(bs)
|
bs := make([]byte, 2048)
|
||||||
if err != nil { panic(err) }
|
for {
|
||||||
//if rcm == nil { continue } // wat
|
nBytes, rcm, fromAddr, err := n.sock.ReadFrom(bs)
|
||||||
//fmt.Println("DEBUG:", "packet from:", fromAddr.String())
|
if err != nil {
|
||||||
if !rcm.Dst.IsLinkLocalMulticast() { continue }
|
panic(err)
|
||||||
if !rcm.Dst.Equal(groupAddr.IP) { continue }
|
}
|
||||||
anAddr := string(bs[:nBytes])
|
//if rcm == nil { continue } // wat
|
||||||
addr, err := net.ResolveTCPAddr("tcp", anAddr)
|
//fmt.Println("DEBUG:", "packet from:", fromAddr.String())
|
||||||
if err != nil { panic(err) ; continue } // Panic for testing, remove later
|
if rcm != nil {
|
||||||
from := fromAddr.(*net.UDPAddr)
|
// Windows can't set the flag needed to return a non-nil value here
|
||||||
//fmt.Println("DEBUG:", "heard:", addr.IP.String(), "from:", from.IP.String())
|
// So only make these checks if we get something useful back
|
||||||
if addr.IP.String() != from.IP.String() { continue }
|
// TODO? Skip them always, I'm not sure if they're really needed...
|
||||||
addr.Zone = from.Zone
|
if !rcm.Dst.IsLinkLocalMulticast() {
|
||||||
saddr := addr.String()
|
continue
|
||||||
//if _, isIn := n.peers[saddr]; isIn { continue }
|
}
|
||||||
//n.peers[saddr] = struct{}{}
|
if !rcm.Dst.Equal(groupAddr.IP) {
|
||||||
n.core.DEBUG_addTCPConn(saddr)
|
continue
|
||||||
//fmt.Println("DEBUG:", "added multicast peer:", saddr)
|
}
|
||||||
}
|
}
|
||||||
|
anAddr := string(bs[:nBytes])
|
||||||
|
addr, err := net.ResolveTCPAddr("tcp6", anAddr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
continue
|
||||||
|
} // Panic for testing, remove later
|
||||||
|
from := fromAddr.(*net.UDPAddr)
|
||||||
|
//fmt.Println("DEBUG:", "heard:", addr.IP.String(), "from:", from.IP.String())
|
||||||
|
if addr.IP.String() != from.IP.String() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addr.Zone = from.Zone
|
||||||
|
saddr := addr.String()
|
||||||
|
//if _, isIn := n.peers[saddr]; isIn { continue }
|
||||||
|
//n.peers[saddr] = struct{}{}
|
||||||
|
n.core.DEBUG_addTCPConn(saddr)
|
||||||
|
//fmt.Println("DEBUG:", "added multicast peer:", saddr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
destAddr, err := net.ResolveUDPAddr("udp6", multicastAddr)
|
tcpAddr := n.core.DEBUG_getGlobalTCPAddr()
|
||||||
if err != nil { panic(err) }
|
anAddr.Port = tcpAddr.Port
|
||||||
for {
|
destAddr, err := net.ResolveUDPAddr("udp6", multicastAddr)
|
||||||
ifaces, err := net.Interfaces()
|
if err != nil {
|
||||||
if err != nil { panic(err) }
|
panic(err)
|
||||||
for _, iface := range ifaces {
|
}
|
||||||
n.sock.JoinGroup(&iface, groupAddr)
|
for {
|
||||||
//err := n.sock.JoinGroup(&iface, groupAddr)
|
ifaces, err := net.Interfaces()
|
||||||
//if err != nil { panic(err) }
|
if err != nil {
|
||||||
addrs, err := iface.Addrs()
|
panic(err)
|
||||||
if err != nil { panic(err) }
|
}
|
||||||
for _, addr := range addrs {
|
for _, iface := range ifaces {
|
||||||
addrIP, _, _ := net.ParseCIDR(addr.String())
|
n.sock.JoinGroup(&iface, groupAddr)
|
||||||
if addrIP.To4() != nil { continue } // IPv6 only
|
//err := n.sock.JoinGroup(&iface, groupAddr)
|
||||||
if !addrIP.IsLinkLocalUnicast() { continue }
|
//if err != nil { panic(err) }
|
||||||
anAddr.IP = addrIP
|
addrs, err := iface.Addrs()
|
||||||
anAddr.Zone = iface.Name
|
if err != nil {
|
||||||
destAddr.Zone = iface.Name
|
panic(err)
|
||||||
msg := []byte(anAddr.String())
|
}
|
||||||
n.sock.WriteTo(msg, nil, destAddr)
|
for _, addr := range addrs {
|
||||||
break
|
addrIP, _, _ := net.ParseCIDR(addr.String())
|
||||||
}
|
if addrIP.To4() != nil {
|
||||||
time.Sleep(time.Second)
|
continue
|
||||||
}
|
} // IPv6 only
|
||||||
time.Sleep(time.Second)
|
if !addrIP.IsLinkLocalUnicast() {
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
anAddr.IP = addrIP
|
||||||
|
anAddr.Zone = iface.Name
|
||||||
|
destAddr.Zone = iface.Name
|
||||||
|
msg := []byte(anAddr.String())
|
||||||
|
n.sock.WriteTo(msg, nil, destAddr)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var pprof = flag.Bool("pprof", false, "Run pprof, see http://localhost:6060/debug/pprof/")
|
var pprof = flag.Bool("pprof", false, "Run pprof, see http://localhost:6060/debug/pprof/")
|
||||||
@ -157,53 +215,69 @@ var useconf = flag.Bool("useconf", false, "read config from stdin")
|
|||||||
var autoconf = flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
|
var autoconf = flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
var cfg *nodeConfig
|
var cfg *nodeConfig
|
||||||
switch {
|
switch {
|
||||||
case *autoconf: cfg = generateConfig()
|
case *autoconf:
|
||||||
case *useconf:
|
cfg = generateConfig()
|
||||||
config, err := ioutil.ReadAll(os.Stdin)
|
case *useconf:
|
||||||
if err != nil { panic(err) }
|
config, err := ioutil.ReadAll(os.Stdin)
|
||||||
decoder := json.NewDecoder(bytes.NewReader(config))
|
if err != nil {
|
||||||
err = decoder.Decode(&cfg)
|
panic(err)
|
||||||
if err != nil { panic(err) }
|
}
|
||||||
case *genconf: fmt.Println(doGenconf())
|
decoder := json.NewDecoder(bytes.NewReader(config))
|
||||||
default: flag.PrintDefaults()
|
cfg = generateConfig()
|
||||||
}
|
err = decoder.Decode(cfg)
|
||||||
if cfg == nil { return }
|
if err != nil {
|
||||||
logger := log.New(os.Stdout, "", log.Flags())
|
panic(err)
|
||||||
if *pprof {
|
}
|
||||||
runtime.SetBlockProfileRate(1)
|
case *genconf:
|
||||||
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
|
fmt.Println(doGenconf())
|
||||||
}
|
default:
|
||||||
// Setup
|
flag.PrintDefaults()
|
||||||
logger.Println("Initializing...")
|
}
|
||||||
n := node{}
|
if cfg == nil {
|
||||||
n.init(cfg, logger)
|
return
|
||||||
logger.Println("Starting tun...")
|
}
|
||||||
//n.core.DEBUG_startTun() // 1280, the smallest supported MTU
|
logger := log.New(os.Stdout, "", log.Flags())
|
||||||
n.core.DEBUG_startTunWithMTU(65535) // Largest supported MTU
|
if *pprof {
|
||||||
defer func() {
|
runtime.SetBlockProfileRate(1)
|
||||||
logger.Println("Closing...")
|
go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }()
|
||||||
n.core.DEBUG_stopTun()
|
}
|
||||||
}()
|
// Setup
|
||||||
logger.Println("Started...")
|
logger.Println("Initializing...")
|
||||||
if cfg.Multicast {
|
n := node{}
|
||||||
addr, err := net.ResolveUDPAddr("udp", multicastAddr)
|
n.init(cfg, logger)
|
||||||
if err != nil { panic(err) }
|
logger.Println("Starting tun...")
|
||||||
listenString := fmt.Sprintf("[::]:%v", addr.Port)
|
n.core.DEBUG_startTun(cfg.IfName) // 1280, the smallest supported MTU
|
||||||
conn, err := net.ListenPacket("udp6", listenString)
|
//n.core.DEBUG_startTunWithMTU(cfg.IfName, 65535) // Largest supported MTU
|
||||||
if err != nil { panic(err) }
|
defer func() {
|
||||||
//defer conn.Close() // Let it close on its own when the application exits
|
logger.Println("Closing...")
|
||||||
n.sock = ipv6.NewPacketConn(conn)
|
n.core.DEBUG_stopTun()
|
||||||
if err = n.sock.SetControlMessage(ipv6.FlagDst, true) ; err != nil { panic(err) }
|
}()
|
||||||
go n.listen()
|
logger.Println("Started...")
|
||||||
go n.announce()
|
if cfg.Multicast {
|
||||||
}
|
addr, err := net.ResolveUDPAddr("udp", multicastAddr)
|
||||||
// Catch interrupt to exit gracefully
|
if err != nil {
|
||||||
c := make(chan os.Signal, 1)
|
panic(err)
|
||||||
signal.Notify(c, os.Interrupt)
|
}
|
||||||
<-c
|
listenString := fmt.Sprintf("[::]:%v", addr.Port)
|
||||||
logger.Println("Stopping...")
|
conn, err := net.ListenPacket("udp6", listenString)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
//defer conn.Close() // Let it close on its own when the application exits
|
||||||
|
n.sock = ipv6.NewPacketConn(conn)
|
||||||
|
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.announce()
|
||||||
|
}
|
||||||
|
// Catch interrupt to exit gracefully
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt, os.Kill)
|
||||||
|
<-c
|
||||||
|
logger.Println("Stopping...")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user