Merge branch 'develop' into misc

This commit is contained in:
Neil Alexander 2020-01-05 23:04:51 +00:00
commit 8c12fc4fdb
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
9 changed files with 144 additions and 85 deletions

View File

@ -132,6 +132,32 @@ func doGenconf(isjson bool) string {
return string(bs) return string(bs)
} }
func setLogLevel(loglevel string, logger *log.Logger) {
levels := [...]string{"error", "warn", "info", "debug", "trace"}
loglevel = strings.ToLower(loglevel)
contains := func() bool {
for _, l := range levels {
if l == loglevel {
return true
}
}
return false
}
if !contains() { // set default log level
logger.Infoln("Loglevel parse failed. Set default level(info)")
loglevel = "info"
}
for _, l := range levels {
logger.EnableLevel(l)
if l == loglevel {
break
}
}
}
// The main function is responsible for configuring and starting Yggdrasil. // The main function is responsible for configuring and starting Yggdrasil.
func main() { func main() {
// Configure the command line parameters. // Configure the command line parameters.
@ -142,10 +168,10 @@ func main() {
confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON")
autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)")
ver := flag.Bool("version", false, "prints the version of this build") ver := flag.Bool("version", false, "prints the version of this build")
logging := flag.String("logging", "info,warn,error", "comma-separated list of logging levels to enable")
logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"")
getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration") getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration")
getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration") getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration")
loglevel := flag.String("loglevel", "info", "loglevel to enable")
flag.Parse() flag.Parse()
var cfg *config.NodeConfig var cfg *config.NodeConfig
@ -239,20 +265,9 @@ func main() {
logger = log.New(os.Stdout, "", log.Flags()) logger = log.New(os.Stdout, "", log.Flags())
logger.Warnln("Logging defaulting to stdout") logger.Warnln("Logging defaulting to stdout")
} }
//logger.EnableLevel("error")
//logger.EnableLevel("warn") setLogLevel(*loglevel, logger)
//logger.EnableLevel("info")
if levels := strings.Split(*logging, ","); len(levels) > 0 {
for _, level := range levels {
l := strings.TrimSpace(level)
switch l {
case "error", "warn", "info", "trace", "debug":
logger.EnableLevel(l)
default:
continue
}
}
}
// Setup the Yggdrasil node itself. The node{} type includes a Core, so we // Setup the Yggdrasil node itself. The node{} type includes a Core, so we
// don't need to create this manually. // don't need to create this manually.
n := node{} n := node{}

View File

@ -5,13 +5,14 @@ WORKDIR /src
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0
RUN apk add git && ./build RUN apk add git && ./build && go build -o /src/genkeys cmd/genkeys/main.go
FROM docker.io/alpine FROM docker.io/alpine
LABEL maintainer="Christer Waren/CWINFO <christer.waren@cwinfo.org>" LABEL maintainer="Christer Waren/CWINFO <christer.waren@cwinfo.org>"
COPY --from=builder /src/yggdrasil /usr/bin/yggdrasil COPY --from=builder /src/yggdrasil /usr/bin/yggdrasil
COPY --from=builder /src/yggdrasilctl /usr/bin/yggdrasilctl COPY --from=builder /src/yggdrasilctl /usr/bin/yggdrasilctl
COPY --from=builder /src/genkeys /usr/bin/genkeys
COPY contrib/docker/entrypoint.sh /usr/bin/entrypoint.sh COPY contrib/docker/entrypoint.sh /usr/bin/entrypoint.sh
# RUN addgroup -g 1000 -S yggdrasil-network \ # RUN addgroup -g 1000 -S yggdrasil-network \

View File

@ -83,12 +83,18 @@ else
exit 1 exit 1
fi fi
if [ $PKGNAME != "master" ]; then
PKGDISPLAYNAME="Yggdrasil Network (${PKGNAME} branch)"
else
PKGDISPLAYNAME="Yggdrasil Network"
fi
# Generate the wix.xml file # Generate the wix.xml file
cat > wix.xml << EOF cat > wix.xml << EOF
<?xml version="1.0" encoding="windows-1252"?> <?xml version="1.0" encoding="windows-1252"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product <Product
Name="Yggdrasil (${PKGNAME} branch)" Name="${PKGDISPLAYNAME}"
Id="*" Id="*"
UpgradeCode="${PKGGUID}" UpgradeCode="${PKGGUID}"
Language="1033" Language="1033"
@ -100,7 +106,7 @@ cat > wix.xml << EOF
Id="*" Id="*"
Keywords="Installer" Keywords="Installer"
Description="Yggdrasil Network Installer" Description="Yggdrasil Network Installer"
Comments="This is the Yggdrasil Network router for Windows." Comments="Yggdrasil Network standalone router for Windows."
Manufacturer="github.com/yggdrasil-network" Manufacturer="github.com/yggdrasil-network"
InstallerVersion="200" InstallerVersion="200"
InstallScope="perMachine" InstallScope="perMachine"
@ -115,7 +121,8 @@ cat > wix.xml << EOF
<Media <Media
Id="1" Id="1"
Cabinet="Media.cab" Cabinet="Media.cab"
EmbedCab="yes" /> EmbedCab="yes"
CompressionLevel="high" />
<Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="${PKGINSTFOLDER}" Name="PFiles"> <Directory Id="${PKGINSTFOLDER}" Name="PFiles">
@ -136,7 +143,7 @@ cat > wix.xml << EOF
DisplayName="Yggdrasil Service" DisplayName="Yggdrasil Service"
ErrorControl="normal" ErrorControl="normal"
LoadOrderGroup="NetworkProvider" LoadOrderGroup="NetworkProvider"
Name="yggdrasil" Name="Yggdrasil"
Start="auto" Start="auto"
Type="ownProcess" Type="ownProcess"
Arguments='-useconffile "%ALLUSERSPROFILE%\\Yggdrasil\\yggdrasil.conf" -logto "%ALLUSERSPROFILE%\\Yggdrasil\\yggdrasil.log"' Arguments='-useconffile "%ALLUSERSPROFILE%\\Yggdrasil\\yggdrasil.conf" -logto "%ALLUSERSPROFILE%\\Yggdrasil\\yggdrasil.log"'
@ -177,13 +184,19 @@ cat > wix.xml << EOF
SourceFile="${PKGMSMNAME}" /> SourceFile="${PKGMSMNAME}" />
</Directory> </Directory>
<Feature Id="Complete" Level="1"> <Feature Id="YggdrasilFeature" Title="Yggdrasil" Level="1">
<MergeRef Id="Wintun" />
<ComponentRef Id="MainExecutable" /> <ComponentRef Id="MainExecutable" />
<ComponentRef Id="CtrlExecutable" /> <ComponentRef Id="CtrlExecutable" />
<ComponentRef Id="ConfigScript" /> <ComponentRef Id="ConfigScript" />
</Feature> </Feature>
<Feature Id="WintunFeature" Title="Wintun" Level="1">
<Condition Level="0">
UPGRADINGPRODUCTCODE
</Condition>
<MergeRef Id="Wintun" />
</Feature>
<CustomAction <CustomAction
Id="UpdateGenerateConfig" Id="UpdateGenerateConfig"
Directory="YggdrasilInstallFolder" Directory="YggdrasilInstallFolder"

View File

@ -0,0 +1,13 @@
[Unit]
Description=yggdrasil default config generator
ConditionPathExists=|!/etc/yggdrasil.conf
ConditionFileNotEmpty=|!/etc/yggdrasil.conf
Wants=local-fs.target
After=local-fs.target
[Service]
Type=oneshot
Group=yggdrasil
StandardOutput=file:/etc/yggdrasil.conf
ExecStart=/usr/bin/yggdrasil -genconf
ExecStartPost=/usr/bin/chmod 0640 /etc/yggdrasil.conf

View File

@ -1,7 +1,9 @@
[Unit] [Unit]
Description=yggdrasil Description=yggdrasil
Wants=network.target Wants=network.target
Wants=yggdrasil-default-config.service
After=network.target After=network.target
After=yggdrasil-default-config.service
[Service] [Service]
Group=yggdrasil Group=yggdrasil
@ -10,11 +12,6 @@ ProtectSystem=true
SyslogIdentifier=yggdrasil SyslogIdentifier=yggdrasil
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW
ExecStartPre=+-/sbin/modprobe tun ExecStartPre=+-/sbin/modprobe tun
ExecStartPre=/bin/sh -ec "if ! test -s /etc/yggdrasil.conf; \
then umask 077; \
yggdrasil -genconf > /etc/yggdrasil.conf; \
echo 'WARNING: A new /etc/yggdrasil.conf file has been generated.'; \
fi"
ExecStart=/usr/bin/yggdrasil -useconffile /etc/yggdrasil.conf ExecStart=/usr/bin/yggdrasil -useconffile /etc/yggdrasil.conf
ExecReload=/bin/kill -HUP $MAINPID ExecReload=/bin/kill -HUP $MAINPID
Restart=always Restart=always

View File

@ -112,7 +112,7 @@ func (m *Multicast) Stop() error {
err = m._stop() err = m._stop()
}) })
m.log.Debugln("Stopped multicast module") m.log.Debugln("Stopped multicast module")
return nil return err
} }
func (m *Multicast) _stop() error { func (m *Multicast) _stop() error {

View File

@ -390,17 +390,14 @@ func (c *Core) SetMaximumSessionMTU(mtu uint16) {
// necessary when, e.g. crawling the network. // necessary when, e.g. crawling the network.
func (c *Core) GetNodeInfo(key crypto.BoxPubKey, coords []uint64, nocache bool) (NodeInfoPayload, error) { func (c *Core) GetNodeInfo(key crypto.BoxPubKey, coords []uint64, nocache bool) (NodeInfoPayload, error) {
response := make(chan *NodeInfoPayload, 1) response := make(chan *NodeInfoPayload, 1)
sendNodeInfoRequest := func() { c.router.nodeinfo.addCallback(key, func(nodeinfo *NodeInfoPayload) {
c.router.nodeinfo.addCallback(key, func(nodeinfo *NodeInfoPayload) { defer func() { recover() }()
defer func() { recover() }() select {
select { case response <- nodeinfo:
case response <- nodeinfo: default:
default: }
} })
}) c.router.nodeinfo.sendNodeInfo(key, wire_coordsUint64stoBytes(coords), false)
c.router.nodeinfo.sendNodeInfo(key, wire_coordsUint64stoBytes(coords), false)
}
phony.Block(&c.router, sendNodeInfoRequest)
timer := time.AfterFunc(6*time.Second, func() { close(response) }) timer := time.AfterFunc(6*time.Second, func() { close(response) })
defer timer.Stop() defer timer.Stop()
for res := range response { for res := range response {

View File

@ -5,21 +5,19 @@ import (
"errors" "errors"
"runtime" "runtime"
"strings" "strings"
"sync"
"time" "time"
"github.com/Arceliar/phony"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto" "github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/version" "github.com/yggdrasil-network/yggdrasil-go/src/version"
) )
type nodeinfo struct { type nodeinfo struct {
core *Core phony.Inbox
myNodeInfo NodeInfoPayload core *Core
myNodeInfoMutex sync.RWMutex myNodeInfo NodeInfoPayload
callbacks map[crypto.BoxPubKey]nodeinfoCallback callbacks map[crypto.BoxPubKey]nodeinfoCallback
callbacksMutex sync.Mutex cache map[crypto.BoxPubKey]nodeinfoCached
cache map[crypto.BoxPubKey]nodeinfoCached
cacheMutex sync.RWMutex
} }
type nodeinfoCached struct { type nodeinfoCached struct {
@ -43,35 +41,43 @@ type nodeinfoReqRes struct {
// Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep // Initialises the nodeinfo cache/callback maps, and starts a goroutine to keep
// the cache/callback maps clean of stale entries // the cache/callback maps clean of stale entries
func (m *nodeinfo) init(core *Core) { func (m *nodeinfo) init(core *Core) {
m.Act(m, func() {
m._init(core)
})
}
func (m *nodeinfo) _init(core *Core) {
m.core = core m.core = core
m.callbacks = make(map[crypto.BoxPubKey]nodeinfoCallback) m.callbacks = make(map[crypto.BoxPubKey]nodeinfoCallback)
m.cache = make(map[crypto.BoxPubKey]nodeinfoCached) m.cache = make(map[crypto.BoxPubKey]nodeinfoCached)
var f func() m._cleanup()
f = func() { }
m.callbacksMutex.Lock()
for boxPubKey, callback := range m.callbacks { func (m *nodeinfo) _cleanup() {
if time.Since(callback.created) > time.Minute { for boxPubKey, callback := range m.callbacks {
delete(m.callbacks, boxPubKey) if time.Since(callback.created) > time.Minute {
} delete(m.callbacks, boxPubKey)
} }
m.callbacksMutex.Unlock()
m.cacheMutex.Lock()
for boxPubKey, cache := range m.cache {
if time.Since(cache.created) > time.Hour {
delete(m.cache, boxPubKey)
}
}
m.cacheMutex.Unlock()
time.AfterFunc(time.Second*30, f)
} }
go f() for boxPubKey, cache := range m.cache {
if time.Since(cache.created) > time.Hour {
delete(m.cache, boxPubKey)
}
}
time.AfterFunc(time.Second*30, func() {
m.Act(m, m._cleanup)
})
} }
// Add a callback for a nodeinfo lookup // Add a callback for a nodeinfo lookup
func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) { func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
m.callbacksMutex.Lock() m.Act(m, func() {
defer m.callbacksMutex.Unlock() m._addCallback(sender, call)
})
}
func (m *nodeinfo) _addCallback(sender crypto.BoxPubKey, call func(nodeinfo *NodeInfoPayload)) {
m.callbacks[sender] = nodeinfoCallback{ m.callbacks[sender] = nodeinfoCallback{
created: time.Now(), created: time.Now(),
call: call, call: call,
@ -79,9 +85,7 @@ func (m *nodeinfo) addCallback(sender crypto.BoxPubKey, call func(nodeinfo *Node
} }
// Handles the callback, if there is one // Handles the callback, if there is one
func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) { func (m *nodeinfo) _callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
m.callbacksMutex.Lock()
defer m.callbacksMutex.Unlock()
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)
@ -89,16 +93,26 @@ func (m *nodeinfo) callback(sender crypto.BoxPubKey, nodeinfo NodeInfoPayload) {
} }
// Get the current node's nodeinfo // Get the current node's nodeinfo
func (m *nodeinfo) getNodeInfo() NodeInfoPayload { func (m *nodeinfo) getNodeInfo() (p NodeInfoPayload) {
m.myNodeInfoMutex.RLock() phony.Block(m, func() {
defer m.myNodeInfoMutex.RUnlock() p = m._getNodeInfo()
})
return
}
func (m *nodeinfo) _getNodeInfo() NodeInfoPayload {
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) error { func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) (err error) {
m.myNodeInfoMutex.Lock() phony.Block(m, func() {
defer m.myNodeInfoMutex.Unlock() err = m._setNodeInfo(given, privacy)
})
return
}
func (m *nodeinfo) _setNodeInfo(given interface{}, privacy bool) error {
defaults := map[string]interface{}{ defaults := map[string]interface{}{
"buildname": version.BuildName(), "buildname": version.BuildName(),
"buildversion": version.BuildVersion(), "buildversion": version.BuildVersion(),
@ -134,9 +148,7 @@ func (m *nodeinfo) setNodeInfo(given interface{}, privacy bool) error {
} }
// Add nodeinfo into the cache for a node // Add nodeinfo into the cache for a node
func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPayload) { func (m *nodeinfo) _addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPayload) {
m.cacheMutex.Lock()
defer m.cacheMutex.Unlock()
m.cache[key] = nodeinfoCached{ m.cache[key] = nodeinfoCached{
created: time.Now(), created: time.Now(),
payload: payload, payload: payload,
@ -144,9 +156,7 @@ func (m *nodeinfo) addCachedNodeInfo(key crypto.BoxPubKey, payload NodeInfoPaylo
} }
// Get a nodeinfo entry from the cache // Get a nodeinfo entry from the cache
func (m *nodeinfo) getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, error) { func (m *nodeinfo) _getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, error) {
m.cacheMutex.RLock()
defer m.cacheMutex.RUnlock()
if nodeinfo, ok := m.cache[key]; ok { if nodeinfo, ok := m.cache[key]; ok {
return nodeinfo.payload, nil return nodeinfo.payload, nil
} }
@ -155,21 +165,33 @@ func (m *nodeinfo) getCachedNodeInfo(key crypto.BoxPubKey) (NodeInfoPayload, err
// Handles a nodeinfo request/response - called from the router // Handles a nodeinfo request/response - called from the router
func (m *nodeinfo) handleNodeInfo(nodeinfo *nodeinfoReqRes) { func (m *nodeinfo) handleNodeInfo(nodeinfo *nodeinfoReqRes) {
m.Act(m, func() {
m._handleNodeInfo(nodeinfo)
})
}
func (m *nodeinfo) _handleNodeInfo(nodeinfo *nodeinfoReqRes) {
if nodeinfo.IsResponse { if nodeinfo.IsResponse {
m.callback(nodeinfo.SendPermPub, nodeinfo.NodeInfo) m._callback(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
m.addCachedNodeInfo(nodeinfo.SendPermPub, nodeinfo.NodeInfo) m._addCachedNodeInfo(nodeinfo.SendPermPub, nodeinfo.NodeInfo)
} else { } else {
m.sendNodeInfo(nodeinfo.SendPermPub, nodeinfo.SendCoords, true) m._sendNodeInfo(nodeinfo.SendPermPub, nodeinfo.SendCoords, true)
} }
} }
// Send nodeinfo request or response - called from the router // Send nodeinfo request or response - called from the router
func (m *nodeinfo) sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) { func (m *nodeinfo) sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) {
m.Act(m, func() {
m._sendNodeInfo(key, coords, isResponse)
})
}
func (m *nodeinfo) _sendNodeInfo(key crypto.BoxPubKey, coords []byte, isResponse bool) {
table := m.core.switchTable.table.Load().(lookupTable) table := m.core.switchTable.table.Load().(lookupTable)
nodeinfo := nodeinfoReqRes{ nodeinfo := nodeinfoReqRes{
SendCoords: table.self.getCoords(), SendCoords: table.self.getCoords(),
IsResponse: isResponse, IsResponse: isResponse,
NodeInfo: m.getNodeInfo(), NodeInfo: m._getNodeInfo(),
} }
bs := nodeinfo.encode() bs := nodeinfo.encode()
shared := m.core.router.sessions.getSharedKey(&m.core.boxPriv, &key) shared := m.core.router.sessions.getSharedKey(&m.core.boxPriv, &key)

View File

@ -78,6 +78,7 @@ func (r *router) init(core *Core) {
func (r *router) reconfigure() { func (r *router) reconfigure() {
// Reconfigure the router // Reconfigure the router
current := r.core.config.GetCurrent() current := r.core.config.GetCurrent()
r.core.log.Println("Reloading NodeInfo...")
if err := r.nodeinfo.setNodeInfo(current.NodeInfo, current.NodeInfoPrivacy); err != nil { if err := r.nodeinfo.setNodeInfo(current.NodeInfo, current.NodeInfoPrivacy); err != nil {
r.core.log.Errorln("Error reloading NodeInfo:", err) r.core.log.Errorln("Error reloading NodeInfo:", err)
} else { } else {