Skip to content

Commit d8c0952

Browse files
committed
feat: auto-detect public IP for geth P2P peer discovery
Previously, HOST_IP was hardcoded to empty string and users had to manually edit the entrypoint to enable --nat=extip. This change: - Adds the same get_public_ip() function used by base-consensus-entrypoint and op-node-entrypoint (tries ifconfig.me, api.ipify.org, ipecho.net, v4.ident.me in sequence) - Auto-detects public IP if HOST_IP is not manually set - Falls back gracefully with a warning if detection fails - Respects manual HOST_IP override when set via environment
1 parent 4e715e4 commit d8c0952

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

geth/geth-entrypoint

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RPC_PORT="${RPC_PORT:-8545}"
77
WS_PORT="${WS_PORT:-8546}"
88
AUTHRPC_PORT="${AUTHRPC_PORT:-8551}"
99
METRICS_PORT="${METRICS_PORT:-6060}"
10-
HOST_IP="" # put your external IP address here and open port 30303 to improve peer connectivity
10+
HOST_IP="" # Will be auto-detected; set manually to override
1111
P2P_PORT="${P2P_PORT:-30303}"
1212
DISCOVERY_PORT="${DISCOVERY_PORT:-30303}"
1313
ADDITIONAL_ARGS=""
@@ -26,7 +26,36 @@ if [[ -z "$OP_NODE_NETWORK" ]]; then
2626
exit 1
2727
fi
2828

29-
mkdir -p $GETH_DATA_DIR
29+
# Auto-detect public IP for P2P peer discovery if not manually set
30+
get_public_ip() {
31+
local PROVIDERS=(
32+
"http://ifconfig.me"
33+
"http://api.ipify.org"
34+
"http://ipecho.net/plain"
35+
"http://v4.ident.me"
36+
)
37+
for provider in "${PROVIDERS[@]}"; do
38+
local IP
39+
IP=$(curl -s --max-time 10 --connect-timeout 5 "$provider")
40+
if [[ $IP =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
41+
echo "$IP"
42+
return 0
43+
fi
44+
done
45+
return 1
46+
}
47+
48+
# If HOST_IP is not manually set (still empty), try auto-detection
49+
if [[ -z "$HOST_IP" ]]; then
50+
if PUBLIC_IP=$(get_public_ip); then
51+
echo "Auto-detected public IP: $PUBLIC_IP"
52+
HOST_IP="$PUBLIC_IP"
53+
else
54+
echo "Could not auto-detect public IP. Peer connectivity may be limited." >&2
55+
fi
56+
fi
57+
58+
mkdir -p "$GETH_DATA_DIR"
3059

3160
echo "$BASE_NODE_L2_ENGINE_AUTH_RAW" > "$BASE_NODE_L2_ENGINE_AUTH"
3261

0 commit comments

Comments
 (0)