mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-06-17 06:35:07 +03:00
Merge branch 'develop' into neilalexander/v045
This commit is contained in:
commit
d84da000ba
2 changed files with 49 additions and 70 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"golang.org/x/text/encoding/unicode"
|
"golang.org/x/text/encoding/unicode"
|
||||||
|
@ -183,8 +184,7 @@ func getArgs() yggArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The main function is responsible for configuring and starting Yggdrasil.
|
// The main function is responsible for configuring and starting Yggdrasil.
|
||||||
func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
func run(args yggArgs, ctx context.Context) {
|
||||||
defer close(done)
|
|
||||||
// Create a new logger that logs output to stdout.
|
// Create a new logger that logs output to stdout.
|
||||||
var logger *log.Logger
|
var logger *log.Logger
|
||||||
switch args.logto {
|
switch args.logto {
|
||||||
|
@ -290,7 +290,10 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
options := []core.SetupOption{}
|
options := []core.SetupOption{
|
||||||
|
core.NodeInfo(cfg.NodeInfo),
|
||||||
|
core.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
|
||||||
|
}
|
||||||
for _, addr := range cfg.Listen {
|
for _, addr := range cfg.Listen {
|
||||||
options = append(options, core.ListenAddress(addr))
|
options = append(options, core.ListenAddress(addr))
|
||||||
}
|
}
|
||||||
|
@ -368,14 +371,11 @@ func run(args yggArgs, ctx context.Context, done chan struct{}) {
|
||||||
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
|
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
|
||||||
logger.Infof("Your IPv6 address is %s", address.String())
|
logger.Infof("Your IPv6 address is %s", address.String())
|
||||||
logger.Infof("Your IPv6 subnet is %s", subnet.String())
|
logger.Infof("Your IPv6 subnet is %s", subnet.String())
|
||||||
// Catch interrupts from the operating system to exit gracefully.
|
|
||||||
<-ctx.Done()
|
|
||||||
// Capture the service being stopped on Windows.
|
|
||||||
minwinsvc.SetOnExit(n.shutdown)
|
|
||||||
n.shutdown()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *node) shutdown() {
|
// Block until we are told to shut down.
|
||||||
|
<-ctx.Done()
|
||||||
|
|
||||||
|
// Shut down the node.
|
||||||
_ = n.admin.Stop()
|
_ = n.admin.Stop()
|
||||||
_ = n.multicast.Stop()
|
_ = n.multicast.Stop()
|
||||||
_ = n.tun.Stop()
|
_ = n.tun.Stop()
|
||||||
|
@ -384,24 +384,19 @@ func (n *node) shutdown() {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
args := getArgs()
|
args := getArgs()
|
||||||
hup := make(chan os.Signal, 1)
|
|
||||||
//signal.Notify(hup, os.Interrupt, syscall.SIGHUP)
|
// Catch interrupts from the operating system to exit gracefully.
|
||||||
term := make(chan os.Signal, 1)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
|
|
||||||
for {
|
// Capture the service being stopped on Windows.
|
||||||
done := make(chan struct{})
|
minwinsvc.SetOnExit(cancel)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
go run(args, ctx, done)
|
// Start the node, block and then wait for it to shut down.
|
||||||
select {
|
var wg sync.WaitGroup
|
||||||
case <-hup:
|
wg.Add(1)
|
||||||
cancel()
|
go func() {
|
||||||
<-done
|
defer wg.Done()
|
||||||
case <-term:
|
run(args, ctx)
|
||||||
cancel()
|
}()
|
||||||
<-done
|
wg.Wait()
|
||||||
return
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
iwt "github.com/Arceliar/ironwood/types"
|
iwt "github.com/Arceliar/ironwood/types"
|
||||||
|
@ -14,18 +13,15 @@ import (
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NodeInfoPayload represents a RequestNodeInfo response, in bytes.
|
|
||||||
type NodeInfoPayload []byte
|
|
||||||
|
|
||||||
type nodeinfo struct {
|
type nodeinfo struct {
|
||||||
phony.Inbox
|
phony.Inbox
|
||||||
proto *protoHandler
|
proto *protoHandler
|
||||||
myNodeInfo NodeInfoPayload
|
myNodeInfo json.RawMessage
|
||||||
callbacks map[keyArray]nodeinfoCallback
|
callbacks map[keyArray]nodeinfoCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
type nodeinfoCallback struct {
|
type nodeinfoCallback struct {
|
||||||
call func(nodeinfo NodeInfoPayload)
|
call func(nodeinfo json.RawMessage)
|
||||||
created time.Time
|
created time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +50,7 @@ func (m *nodeinfo) _cleanup() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayload)) {
|
func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo json.RawMessage)) {
|
||||||
m.callbacks[sender] = nodeinfoCallback{
|
m.callbacks[sender] = nodeinfoCallback{
|
||||||
created: time.Now(),
|
created: time.Now(),
|
||||||
call: call,
|
call: call,
|
||||||
|
@ -62,67 +58,55 @@ func (m *nodeinfo) _addCallback(sender keyArray, call func(nodeinfo NodeInfoPayl
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handles the callback, if there is one
|
// Handles the callback, if there is one
|
||||||
func (m *nodeinfo) _callback(sender keyArray, nodeinfo NodeInfoPayload) {
|
func (m *nodeinfo) _callback(sender keyArray, nodeinfo json.RawMessage) {
|
||||||
if callback, ok := m.callbacks[sender]; ok {
|
if callback, ok := m.callbacks[sender]; ok {
|
||||||
callback.call(nodeinfo)
|
callback.call(nodeinfo)
|
||||||
delete(m.callbacks, sender)
|
delete(m.callbacks, sender)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload {
|
func (m *nodeinfo) _getNodeInfo() json.RawMessage {
|
||||||
return m.myNodeInfo
|
return m.myNodeInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the current node's nodeinfo
|
// Set the current node's nodeinfo
|
||||||
func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) (err error) {
|
func (m *nodeinfo) setNodeInfo(given map[string]interface{}, privacy bool) (err error) {
|
||||||
phony.Block(m, func() {
|
phony.Block(m, func() {
|
||||||
err = m._setNodeInfo(given, privacy)
|
err = m._setNodeInfo(given, privacy)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error {
|
func (m *nodeinfo) _setNodeInfo(given map[string]interface{}, privacy bool) error {
|
||||||
defaults := map[string]interface{}{
|
newnodeinfo := make(map[string]interface{}, len(given))
|
||||||
"buildname": version.BuildName(),
|
for k, v := range given {
|
||||||
"buildversion": version.BuildVersion(),
|
newnodeinfo[k] = v
|
||||||
"buildplatform": runtime.GOOS,
|
|
||||||
"buildarch": runtime.GOARCH,
|
|
||||||
}
|
}
|
||||||
newnodeinfo := make(map[string]interface{})
|
|
||||||
if !privacy {
|
if !privacy {
|
||||||
for k, v := range defaults {
|
newnodeinfo["buildname"] = version.BuildName()
|
||||||
newnodeinfo[k] = v
|
newnodeinfo["buildversion"] = version.BuildVersion()
|
||||||
}
|
newnodeinfo["buildplatform"] = runtime.GOOS
|
||||||
}
|
newnodeinfo["buildarch"] = runtime.GOARCH
|
||||||
if nodeinfomap, ok := given.(map[string]interface{}); ok {
|
|
||||||
for key, value := range nodeinfomap {
|
|
||||||
if _, ok := defaults[key]; ok {
|
|
||||||
if strvalue, strok := value.(string); strok && strings.EqualFold(strvalue, "null") || value == nil {
|
|
||||||
delete(newnodeinfo, key)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
newnodeinfo[key] = value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
newjson, err := json.Marshal(newnodeinfo)
|
newjson, err := json.Marshal(newnodeinfo)
|
||||||
if err == nil {
|
switch {
|
||||||
if len(newjson) > 16384 {
|
case err != nil:
|
||||||
return errors.New("NodeInfo exceeds max length of 16384 bytes")
|
return fmt.Errorf("NodeInfo marshalling failed: %w", err)
|
||||||
}
|
case len(newjson) > 16384:
|
||||||
|
return fmt.Errorf("NodeInfo exceeds max length of 16384 bytes")
|
||||||
|
default:
|
||||||
m.myNodeInfo = newjson
|
m.myNodeInfo = newjson
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo NodeInfoPayload)) {
|
func (m *nodeinfo) sendReq(from phony.Actor, key keyArray, callback func(nodeinfo json.RawMessage)) {
|
||||||
m.Act(from, func() {
|
m.Act(from, func() {
|
||||||
m._sendReq(key, callback)
|
m._sendReq(key, callback)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo NodeInfoPayload)) {
|
func (m *nodeinfo) _sendReq(key keyArray, callback func(nodeinfo json.RawMessage)) {
|
||||||
if callback != nil {
|
if callback != nil {
|
||||||
m._addCallback(key, callback)
|
m._addCallback(key, callback)
|
||||||
}
|
}
|
||||||
|
@ -135,7 +119,7 @@ func (m *nodeinfo) handleReq(from phony.Actor, key keyArray) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info NodeInfoPayload) {
|
func (m *nodeinfo) handleRes(from phony.Actor, key keyArray, info json.RawMessage) {
|
||||||
m.Act(from, func() {
|
m.Act(from, func() {
|
||||||
m._callback(key, info)
|
m._callback(key, info)
|
||||||
})
|
})
|
||||||
|
@ -169,7 +153,7 @@ func (m *nodeinfo) nodeInfoAdminHandler(in json.RawMessage) (interface{}, error)
|
||||||
}
|
}
|
||||||
copy(key[:], kbs)
|
copy(key[:], kbs)
|
||||||
ch := make(chan []byte, 1)
|
ch := make(chan []byte, 1)
|
||||||
m.sendReq(nil, key, func(info NodeInfoPayload) {
|
m.sendReq(nil, key, func(info json.RawMessage) {
|
||||||
ch <- info
|
ch <- info
|
||||||
})
|
})
|
||||||
timer := time.NewTimer(6 * time.Second)
|
timer := time.NewTimer(6 * time.Second)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue