mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
Merge branch 'yggdrasil-network:develop' into develop
This commit is contained in:
commit
3f44e8b364
27 changed files with 102 additions and 47 deletions
|
@ -105,7 +105,7 @@ jobs:
|
|||
|
||||
build-macos:
|
||||
macos:
|
||||
xcode: "10.0.0"
|
||||
xcode: "13.0.0"
|
||||
|
||||
working_directory: ~/go/src/github.com/RiV-chain/RiV-mesh
|
||||
|
||||
|
|
|
@ -38,8 +38,8 @@ type AdminSocketResponse struct {
|
|||
}
|
||||
|
||||
type handler struct {
|
||||
args []string // List of human-readable argument names
|
||||
handler func(json.RawMessage) (interface{}, error) // First is input map, second is output
|
||||
args []string // List of human-readable argument names
|
||||
handler core.AddHandlerFunc // First is input map, second is output
|
||||
}
|
||||
|
||||
type ListResponse struct {
|
||||
|
@ -51,7 +51,7 @@ type ListEntry struct {
|
|||
}
|
||||
|
||||
// AddHandler is called for each admin function to add the handler and help documentation to the API.
|
||||
func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc func(json.RawMessage) (interface{}, error)) error {
|
||||
func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc core.AddHandlerFunc) error {
|
||||
if _, ok := a.handlers[strings.ToLower(name)]; ok {
|
||||
return errors.New("handler already exists")
|
||||
}
|
||||
|
|
|
@ -242,9 +242,11 @@ func (c *Core) PublicKey() ed25519.PublicKey {
|
|||
// Hack to get the admin stuff working, TODO something cleaner
|
||||
|
||||
type AddHandler interface {
|
||||
AddHandler(name string, args []string, handlerfunc func(json.RawMessage) (interface{}, error)) error
|
||||
AddHandler(name string, args []string, handlerfunc AddHandlerFunc) error
|
||||
}
|
||||
|
||||
type AddHandlerFunc func(json.RawMessage) (interface{}, error)
|
||||
|
||||
// SetAdmin must be called after Init and before Start.
|
||||
// It sets the admin handler for NodeInfo and the Debug admin functions.
|
||||
func (c *Core) SetAdmin(a AddHandler) error {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build debug
|
||||
// +build debug
|
||||
|
||||
package core
|
||||
|
|
|
@ -98,6 +98,22 @@ func (l *links) call(u *url.URL, sintf string) error {
|
|||
l.tcp.call(pathtokens[0], tcpOpts, sintf)
|
||||
case "tls":
|
||||
tcpOpts.upgrade = l.tcp.tls.forDialer
|
||||
// SNI headers must contain hostnames and not IP addresses, so we must make sure
|
||||
// that we do not populate the SNI with an IP literal. We do this by splitting
|
||||
// the host-port combo from the query option and then seeing if it parses to an
|
||||
// IP address successfully or not.
|
||||
if sni := u.Query().Get("sni"); sni != "" {
|
||||
if net.ParseIP(sni) == nil {
|
||||
tcpOpts.tlsSNI = sni
|
||||
}
|
||||
}
|
||||
// If the SNI is not configured still because the above failed then we'll try
|
||||
// again but this time we'll use the host part of the peering URI instead.
|
||||
if tcpOpts.tlsSNI == "" {
|
||||
if host, _, err := net.SplitHostPort(u.Host); err == nil && net.ParseIP(host) == nil {
|
||||
tcpOpts.tlsSNI = host
|
||||
}
|
||||
}
|
||||
l.tcp.call(u.Host, tcpOpts, sintf)
|
||||
default:
|
||||
return errors.New("unknown call scheme: " + u.Scheme)
|
||||
|
|
|
@ -34,21 +34,26 @@ type keyArray [ed25519.PublicKeySize]byte
|
|||
|
||||
type protoHandler struct {
|
||||
phony.Inbox
|
||||
|
||||
core *Core
|
||||
nodeinfo nodeinfo
|
||||
sreqs map[keyArray]*reqInfo
|
||||
preqs map[keyArray]*reqInfo
|
||||
dreqs map[keyArray]*reqInfo
|
||||
|
||||
selfRequests map[keyArray]*reqInfo
|
||||
peersRequests map[keyArray]*reqInfo
|
||||
dhtRequests map[keyArray]*reqInfo
|
||||
}
|
||||
|
||||
func (p *protoHandler) init(core *Core) {
|
||||
p.core = core
|
||||
p.nodeinfo.init(p)
|
||||
p.sreqs = make(map[keyArray]*reqInfo)
|
||||
p.preqs = make(map[keyArray]*reqInfo)
|
||||
p.dreqs = make(map[keyArray]*reqInfo)
|
||||
|
||||
p.selfRequests = make(map[keyArray]*reqInfo)
|
||||
p.peersRequests = make(map[keyArray]*reqInfo)
|
||||
p.dhtRequests = make(map[keyArray]*reqInfo)
|
||||
}
|
||||
|
||||
// Common functions
|
||||
|
||||
func (p *protoHandler) handleProto(from phony.Actor, key keyArray, bs []byte) {
|
||||
if len(bs) == 0 {
|
||||
return
|
||||
|
@ -85,22 +90,29 @@ func (p *protoHandler) _handleDebug(key keyArray, bs []byte) {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) {
|
||||
bs := append([]byte{typeSessionProto, typeProtoDebug, dType}, data...)
|
||||
_, _ = p.core.PacketConn.WriteTo(bs, iwt.Addr(key[:]))
|
||||
}
|
||||
|
||||
// Get self
|
||||
|
||||
func (p *protoHandler) sendGetSelfRequest(key keyArray, callback func([]byte)) {
|
||||
p.Act(nil, func() {
|
||||
if info := p.sreqs[key]; info != nil {
|
||||
if info := p.selfRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
delete(p.sreqs, key)
|
||||
delete(p.selfRequests, key)
|
||||
}
|
||||
info := new(reqInfo)
|
||||
info.callback = callback
|
||||
info.timer = time.AfterFunc(time.Minute, func() {
|
||||
p.Act(nil, func() {
|
||||
if p.sreqs[key] == info {
|
||||
delete(p.sreqs, key)
|
||||
if p.selfRequests[key] == info {
|
||||
delete(p.selfRequests, key)
|
||||
}
|
||||
})
|
||||
})
|
||||
p.sreqs[key] = info
|
||||
p.selfRequests[key] = info
|
||||
p._sendDebug(key, typeDebugGetSelfRequest, nil)
|
||||
})
|
||||
}
|
||||
|
@ -119,29 +131,31 @@ func (p *protoHandler) _handleGetSelfRequest(key keyArray) {
|
|||
}
|
||||
|
||||
func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) {
|
||||
if info := p.sreqs[key]; info != nil {
|
||||
if info := p.selfRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
info.callback(bs)
|
||||
delete(p.sreqs, key)
|
||||
delete(p.selfRequests, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Get peers
|
||||
|
||||
func (p *protoHandler) sendGetPeersRequest(key keyArray, callback func([]byte)) {
|
||||
p.Act(nil, func() {
|
||||
if info := p.preqs[key]; info != nil {
|
||||
if info := p.peersRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
delete(p.preqs, key)
|
||||
delete(p.peersRequests, key)
|
||||
}
|
||||
info := new(reqInfo)
|
||||
info.callback = callback
|
||||
info.timer = time.AfterFunc(time.Minute, func() {
|
||||
p.Act(nil, func() {
|
||||
if p.preqs[key] == info {
|
||||
delete(p.preqs, key)
|
||||
if p.peersRequests[key] == info {
|
||||
delete(p.peersRequests, key)
|
||||
}
|
||||
})
|
||||
})
|
||||
p.preqs[key] = info
|
||||
p.peersRequests[key] = info
|
||||
p._sendDebug(key, typeDebugGetPeersRequest, nil)
|
||||
})
|
||||
}
|
||||
|
@ -161,29 +175,31 @@ func (p *protoHandler) _handleGetPeersRequest(key keyArray) {
|
|||
}
|
||||
|
||||
func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) {
|
||||
if info := p.preqs[key]; info != nil {
|
||||
if info := p.peersRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
info.callback(bs)
|
||||
delete(p.preqs, key)
|
||||
delete(p.peersRequests, key)
|
||||
}
|
||||
}
|
||||
|
||||
// Get DHT
|
||||
|
||||
func (p *protoHandler) sendGetDHTRequest(key keyArray, callback func([]byte)) {
|
||||
p.Act(nil, func() {
|
||||
if info := p.dreqs[key]; info != nil {
|
||||
if info := p.dhtRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
delete(p.dreqs, key)
|
||||
delete(p.dhtRequests, key)
|
||||
}
|
||||
info := new(reqInfo)
|
||||
info.callback = callback
|
||||
info.timer = time.AfterFunc(time.Minute, func() {
|
||||
p.Act(nil, func() {
|
||||
if p.dreqs[key] == info {
|
||||
delete(p.dreqs, key)
|
||||
if p.dhtRequests[key] == info {
|
||||
delete(p.dhtRequests, key)
|
||||
}
|
||||
})
|
||||
})
|
||||
p.dreqs[key] = info
|
||||
p.dhtRequests[key] = info
|
||||
p._sendDebug(key, typeDebugGetDHTRequest, nil)
|
||||
})
|
||||
}
|
||||
|
@ -203,19 +219,14 @@ func (p *protoHandler) _handleGetDHTRequest(key keyArray) {
|
|||
}
|
||||
|
||||
func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) {
|
||||
if info := p.dreqs[key]; info != nil {
|
||||
if info := p.dhtRequests[key]; info != nil {
|
||||
info.timer.Stop()
|
||||
info.callback(bs)
|
||||
delete(p.dreqs, key)
|
||||
delete(p.dhtRequests, key)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) {
|
||||
bs := append([]byte{typeSessionProto, typeProtoDebug, dType}, data...)
|
||||
_, _ = p.core.PacketConn.WriteTo(bs, iwt.Addr(key[:]))
|
||||
}
|
||||
|
||||
// Admin socket stuff
|
||||
// Admin socket stuff for "Get self"
|
||||
|
||||
type DebugGetSelfRequest struct {
|
||||
Key string `json:"key"`
|
||||
|
@ -255,6 +266,8 @@ func (p *protoHandler) getSelfHandler(in json.RawMessage) (interface{}, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// Admin socket stuff for "Get peers"
|
||||
|
||||
type DebugGetPeersRequest struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
@ -303,6 +316,8 @@ func (p *protoHandler) getPeersHandler(in json.RawMessage) (interface{}, error)
|
|||
}
|
||||
}
|
||||
|
||||
// Admin socket stuff for "Get DHT"
|
||||
|
||||
type DebugGetDHTRequest struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
|
|
@ -64,6 +64,7 @@ type tcpOptions struct {
|
|||
socksProxyAddr string
|
||||
socksProxyAuth *proxy.Auth
|
||||
socksPeerAddr string
|
||||
tlsSNI string
|
||||
}
|
||||
|
||||
func (l *TcpListener) Stop() {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package core
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package core
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !darwin && !linux
|
||||
// +build !darwin,!linux
|
||||
|
||||
package core
|
||||
|
|
|
@ -78,7 +78,7 @@ func (t *tcptls) init(tcp *tcp) {
|
|||
}
|
||||
|
||||
func (t *tcptls) configForOptions(options *tcpOptions) *tls.Config {
|
||||
config := *t.config
|
||||
config := t.config.Clone()
|
||||
config.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
||||
if len(rawCerts) != 1 {
|
||||
return errors.New("tls not exactly 1 cert")
|
||||
|
@ -103,7 +103,7 @@ func (t *tcptls) configForOptions(options *tcpOptions) *tls.Config {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
return &config
|
||||
return config
|
||||
}
|
||||
|
||||
func (t *tcptls) upgradeListener(c net.Conn, options *tcpOptions) (net.Conn, error) {
|
||||
|
@ -117,6 +117,7 @@ func (t *tcptls) upgradeListener(c net.Conn, options *tcpOptions) (net.Conn, err
|
|||
|
||||
func (t *tcptls) upgradeDialer(c net.Conn, options *tcpOptions) (net.Conn, error) {
|
||||
config := t.configForOptions(options)
|
||||
config.ServerName = options.tlsSNI
|
||||
conn := tls.Client(c, config)
|
||||
if err := conn.Handshake(); err != nil {
|
||||
return c, err
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build freebsd
|
||||
// +build freebsd
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build openbsd
|
||||
// +build openbsd
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !linux && !darwin && !windows && !openbsd && !freebsd
|
||||
// +build !linux,!darwin,!windows,!openbsd,!freebsd
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package defaults
|
||||
|
|
|
@ -132,6 +132,7 @@ func (k *keyStore) update(key ed25519.PublicKey) *keyInfo {
|
|||
var kArray keyArray
|
||||
copy(kArray[:], key)
|
||||
var info *keyInfo
|
||||
var packets [][]byte
|
||||
if info = k.keyToInfo[kArray]; info == nil {
|
||||
info = new(keyInfo)
|
||||
info.key = kArray
|
||||
|
@ -140,19 +141,19 @@ func (k *keyStore) update(key ed25519.PublicKey) *keyInfo {
|
|||
k.keyToInfo[info.key] = info
|
||||
k.addrToInfo[info.address] = info
|
||||
k.subnetToInfo[info.subnet] = info
|
||||
k.resetTimeout(info)
|
||||
k.mutex.Unlock()
|
||||
if buf := k.addrBuffer[info.address]; buf != nil {
|
||||
k.core.WriteTo(buf.packet, iwt.Addr(info.key[:]))
|
||||
packets = append(packets, buf.packet)
|
||||
delete(k.addrBuffer, info.address)
|
||||
}
|
||||
if buf := k.subnetBuffer[info.subnet]; buf != nil {
|
||||
k.core.WriteTo(buf.packet, iwt.Addr(info.key[:]))
|
||||
packets = append(packets, buf.packet)
|
||||
delete(k.subnetBuffer, info.subnet)
|
||||
}
|
||||
} else {
|
||||
k.resetTimeout(info)
|
||||
k.mutex.Unlock()
|
||||
}
|
||||
k.resetTimeout(info)
|
||||
k.mutex.Unlock()
|
||||
for _, packet := range packets {
|
||||
k.core.WriteTo(packet, iwt.Addr(info.key[:]))
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package multicast
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !linux && !darwin && !netbsd && !freebsd && !openbsd && !dragonflybsd && !windows
|
||||
// +build !linux,!darwin,!netbsd,!freebsd,!openbsd,!dragonflybsd,!windows
|
||||
|
||||
package multicast
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build linux || netbsd || freebsd || openbsd || dragonflybsd
|
||||
// +build linux netbsd freebsd openbsd dragonflybsd
|
||||
|
||||
package multicast
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package multicast
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build openbsd || freebsd
|
||||
// +build openbsd freebsd
|
||||
|
||||
package tuntap
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !mobile
|
||||
// +build !mobile
|
||||
|
||||
package tuntap
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !mobile
|
||||
// +build !mobile
|
||||
|
||||
package tuntap
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build !linux && !darwin && !windows && !openbsd && !freebsd && !mobile
|
||||
// +build !linux,!darwin,!windows,!openbsd,!freebsd,!mobile
|
||||
|
||||
package tuntap
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package tuntap
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue