mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-19 16:56:39 +00:00
Compare commits
14 Commits
cli-ws-pro
...
v0.59.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
000e99e7f3 | ||
|
|
0d2e67983a | ||
|
|
5151f19d29 | ||
|
|
bedd3cabc9 | ||
|
|
d35a845dbd | ||
|
|
4e03f708a4 | ||
|
|
654aa9581d | ||
|
|
9021bb512b | ||
|
|
768332820e | ||
|
|
229c65ffa1 | ||
|
|
4d33567888 | ||
|
|
88467883fc | ||
|
|
954f40991f | ||
|
|
34341d95a9 |
@@ -29,7 +29,8 @@ func Backoff(ctx context.Context) backoff.BackOff {
|
|||||||
// The component parameter specifies the WebSocket proxy component path (e.g., "/management", "/signal").
|
// The component parameter specifies the WebSocket proxy component path (e.g., "/management", "/signal").
|
||||||
func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, component string) (*grpc.ClientConn, error) {
|
func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, component string) (*grpc.ClientConn, error) {
|
||||||
transportOption := grpc.WithTransportCredentials(insecure.NewCredentials())
|
transportOption := grpc.WithTransportCredentials(insecure.NewCredentials())
|
||||||
if tlsEnabled {
|
// for js, the outer websocket layer takes care of tls
|
||||||
|
if tlsEnabled && runtime.GOOS != "js" {
|
||||||
certPool, err := x509.SystemCertPool()
|
certPool, err := x509.SystemCertPool()
|
||||||
if err != nil || certPool == nil {
|
if err != nil || certPool == nil {
|
||||||
log.Debugf("System cert pool not available; falling back to embedded cert, error: %v", err)
|
log.Debugf("System cert pool not available; falling back to embedded cert, error: %v", err)
|
||||||
@@ -37,9 +38,7 @@ func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, compone
|
|||||||
}
|
}
|
||||||
|
|
||||||
transportOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
transportOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
|
||||||
// for js, outer websocket layer takes care of tls verification via WithCustomDialer
|
RootCAs: certPool,
|
||||||
InsecureSkipVerify: runtime.GOOS == "js",
|
|
||||||
RootCAs: certPool,
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,44 @@ func (c *KernelConfigurer) UpdatePeer(peerKey string, allowedIps []netip.Prefix,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *KernelConfigurer) RemoveEndpointAddress(peerKey string) error {
|
||||||
|
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the existing peer to preserve its allowed IPs
|
||||||
|
existingPeer, err := c.getPeer(c.deviceName, peerKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get peer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
removePeerCfg := wgtypes.PeerConfig{
|
||||||
|
PublicKey: peerKeyParsed,
|
||||||
|
Remove: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.configure(wgtypes.Config{Peers: []wgtypes.PeerConfig{removePeerCfg}}); err != nil {
|
||||||
|
return fmt.Errorf(`error removing peer %s from interface %s: %w`, peerKey, c.deviceName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Re-add the peer without the endpoint but same AllowedIPs
|
||||||
|
reAddPeerCfg := wgtypes.PeerConfig{
|
||||||
|
PublicKey: peerKeyParsed,
|
||||||
|
AllowedIPs: existingPeer.AllowedIPs,
|
||||||
|
ReplaceAllowedIPs: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.configure(wgtypes.Config{Peers: []wgtypes.PeerConfig{reAddPeerCfg}}); err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
`error re-adding peer %s to interface %s with allowed IPs %v: %w`,
|
||||||
|
peerKey, c.deviceName, existingPeer.AllowedIPs, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *KernelConfigurer) RemovePeer(peerKey string) error {
|
func (c *KernelConfigurer) RemovePeer(peerKey string) error {
|
||||||
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -106,6 +106,67 @@ func (c *WGUSPConfigurer) UpdatePeer(peerKey string, allowedIps []netip.Prefix,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *WGUSPConfigurer) RemoveEndpointAddress(peerKey string) error {
|
||||||
|
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse peer key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ipcStr, err := c.device.IpcGet()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get IPC config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse current status to get allowed IPs for the peer
|
||||||
|
stats, err := parseStatus(c.deviceName, ipcStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parse IPC config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var allowedIPs []net.IPNet
|
||||||
|
found := false
|
||||||
|
for _, peer := range stats.Peers {
|
||||||
|
if peer.PublicKey == peerKey {
|
||||||
|
allowedIPs = peer.AllowedIPs
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return fmt.Errorf("peer %s not found", peerKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the peer from the WireGuard configuration
|
||||||
|
peer := wgtypes.PeerConfig{
|
||||||
|
PublicKey: peerKeyParsed,
|
||||||
|
Remove: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
config := wgtypes.Config{
|
||||||
|
Peers: []wgtypes.PeerConfig{peer},
|
||||||
|
}
|
||||||
|
if ipcErr := c.device.IpcSet(toWgUserspaceString(config)); ipcErr != nil {
|
||||||
|
return fmt.Errorf("failed to remove peer: %s", ipcErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the peer config
|
||||||
|
peer = wgtypes.PeerConfig{
|
||||||
|
PublicKey: peerKeyParsed,
|
||||||
|
ReplaceAllowedIPs: true,
|
||||||
|
AllowedIPs: allowedIPs,
|
||||||
|
}
|
||||||
|
|
||||||
|
config = wgtypes.Config{
|
||||||
|
Peers: []wgtypes.PeerConfig{peer},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.device.IpcSet(toWgUserspaceString(config)); err != nil {
|
||||||
|
return fmt.Errorf("remove endpoint address: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *WGUSPConfigurer) RemovePeer(peerKey string) error {
|
func (c *WGUSPConfigurer) RemovePeer(peerKey string) error {
|
||||||
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
peerKeyParsed, err := wgtypes.ParseKey(peerKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -21,4 +21,5 @@ type WGConfigurer interface {
|
|||||||
GetStats() (map[string]configurer.WGStats, error)
|
GetStats() (map[string]configurer.WGStats, error)
|
||||||
FullStats() (*configurer.Stats, error)
|
FullStats() (*configurer.Stats, error)
|
||||||
LastActivities() map[string]monotime.Time
|
LastActivities() map[string]monotime.Time
|
||||||
|
RemoveEndpointAddress(peerKey string) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,6 +148,17 @@ func (w *WGIface) UpdatePeer(peerKey string, allowedIps []netip.Prefix, keepAliv
|
|||||||
return w.configurer.UpdatePeer(peerKey, allowedIps, keepAlive, endpoint, preSharedKey)
|
return w.configurer.UpdatePeer(peerKey, allowedIps, keepAlive, endpoint, preSharedKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WGIface) RemoveEndpointAddress(peerKey string) error {
|
||||||
|
w.mu.Lock()
|
||||||
|
defer w.mu.Unlock()
|
||||||
|
if w.configurer == nil {
|
||||||
|
return ErrIfaceNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Removing endpoint address: %s", peerKey)
|
||||||
|
return w.configurer.RemoveEndpointAddress(peerKey)
|
||||||
|
}
|
||||||
|
|
||||||
// RemovePeer removes a Wireguard Peer from the interface iface
|
// RemovePeer removes a Wireguard Peer from the interface iface
|
||||||
func (w *WGIface) RemovePeer(peerKey string) error {
|
func (w *WGIface) RemovePeer(peerKey string) error {
|
||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ const (
|
|||||||
systemdDbusSetDefaultRouteMethodSuffix = systemdDbusLinkInterface + ".SetDefaultRoute"
|
systemdDbusSetDefaultRouteMethodSuffix = systemdDbusLinkInterface + ".SetDefaultRoute"
|
||||||
systemdDbusSetDomainsMethodSuffix = systemdDbusLinkInterface + ".SetDomains"
|
systemdDbusSetDomainsMethodSuffix = systemdDbusLinkInterface + ".SetDomains"
|
||||||
systemdDbusSetDNSSECMethodSuffix = systemdDbusLinkInterface + ".SetDNSSEC"
|
systemdDbusSetDNSSECMethodSuffix = systemdDbusLinkInterface + ".SetDNSSEC"
|
||||||
|
systemdDbusSetDNSOverTLSMethodSuffix = systemdDbusLinkInterface + ".SetDNSOverTLS"
|
||||||
systemdDbusResolvConfModeForeign = "foreign"
|
systemdDbusResolvConfModeForeign = "foreign"
|
||||||
|
|
||||||
dbusErrorUnknownObject = "org.freedesktop.DBus.Error.UnknownObject"
|
dbusErrorUnknownObject = "org.freedesktop.DBus.Error.UnknownObject"
|
||||||
@@ -102,6 +103,11 @@ func (s *systemdDbusConfigurator) applyDNSConfig(config HostDNSConfig, stateMana
|
|||||||
log.Warnf("failed to set DNSSEC to 'no': %v", err)
|
log.Warnf("failed to set DNSSEC to 'no': %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We don't support DNSOverTLS. On some machines this is default on so we explicitly set it to off
|
||||||
|
if err := s.callLinkMethod(systemdDbusSetDNSOverTLSMethodSuffix, dnsSecDisabled); err != nil {
|
||||||
|
log.Warnf("failed to set DNSOverTLS to 'no': %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
searchDomains []string
|
searchDomains []string
|
||||||
matchDomains []string
|
matchDomains []string
|
||||||
|
|||||||
78
client/internal/dnsfwd/cache.go
Normal file
78
client/internal/dnsfwd/cache.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package dnsfwd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cache struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
records map[string]*cacheEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
type cacheEntry struct {
|
||||||
|
ip4Addrs []netip.Addr
|
||||||
|
ip6Addrs []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func newCache() *cache {
|
||||||
|
return &cache{
|
||||||
|
records: make(map[string]*cacheEntry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cache) get(domain string, reqType uint16) ([]netip.Addr, bool) {
|
||||||
|
c.mu.RLock()
|
||||||
|
defer c.mu.RUnlock()
|
||||||
|
|
||||||
|
entry, exists := c.records[normalizeDomain(domain)]
|
||||||
|
if !exists {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch reqType {
|
||||||
|
case dns.TypeA:
|
||||||
|
return slices.Clone(entry.ip4Addrs), true
|
||||||
|
case dns.TypeAAAA:
|
||||||
|
return slices.Clone(entry.ip6Addrs), true
|
||||||
|
default:
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cache) set(domain string, reqType uint16, addrs []netip.Addr) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
norm := normalizeDomain(domain)
|
||||||
|
entry, exists := c.records[norm]
|
||||||
|
if !exists {
|
||||||
|
entry = &cacheEntry{}
|
||||||
|
c.records[norm] = entry
|
||||||
|
}
|
||||||
|
|
||||||
|
switch reqType {
|
||||||
|
case dns.TypeA:
|
||||||
|
entry.ip4Addrs = slices.Clone(addrs)
|
||||||
|
case dns.TypeAAAA:
|
||||||
|
entry.ip6Addrs = slices.Clone(addrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// unset removes cached entries for the given domain and request type.
|
||||||
|
func (c *cache) unset(domain string) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
delete(c.records, normalizeDomain(domain))
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeDomain converts an input domain into a canonical form used as cache key:
|
||||||
|
// lowercase and fully-qualified (with trailing dot).
|
||||||
|
func normalizeDomain(domain string) string {
|
||||||
|
// dns.Fqdn ensures trailing dot; ToLower for consistent casing
|
||||||
|
return dns.Fqdn(strings.ToLower(domain))
|
||||||
|
}
|
||||||
86
client/internal/dnsfwd/cache_test.go
Normal file
86
client/internal/dnsfwd/cache_test.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package dnsfwd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mustAddr(t *testing.T, s string) netip.Addr {
|
||||||
|
t.Helper()
|
||||||
|
a, err := netip.ParseAddr(s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse addr %s: %v", s, err)
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheNormalization(t *testing.T) {
|
||||||
|
c := newCache()
|
||||||
|
|
||||||
|
// Mixed case, without trailing dot
|
||||||
|
domainInput := "ExAmPlE.CoM"
|
||||||
|
ipv4 := []netip.Addr{mustAddr(t, "1.2.3.4")}
|
||||||
|
c.set(domainInput, 1 /* dns.TypeA */, ipv4)
|
||||||
|
|
||||||
|
// Lookup with lower, with trailing dot
|
||||||
|
if got, ok := c.get("example.com.", 1); !ok || len(got) != 1 || got[0].String() != "1.2.3.4" {
|
||||||
|
t.Fatalf("expected cached IPv4 result via normalized key, got=%v ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup with different casing again
|
||||||
|
if got, ok := c.get("EXAMPLE.COM", 1); !ok || len(got) != 1 || got[0].String() != "1.2.3.4" {
|
||||||
|
t.Fatalf("expected cached IPv4 result via different casing, got=%v ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheSeparateTypes(t *testing.T) {
|
||||||
|
c := newCache()
|
||||||
|
|
||||||
|
domain := "test.local"
|
||||||
|
ipv4 := []netip.Addr{mustAddr(t, "10.0.0.1")}
|
||||||
|
ipv6 := []netip.Addr{mustAddr(t, "2001:db8::1")}
|
||||||
|
|
||||||
|
c.set(domain, 1 /* A */, ipv4)
|
||||||
|
c.set(domain, 28 /* AAAA */, ipv6)
|
||||||
|
|
||||||
|
got4, ok4 := c.get(domain, 1)
|
||||||
|
if !ok4 || len(got4) != 1 || got4[0] != ipv4[0] {
|
||||||
|
t.Fatalf("expected A record from cache, got=%v ok=%v", got4, ok4)
|
||||||
|
}
|
||||||
|
|
||||||
|
got6, ok6 := c.get(domain, 28)
|
||||||
|
if !ok6 || len(got6) != 1 || got6[0] != ipv6[0] {
|
||||||
|
t.Fatalf("expected AAAA record from cache, got=%v ok=%v", got6, ok6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheCloneOnGetAndSet(t *testing.T) {
|
||||||
|
c := newCache()
|
||||||
|
domain := "clone.test"
|
||||||
|
|
||||||
|
src := []netip.Addr{mustAddr(t, "8.8.8.8")}
|
||||||
|
c.set(domain, 1, src)
|
||||||
|
|
||||||
|
// Mutate source slice; cache should be unaffected
|
||||||
|
src[0] = mustAddr(t, "9.9.9.9")
|
||||||
|
|
||||||
|
got, ok := c.get(domain, 1)
|
||||||
|
if !ok || len(got) != 1 || got[0].String() != "8.8.8.8" {
|
||||||
|
t.Fatalf("expected cached value to be independent of source slice, got=%v ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mutate returned slice; internal cache should remain unchanged
|
||||||
|
got[0] = mustAddr(t, "4.4.4.4")
|
||||||
|
got2, ok2 := c.get(domain, 1)
|
||||||
|
if !ok2 || len(got2) != 1 || got2[0].String() != "8.8.8.8" {
|
||||||
|
t.Fatalf("expected returned slice to be a clone, got=%v ok=%v", got2, ok2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCacheMiss(t *testing.T) {
|
||||||
|
c := newCache()
|
||||||
|
if got, ok := c.get("missing.example", 1); ok || got != nil {
|
||||||
|
t.Fatalf("expected cache miss, got=%v ok=%v", got, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -46,6 +46,7 @@ type DNSForwarder struct {
|
|||||||
fwdEntries []*ForwarderEntry
|
fwdEntries []*ForwarderEntry
|
||||||
firewall firewaller
|
firewall firewaller
|
||||||
resolver resolver
|
resolver resolver
|
||||||
|
cache *cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSForwarder(listenAddress string, ttl uint32, firewall firewaller, statusRecorder *peer.Status) *DNSForwarder {
|
func NewDNSForwarder(listenAddress string, ttl uint32, firewall firewaller, statusRecorder *peer.Status) *DNSForwarder {
|
||||||
@@ -56,6 +57,7 @@ func NewDNSForwarder(listenAddress string, ttl uint32, firewall firewaller, stat
|
|||||||
firewall: firewall,
|
firewall: firewall,
|
||||||
statusRecorder: statusRecorder,
|
statusRecorder: statusRecorder,
|
||||||
resolver: net.DefaultResolver,
|
resolver: net.DefaultResolver,
|
||||||
|
cache: newCache(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,10 +105,39 @@ func (f *DNSForwarder) UpdateDomains(entries []*ForwarderEntry) {
|
|||||||
f.mutex.Lock()
|
f.mutex.Lock()
|
||||||
defer f.mutex.Unlock()
|
defer f.mutex.Unlock()
|
||||||
|
|
||||||
|
// remove cache entries for domains that no longer appear
|
||||||
|
f.removeStaleCacheEntries(f.fwdEntries, entries)
|
||||||
|
|
||||||
f.fwdEntries = entries
|
f.fwdEntries = entries
|
||||||
log.Debugf("Updated DNS forwarder with %d domains", len(entries))
|
log.Debugf("Updated DNS forwarder with %d domains", len(entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeStaleCacheEntries unsets cache items for domains that were present
|
||||||
|
// in the old list but not present in the new list.
|
||||||
|
func (f *DNSForwarder) removeStaleCacheEntries(oldEntries, newEntries []*ForwarderEntry) {
|
||||||
|
if f.cache == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newSet := make(map[string]struct{}, len(newEntries))
|
||||||
|
for _, e := range newEntries {
|
||||||
|
if e == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newSet[e.Domain.PunycodeString()] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range oldEntries {
|
||||||
|
if e == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pattern := e.Domain.PunycodeString()
|
||||||
|
if _, ok := newSet[pattern]; !ok {
|
||||||
|
f.cache.unset(pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (f *DNSForwarder) Close(ctx context.Context) error {
|
func (f *DNSForwarder) Close(ctx context.Context) error {
|
||||||
var result *multierror.Error
|
var result *multierror.Error
|
||||||
|
|
||||||
@@ -171,6 +202,7 @@ func (f *DNSForwarder) handleDNSQuery(w dns.ResponseWriter, query *dns.Msg) *dns
|
|||||||
|
|
||||||
f.updateInternalState(ips, mostSpecificResId, matchingEntries)
|
f.updateInternalState(ips, mostSpecificResId, matchingEntries)
|
||||||
f.addIPsToResponse(resp, domain, ips)
|
f.addIPsToResponse(resp, domain, ips)
|
||||||
|
f.cache.set(domain, question.Qtype, ips)
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
@@ -282,29 +314,69 @@ func (f *DNSForwarder) setResponseCodeForNotFound(ctx context.Context, resp *dns
|
|||||||
resp.Rcode = dns.RcodeSuccess
|
resp.Rcode = dns.RcodeSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleDNSError processes DNS lookup errors and sends an appropriate error response
|
// handleDNSError processes DNS lookup errors and sends an appropriate error response.
|
||||||
func (f *DNSForwarder) handleDNSError(ctx context.Context, w dns.ResponseWriter, question dns.Question, resp *dns.Msg, domain string, err error) {
|
func (f *DNSForwarder) handleDNSError(
|
||||||
|
ctx context.Context,
|
||||||
|
w dns.ResponseWriter,
|
||||||
|
question dns.Question,
|
||||||
|
resp *dns.Msg,
|
||||||
|
domain string,
|
||||||
|
err error,
|
||||||
|
) {
|
||||||
|
// Default to SERVFAIL; override below when appropriate.
|
||||||
|
resp.Rcode = dns.RcodeServerFailure
|
||||||
|
|
||||||
|
qType := question.Qtype
|
||||||
|
qTypeName := dns.TypeToString[qType]
|
||||||
|
|
||||||
|
// Prefer typed DNS errors; fall back to generic logging otherwise.
|
||||||
var dnsErr *net.DNSError
|
var dnsErr *net.DNSError
|
||||||
|
if !errors.As(err, &dnsErr) {
|
||||||
switch {
|
log.Warnf(errResolveFailed, domain, err)
|
||||||
case errors.As(err, &dnsErr):
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
resp.Rcode = dns.RcodeServerFailure
|
log.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
if dnsErr.IsNotFound {
|
|
||||||
f.setResponseCodeForNotFound(ctx, resp, domain, question.Qtype)
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if dnsErr.Server != "" {
|
// NotFound: set NXDOMAIN / appropriate code via helper.
|
||||||
log.Warnf("failed to resolve query for type=%s domain=%s server=%s: %v", dns.TypeToString[question.Qtype], domain, dnsErr.Server, err)
|
if dnsErr.IsNotFound {
|
||||||
} else {
|
f.setResponseCodeForNotFound(ctx, resp, domain, qType)
|
||||||
log.Warnf(errResolveFailed, domain, err)
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
log.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
}
|
}
|
||||||
default:
|
f.cache.set(domain, question.Qtype, nil)
|
||||||
resp.Rcode = dns.RcodeServerFailure
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upstream failed but we might have a cached answer—serve it if present.
|
||||||
|
if ips, ok := f.cache.get(domain, qType); ok {
|
||||||
|
if len(ips) > 0 {
|
||||||
|
log.Debugf("serving cached DNS response after upstream failure: domain=%s type=%s", domain, qTypeName)
|
||||||
|
f.addIPsToResponse(resp, domain, ips)
|
||||||
|
resp.Rcode = dns.RcodeSuccess
|
||||||
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
log.Errorf("failed to write cached DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
|
} else { // send NXDOMAIN / appropriate code if cache is empty
|
||||||
|
f.setResponseCodeForNotFound(ctx, resp, domain, qType)
|
||||||
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
log.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// No cache. Log with or without the server field for more context.
|
||||||
|
if dnsErr.Server != "" {
|
||||||
|
log.Warnf("failed to resolve: type=%s domain=%s server=%s: %v", qTypeName, domain, dnsErr.Server, err)
|
||||||
|
} else {
|
||||||
log.Warnf(errResolveFailed, domain, err)
|
log.Warnf(errResolveFailed, domain, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := w.WriteMsg(resp); err != nil {
|
// Write final failure response.
|
||||||
log.Errorf("failed to write failure DNS response: %v", err)
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
log.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -648,6 +648,95 @@ func TestDNSForwarder_TCPTruncation(t *testing.T) {
|
|||||||
assert.LessOrEqual(t, writtenResp.Len(), dns.MinMsgSize, "Response should fit in minimum UDP size")
|
assert.LessOrEqual(t, writtenResp.Len(), dns.MinMsgSize, "Response should fit in minimum UDP size")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensures that when the first query succeeds and populates the cache,
|
||||||
|
// a subsequent upstream failure still returns a successful response from cache.
|
||||||
|
func TestDNSForwarder_ServeFromCacheOnUpstreamFailure(t *testing.T) {
|
||||||
|
mockResolver := &MockResolver{}
|
||||||
|
forwarder := NewDNSForwarder("127.0.0.1:0", 300, nil, &peer.Status{})
|
||||||
|
forwarder.resolver = mockResolver
|
||||||
|
|
||||||
|
d, err := domain.FromString("example.com")
|
||||||
|
require.NoError(t, err)
|
||||||
|
entries := []*ForwarderEntry{{Domain: d, ResID: "res-cache"}}
|
||||||
|
forwarder.UpdateDomains(entries)
|
||||||
|
|
||||||
|
ip := netip.MustParseAddr("1.2.3.4")
|
||||||
|
|
||||||
|
// First call resolves successfully and populates cache
|
||||||
|
mockResolver.On("LookupNetIP", mock.Anything, "ip4", dns.Fqdn("example.com")).
|
||||||
|
Return([]netip.Addr{ip}, nil).Once()
|
||||||
|
|
||||||
|
// Second call fails upstream; forwarder should serve from cache
|
||||||
|
mockResolver.On("LookupNetIP", mock.Anything, "ip4", dns.Fqdn("example.com")).
|
||||||
|
Return([]netip.Addr{}, &net.DNSError{Err: "temporary failure"}).Once()
|
||||||
|
|
||||||
|
// First query: populate cache
|
||||||
|
q1 := &dns.Msg{}
|
||||||
|
q1.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
||||||
|
w1 := &test.MockResponseWriter{}
|
||||||
|
resp1 := forwarder.handleDNSQuery(w1, q1)
|
||||||
|
require.NotNil(t, resp1)
|
||||||
|
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
||||||
|
require.Len(t, resp1.Answer, 1)
|
||||||
|
|
||||||
|
// Second query: serve from cache after upstream failure
|
||||||
|
q2 := &dns.Msg{}
|
||||||
|
q2.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
||||||
|
var writtenResp *dns.Msg
|
||||||
|
w2 := &test.MockResponseWriter{WriteMsgFunc: func(m *dns.Msg) error { writtenResp = m; return nil }}
|
||||||
|
_ = forwarder.handleDNSQuery(w2, q2)
|
||||||
|
|
||||||
|
require.NotNil(t, writtenResp, "expected response to be written")
|
||||||
|
require.Equal(t, dns.RcodeSuccess, writtenResp.Rcode)
|
||||||
|
require.Len(t, writtenResp.Answer, 1)
|
||||||
|
|
||||||
|
mockResolver.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that cache normalization works across casing and trailing dot variations.
|
||||||
|
func TestDNSForwarder_CacheNormalizationCasingAndDot(t *testing.T) {
|
||||||
|
mockResolver := &MockResolver{}
|
||||||
|
forwarder := NewDNSForwarder("127.0.0.1:0", 300, nil, &peer.Status{})
|
||||||
|
forwarder.resolver = mockResolver
|
||||||
|
|
||||||
|
d, err := domain.FromString("ExAmPlE.CoM")
|
||||||
|
require.NoError(t, err)
|
||||||
|
entries := []*ForwarderEntry{{Domain: d, ResID: "res-norm"}}
|
||||||
|
forwarder.UpdateDomains(entries)
|
||||||
|
|
||||||
|
ip := netip.MustParseAddr("9.8.7.6")
|
||||||
|
|
||||||
|
// Initial resolution with mixed case to populate cache
|
||||||
|
mixedQuery := "ExAmPlE.CoM"
|
||||||
|
mockResolver.On("LookupNetIP", mock.Anything, "ip4", dns.Fqdn(strings.ToLower(mixedQuery))).
|
||||||
|
Return([]netip.Addr{ip}, nil).Once()
|
||||||
|
|
||||||
|
q1 := &dns.Msg{}
|
||||||
|
q1.SetQuestion(mixedQuery+".", dns.TypeA)
|
||||||
|
w1 := &test.MockResponseWriter{}
|
||||||
|
resp1 := forwarder.handleDNSQuery(w1, q1)
|
||||||
|
require.NotNil(t, resp1)
|
||||||
|
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
||||||
|
require.Len(t, resp1.Answer, 1)
|
||||||
|
|
||||||
|
// Subsequent query without dot and upper case should hit cache even if upstream fails
|
||||||
|
// Forwarder lowercases and uses the question name as-is (no trailing dot here)
|
||||||
|
mockResolver.On("LookupNetIP", mock.Anything, "ip4", strings.ToLower("EXAMPLE.COM")).
|
||||||
|
Return([]netip.Addr{}, &net.DNSError{Err: "temporary failure"}).Once()
|
||||||
|
|
||||||
|
q2 := &dns.Msg{}
|
||||||
|
q2.SetQuestion("EXAMPLE.COM", dns.TypeA)
|
||||||
|
var writtenResp *dns.Msg
|
||||||
|
w2 := &test.MockResponseWriter{WriteMsgFunc: func(m *dns.Msg) error { writtenResp = m; return nil }}
|
||||||
|
_ = forwarder.handleDNSQuery(w2, q2)
|
||||||
|
|
||||||
|
require.NotNil(t, writtenResp)
|
||||||
|
require.Equal(t, dns.RcodeSuccess, writtenResp.Rcode)
|
||||||
|
require.Len(t, writtenResp.Answer, 1)
|
||||||
|
|
||||||
|
mockResolver.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDNSForwarder_MultipleOverlappingPatterns(t *testing.T) {
|
func TestDNSForwarder_MultipleOverlappingPatterns(t *testing.T) {
|
||||||
// Test complex overlapping pattern scenarios
|
// Test complex overlapping pattern scenarios
|
||||||
mockFirewall := &MockFirewall{}
|
mockFirewall := &MockFirewall{}
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ type Manager struct {
|
|||||||
fwRules []firewall.Rule
|
fwRules []firewall.Rule
|
||||||
tcpRules []firewall.Rule
|
tcpRules []firewall.Rule
|
||||||
dnsForwarder *DNSForwarder
|
dnsForwarder *DNSForwarder
|
||||||
port uint16
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenPort() uint16 {
|
func ListenPort() uint16 {
|
||||||
@@ -49,11 +48,16 @@ func ListenPort() uint16 {
|
|||||||
return listenPort
|
return listenPort
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewManager(fw firewall.Manager, statusRecorder *peer.Status, port uint16) *Manager {
|
func SetListenPort(port uint16) {
|
||||||
|
listenPortMu.Lock()
|
||||||
|
listenPort = port
|
||||||
|
listenPortMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewManager(fw firewall.Manager, statusRecorder *peer.Status) *Manager {
|
||||||
return &Manager{
|
return &Manager{
|
||||||
firewall: fw,
|
firewall: fw,
|
||||||
statusRecorder: statusRecorder,
|
statusRecorder: statusRecorder,
|
||||||
port: port,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,12 +71,6 @@ func (m *Manager) Start(fwdEntries []*ForwarderEntry) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.port > 0 {
|
|
||||||
listenPortMu.Lock()
|
|
||||||
listenPort = m.port
|
|
||||||
listenPortMu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
m.dnsForwarder = NewDNSForwarder(fmt.Sprintf(":%d", ListenPort()), dnsTTL, m.firewall, m.statusRecorder)
|
m.dnsForwarder = NewDNSForwarder(fmt.Sprintf(":%d", ListenPort()), dnsTTL, m.firewall, m.statusRecorder)
|
||||||
go func() {
|
go func() {
|
||||||
if err := m.dnsForwarder.Listen(fwdEntries); err != nil {
|
if err := m.dnsForwarder.Listen(fwdEntries); err != nil {
|
||||||
|
|||||||
@@ -1849,6 +1849,10 @@ func (e *Engine) updateDNSForwarder(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if forwarderPort > 0 {
|
||||||
|
dnsfwd.SetListenPort(forwarderPort)
|
||||||
|
}
|
||||||
|
|
||||||
if !enabled {
|
if !enabled {
|
||||||
if e.dnsForwardMgr == nil {
|
if e.dnsForwardMgr == nil {
|
||||||
return
|
return
|
||||||
@@ -1862,7 +1866,7 @@ func (e *Engine) updateDNSForwarder(
|
|||||||
if len(fwdEntries) > 0 {
|
if len(fwdEntries) > 0 {
|
||||||
switch {
|
switch {
|
||||||
case e.dnsForwardMgr == nil:
|
case e.dnsForwardMgr == nil:
|
||||||
e.dnsForwardMgr = dnsfwd.NewManager(e.firewall, e.statusRecorder, forwarderPort)
|
e.dnsForwardMgr = dnsfwd.NewManager(e.firewall, e.statusRecorder)
|
||||||
if err := e.dnsForwardMgr.Start(fwdEntries); err != nil {
|
if err := e.dnsForwardMgr.Start(fwdEntries); err != nil {
|
||||||
log.Errorf("failed to start DNS forward: %v", err)
|
log.Errorf("failed to start DNS forward: %v", err)
|
||||||
e.dnsForwardMgr = nil
|
e.dnsForwardMgr = nil
|
||||||
@@ -1892,7 +1896,7 @@ func (e *Engine) restartDnsFwd(fwdEntries []*dnsfwd.ForwarderEntry, forwarderPor
|
|||||||
if err := e.dnsForwardMgr.Stop(context.Background()); err != nil {
|
if err := e.dnsForwardMgr.Stop(context.Background()); err != nil {
|
||||||
log.Errorf("failed to stop DNS forward: %v", err)
|
log.Errorf("failed to stop DNS forward: %v", err)
|
||||||
}
|
}
|
||||||
e.dnsForwardMgr = dnsfwd.NewManager(e.firewall, e.statusRecorder, forwarderPort)
|
e.dnsForwardMgr = dnsfwd.NewManager(e.firewall, e.statusRecorder)
|
||||||
if err := e.dnsForwardMgr.Start(fwdEntries); err != nil {
|
if err := e.dnsForwardMgr.Start(fwdEntries); err != nil {
|
||||||
log.Errorf("failed to start DNS forward: %v", err)
|
log.Errorf("failed to start DNS forward: %v", err)
|
||||||
e.dnsForwardMgr = nil
|
e.dnsForwardMgr = nil
|
||||||
|
|||||||
@@ -105,6 +105,10 @@ type MockWGIface struct {
|
|||||||
LastActivitiesFunc func() map[string]monotime.Time
|
LastActivitiesFunc func() map[string]monotime.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MockWGIface) RemoveEndpointAddress(_ string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MockWGIface) FullStats() (*configurer.Stats, error) {
|
func (m *MockWGIface) FullStats() (*configurer.Stats, error) {
|
||||||
return nil, fmt.Errorf("not implemented")
|
return nil, fmt.Errorf("not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ type wgIfaceBase interface {
|
|||||||
UpdateAddr(newAddr string) error
|
UpdateAddr(newAddr string) error
|
||||||
GetProxy() wgproxy.Proxy
|
GetProxy() wgproxy.Proxy
|
||||||
UpdatePeer(peerKey string, allowedIps []netip.Prefix, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error
|
UpdatePeer(peerKey string, allowedIps []netip.Prefix, keepAlive time.Duration, endpoint *net.UDPAddr, preSharedKey *wgtypes.Key) error
|
||||||
|
RemoveEndpointAddress(key string) error
|
||||||
RemovePeer(peerKey string) error
|
RemovePeer(peerKey string) error
|
||||||
AddAllowedIP(peerKey string, allowedIP netip.Prefix) error
|
AddAllowedIP(peerKey string, allowedIP netip.Prefix) error
|
||||||
RemoveAllowedIP(peerKey string, allowedIP netip.Prefix) error
|
RemoveAllowedIP(peerKey string, allowedIP netip.Prefix) error
|
||||||
|
|||||||
@@ -171,9 +171,9 @@ func (conn *Conn) Open(engineCtx context.Context) error {
|
|||||||
|
|
||||||
conn.handshaker = NewHandshaker(conn.Log, conn.config, conn.signaler, conn.workerICE, conn.workerRelay)
|
conn.handshaker = NewHandshaker(conn.Log, conn.config, conn.signaler, conn.workerICE, conn.workerRelay)
|
||||||
|
|
||||||
conn.handshaker.AddOnNewOfferListener(conn.workerRelay.OnNewOffer)
|
conn.handshaker.AddRelayListener(conn.workerRelay.OnNewOffer)
|
||||||
if !isForceRelayed() {
|
if !isForceRelayed() {
|
||||||
conn.handshaker.AddOnNewOfferListener(conn.workerICE.OnNewOffer)
|
conn.handshaker.AddICEListener(conn.workerICE.OnNewOffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.guard = guard.NewGuard(conn.Log, conn.isConnectedOnAllWay, conn.config.Timeout, conn.srWatcher)
|
conn.guard = guard.NewGuard(conn.Log, conn.isConnectedOnAllWay, conn.config.Timeout, conn.srWatcher)
|
||||||
@@ -430,6 +430,9 @@ func (conn *Conn) onICEStateDisconnected() {
|
|||||||
} else {
|
} else {
|
||||||
conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String())
|
conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String())
|
||||||
conn.currentConnPriority = conntype.None
|
conn.currentConnPriority = conntype.None
|
||||||
|
if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil {
|
||||||
|
conn.Log.Errorf("failed to remove wg endpoint: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
changed := conn.statusICE.Get() != worker.StatusDisconnected
|
changed := conn.statusICE.Get() != worker.StatusDisconnected
|
||||||
@@ -523,6 +526,9 @@ func (conn *Conn) onRelayDisconnected() {
|
|||||||
if conn.currentConnPriority == conntype.Relay {
|
if conn.currentConnPriority == conntype.Relay {
|
||||||
conn.Log.Debugf("clean up WireGuard config")
|
conn.Log.Debugf("clean up WireGuard config")
|
||||||
conn.currentConnPriority = conntype.None
|
conn.currentConnPriority = conntype.None
|
||||||
|
if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil {
|
||||||
|
conn.Log.Errorf("failed to remove wg endpoint: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.wgProxyRelay != nil {
|
if conn.wgProxyRelay != nil {
|
||||||
|
|||||||
@@ -79,10 +79,10 @@ func TestConn_OnRemoteOffer(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewOffeChan := make(chan struct{})
|
onNewOfferChan := make(chan struct{})
|
||||||
|
|
||||||
conn.handshaker.AddOnNewOfferListener(func(remoteOfferAnswer *OfferAnswer) {
|
conn.handshaker.AddRelayListener(func(remoteOfferAnswer *OfferAnswer) {
|
||||||
onNewOffeChan <- struct{}{}
|
onNewOfferChan <- struct{}{}
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.OnRemoteOffer(OfferAnswer{
|
conn.OnRemoteOffer(OfferAnswer{
|
||||||
@@ -98,7 +98,7 @@ func TestConn_OnRemoteOffer(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-onNewOffeChan:
|
case <-onNewOfferChan:
|
||||||
// success
|
// success
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
t.Error("expected to receive a new offer notification, but timed out")
|
t.Error("expected to receive a new offer notification, but timed out")
|
||||||
@@ -118,10 +118,10 @@ func TestConn_OnRemoteAnswer(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewOffeChan := make(chan struct{})
|
onNewOfferChan := make(chan struct{})
|
||||||
|
|
||||||
conn.handshaker.AddOnNewOfferListener(func(remoteOfferAnswer *OfferAnswer) {
|
conn.handshaker.AddRelayListener(func(remoteOfferAnswer *OfferAnswer) {
|
||||||
onNewOffeChan <- struct{}{}
|
onNewOfferChan <- struct{}{}
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.OnRemoteAnswer(OfferAnswer{
|
conn.OnRemoteAnswer(OfferAnswer{
|
||||||
@@ -136,7 +136,7 @@ func TestConn_OnRemoteAnswer(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-onNewOffeChan:
|
case <-onNewOfferChan:
|
||||||
// success
|
// success
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
t.Error("expected to receive a new offer notification, but timed out")
|
t.Error("expected to receive a new offer notification, but timed out")
|
||||||
|
|||||||
20
client/internal/peer/guard/env.go
Normal file
20
client/internal/peer/guard/env.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package guard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
envICEMonitorPeriod = "NB_ICE_MONITOR_PERIOD"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetICEMonitorPeriod() time.Duration {
|
||||||
|
if envVal := os.Getenv(envICEMonitorPeriod); envVal != "" {
|
||||||
|
if seconds, err := strconv.Atoi(envVal); err == nil && seconds > 0 {
|
||||||
|
return time.Duration(seconds) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultCandidatesMonitorPeriod
|
||||||
|
}
|
||||||
@@ -16,8 +16,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
candidatesMonitorPeriod = 5 * time.Minute
|
defaultCandidatesMonitorPeriod = 5 * time.Minute
|
||||||
candidateGatheringTimeout = 5 * time.Second
|
candidateGatheringTimeout = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
type ICEMonitor struct {
|
type ICEMonitor struct {
|
||||||
@@ -25,16 +25,19 @@ type ICEMonitor struct {
|
|||||||
|
|
||||||
iFaceDiscover stdnet.ExternalIFaceDiscover
|
iFaceDiscover stdnet.ExternalIFaceDiscover
|
||||||
iceConfig icemaker.Config
|
iceConfig icemaker.Config
|
||||||
|
tickerPeriod time.Duration
|
||||||
|
|
||||||
currentCandidatesAddress []string
|
currentCandidatesAddress []string
|
||||||
candidatesMu sync.Mutex
|
candidatesMu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewICEMonitor(iFaceDiscover stdnet.ExternalIFaceDiscover, config icemaker.Config) *ICEMonitor {
|
func NewICEMonitor(iFaceDiscover stdnet.ExternalIFaceDiscover, config icemaker.Config, period time.Duration) *ICEMonitor {
|
||||||
|
log.Debugf("prepare ICE monitor with period: %s", period)
|
||||||
cm := &ICEMonitor{
|
cm := &ICEMonitor{
|
||||||
ReconnectCh: make(chan struct{}, 1),
|
ReconnectCh: make(chan struct{}, 1),
|
||||||
iFaceDiscover: iFaceDiscover,
|
iFaceDiscover: iFaceDiscover,
|
||||||
iceConfig: config,
|
iceConfig: config,
|
||||||
|
tickerPeriod: period,
|
||||||
}
|
}
|
||||||
return cm
|
return cm
|
||||||
}
|
}
|
||||||
@@ -46,7 +49,12 @@ func (cm *ICEMonitor) Start(ctx context.Context, onChanged func()) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(candidatesMonitorPeriod)
|
// Initial check to populate the candidates for later comparison
|
||||||
|
if _, err := cm.handleCandidateTick(ctx, ufrag, pwd); err != nil {
|
||||||
|
log.Warnf("Failed to check initial ICE candidates: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(cm.tickerPeriod)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func (w *SRWatcher) Start() {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
w.cancelIceMonitor = cancel
|
w.cancelIceMonitor = cancel
|
||||||
|
|
||||||
iceMonitor := NewICEMonitor(w.iFaceDiscover, w.iceConfig)
|
iceMonitor := NewICEMonitor(w.iFaceDiscover, w.iceConfig, GetICEMonitorPeriod())
|
||||||
go iceMonitor.Start(ctx, w.onICEChanged)
|
go iceMonitor.Start(ctx, w.onICEChanged)
|
||||||
w.signalClient.SetOnReconnectedListener(w.onReconnected)
|
w.signalClient.SetOnReconnectedListener(w.onReconnected)
|
||||||
w.relayManager.SetOnReconnectedListener(w.onReconnected)
|
w.relayManager.SetOnReconnectedListener(w.onReconnected)
|
||||||
|
|||||||
@@ -44,13 +44,19 @@ type OfferAnswer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Handshaker struct {
|
type Handshaker struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
log *log.Entry
|
log *log.Entry
|
||||||
config ConnConfig
|
config ConnConfig
|
||||||
signaler *Signaler
|
signaler *Signaler
|
||||||
ice *WorkerICE
|
ice *WorkerICE
|
||||||
relay *WorkerRelay
|
relay *WorkerRelay
|
||||||
onNewOfferListeners []*OfferListener
|
// relayListener is not blocking because the listener is using a goroutine to process the messages
|
||||||
|
// and it will only keep the latest message if multiple offers are received in a short time
|
||||||
|
// this is to avoid blocking the handshaker if the listener is doing some heavy processing
|
||||||
|
// and also to avoid processing old offers if multiple offers are received in a short time
|
||||||
|
// the listener will always process the latest offer
|
||||||
|
relayListener *AsyncOfferListener
|
||||||
|
iceListener func(remoteOfferAnswer *OfferAnswer)
|
||||||
|
|
||||||
// remoteOffersCh is a channel used to wait for remote credentials to proceed with the connection
|
// remoteOffersCh is a channel used to wait for remote credentials to proceed with the connection
|
||||||
remoteOffersCh chan OfferAnswer
|
remoteOffersCh chan OfferAnswer
|
||||||
@@ -70,28 +76,39 @@ func NewHandshaker(log *log.Entry, config ConnConfig, signaler *Signaler, ice *W
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handshaker) AddOnNewOfferListener(offer func(remoteOfferAnswer *OfferAnswer)) {
|
func (h *Handshaker) AddRelayListener(offer func(remoteOfferAnswer *OfferAnswer)) {
|
||||||
l := NewOfferListener(offer)
|
h.relayListener = NewAsyncOfferListener(offer)
|
||||||
h.onNewOfferListeners = append(h.onNewOfferListeners, l)
|
}
|
||||||
|
|
||||||
|
func (h *Handshaker) AddICEListener(offer func(remoteOfferAnswer *OfferAnswer)) {
|
||||||
|
h.iceListener = offer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handshaker) Listen(ctx context.Context) {
|
func (h *Handshaker) Listen(ctx context.Context) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case remoteOfferAnswer := <-h.remoteOffersCh:
|
case remoteOfferAnswer := <-h.remoteOffersCh:
|
||||||
// received confirmation from the remote peer -> ready to proceed
|
h.log.Infof("received offer, running version %s, remote WireGuard listen port %d, session id: %s", remoteOfferAnswer.Version, remoteOfferAnswer.WgListenPort, remoteOfferAnswer.SessionIDString())
|
||||||
|
if h.relayListener != nil {
|
||||||
|
h.relayListener.Notify(&remoteOfferAnswer)
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.iceListener != nil {
|
||||||
|
h.iceListener(&remoteOfferAnswer)
|
||||||
|
}
|
||||||
|
|
||||||
if err := h.sendAnswer(); err != nil {
|
if err := h.sendAnswer(); err != nil {
|
||||||
h.log.Errorf("failed to send remote offer confirmation: %s", err)
|
h.log.Errorf("failed to send remote offer confirmation: %s", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, listener := range h.onNewOfferListeners {
|
|
||||||
listener.Notify(&remoteOfferAnswer)
|
|
||||||
}
|
|
||||||
h.log.Infof("received offer, running version %s, remote WireGuard listen port %d, session id: %s", remoteOfferAnswer.Version, remoteOfferAnswer.WgListenPort, remoteOfferAnswer.SessionIDString())
|
|
||||||
case remoteOfferAnswer := <-h.remoteAnswerCh:
|
case remoteOfferAnswer := <-h.remoteAnswerCh:
|
||||||
h.log.Infof("received answer, running version %s, remote WireGuard listen port %d, session id: %s", remoteOfferAnswer.Version, remoteOfferAnswer.WgListenPort, remoteOfferAnswer.SessionIDString())
|
h.log.Infof("received answer, running version %s, remote WireGuard listen port %d, session id: %s", remoteOfferAnswer.Version, remoteOfferAnswer.WgListenPort, remoteOfferAnswer.SessionIDString())
|
||||||
for _, listener := range h.onNewOfferListeners {
|
if h.relayListener != nil {
|
||||||
listener.Notify(&remoteOfferAnswer)
|
h.relayListener.Notify(&remoteOfferAnswer)
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.iceListener != nil {
|
||||||
|
h.iceListener(&remoteOfferAnswer)
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
h.log.Infof("stop listening for remote offers and answers")
|
h.log.Infof("stop listening for remote offers and answers")
|
||||||
|
|||||||
@@ -13,20 +13,20 @@ func (oa *OfferAnswer) SessionIDString() string {
|
|||||||
return oa.SessionID.String()
|
return oa.SessionID.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
type OfferListener struct {
|
type AsyncOfferListener struct {
|
||||||
fn callbackFunc
|
fn callbackFunc
|
||||||
running bool
|
running bool
|
||||||
latest *OfferAnswer
|
latest *OfferAnswer
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOfferListener(fn callbackFunc) *OfferListener {
|
func NewAsyncOfferListener(fn callbackFunc) *AsyncOfferListener {
|
||||||
return &OfferListener{
|
return &AsyncOfferListener{
|
||||||
fn: fn,
|
fn: fn,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OfferListener) Notify(remoteOfferAnswer *OfferAnswer) {
|
func (o *AsyncOfferListener) Notify(remoteOfferAnswer *OfferAnswer) {
|
||||||
o.mu.Lock()
|
o.mu.Lock()
|
||||||
defer o.mu.Unlock()
|
defer o.mu.Unlock()
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func Test_newOfferListener(t *testing.T) {
|
|||||||
runChan <- struct{}{}
|
runChan <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
hl := NewOfferListener(longRunningFn)
|
hl := NewAsyncOfferListener(longRunningFn)
|
||||||
|
|
||||||
hl.Notify(dummyOfferAnswer)
|
hl.Notify(dummyOfferAnswer)
|
||||||
hl.Notify(dummyOfferAnswer)
|
hl.Notify(dummyOfferAnswer)
|
||||||
|
|||||||
@@ -18,4 +18,5 @@ type WGIface interface {
|
|||||||
GetStats() (map[string]configurer.WGStats, error)
|
GetStats() (map[string]configurer.WGStats, error)
|
||||||
GetProxy() wgproxy.Proxy
|
GetProxy() wgproxy.Proxy
|
||||||
Address() wgaddr.Address
|
Address() wgaddr.Address
|
||||||
|
RemoveEndpointAddress(key string) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,23 +92,16 @@ func NewWorkerICE(ctx context.Context, log *log.Entry, config ConnConfig, conn *
|
|||||||
func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
|
func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
|
||||||
w.log.Debugf("OnNewOffer for ICE, serial: %s", remoteOfferAnswer.SessionIDString())
|
w.log.Debugf("OnNewOffer for ICE, serial: %s", remoteOfferAnswer.SessionIDString())
|
||||||
w.muxAgent.Lock()
|
w.muxAgent.Lock()
|
||||||
|
defer w.muxAgent.Unlock()
|
||||||
|
|
||||||
if w.agentConnecting {
|
if w.agent != nil || w.agentConnecting {
|
||||||
w.log.Debugf("agent connection is in progress, skipping the offer")
|
|
||||||
w.muxAgent.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.agent != nil {
|
|
||||||
// backward compatibility with old clients that do not send session ID
|
// backward compatibility with old clients that do not send session ID
|
||||||
if remoteOfferAnswer.SessionID == nil {
|
if remoteOfferAnswer.SessionID == nil {
|
||||||
w.log.Debugf("agent already exists, skipping the offer")
|
w.log.Debugf("agent already exists, skipping the offer")
|
||||||
w.muxAgent.Unlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if w.remoteSessionID == *remoteOfferAnswer.SessionID {
|
if w.remoteSessionID == *remoteOfferAnswer.SessionID {
|
||||||
w.log.Debugf("agent already exists and session ID matches, skipping the offer: %s", remoteOfferAnswer.SessionIDString())
|
w.log.Debugf("agent already exists and session ID matches, skipping the offer: %s", remoteOfferAnswer.SessionIDString())
|
||||||
w.muxAgent.Unlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.log.Debugf("agent already exists, recreate the connection")
|
w.log.Debugf("agent already exists, recreate the connection")
|
||||||
@@ -116,6 +109,12 @@ func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
|
|||||||
if err := w.agent.Close(); err != nil {
|
if err := w.agent.Close(); err != nil {
|
||||||
w.log.Warnf("failed to close ICE agent: %s", err)
|
w.log.Warnf("failed to close ICE agent: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sessionID, err := NewICESessionID()
|
||||||
|
if err != nil {
|
||||||
|
w.log.Errorf("failed to create new session ID: %s", err)
|
||||||
|
}
|
||||||
|
w.sessionID = sessionID
|
||||||
w.agent = nil
|
w.agent = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,18 +125,23 @@ func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
|
|||||||
preferredCandidateTypes = icemaker.CandidateTypes()
|
preferredCandidateTypes = icemaker.CandidateTypes()
|
||||||
}
|
}
|
||||||
|
|
||||||
w.log.Debugf("recreate ICE agent")
|
if remoteOfferAnswer.SessionID != nil {
|
||||||
|
w.log.Debugf("recreate ICE agent: %s / %s", w.sessionID, *remoteOfferAnswer.SessionID)
|
||||||
|
}
|
||||||
dialerCtx, dialerCancel := context.WithCancel(w.ctx)
|
dialerCtx, dialerCancel := context.WithCancel(w.ctx)
|
||||||
agent, err := w.reCreateAgent(dialerCancel, preferredCandidateTypes)
|
agent, err := w.reCreateAgent(dialerCancel, preferredCandidateTypes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.log.Errorf("failed to recreate ICE Agent: %s", err)
|
w.log.Errorf("failed to recreate ICE Agent: %s", err)
|
||||||
w.muxAgent.Unlock()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.agent = agent
|
w.agent = agent
|
||||||
w.agentDialerCancel = dialerCancel
|
w.agentDialerCancel = dialerCancel
|
||||||
w.agentConnecting = true
|
w.agentConnecting = true
|
||||||
w.muxAgent.Unlock()
|
if remoteOfferAnswer.SessionID != nil {
|
||||||
|
w.remoteSessionID = *remoteOfferAnswer.SessionID
|
||||||
|
} else {
|
||||||
|
w.remoteSessionID = ""
|
||||||
|
}
|
||||||
|
|
||||||
go w.connect(dialerCtx, agent, remoteOfferAnswer)
|
go w.connect(dialerCtx, agent, remoteOfferAnswer)
|
||||||
}
|
}
|
||||||
@@ -293,9 +297,6 @@ func (w *WorkerICE) connect(ctx context.Context, agent *icemaker.ThreadSafeAgent
|
|||||||
w.muxAgent.Lock()
|
w.muxAgent.Lock()
|
||||||
w.agentConnecting = false
|
w.agentConnecting = false
|
||||||
w.lastSuccess = time.Now()
|
w.lastSuccess = time.Now()
|
||||||
if remoteOfferAnswer.SessionID != nil {
|
|
||||||
w.remoteSessionID = *remoteOfferAnswer.SessionID
|
|
||||||
}
|
|
||||||
w.muxAgent.Unlock()
|
w.muxAgent.Unlock()
|
||||||
|
|
||||||
// todo: the potential problem is a race between the onConnectionStateChange
|
// todo: the potential problem is a race between the onConnectionStateChange
|
||||||
@@ -309,16 +310,17 @@ func (w *WorkerICE) closeAgent(agent *icemaker.ThreadSafeAgent, cancel context.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.muxAgent.Lock()
|
w.muxAgent.Lock()
|
||||||
// todo review does it make sense to generate new session ID all the time when w.agent==agent
|
|
||||||
sessionID, err := NewICESessionID()
|
|
||||||
if err != nil {
|
|
||||||
w.log.Errorf("failed to create new session ID: %s", err)
|
|
||||||
}
|
|
||||||
w.sessionID = sessionID
|
|
||||||
|
|
||||||
if w.agent == agent {
|
if w.agent == agent {
|
||||||
|
// consider to remove from here and move to the OnNewOffer
|
||||||
|
sessionID, err := NewICESessionID()
|
||||||
|
if err != nil {
|
||||||
|
w.log.Errorf("failed to create new session ID: %s", err)
|
||||||
|
}
|
||||||
|
w.sessionID = sessionID
|
||||||
w.agent = nil
|
w.agent = nil
|
||||||
w.agentConnecting = false
|
w.agentConnecting = false
|
||||||
|
w.remoteSessionID = ""
|
||||||
}
|
}
|
||||||
w.muxAgent.Unlock()
|
w.muxAgent.Unlock()
|
||||||
}
|
}
|
||||||
@@ -395,11 +397,12 @@ func (w *WorkerICE) onConnectionStateChange(agent *icemaker.ThreadSafeAgent, dia
|
|||||||
// ice.ConnectionStateClosed happens when we recreate the agent. For the P2P to TURN switch important to
|
// ice.ConnectionStateClosed happens when we recreate the agent. For the P2P to TURN switch important to
|
||||||
// notify the conn.onICEStateDisconnected changes to update the current used priority
|
// notify the conn.onICEStateDisconnected changes to update the current used priority
|
||||||
|
|
||||||
|
w.closeAgent(agent, dialerCancel)
|
||||||
|
|
||||||
if w.lastKnownState == ice.ConnectionStateConnected {
|
if w.lastKnownState == ice.ConnectionStateConnected {
|
||||||
w.lastKnownState = ice.ConnectionStateDisconnected
|
w.lastKnownState = ice.ConnectionStateDisconnected
|
||||||
w.conn.onICEStateDisconnected()
|
w.conn.onICEStateDisconnected()
|
||||||
}
|
}
|
||||||
w.closeAgent(agent, dialerCancel)
|
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1354,7 +1354,13 @@ func (s *serviceClient) updateConfig() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// showLoginURL creates a borderless window styled like a pop-up in the top-right corner using s.wLoginURL.
|
// showLoginURL creates a borderless window styled like a pop-up in the top-right corner using s.wLoginURL.
|
||||||
func (s *serviceClient) showLoginURL() {
|
// It also starts a background goroutine that periodically checks if the client is already connected
|
||||||
|
// and closes the window if so. The goroutine can be cancelled by the returned CancelFunc, and it is
|
||||||
|
// also cancelled when the window is closed.
|
||||||
|
func (s *serviceClient) showLoginURL() context.CancelFunc {
|
||||||
|
|
||||||
|
// create a cancellable context for the background check goroutine
|
||||||
|
ctx, cancel := context.WithCancel(s.ctx)
|
||||||
|
|
||||||
resIcon := fyne.NewStaticResource("netbird.png", iconAbout)
|
resIcon := fyne.NewStaticResource("netbird.png", iconAbout)
|
||||||
|
|
||||||
@@ -1363,6 +1369,8 @@ func (s *serviceClient) showLoginURL() {
|
|||||||
s.wLoginURL.Resize(fyne.NewSize(400, 200))
|
s.wLoginURL.Resize(fyne.NewSize(400, 200))
|
||||||
s.wLoginURL.SetIcon(resIcon)
|
s.wLoginURL.SetIcon(resIcon)
|
||||||
}
|
}
|
||||||
|
// ensure goroutine is cancelled when the window is closed
|
||||||
|
s.wLoginURL.SetOnClosed(func() { cancel() })
|
||||||
// add a description label
|
// add a description label
|
||||||
label := widget.NewLabel("Your NetBird session has expired.\nPlease re-authenticate to continue using NetBird.")
|
label := widget.NewLabel("Your NetBird session has expired.\nPlease re-authenticate to continue using NetBird.")
|
||||||
|
|
||||||
@@ -1443,7 +1451,39 @@ func (s *serviceClient) showLoginURL() {
|
|||||||
)
|
)
|
||||||
s.wLoginURL.SetContent(container.NewCenter(content))
|
s.wLoginURL.SetContent(container.NewCenter(content))
|
||||||
|
|
||||||
|
// start a goroutine to check connection status and close the window if connected
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
conn, err := s.getSrvClient(failFastTimeout)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
status, err := conn.Status(s.ctx, &proto.StatusRequest{})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if status.Status == string(internal.StatusConnected) {
|
||||||
|
if s.wLoginURL != nil {
|
||||||
|
s.wLoginURL.Close()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
s.wLoginURL.Show()
|
s.wLoginURL.Show()
|
||||||
|
|
||||||
|
// return cancel func so callers can stop the background goroutine if desired
|
||||||
|
return cancel
|
||||||
}
|
}
|
||||||
|
|
||||||
func openURL(url string) error {
|
func openURL(url string) error {
|
||||||
|
|||||||
@@ -73,8 +73,8 @@ func (p *RDCleanPathProxy) validateCertificateWithJS(conn *proxyConnection, cert
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) getTLSConfigWithValidation(conn *proxyConnection) *tls.Config {
|
func (p *RDCleanPathProxy) getTLSConfigWithValidation(conn *proxyConnection, requiresCredSSP bool) *tls.Config {
|
||||||
return &tls.Config{
|
config := &tls.Config{
|
||||||
InsecureSkipVerify: true, // We'll validate manually after handshake
|
InsecureSkipVerify: true, // We'll validate manually after handshake
|
||||||
VerifyConnection: func(cs tls.ConnectionState) error {
|
VerifyConnection: func(cs tls.ConnectionState) error {
|
||||||
var certChain [][]byte
|
var certChain [][]byte
|
||||||
@@ -93,4 +93,15 @@ func (p *RDCleanPathProxy) getTLSConfigWithValidation(conn *proxyConnection) *tl
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CredSSP (NLA) requires TLS 1.2 - it's incompatible with TLS 1.3
|
||||||
|
if requiresCredSSP {
|
||||||
|
config.MinVersion = tls.VersionTLS12
|
||||||
|
config.MaxVersion = tls.VersionTLS12
|
||||||
|
} else {
|
||||||
|
config.MinVersion = tls.VersionTLS12
|
||||||
|
config.MaxVersion = tls.VersionTLS13
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/asn1"
|
"encoding/asn1"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall/js"
|
"syscall/js"
|
||||||
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -19,18 +21,34 @@ const (
|
|||||||
RDCleanPathVersion = 3390
|
RDCleanPathVersion = 3390
|
||||||
RDCleanPathProxyHost = "rdcleanpath.proxy.local"
|
RDCleanPathProxyHost = "rdcleanpath.proxy.local"
|
||||||
RDCleanPathProxyScheme = "ws"
|
RDCleanPathProxyScheme = "ws"
|
||||||
|
|
||||||
|
rdpDialTimeout = 15 * time.Second
|
||||||
|
|
||||||
|
GeneralErrorCode = 1
|
||||||
|
WSAETimedOut = 10060
|
||||||
|
WSAEConnRefused = 10061
|
||||||
|
WSAEConnAborted = 10053
|
||||||
|
WSAEConnReset = 10054
|
||||||
|
WSAEGenericError = 10050
|
||||||
)
|
)
|
||||||
|
|
||||||
type RDCleanPathPDU struct {
|
type RDCleanPathPDU struct {
|
||||||
Version int64 `asn1:"tag:0,explicit"`
|
Version int64 `asn1:"tag:0,explicit"`
|
||||||
Error []byte `asn1:"tag:1,explicit,optional"`
|
Error RDCleanPathErr `asn1:"tag:1,explicit,optional"`
|
||||||
Destination string `asn1:"utf8,tag:2,explicit,optional"`
|
Destination string `asn1:"utf8,tag:2,explicit,optional"`
|
||||||
ProxyAuth string `asn1:"utf8,tag:3,explicit,optional"`
|
ProxyAuth string `asn1:"utf8,tag:3,explicit,optional"`
|
||||||
ServerAuth string `asn1:"utf8,tag:4,explicit,optional"`
|
ServerAuth string `asn1:"utf8,tag:4,explicit,optional"`
|
||||||
PreconnectionBlob string `asn1:"utf8,tag:5,explicit,optional"`
|
PreconnectionBlob string `asn1:"utf8,tag:5,explicit,optional"`
|
||||||
X224ConnectionPDU []byte `asn1:"tag:6,explicit,optional"`
|
X224ConnectionPDU []byte `asn1:"tag:6,explicit,optional"`
|
||||||
ServerCertChain [][]byte `asn1:"tag:7,explicit,optional"`
|
ServerCertChain [][]byte `asn1:"tag:7,explicit,optional"`
|
||||||
ServerAddr string `asn1:"utf8,tag:9,explicit,optional"`
|
ServerAddr string `asn1:"utf8,tag:9,explicit,optional"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RDCleanPathErr struct {
|
||||||
|
ErrorCode int16 `asn1:"tag:0,explicit"`
|
||||||
|
HTTPStatusCode int16 `asn1:"tag:1,explicit,optional"`
|
||||||
|
WSALastError int16 `asn1:"tag:2,explicit,optional"`
|
||||||
|
TLSAlertCode int8 `asn1:"tag:3,explicit,optional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RDCleanPathProxy struct {
|
type RDCleanPathProxy struct {
|
||||||
@@ -210,9 +228,13 @@ func (p *RDCleanPathProxy) handleDirectRDP(conn *proxyConnection, firstPacket []
|
|||||||
destination := conn.destination
|
destination := conn.destination
|
||||||
log.Infof("Direct RDP mode: Connecting to %s via NetBird", destination)
|
log.Infof("Direct RDP mode: Connecting to %s via NetBird", destination)
|
||||||
|
|
||||||
rdpConn, err := p.nbClient.Dial(conn.ctx, "tcp", destination)
|
ctx, cancel := context.WithTimeout(conn.ctx, rdpDialTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
rdpConn, err := p.nbClient.Dial(ctx, "tcp", destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to connect to %s: %v", destination, err)
|
log.Errorf("Failed to connect to %s: %v", destination, err)
|
||||||
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.rdpConn = rdpConn
|
conn.rdpConn = rdpConn
|
||||||
@@ -220,6 +242,7 @@ func (p *RDCleanPathProxy) handleDirectRDP(conn *proxyConnection, firstPacket []
|
|||||||
_, err = rdpConn.Write(firstPacket)
|
_, err = rdpConn.Write(firstPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to write first packet: %v", err)
|
log.Errorf("Failed to write first packet: %v", err)
|
||||||
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,6 +250,7 @@ func (p *RDCleanPathProxy) handleDirectRDP(conn *proxyConnection, firstPacket []
|
|||||||
n, err := rdpConn.Read(response)
|
n, err := rdpConn.Read(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to read X.224 response: %v", err)
|
log.Errorf("Failed to read X.224 response: %v", err)
|
||||||
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,3 +293,52 @@ func (p *RDCleanPathProxy) sendToWebSocket(conn *proxyConnection, data []byte) {
|
|||||||
conn.wsHandlers.Call("send", uint8Array.Get("buffer"))
|
conn.wsHandlers.Call("send", uint8Array.Get("buffer"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *RDCleanPathProxy) sendRDCleanPathError(conn *proxyConnection, pdu RDCleanPathPDU) {
|
||||||
|
data, err := asn1.Marshal(pdu)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to marshal error PDU: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p.sendToWebSocket(conn, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorToWSACode(err error) int16 {
|
||||||
|
if err == nil {
|
||||||
|
return WSAEGenericError
|
||||||
|
}
|
||||||
|
var netErr *net.OpError
|
||||||
|
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||||
|
return WSAETimedOut
|
||||||
|
}
|
||||||
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
return WSAETimedOut
|
||||||
|
}
|
||||||
|
if errors.Is(err, context.Canceled) {
|
||||||
|
return WSAEConnAborted
|
||||||
|
}
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
return WSAEConnReset
|
||||||
|
}
|
||||||
|
return WSAEGenericError
|
||||||
|
}
|
||||||
|
|
||||||
|
func newWSAError(err error) RDCleanPathPDU {
|
||||||
|
return RDCleanPathPDU{
|
||||||
|
Version: RDCleanPathVersion,
|
||||||
|
Error: RDCleanPathErr{
|
||||||
|
ErrorCode: GeneralErrorCode,
|
||||||
|
WSALastError: errorToWSACode(err),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHTTPError(statusCode int16) RDCleanPathPDU {
|
||||||
|
return RDCleanPathPDU{
|
||||||
|
Version: RDCleanPathVersion,
|
||||||
|
Error: RDCleanPathErr{
|
||||||
|
ErrorCode: GeneralErrorCode,
|
||||||
|
HTTPStatusCode: statusCode,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
package rdp
|
package rdp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/asn1"
|
"encoding/asn1"
|
||||||
"io"
|
"io"
|
||||||
@@ -11,11 +12,17 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MS-RDPBCGR: confusingly named, actually means PROTOCOL_HYBRID (CredSSP)
|
||||||
|
protocolSSL = 0x00000001
|
||||||
|
protocolHybridEx = 0x00000008
|
||||||
|
)
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) processRDCleanPathPDU(conn *proxyConnection, pdu RDCleanPathPDU) {
|
func (p *RDCleanPathProxy) processRDCleanPathPDU(conn *proxyConnection, pdu RDCleanPathPDU) {
|
||||||
log.Infof("Processing RDCleanPath PDU: Version=%d, Destination=%s", pdu.Version, pdu.Destination)
|
log.Infof("Processing RDCleanPath PDU: Version=%d, Destination=%s", pdu.Version, pdu.Destination)
|
||||||
|
|
||||||
if pdu.Version != RDCleanPathVersion {
|
if pdu.Version != RDCleanPathVersion {
|
||||||
p.sendRDCleanPathError(conn, "Unsupported version")
|
p.sendRDCleanPathError(conn, newHTTPError(400))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,10 +31,13 @@ func (p *RDCleanPathProxy) processRDCleanPathPDU(conn *proxyConnection, pdu RDCl
|
|||||||
destination = pdu.Destination
|
destination = pdu.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
rdpConn, err := p.nbClient.Dial(conn.ctx, "tcp", destination)
|
ctx, cancel := context.WithTimeout(conn.ctx, rdpDialTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
rdpConn, err := p.nbClient.Dial(ctx, "tcp", destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to connect to %s: %v", destination, err)
|
log.Errorf("Failed to connect to %s: %v", destination, err)
|
||||||
p.sendRDCleanPathError(conn, "Connection failed")
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
p.cleanupConnection(conn)
|
p.cleanupConnection(conn)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -40,6 +50,34 @@ func (p *RDCleanPathProxy) processRDCleanPathPDU(conn *proxyConnection, pdu RDCl
|
|||||||
p.setupTLSConnection(conn, pdu)
|
p.setupTLSConnection(conn, pdu)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// detectCredSSPFromX224 checks if the X.224 response indicates NLA/CredSSP is required.
|
||||||
|
// Per MS-RDPBCGR spec: byte 11 = TYPE_RDP_NEG_RSP (0x02), bytes 15-18 = selectedProtocol flags.
|
||||||
|
// Returns (requiresTLS12, selectedProtocol, detectionSuccessful).
|
||||||
|
func (p *RDCleanPathProxy) detectCredSSPFromX224(x224Response []byte) (bool, uint32, bool) {
|
||||||
|
const minResponseLength = 19
|
||||||
|
|
||||||
|
if len(x224Response) < minResponseLength {
|
||||||
|
return false, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Per X.224 specification:
|
||||||
|
// x224Response[0] == 0x03: Length of X.224 header (3 bytes)
|
||||||
|
// x224Response[5] == 0xD0: X.224 Data TPDU code
|
||||||
|
if x224Response[0] != 0x03 || x224Response[5] != 0xD0 {
|
||||||
|
return false, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if x224Response[11] == 0x02 {
|
||||||
|
flags := uint32(x224Response[15]) | uint32(x224Response[16])<<8 |
|
||||||
|
uint32(x224Response[17])<<16 | uint32(x224Response[18])<<24
|
||||||
|
|
||||||
|
hasNLA := (flags & (protocolSSL | protocolHybridEx)) != 0
|
||||||
|
return hasNLA, flags, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) setupTLSConnection(conn *proxyConnection, pdu RDCleanPathPDU) {
|
func (p *RDCleanPathProxy) setupTLSConnection(conn *proxyConnection, pdu RDCleanPathPDU) {
|
||||||
var x224Response []byte
|
var x224Response []byte
|
||||||
if len(pdu.X224ConnectionPDU) > 0 {
|
if len(pdu.X224ConnectionPDU) > 0 {
|
||||||
@@ -47,7 +85,7 @@ func (p *RDCleanPathProxy) setupTLSConnection(conn *proxyConnection, pdu RDClean
|
|||||||
_, err := conn.rdpConn.Write(pdu.X224ConnectionPDU)
|
_, err := conn.rdpConn.Write(pdu.X224ConnectionPDU)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to write X.224 PDU: %v", err)
|
log.Errorf("Failed to write X.224 PDU: %v", err)
|
||||||
p.sendRDCleanPathError(conn, "Failed to forward X.224")
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,21 +93,32 @@ func (p *RDCleanPathProxy) setupTLSConnection(conn *proxyConnection, pdu RDClean
|
|||||||
n, err := conn.rdpConn.Read(response)
|
n, err := conn.rdpConn.Read(response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to read X.224 response: %v", err)
|
log.Errorf("Failed to read X.224 response: %v", err)
|
||||||
p.sendRDCleanPathError(conn, "Failed to read X.224 response")
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
x224Response = response[:n]
|
x224Response = response[:n]
|
||||||
log.Debugf("Received X.224 Connection Confirm (%d bytes)", n)
|
log.Debugf("Received X.224 Connection Confirm (%d bytes)", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := p.getTLSConfigWithValidation(conn)
|
requiresCredSSP, selectedProtocol, detected := p.detectCredSSPFromX224(x224Response)
|
||||||
|
if detected {
|
||||||
|
if requiresCredSSP {
|
||||||
|
log.Warnf("Detected NLA/CredSSP (selectedProtocol: 0x%08X), forcing TLS 1.2 for compatibility", selectedProtocol)
|
||||||
|
} else {
|
||||||
|
log.Warnf("No NLA/CredSSP detected (selectedProtocol: 0x%08X), allowing up to TLS 1.3", selectedProtocol)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Warnf("Could not detect RDP security protocol, allowing up to TLS 1.3")
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig := p.getTLSConfigWithValidation(conn, requiresCredSSP)
|
||||||
|
|
||||||
tlsConn := tls.Client(conn.rdpConn, tlsConfig)
|
tlsConn := tls.Client(conn.rdpConn, tlsConfig)
|
||||||
conn.tlsConn = tlsConn
|
conn.tlsConn = tlsConn
|
||||||
|
|
||||||
if err := tlsConn.Handshake(); err != nil {
|
if err := tlsConn.Handshake(); err != nil {
|
||||||
log.Errorf("TLS handshake failed: %v", err)
|
log.Errorf("TLS handshake failed: %v", err)
|
||||||
p.sendRDCleanPathError(conn, "TLS handshake failed")
|
p.sendRDCleanPathError(conn, newWSAError(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,47 +155,6 @@ func (p *RDCleanPathProxy) setupTLSConnection(conn *proxyConnection, pdu RDClean
|
|||||||
p.cleanupConnection(conn)
|
p.cleanupConnection(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) setupPlainConnection(conn *proxyConnection, pdu RDCleanPathPDU) {
|
|
||||||
if len(pdu.X224ConnectionPDU) > 0 {
|
|
||||||
log.Debugf("Forwarding X.224 Connection Request (%d bytes)", len(pdu.X224ConnectionPDU))
|
|
||||||
_, err := conn.rdpConn.Write(pdu.X224ConnectionPDU)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to write X.224 PDU: %v", err)
|
|
||||||
p.sendRDCleanPathError(conn, "Failed to forward X.224")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response := make([]byte, 1024)
|
|
||||||
n, err := conn.rdpConn.Read(response)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to read X.224 response: %v", err)
|
|
||||||
p.sendRDCleanPathError(conn, "Failed to read X.224 response")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
responsePDU := RDCleanPathPDU{
|
|
||||||
Version: RDCleanPathVersion,
|
|
||||||
X224ConnectionPDU: response[:n],
|
|
||||||
ServerAddr: conn.destination,
|
|
||||||
}
|
|
||||||
|
|
||||||
p.sendRDCleanPathPDU(conn, responsePDU)
|
|
||||||
} else {
|
|
||||||
responsePDU := RDCleanPathPDU{
|
|
||||||
Version: RDCleanPathVersion,
|
|
||||||
ServerAddr: conn.destination,
|
|
||||||
}
|
|
||||||
p.sendRDCleanPathPDU(conn, responsePDU)
|
|
||||||
}
|
|
||||||
|
|
||||||
go p.forwardConnToWS(conn, conn.rdpConn, "TCP")
|
|
||||||
go p.forwardWSToConn(conn, conn.rdpConn, "TCP")
|
|
||||||
|
|
||||||
<-conn.ctx.Done()
|
|
||||||
log.Debug("TCP connection context done, cleaning up")
|
|
||||||
p.cleanupConnection(conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) sendRDCleanPathPDU(conn *proxyConnection, pdu RDCleanPathPDU) {
|
func (p *RDCleanPathProxy) sendRDCleanPathPDU(conn *proxyConnection, pdu RDCleanPathPDU) {
|
||||||
data, err := asn1.Marshal(pdu)
|
data, err := asn1.Marshal(pdu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -158,21 +166,6 @@ func (p *RDCleanPathProxy) sendRDCleanPathPDU(conn *proxyConnection, pdu RDClean
|
|||||||
p.sendToWebSocket(conn, data)
|
p.sendToWebSocket(conn, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) sendRDCleanPathError(conn *proxyConnection, errorMsg string) {
|
|
||||||
pdu := RDCleanPathPDU{
|
|
||||||
Version: RDCleanPathVersion,
|
|
||||||
Error: []byte(errorMsg),
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := asn1.Marshal(pdu)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to marshal error PDU: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p.sendToWebSocket(conn, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *RDCleanPathProxy) readWebSocketMessage(conn *proxyConnection) ([]byte, error) {
|
func (p *RDCleanPathProxy) readWebSocketMessage(conn *proxyConnection) ([]byte, error) {
|
||||||
msgChan := make(chan []byte)
|
msgChan := make(chan []byte)
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -62,7 +62,7 @@ require (
|
|||||||
github.com/miekg/dns v1.1.59
|
github.com/miekg/dns v1.1.59
|
||||||
github.com/mitchellh/hashstructure/v2 v2.0.2
|
github.com/mitchellh/hashstructure/v2 v2.0.2
|
||||||
github.com/nadoo/ipset v0.5.0
|
github.com/nadoo/ipset v0.5.0
|
||||||
github.com/netbirdio/management-integrations/integrations v0.0.0-20250906095204-f87a07690ba0
|
github.com/netbirdio/management-integrations/integrations v0.0.0-20251010134843-7af36217ac1f
|
||||||
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250805121659-6b4ac470ca45
|
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250805121659-6b4ac470ca45
|
||||||
github.com/okta/okta-sdk-golang/v2 v2.18.0
|
github.com/okta/okta-sdk-golang/v2 v2.18.0
|
||||||
github.com/oschwald/maxminddb-golang v1.12.0
|
github.com/oschwald/maxminddb-golang v1.12.0
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -503,8 +503,8 @@ github.com/netbirdio/go-netroute v0.0.0-20240611143515-f59b0e1d3944 h1:TDtJKmM6S
|
|||||||
github.com/netbirdio/go-netroute v0.0.0-20240611143515-f59b0e1d3944/go.mod h1:sHA6TRxjQ6RLbnI+3R4DZo2Eseg/iKiPRfNmcuNySVQ=
|
github.com/netbirdio/go-netroute v0.0.0-20240611143515-f59b0e1d3944/go.mod h1:sHA6TRxjQ6RLbnI+3R4DZo2Eseg/iKiPRfNmcuNySVQ=
|
||||||
github.com/netbirdio/ice/v4 v4.0.0-20250908184934-6202be846b51 h1:Ov4qdafATOgGMB1wbSuh+0aAHcwz9hdvB6VZjh1mVMI=
|
github.com/netbirdio/ice/v4 v4.0.0-20250908184934-6202be846b51 h1:Ov4qdafATOgGMB1wbSuh+0aAHcwz9hdvB6VZjh1mVMI=
|
||||||
github.com/netbirdio/ice/v4 v4.0.0-20250908184934-6202be846b51/go.mod h1:ZSIbPdBn5hePO8CpF1PekH2SfpTxg1PDhEwtbqZS7R8=
|
github.com/netbirdio/ice/v4 v4.0.0-20250908184934-6202be846b51/go.mod h1:ZSIbPdBn5hePO8CpF1PekH2SfpTxg1PDhEwtbqZS7R8=
|
||||||
github.com/netbirdio/management-integrations/integrations v0.0.0-20250906095204-f87a07690ba0 h1:9BUqQHPVOGr0edk8EifUBUfTr2Ob0ypAPxtasUApBxQ=
|
github.com/netbirdio/management-integrations/integrations v0.0.0-20251010134843-7af36217ac1f h1:XIpRDlpPz3zFUkpwaqDRHjwpQRsf2ZKHggoex1MTafs=
|
||||||
github.com/netbirdio/management-integrations/integrations v0.0.0-20250906095204-f87a07690ba0/go.mod h1:v0nUbbHbuQnqR7yKIYnKzsLBCswLtp2JctmKYmGgVhc=
|
github.com/netbirdio/management-integrations/integrations v0.0.0-20251010134843-7af36217ac1f/go.mod h1:v0nUbbHbuQnqR7yKIYnKzsLBCswLtp2JctmKYmGgVhc=
|
||||||
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502 h1:3tHlFmhTdX9axERMVN63dqyFqnvuD+EMJHzM7mNGON8=
|
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502 h1:3tHlFmhTdX9axERMVN63dqyFqnvuD+EMJHzM7mNGON8=
|
||||||
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
|
github.com/netbirdio/service v0.0.0-20240911161631-f62744f42502/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
|
||||||
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250805121659-6b4ac470ca45 h1:ujgviVYmx243Ksy7NdSwrdGPSRNE3pb8kEDSpH0QuAQ=
|
github.com/netbirdio/signal-dispatcher/dispatcher v0.0.0-20250805121659-6b4ac470ca45 h1:ujgviVYmx243Ksy7NdSwrdGPSRNE3pb8kEDSpH0QuAQ=
|
||||||
|
|||||||
@@ -47,8 +47,9 @@ services:
|
|||||||
- traefik.enable=true
|
- traefik.enable=true
|
||||||
- traefik.http.routers.netbird-wsproxy-signal.rule=Host(`$NETBIRD_DOMAIN`) && PathPrefix(`/ws-proxy/signal`)
|
- traefik.http.routers.netbird-wsproxy-signal.rule=Host(`$NETBIRD_DOMAIN`) && PathPrefix(`/ws-proxy/signal`)
|
||||||
- traefik.http.routers.netbird-wsproxy-signal.service=netbird-wsproxy-signal
|
- traefik.http.routers.netbird-wsproxy-signal.service=netbird-wsproxy-signal
|
||||||
- traefik.http.services.netbird-wsproxy-signal.loadbalancer.server.port=10000
|
- traefik.http.services.netbird-wsproxy-signal.loadbalancer.server.port=80
|
||||||
- traefik.http.routers.netbird-signal.rule=Host(`$NETBIRD_DOMAIN`) && PathPrefix(`/signalexchange.SignalExchange/`)
|
- traefik.http.routers.netbird-signal.rule=Host(`$NETBIRD_DOMAIN`) && PathPrefix(`/signalexchange.SignalExchange/`)
|
||||||
|
- traefik.http.routers.netbird-signal.service=netbird-signal
|
||||||
- traefik.http.services.netbird-signal.loadbalancer.server.port=10000
|
- traefik.http.services.netbird-signal.loadbalancer.server.port=10000
|
||||||
- traefik.http.services.netbird-signal.loadbalancer.server.scheme=h2c
|
- traefik.http.services.netbird-signal.loadbalancer.server.scheme=h2c
|
||||||
|
|
||||||
|
|||||||
@@ -621,7 +621,7 @@ renderCaddyfile() {
|
|||||||
# relay
|
# relay
|
||||||
reverse_proxy /relay* relay:80
|
reverse_proxy /relay* relay:80
|
||||||
# Signal
|
# Signal
|
||||||
reverse_proxy /ws-proxy/signal* signal:10000
|
reverse_proxy /ws-proxy/signal* signal:80
|
||||||
reverse_proxy /signalexchange.SignalExchange/* h2c://signal:10000
|
reverse_proxy /signalexchange.SignalExchange/* h2c://signal:10000
|
||||||
# Management
|
# Management
|
||||||
reverse_proxy /api/* management:80
|
reverse_proxy /api/* management:80
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -252,7 +251,7 @@ func updateMgmtConfig(ctx context.Context, path string, config *nbconfig.Config)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *BaseServer) handlerFunc(gRPCHandler *grpc.Server, httpHandler http.Handler, meter metric.Meter) http.Handler {
|
func (s *BaseServer) handlerFunc(gRPCHandler *grpc.Server, httpHandler http.Handler, meter metric.Meter) http.Handler {
|
||||||
wsProxy := wsproxyserver.New(netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), ManagementLegacyPort), wsproxyserver.WithOTelMeter(meter))
|
wsProxy := wsproxyserver.New(gRPCHandler, wsproxyserver.WithOTelMeter(meter))
|
||||||
|
|
||||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ func (a MockIntegratedValidator) GetValidatedPeers(_ context.Context, accountID
|
|||||||
return validatedPeers, nil
|
return validatedPeers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (MockIntegratedValidator) PreparePeer(_ context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings) *nbpeer.Peer {
|
func (MockIntegratedValidator) PreparePeer(_ context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings, temporary bool) *nbpeer.Peer {
|
||||||
return peer
|
return peer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ package integrated_validator
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/shared/management/proto"
|
|
||||||
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
"github.com/netbirdio/netbird/management/server/types"
|
||||||
|
"github.com/netbirdio/netbird/shared/management/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IntegratedValidator interface exists to avoid the circle dependencies
|
// IntegratedValidator interface exists to avoid the circle dependencies
|
||||||
type IntegratedValidator interface {
|
type IntegratedValidator interface {
|
||||||
ValidateExtraSettings(ctx context.Context, newExtraSettings *types.ExtraSettings, oldExtraSettings *types.ExtraSettings, peers map[string]*nbpeer.Peer, userID string, accountID string) error
|
ValidateExtraSettings(ctx context.Context, newExtraSettings *types.ExtraSettings, oldExtraSettings *types.ExtraSettings, peers map[string]*nbpeer.Peer, userID string, accountID string) error
|
||||||
ValidatePeer(ctx context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *types.ExtraSettings) (*nbpeer.Peer, bool, error)
|
ValidatePeer(ctx context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *types.ExtraSettings) (*nbpeer.Peer, bool, error)
|
||||||
PreparePeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings) *nbpeer.Peer
|
PreparePeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings, temporary bool) *nbpeer.Peer
|
||||||
IsNotValidPeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings) (bool, bool, error)
|
IsNotValidPeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *types.ExtraSettings) (bool, bool, error)
|
||||||
GetValidatedPeers(ctx context.Context, accountID string, groups []*types.Group, peers []*nbpeer.Peer, extraSettings *types.ExtraSettings) (map[string]struct{}, error)
|
GetValidatedPeers(ctx context.Context, accountID string, groups []*types.Group, peers []*nbpeer.Peer, extraSettings *types.ExtraSettings) (map[string]struct{}, error)
|
||||||
PeerDeleted(ctx context.Context, accountID, peerID string, extraSettings *types.ExtraSettings) error
|
PeerDeleted(ctx context.Context, accountID, peerID string, extraSettings *types.ExtraSettings) error
|
||||||
|
|||||||
@@ -350,7 +350,6 @@ func (am *DefaultAccountManager) DeletePeer(ctx context.Context, accountID, peer
|
|||||||
}
|
}
|
||||||
|
|
||||||
var peer *nbpeer.Peer
|
var peer *nbpeer.Peer
|
||||||
var updateAccountPeers bool
|
|
||||||
var eventsToStore []func()
|
var eventsToStore []func()
|
||||||
|
|
||||||
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction store.Store) error {
|
||||||
@@ -363,11 +362,6 @@ func (am *DefaultAccountManager) DeletePeer(ctx context.Context, accountID, peer
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAccountPeers, err = isPeerInActiveGroup(ctx, transaction, accountID, peerID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
eventsToStore, err = deletePeers(ctx, am, transaction, accountID, userID, []*nbpeer.Peer{peer})
|
eventsToStore, err = deletePeers(ctx, am, transaction, accountID, userID, []*nbpeer.Peer{peer})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to delete peer: %w", err)
|
return fmt.Errorf("failed to delete peer: %w", err)
|
||||||
@@ -387,7 +381,7 @@ func (am *DefaultAccountManager) DeletePeer(ctx context.Context, accountID, peer
|
|||||||
storeEvent()
|
storeEvent()
|
||||||
}
|
}
|
||||||
|
|
||||||
if updateAccountPeers && userID != activity.SystemInitiator {
|
if userID != activity.SystemInitiator {
|
||||||
am.BufferUpdateAccountPeers(ctx, accountID)
|
am.BufferUpdateAccountPeers(ctx, accountID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -584,7 +578,7 @@ func (am *DefaultAccountManager) AddPeer(ctx context.Context, accountID, setupKe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newPeer = am.integratedPeerValidator.PreparePeer(ctx, accountID, newPeer, groupsToAdd, settings.Extra)
|
newPeer = am.integratedPeerValidator.PreparePeer(ctx, accountID, newPeer, groupsToAdd, settings.Extra, temporary)
|
||||||
|
|
||||||
network, err := am.Store.GetAccountNetwork(ctx, store.LockingStrengthNone, accountID)
|
network, err := am.Store.GetAccountNetwork(ctx, store.LockingStrengthNone, accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -684,11 +678,6 @@ func (am *DefaultAccountManager) AddPeer(ctx context.Context, accountID, setupKe
|
|||||||
return nil, nil, nil, fmt.Errorf("failed to add peer to database after %d attempts: %w", maxAttempts, err)
|
return nil, nil, nil, fmt.Errorf("failed to add peer to database after %d attempts: %w", maxAttempts, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAccountPeers, err := isPeerInActiveGroup(ctx, am.Store, accountID, newPeer.ID)
|
|
||||||
if err != nil {
|
|
||||||
updateAccountPeers = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if newPeer == nil {
|
if newPeer == nil {
|
||||||
return nil, nil, nil, fmt.Errorf("new peer is nil")
|
return nil, nil, nil, fmt.Errorf("new peer is nil")
|
||||||
}
|
}
|
||||||
@@ -701,9 +690,7 @@ func (am *DefaultAccountManager) AddPeer(ctx context.Context, accountID, setupKe
|
|||||||
|
|
||||||
am.StoreEvent(ctx, opEvent.InitiatorID, opEvent.TargetID, opEvent.AccountID, opEvent.Activity, opEvent.Meta)
|
am.StoreEvent(ctx, opEvent.InitiatorID, opEvent.TargetID, opEvent.AccountID, opEvent.Activity, opEvent.Meta)
|
||||||
|
|
||||||
if updateAccountPeers {
|
am.BufferUpdateAccountPeers(ctx, accountID)
|
||||||
am.BufferUpdateAccountPeers(ctx, accountID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return am.getValidatedPeerWithMap(ctx, false, accountID, newPeer)
|
return am.getValidatedPeerWithMap(ctx, false, accountID, newPeer)
|
||||||
}
|
}
|
||||||
@@ -1527,16 +1514,6 @@ func getPeerGroupIDs(ctx context.Context, transaction store.Store, accountID str
|
|||||||
return transaction.GetPeerGroupIDs(ctx, store.LockingStrengthNone, accountID, peerID)
|
return transaction.GetPeerGroupIDs(ctx, store.LockingStrengthNone, accountID, peerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPeerInActiveGroup checks if the given peer is part of a group that is used
|
|
||||||
// in an active DNS, route, or ACL configuration.
|
|
||||||
func isPeerInActiveGroup(ctx context.Context, transaction store.Store, accountID, peerID string) (bool, error) {
|
|
||||||
peerGroupIDs, err := getPeerGroupIDs(ctx, transaction, accountID, peerID)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return areGroupChangesAffectPeers(ctx, transaction, accountID, peerGroupIDs) // TODO: use transaction
|
|
||||||
}
|
|
||||||
|
|
||||||
// deletePeers deletes all specified peers and sends updates to the remote peers.
|
// deletePeers deletes all specified peers and sends updates to the remote peers.
|
||||||
// Returns a slice of functions to save events after successful peer deletion.
|
// Returns a slice of functions to save events after successful peer deletion.
|
||||||
func deletePeers(ctx context.Context, am *DefaultAccountManager, transaction store.Store, accountID, userID string, peers []*nbpeer.Peer) ([]func(), error) {
|
func deletePeers(ctx context.Context, am *DefaultAccountManager, transaction store.Store, accountID, userID string, peers []*nbpeer.Peer) ([]func(), error) {
|
||||||
|
|||||||
@@ -1790,7 +1790,7 @@ func TestPeerAccountPeersUpdate(t *testing.T) {
|
|||||||
t.Run("adding peer to unlinked group", func(t *testing.T) {
|
t.Run("adding peer to unlinked group", func(t *testing.T) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
peerShouldNotReceiveUpdate(t, updMsg) //
|
peerShouldReceiveUpdate(t, updMsg) //
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -1815,7 +1815,7 @@ func TestPeerAccountPeersUpdate(t *testing.T) {
|
|||||||
t.Run("deleting peer with unlinked group", func(t *testing.T) {
|
t.Run("deleting peer with unlinked group", func(t *testing.T) {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
peerShouldNotReceiveUpdate(t, updMsg)
|
peerShouldReceiveUpdate(t, updMsg)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
// nolint:gosec
|
// nolint:gosec
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"net/netip"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||||
@@ -63,10 +62,10 @@ var (
|
|||||||
Use: "run",
|
Use: "run",
|
||||||
Short: "start NetBird Signal Server daemon",
|
Short: "start NetBird Signal Server daemon",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
err := util.InitLog(logLevel, logFile)
|
err := util.InitLog(logLevel, logFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed initializing log %v", err)
|
return fmt.Errorf("failed initializing log: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -87,6 +86,8 @@ var (
|
|||||||
signalPort = 80
|
signalPort = 80
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -254,7 +255,7 @@ func startServerWithCertManager(certManager *autocert.Manager, grpcRootHandler h
|
|||||||
}
|
}
|
||||||
|
|
||||||
func grpcHandlerFunc(grpcServer *grpc.Server, meter metric.Meter) http.Handler {
|
func grpcHandlerFunc(grpcServer *grpc.Server, meter metric.Meter) http.Handler {
|
||||||
wsProxy := wsproxyserver.New(netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), legacyGRPCPort), wsproxyserver.WithOTelMeter(meter))
|
wsProxy := wsproxyserver.New(grpcServer, wsproxyserver.WithOTelMeter(meter))
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
@@ -2,42 +2,41 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/util/wsproxy"
|
"github.com/netbirdio/netbird/util/wsproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
dialTimeout = 10 * time.Second
|
bufferSize = 32 * 1024
|
||||||
bufferSize = 32 * 1024
|
ioTimeout = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config contains the configuration for the WebSocket proxy.
|
// Config contains the configuration for the WebSocket proxy.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
LocalGRPCAddr netip.AddrPort
|
Handler http.Handler
|
||||||
Path string
|
Path string
|
||||||
MetricsRecorder MetricsRecorder
|
MetricsRecorder MetricsRecorder
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxy handles WebSocket to TCP proxying for gRPC connections.
|
// Proxy handles WebSocket to gRPC handler proxying.
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
config Config
|
config Config
|
||||||
metrics MetricsRecorder
|
metrics MetricsRecorder
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new WebSocket proxy instance with optional configuration
|
// New creates a new WebSocket proxy instance with optional configuration
|
||||||
func New(localGRPCAddr netip.AddrPort, opts ...Option) *Proxy {
|
func New(handler http.Handler, opts ...Option) *Proxy {
|
||||||
config := Config{
|
config := Config{
|
||||||
LocalGRPCAddr: localGRPCAddr,
|
Handler: handler,
|
||||||
Path: wsproxy.ProxyPath,
|
Path: wsproxy.ProxyPath,
|
||||||
MetricsRecorder: NoOpMetricsRecorder{}, // Default to no-op
|
MetricsRecorder: NoOpMetricsRecorder{}, // Default to no-op
|
||||||
}
|
}
|
||||||
@@ -63,7 +62,7 @@ func (p *Proxy) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
p.metrics.RecordConnection(ctx)
|
p.metrics.RecordConnection(ctx)
|
||||||
defer p.metrics.RecordDisconnection(ctx)
|
defer p.metrics.RecordDisconnection(ctx)
|
||||||
|
|
||||||
log.Debugf("WebSocket proxy handling connection from %s, forwarding to %s", r.RemoteAddr, p.config.LocalGRPCAddr)
|
log.Debugf("WebSocket proxy handling connection from %s, forwarding to internal gRPC handler", r.RemoteAddr)
|
||||||
acceptOptions := &websocket.AcceptOptions{
|
acceptOptions := &websocket.AcceptOptions{
|
||||||
OriginPatterns: []string{"*"},
|
OriginPatterns: []string{"*"},
|
||||||
}
|
}
|
||||||
@@ -75,71 +74,41 @@ func (p *Proxy) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := wsConn.Close(websocket.StatusNormalClosure, ""); err != nil {
|
_ = wsConn.Close(websocket.StatusNormalClosure, "")
|
||||||
log.Debugf("Failed to close WebSocket: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Debugf("WebSocket proxy attempting to connect to local gRPC at %s", p.config.LocalGRPCAddr)
|
clientConn, serverConn := net.Pipe()
|
||||||
tcpConn, err := net.DialTimeout("tcp", p.config.LocalGRPCAddr.String(), dialTimeout)
|
|
||||||
if err != nil {
|
|
||||||
p.metrics.RecordError(ctx, "tcp_dial_failed")
|
|
||||||
log.Warnf("Failed to connect to local gRPC server at %s: %v", p.config.LocalGRPCAddr, err)
|
|
||||||
if err := wsConn.Close(websocket.StatusInternalError, "Backend unavailable"); err != nil {
|
|
||||||
log.Debugf("Failed to close WebSocket after connection failure: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := tcpConn.Close(); err != nil {
|
_ = clientConn.Close()
|
||||||
log.Debugf("Failed to close TCP connection: %v", err)
|
_ = serverConn.Close()
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Debugf("WebSocket proxy established: client %s -> local gRPC %s", r.RemoteAddr, p.config.LocalGRPCAddr)
|
log.Debugf("WebSocket proxy established: %s -> gRPC handler", r.RemoteAddr)
|
||||||
|
|
||||||
p.proxyData(ctx, wsConn, tcpConn)
|
go func() {
|
||||||
|
(&http2.Server{}).ServeConn(serverConn, &http2.ServeConnOpts{
|
||||||
|
Context: ctx,
|
||||||
|
Handler: p.config.Handler,
|
||||||
|
})
|
||||||
|
}()
|
||||||
|
|
||||||
|
p.proxyData(ctx, wsConn, clientConn, r.RemoteAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) proxyData(ctx context.Context, wsConn *websocket.Conn, tcpConn net.Conn) {
|
func (p *Proxy) proxyData(ctx context.Context, wsConn *websocket.Conn, pipeConn net.Conn, clientAddr string) {
|
||||||
proxyCtx, cancel := context.WithCancel(ctx)
|
proxyCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
|
|
||||||
go p.wsToTCP(proxyCtx, cancel, &wg, wsConn, tcpConn)
|
go p.wsToPipe(proxyCtx, cancel, &wg, wsConn, pipeConn, clientAddr)
|
||||||
go p.tcpToWS(proxyCtx, cancel, &wg, wsConn, tcpConn)
|
go p.pipeToWS(proxyCtx, cancel, &wg, wsConn, pipeConn, clientAddr)
|
||||||
|
|
||||||
done := make(chan struct{})
|
wg.Wait()
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
close(done)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
log.Tracef("Proxy data transfer completed, both goroutines terminated")
|
|
||||||
case <-proxyCtx.Done():
|
|
||||||
log.Tracef("Proxy data transfer cancelled, forcing connection closure")
|
|
||||||
|
|
||||||
if err := wsConn.Close(websocket.StatusGoingAway, "proxy cancelled"); err != nil {
|
|
||||||
log.Tracef("Error closing WebSocket during cancellation: %v", err)
|
|
||||||
}
|
|
||||||
if err := tcpConn.Close(); err != nil {
|
|
||||||
log.Tracef("Error closing TCP connection during cancellation: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
log.Tracef("Goroutines terminated after forced connection closure")
|
|
||||||
case <-time.After(2 * time.Second):
|
|
||||||
log.Tracef("Goroutines did not terminate within timeout after connection closure")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) wsToTCP(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, wsConn *websocket.Conn, tcpConn net.Conn) {
|
func (p *Proxy) wsToPipe(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, wsConn *websocket.Conn, pipeConn net.Conn, clientAddr string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -148,80 +117,73 @@ func (p *Proxy) wsToTCP(ctx context.Context, cancel context.CancelFunc, wg *sync
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case ctx.Err() != nil:
|
case ctx.Err() != nil:
|
||||||
log.Debugf("wsToTCP goroutine terminating due to context cancellation")
|
log.Debugf("WebSocket from %s terminating due to context cancellation", clientAddr)
|
||||||
case websocket.CloseStatus(err) == websocket.StatusNormalClosure:
|
case websocket.CloseStatus(err) != -1:
|
||||||
log.Debugf("WebSocket closed normally")
|
log.Debugf("WebSocket from %s disconnected", clientAddr)
|
||||||
default:
|
default:
|
||||||
p.metrics.RecordError(ctx, "websocket_read_error")
|
p.metrics.RecordError(ctx, "websocket_read_error")
|
||||||
log.Errorf("WebSocket read error: %v", err)
|
log.Debugf("WebSocket read error from %s: %v", clientAddr, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if msgType != websocket.MessageBinary {
|
if msgType != websocket.MessageBinary {
|
||||||
log.Warnf("Unexpected WebSocket message type: %v", msgType)
|
log.Warnf("Unexpected WebSocket message type from %s: %v", clientAddr, msgType)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
log.Tracef("wsToTCP goroutine terminating due to context cancellation before TCP write")
|
log.Tracef("wsToPipe goroutine terminating due to context cancellation before pipe write")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tcpConn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
if err := pipeConn.SetWriteDeadline(time.Now().Add(ioTimeout)); err != nil {
|
||||||
log.Debugf("Failed to set TCP write deadline: %v", err)
|
log.Debugf("Failed to set pipe write deadline: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := tcpConn.Write(data)
|
n, err := pipeConn.Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.metrics.RecordError(ctx, "tcp_write_error")
|
p.metrics.RecordError(ctx, "pipe_write_error")
|
||||||
log.Errorf("TCP write error: %v", err)
|
log.Warnf("Pipe write error for %s: %v", clientAddr, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.metrics.RecordBytesTransferred(ctx, "ws_to_tcp", int64(n))
|
p.metrics.RecordBytesTransferred(ctx, "ws_to_grpc", int64(n))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) tcpToWS(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, wsConn *websocket.Conn, tcpConn net.Conn) {
|
func (p *Proxy) pipeToWS(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, wsConn *websocket.Conn, pipeConn net.Conn, clientAddr string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
buf := make([]byte, bufferSize)
|
buf := make([]byte, bufferSize)
|
||||||
for {
|
for {
|
||||||
if err := tcpConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
n, err := pipeConn.Read(buf)
|
||||||
log.Debugf("Failed to set TCP read deadline: %v", err)
|
|
||||||
}
|
|
||||||
n, err := tcpConn.Read(buf)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
log.Tracef("tcpToWS goroutine terminating due to context cancellation")
|
log.Tracef("pipeToWS goroutine terminating due to context cancellation")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var netErr net.Error
|
|
||||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
log.Errorf("TCP read error: %v", err)
|
log.Debugf("Pipe read error for %s: %v", clientAddr, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
log.Tracef("tcpToWS goroutine terminating due to context cancellation before WebSocket write")
|
log.Tracef("pipeToWS goroutine terminating due to context cancellation before WebSocket write")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := wsConn.Write(ctx, websocket.MessageBinary, buf[:n]); err != nil {
|
if n > 0 {
|
||||||
p.metrics.RecordError(ctx, "websocket_write_error")
|
if err := wsConn.Write(ctx, websocket.MessageBinary, buf[:n]); err != nil {
|
||||||
log.Errorf("WebSocket write error: %v", err)
|
p.metrics.RecordError(ctx, "websocket_write_error")
|
||||||
return
|
log.Warnf("WebSocket write error for %s: %v", clientAddr, err)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
p.metrics.RecordBytesTransferred(ctx, "tcp_to_ws", int64(n))
|
p.metrics.RecordBytesTransferred(ctx, "grpc_to_ws", int64(n))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
import "golang.org/x/sys/windows/registry"
|
import (
|
||||||
|
"golang.org/x/sys/windows/registry"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
urlWinExe = "https://pkgs.netbird.io/windows/x64"
|
urlWinExe = "https://pkgs.netbird.io/windows/x64"
|
||||||
|
urlWinExeArm = "https://pkgs.netbird.io/windows/arm64"
|
||||||
)
|
)
|
||||||
|
|
||||||
var regKeyAppPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Netbird"
|
var regKeyAppPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Netbird"
|
||||||
@@ -11,9 +15,14 @@ var regKeyAppPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Ne
|
|||||||
// DownloadUrl return with the proper download link
|
// DownloadUrl return with the proper download link
|
||||||
func DownloadUrl() string {
|
func DownloadUrl() string {
|
||||||
_, err := registry.OpenKey(registry.LOCAL_MACHINE, regKeyAppPath, registry.QUERY_VALUE)
|
_, err := registry.OpenKey(registry.LOCAL_MACHINE, regKeyAppPath, registry.QUERY_VALUE)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
return urlWinExe
|
|
||||||
} else {
|
|
||||||
return downloadURL
|
return downloadURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
url := urlWinExe
|
||||||
|
if runtime.GOARCH == "arm64" {
|
||||||
|
url = urlWinExeArm
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user