mirror of
https://github.com/QuilibriumNetwork/ceremonyclient.git
synced 2026-03-10 02:38:47 +08:00
commit d05a4d5f688dbd09900ceccdcc5f8109dd0671c2
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Wed Jun 12 00:50:16 2024 -0500
merge
commit db57ff1f191f9dedc87ca77da1c71244dd2325bd
Merge: 7b43494 2e3279a
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Wed Jun 12 00:49:32 2024 -0500
Merge branch 'v1.4.19' into not-release
commit 7b43494246e28152b46710c8c9821429d4231f7e
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Wed Jun 12 00:49:13 2024 -0500
pull from release site
commit 2e3279ac930ac630d9ca2b26cf4f3232abe79823
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 06:31:02 2024 -0500
remove binaries
commit 2768a8778b3860c5736352c8aa950e3496a46e56
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 06:24:44 2024 -0500
signatory #8 added
commit 6a944628575ccadd17c9f9f4a11a49c032fa0c1d
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 06:08:26 2024 -0500
signatory #6 added
commit b401fb65e5ddbe0340fe85aab1182d6120a4e161
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 05:39:03 2024 -0500
signatory #3 added
commit e5700913c0f6246fb607bcd3e219c257cb4a80e9
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 05:31:24 2024 -0500
signatory #15 added
commit 9b1da6c03e517135bfcd59226f900adab42f3687
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 05:23:02 2024 -0500
signatories #4 and #16 added
commit 9c97d1bbc399a070ac21b35ed9b1af127fa4c7ea
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 04:59:27 2024 -0500
signatories #1 and #2 added
commit 905e3f78a8121eade1c331ae910ed25dd534f27a
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 04:40:32 2024 -0500
build, binaries, signatory #13
commit ebfb57bc29d9ed1fb25d0dd100e38709354b3d84
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Sat Jun 8 03:38:53 2024 -0500
tests pass, let's go
commit 5d4612c6c624c3dc18f9a5657936034ac9d9d8dd
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 03:53:15 2024 -0500
update version info + readme
commit 6b0dd69e930d01b98acb8d7b56bb5d572e1a4324
Merge: 090d630 859221b
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 08:25:16 2024 +0000
Merge branch 'feat-data-worker-direct-config' into 'v1.4.19'
feat: support detached configuration mode for data workers
See merge request quilibrium/ceremonyclient!7
commit 859221b179ab2631fa474be2494259afaaa6bd51
Author: Cassandra Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 03:24:22 2024 -0500
feat: support detached configuration mode for data workers
commit 090d6301d44a2aa88886120783cd5a6e537aa6d1
Merge: 62db30c d1cae94
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 06:25:43 2024 +0000
Merge branch 'feat-go-1-22' into 'v1.4.19'
feat: go 1.22 support
See merge request quilibrium/ceremonyclient!6
commit d1cae942165f4871f8051e266722c0ca717780cb
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 06:25:43 2024 +0000
feat: go 1.22 support
commit 62db30c54f9258c92113c6664ce817670a339083
Merge: 0cbc0d0 f36cea3
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 03:52:17 2024 +0000
Merge branch 'rust-vdf' into 'v1.4.19'
Switch to Rust VDF
See merge request quilibrium/ceremonyclient!2
commit f36cea323bfe5e56f519f59f9a0cce35f0f8b6ab
Author: Agost Biro <agostbiro@gmail.com>
Date: Fri Jun 7 03:52:16 2024 +0000
Switch to Rust VDF
commit 0cbc0d0d319713e20ca7f48588c4153833e58429
Merge: 986e12c 0c48a83
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Fri Jun 7 00:50:15 2024 +0000
Merge branch 'release_image' into 'v1.4.19'
create docker image based on release binaries
See merge request quilibrium/ceremonyclient!4
commit 0c48a83bb5751abf7c8c0ff188bfdc2130631e78
Author: Marius Scurtescu <marius.scurtescu@gmail.com>
Date: Fri Jun 7 00:50:15 2024 +0000
create docker image based on release binaries
commit 986e12c88bb2d2b412b59f7db1ae39f828304dbe
Merge: 58456c1 a3ef5c6
Author: Cassie Heart <cassandra@quilibrium.com>
Date: Wed Jun 5 22:01:37 2024 +0000
Merge branch 'signature_check' into 'v1.4.19'
add default of signature check from QUILIBRIUM_SIGNATURE_CHECK env var
See merge request quilibrium/ceremonyclient!1
commit a3ef5c6af2d5de107d01c45a62d7324165e2551b
Author: Marius Scurtescu <marius.scurtescu@gmail.com>
Date: Wed Jun 5 14:37:50 2024 -0700
add default of signature check from QUILIBRIUM_SIGNATURE_CHECK env var
300 lines
7.0 KiB
Go
300 lines
7.0 KiB
Go
package basichost
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/netip"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
inat "github.com/libp2p/go-libp2p/p2p/net/nat"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
|
)
|
|
|
|
// NATManager is a simple interface to manage NAT devices.
|
|
// It listens Listen and ListenClose notifications from the network.Network,
|
|
// and tries to obtain port mappings for those.
|
|
type NATManager interface {
|
|
GetMapping(ma.Multiaddr) ma.Multiaddr
|
|
HasDiscoveredNAT() bool
|
|
io.Closer
|
|
}
|
|
|
|
// NewNATManager creates a NAT manager.
|
|
func NewNATManager(net network.Network) NATManager {
|
|
return newNATManager(net)
|
|
}
|
|
|
|
type entry struct {
|
|
protocol string
|
|
port int
|
|
}
|
|
|
|
type nat interface {
|
|
AddMapping(ctx context.Context, protocol string, port int) error
|
|
RemoveMapping(ctx context.Context, protocol string, port int) error
|
|
GetMapping(protocol string, port int) (netip.AddrPort, bool)
|
|
io.Closer
|
|
}
|
|
|
|
// so we can mock it in tests
|
|
var discoverNAT = func(ctx context.Context) (nat, error) { return inat.DiscoverNAT(ctx) }
|
|
|
|
// natManager takes care of adding + removing port mappings to the nat.
|
|
// Initialized with the host if it has a NATPortMap option enabled.
|
|
// natManager receives signals from the network, and check on nat mappings:
|
|
// - natManager listens to the network and adds or closes port mappings
|
|
// as the network signals Listen() or ListenClose().
|
|
// - closing the natManager closes the nat and its mappings.
|
|
type natManager struct {
|
|
net network.Network
|
|
natMx sync.RWMutex
|
|
nat nat
|
|
|
|
syncFlag chan struct{} // cap: 1
|
|
|
|
tracked map[entry]bool // the bool is only used in doSync and has no meaning outside of that function
|
|
|
|
refCount sync.WaitGroup
|
|
ctx context.Context
|
|
ctxCancel context.CancelFunc
|
|
}
|
|
|
|
func newNATManager(net network.Network) *natManager {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
nmgr := &natManager{
|
|
net: net,
|
|
syncFlag: make(chan struct{}, 1),
|
|
ctx: ctx,
|
|
ctxCancel: cancel,
|
|
tracked: make(map[entry]bool),
|
|
}
|
|
nmgr.refCount.Add(1)
|
|
go nmgr.background(ctx)
|
|
return nmgr
|
|
}
|
|
|
|
// Close closes the natManager, closing the underlying nat
|
|
// and unregistering from network events.
|
|
func (nmgr *natManager) Close() error {
|
|
nmgr.ctxCancel()
|
|
nmgr.refCount.Wait()
|
|
return nil
|
|
}
|
|
|
|
func (nmgr *natManager) HasDiscoveredNAT() bool {
|
|
nmgr.natMx.RLock()
|
|
defer nmgr.natMx.RUnlock()
|
|
return nmgr.nat != nil
|
|
}
|
|
|
|
func (nmgr *natManager) background(ctx context.Context) {
|
|
defer nmgr.refCount.Done()
|
|
|
|
defer func() {
|
|
nmgr.natMx.Lock()
|
|
defer nmgr.natMx.Unlock()
|
|
|
|
if nmgr.nat != nil {
|
|
nmgr.nat.Close()
|
|
}
|
|
}()
|
|
|
|
discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
natInstance, err := discoverNAT(discoverCtx)
|
|
if err != nil {
|
|
log.Info("DiscoverNAT error:", err)
|
|
return
|
|
}
|
|
|
|
nmgr.natMx.Lock()
|
|
nmgr.nat = natInstance
|
|
nmgr.natMx.Unlock()
|
|
|
|
// sign natManager up for network notifications
|
|
// we need to sign up here to avoid missing some notifs
|
|
// before the NAT has been found.
|
|
nmgr.net.Notify((*nmgrNetNotifiee)(nmgr))
|
|
defer nmgr.net.StopNotify((*nmgrNetNotifiee)(nmgr))
|
|
|
|
nmgr.doSync() // sync one first.
|
|
for {
|
|
select {
|
|
case <-nmgr.syncFlag:
|
|
nmgr.doSync() // sync when our listen addresses change.
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (nmgr *natManager) sync() {
|
|
select {
|
|
case nmgr.syncFlag <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// doSync syncs the current NAT mappings, removing any outdated mappings and adding any
|
|
// new mappings.
|
|
func (nmgr *natManager) doSync() {
|
|
for e := range nmgr.tracked {
|
|
nmgr.tracked[e] = false
|
|
}
|
|
var newAddresses []entry
|
|
for _, maddr := range nmgr.net.ListenAddresses() {
|
|
// Strip the IP
|
|
maIP, rest := ma.SplitFirst(maddr)
|
|
if maIP == nil || rest == nil {
|
|
continue
|
|
}
|
|
|
|
switch maIP.Protocol().Code {
|
|
case ma.P_IP6, ma.P_IP4:
|
|
default:
|
|
continue
|
|
}
|
|
|
|
// Only bother if we're listening on an unicast / unspecified IP.
|
|
ip := net.IP(maIP.RawValue())
|
|
if !ip.IsGlobalUnicast() && !ip.IsUnspecified() {
|
|
continue
|
|
}
|
|
|
|
// Extract the port/protocol
|
|
proto, _ := ma.SplitFirst(rest)
|
|
if proto == nil {
|
|
continue
|
|
}
|
|
|
|
var protocol string
|
|
switch proto.Protocol().Code {
|
|
case ma.P_TCP:
|
|
protocol = "tcp"
|
|
case ma.P_UDP:
|
|
protocol = "udp"
|
|
default:
|
|
continue
|
|
}
|
|
port, err := strconv.ParseUint(proto.Value(), 10, 16)
|
|
if err != nil {
|
|
// bug in multiaddr
|
|
panic(err)
|
|
}
|
|
e := entry{protocol: protocol, port: int(port)}
|
|
if _, ok := nmgr.tracked[e]; ok {
|
|
nmgr.tracked[e] = true
|
|
} else {
|
|
newAddresses = append(newAddresses, e)
|
|
}
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
defer wg.Wait()
|
|
|
|
// Close old mappings
|
|
for e, v := range nmgr.tracked {
|
|
if !v {
|
|
nmgr.nat.RemoveMapping(nmgr.ctx, e.protocol, e.port)
|
|
delete(nmgr.tracked, e)
|
|
}
|
|
}
|
|
|
|
// Create new mappings.
|
|
for _, e := range newAddresses {
|
|
if err := nmgr.nat.AddMapping(nmgr.ctx, e.protocol, e.port); err != nil {
|
|
log.Errorf("failed to port-map %s port %d: %s", e.protocol, e.port, err)
|
|
}
|
|
nmgr.tracked[e] = false
|
|
}
|
|
}
|
|
|
|
func (nmgr *natManager) GetMapping(addr ma.Multiaddr) ma.Multiaddr {
|
|
nmgr.natMx.Lock()
|
|
defer nmgr.natMx.Unlock()
|
|
|
|
if nmgr.nat == nil { // NAT not yet initialized
|
|
return nil
|
|
}
|
|
|
|
var found bool
|
|
var proto int // ma.P_TCP or ma.P_UDP
|
|
transport, rest := ma.SplitFunc(addr, func(c ma.Component) bool {
|
|
if found {
|
|
return true
|
|
}
|
|
proto = c.Protocol().Code
|
|
found = proto == ma.P_TCP || proto == ma.P_UDP
|
|
return false
|
|
})
|
|
if !manet.IsThinWaist(transport) {
|
|
return nil
|
|
}
|
|
|
|
naddr, err := manet.ToNetAddr(transport)
|
|
if err != nil {
|
|
log.Error("error parsing net multiaddr %q: %s", transport, err)
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
ip net.IP
|
|
port int
|
|
protocol string
|
|
)
|
|
switch naddr := naddr.(type) {
|
|
case *net.TCPAddr:
|
|
ip = naddr.IP
|
|
port = naddr.Port
|
|
protocol = "tcp"
|
|
case *net.UDPAddr:
|
|
ip = naddr.IP
|
|
port = naddr.Port
|
|
protocol = "udp"
|
|
default:
|
|
return nil
|
|
}
|
|
|
|
if !ip.IsGlobalUnicast() && !ip.IsUnspecified() {
|
|
// We only map global unicast & unspecified addresses ports, not broadcast, multicast, etc.
|
|
return nil
|
|
}
|
|
|
|
extAddr, ok := nmgr.nat.GetMapping(protocol, port)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
var mappedAddr net.Addr
|
|
switch naddr.(type) {
|
|
case *net.TCPAddr:
|
|
mappedAddr = net.TCPAddrFromAddrPort(extAddr)
|
|
case *net.UDPAddr:
|
|
mappedAddr = net.UDPAddrFromAddrPort(extAddr)
|
|
}
|
|
mappedMaddr, err := manet.FromNetAddr(mappedAddr)
|
|
if err != nil {
|
|
log.Errorf("mapped addr can't be turned into a multiaddr %q: %s", mappedAddr, err)
|
|
return nil
|
|
}
|
|
extMaddr := mappedMaddr
|
|
if rest != nil {
|
|
extMaddr = ma.Join(extMaddr, rest)
|
|
}
|
|
return extMaddr
|
|
}
|
|
|
|
type nmgrNetNotifiee natManager
|
|
|
|
func (nn *nmgrNetNotifiee) natManager() *natManager { return (*natManager)(nn) }
|
|
func (nn *nmgrNetNotifiee) Listen(network.Network, ma.Multiaddr) { nn.natManager().sync() }
|
|
func (nn *nmgrNetNotifiee) ListenClose(n network.Network, addr ma.Multiaddr) { nn.natManager().sync() }
|
|
func (nn *nmgrNetNotifiee) Connected(network.Network, network.Conn) {}
|
|
func (nn *nmgrNetNotifiee) Disconnected(network.Network, network.Conn) {}
|