Skip to content

Commit bff3ec5

Browse files
committed
Add HTTP proxy support to netstack TCP connections
- Import dialers package for proxy support - Try direct connection first, then fall back to HTTP proxy if configured - Maintain existing TCP keepalive settings for proxied connections - Add FARCASTER_FORCE_TCP environment variable to docker-compose
1 parent 9191b67 commit bff3ec5

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

compose/docker-compose.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ services:
99
#
1010
# FARCASTER_AGENT_TOKEN
1111
# The Agent's token is obtained when creating a Scanning Agent in the Probely app.
12-
# Learn more at https://help.probely.com/en/articles/6503388-how-to-install-a-scanning-agent
12+
# Learn more at https://help.probely.com/en/articles/6503388-how-to-install-a-scanning-agent
1313
#
14-
# FARCASTER_API_URL
15-
# Probely's API URL
14+
# FARCASTER_API_URL
15+
# Probely's API URL
1616
#
1717
# HTTP_PROXY (optional)
1818
# An advanced option that can be used to configure an HTTP proxy for the Agent to connect to Probely.
@@ -21,6 +21,7 @@ services:
2121
- FARCASTER_AGENT_TOKEN
2222
- FARCASTER_API_URL
2323
- HTTP_PROXY
24+
- FARCASTER_FORCE_TCP
2425
tmpfs:
2526
- /run
2627
cap_add:

farcaster-go/wireguard/netstack/tcp.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
1313
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
1414
"gvisor.dev/gvisor/pkg/waiter"
15+
"probely.com/farcaster/dialers"
1516
)
1617

1718
// Forwarder request wrapper to abstract away the details of setting up
@@ -113,20 +114,32 @@ func (r *netstackTCPFwd) ConnectUpstream(timeout time.Duration, count int) (*net
113114
// TODO: Add IPv6 support.
114115
serverIP := parseIPv4(cr.LocalAddress)
115116
serverAddrPort := netip.AddrPortFrom(serverIP, cr.LocalPort)
117+
serverAddr := serverAddrPort.String()
116118

117-
// Dial the upstream server.
119+
// Try direct connection first
118120
var dialer net.Dialer
119-
server, err := dialer.DialContext(*r.ctx, "tcp", serverAddrPort.String())
121+
dialer.Timeout = timeout
122+
server, err := dialer.DialContext(*r.ctx, "tcp", serverAddr)
123+
124+
// If direct connection fails, try HTTP proxy if configured
125+
if err != nil {
126+
if proxyURL := dialers.ParseHTTPProxy(); proxyURL != nil {
127+
proxyDialer := dialers.NewHTTPProxyDialer(proxyURL, serverAddr, timeout)
128+
server, err = proxyDialer.Connect()
129+
}
130+
}
131+
120132
if err != nil {
121-
// Reject the connection if dialing fails.
133+
// Reject the connection if all attempts fail
122134
r.fr.Complete(true)
123135
return nil, err
124136
}
125137

126138
// Set TCP keepalive options for the upstream connection.
127-
setTCPConnTimeouts(server.(*net.TCPConn), keepaliveInterval, keepaliveCount)
139+
tcpConn := server.(*net.TCPConn)
140+
setTCPConnTimeouts(tcpConn, keepaliveInterval, keepaliveCount)
128141

129-
r.upstream = server.(*net.TCPConn)
142+
r.upstream = tcpConn
130143
return r.upstream, nil
131144
}
132145

0 commit comments

Comments
 (0)