-
-
Notifications
You must be signed in to change notification settings - Fork 1
[WIP] - Add support for connection gating and address factory #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5a08486
5e20c55
a724bd2
6c63858
bd8847a
eeb8273
f600d76
f9aae61
f130dc9
6817deb
2cbe26f
b23fb5b
37dd56f
4448a41
9d9d80c
9f4ebd6
40c6a6b
9f0e0ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .history/ | ||
| .idea/ | ||
| p2p_poc | ||
| peer_cache.json |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "log" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "net" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -70,8 +71,8 @@ func NewClient(config Config) (P2PClient, error) { | |||||||||||||||||||||||||||||||||||||||||||||||
| hostOpts = append(hostOpts, libp2p.Identity(config.PrivateKey)) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Configure announce addresses if provided (useful for K8s) | ||||||||||||||||||||||||||||||||||||||||||||||||
| var announceAddrs []multiaddr.Multiaddr | ||||||||||||||||||||||||||||||||||||||||||||||||
| if len(config.AnnounceAddrs) > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| var announceAddrs []multiaddr.Multiaddr | ||||||||||||||||||||||||||||||||||||||||||||||||
| for _, addrStr := range config.AnnounceAddrs { | ||||||||||||||||||||||||||||||||||||||||||||||||
| maddr, err := multiaddr.NewMultiaddr(addrStr) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -87,17 +88,37 @@ func NewClient(config Config) (P2PClient, error) { | |||||||||||||||||||||||||||||||||||||||||||||||
| logger.Infof("Using custom announce addresses: %v", config.AnnounceAddrs) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Create libp2p host | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Simple address factory to only use custom announce addresses if provided | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Otherwise let libp2p/AutoNAT handle address detection automatically | ||||||||||||||||||||||||||||||||||||||||||||||||
| var addressFactory func([]multiaddr.Multiaddr) []multiaddr.Multiaddr | ||||||||||||||||||||||||||||||||||||||||||||||||
| if len(announceAddrs) > 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| addressFactory = func([]multiaddr.Multiaddr) []multiaddr.Multiaddr { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return announceAddrs | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| logger.Infof("Using only custom announce addresses for advertising") | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Get bootstrap peers from DHT library - these can act as relays | ||||||||||||||||||||||||||||||||||||||||||||||||
| bootstrapPeers := dht.GetDefaultBootstrapPeerAddrInfos() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Create libp2p host with NAT traversal options | ||||||||||||||||||||||||||||||||||||||||||||||||
| hostOpts = append(hostOpts, | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.ListenAddrStrings( | ||||||||||||||||||||||||||||||||||||||||||||||||
| "/ip4/0.0.0.0/tcp/0", | ||||||||||||||||||||||||||||||||||||||||||||||||
| "/ip6/::/tcp/0", | ||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", config.Port), // Listen on all interfaces | ||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Sprintf("/ip6/::/tcp/%d", config.Port), | ||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableNATService(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableHolePunching(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableRelay(), | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.NATPortMap(), // Try UPnP/NAT-PMP for automatic port forwarding | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableNATService(), // AutoNAT to detect if we're reachable | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableHolePunching(), // DCUtR protocol for NAT hole punching | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableRelay(), // Act as relay for others | ||||||||||||||||||||||||||||||||||||||||||||||||
| libp2p.EnableAutoRelayWithStaticRelays(bootstrapPeers), // Use bootstrap nodes as potential relays | ||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Only apply address factory if custom announce addresses are provided | ||||||||||||||||||||||||||||||||||||||||||||||||
| if addressFactory != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| hostOpts = append(hostOpts, libp2p.AddrsFactory(addressFactory)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| h, err := libp2p.New(hostOpts...) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| cancel() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -108,7 +129,6 @@ func NewClient(config Config) (P2PClient, error) { | |||||||||||||||||||||||||||||||||||||||||||||||
| logger.Infof("Listening on: %v", h.Addrs()) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Set up DHT with bootstrap peers | ||||||||||||||||||||||||||||||||||||||||||||||||
| bootstrapPeers := dht.GetDefaultBootstrapPeerAddrInfos() | ||||||||||||||||||||||||||||||||||||||||||||||||
| kadDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeServer), dht.BootstrapPeers(bootstrapPeers...)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| h.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -634,3 +654,51 @@ func PrivateKeyFromHex(keyHex string) (crypto.PrivKey, error) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return priv, nil | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Function to check if an IP address is private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Function to check if an IP address is private | |
| // isPrivateIP checks if the given multiaddr represents a private IP address. | |
| // | |
| // Parameters: | |
| // addr: multiaddr.Multiaddr - The multiaddr to check. It should contain an IPv4 or IPv6 address. | |
| // | |
| // Returns: | |
| // bool - Returns true if the IP address is private (e.g., RFC1918 for IPv4, unique local or link-local for IPv6, or loopback addresses), false otherwise. | |
| // | |
| // Examples of private IP addresses: | |
| // IPv4: "10.0.0.1", "172.16.5.4", "192.168.1.100", "127.0.0.1" | |
| // IPv6: "::1" (loopback), "fc00::1" (unique local), "fe80::1" (link-local) |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition ip.To4() == nil will incorrectly return false for all IPv6 addresses, even though the function handles IPv6 private ranges. This should check if the IP is neither IPv4 nor IPv6, or handle IPv6 addresses properly.
| if ip == nil || ip.To4() == nil { | |
| if ip == nil { |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic incorrectly rejects IPv6 addresses. The ip.To4() == nil check means IPv6 addresses will always return false, but IPv6 private ranges are defined below. Remove the ip.To4() == nil condition.
| if ip == nil || ip.To4() == nil { | |
| if ip == nil { |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IPv6 addresses will be incorrectly classified as non-private. The condition ip.To4() == nil returns false for all IPv6 addresses, causing the function to return false even for private IPv6 ranges that are defined later in the function.
| if ip == nil || ip.To4() == nil { | |
| if ip == nil { |
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function only handles IPv4 addresses due to the ip.To4() check, but the extractIPFromMultiaddr function only extracts IPv4 addresses. IPv6 private addresses (like link-local fe80::/10) will not be detected as private. Consider adding IPv6 support or documenting this limitation.
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The private IP ranges are duplicated between the connection gater logic (lines 139-146) and the isPrivateIP function (lines 728-737). This duplication could lead to inconsistencies if one is updated without the other.
Copilot
AI
Oct 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function comment is incomplete. It should document the return values, error conditions, and provide examples of expected input/output formats.
| // Function to extract IP information from a Multiaddr (supports IPv4 and IPv6) | |
| // extractIPFromMultiaddr extracts the IP address (IPv4 or IPv6) from a multiaddr.Multiaddr. | |
| // | |
| // Returns: | |
| // - string: The extracted IP address as a string if found. | |
| // - error: An error if the IP address cannot be extracted or the multiaddr does not contain an IP4/IP6 protocol. | |
| // | |
| // Error conditions: | |
| // - If the multiaddr does not contain an IP4 or IP6 protocol, an error is returned. | |
| // - If the protocol value cannot be extracted, an error is returned. | |
| // | |
| // Example: | |
| // addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/4001") | |
| // ip, err := extractIPFromMultiaddr(addr) | |
| // // ip == "127.0.0.1", err == nil | |
| // | |
| // addr, _ := multiaddr.NewMultiaddr("/ip6/::1/tcp/4001") | |
| // ip, err := extractIPFromMultiaddr(addr) | |
| // // ip == "::1", err == nil | |
| // | |
| // addr, _ := multiaddr.NewMultiaddr("/tcp/4001") | |
| // ip, err := extractIPFromMultiaddr(addr) | |
| // // ip == "", err != nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
announceAddrsvariable is declared but the existing address factory logic usingconfig.AnnounceAddrsis removed without preserving its functionality. This will break the original announce address feature when both custom announce addresses and the new address factory are used together.