Skip to content

Commit b126b97

Browse files
committed
Enhance DNS resolutionn
1 parent a1e062b commit b126b97

File tree

5 files changed

+61
-34
lines changed

5 files changed

+61
-34
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ RUN set -eux \
7777
dnsutils \
7878
wireguard-tools \
7979
ca-certificates \
80+
ipcalc \
8081
&& apt-get clean \
8182
&& for d in bin etc lib run sbin; do mkdir -p /farcaster/"${d}"; done \
8283
&& ln -s /run/farcaster/wg-tunnel.conf /farcaster/etc/ \

scripts/_lib.sh

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,19 @@ get_iface_addr() {
117117

118118
resolve_host() {
119119
host="$1"
120-
dig +short "${host}" | grep '^[.0-9]*$' | sort
121-
return $?
120+
# Check if it's already an IP address using ipcalc
121+
if ! ipcalc -n -b "${host}" 2>&1 | grep -qi "INVALID ADDRESS"; then
122+
echo "${host}"
123+
return 0
124+
fi
125+
# Otherwise, resolve via DNS
126+
resolved=$(dig a +short "${host}" | head -1)
127+
if [ -z "${resolved}" ]; then
128+
# DNS resolution failed, return empty
129+
return 1
130+
fi
131+
echo "${resolved}"
132+
return 0
122133
}
123134

124135
HUB_IP_CHECK_TS=
@@ -229,7 +240,7 @@ get_proxy_address() {
229240
echo "${proxy}" | sed 's|^.*@||'
230241
}
231242

232-
get_proxy_host_from_address() {
243+
extract_host_from_proxy_address() {
233244
local address="$1"
234245
# Remove protocol if present
235246
address=$(echo "${address}" | sed -r 's|^https?://||')
@@ -264,6 +275,11 @@ get_proxy_port() {
264275
start_udp_over_tcp_tunnel() {
265276
local_udp_port="$1"
266277
remote_ip=$(resolve_host "$2")
278+
if [ -z "${remote_ip}" ]; then
279+
echo "Failed to resolve host: $2" >&2
280+
echo "-1"
281+
return 1
282+
fi
267283
remote_tcp_port="$3"
268284
setpriv --reuid=tcptun --regid=tcptun --clear-groups --no-new-privs \
269285
nohup /usr/local/bin/udp2tcp --tcp-forward "${remote_ip}":"${remote_tcp_port}" --udp-listen 127.0.0.1:${local_udp_port} > /dev/null &
@@ -288,8 +304,8 @@ create_moproxy_config() {
288304
user=$(get_proxy_username)
289305
password=$(get_proxy_password)
290306
address=$(get_proxy_address)
291-
host=$(get_proxy_host_from_address "${address}")
292-
ipaddr=$(dig +short "${host}" || echo "")
307+
host=$(extract_host_from_proxy_address "${address}")
308+
ipaddr=$(resolve_host "${host}" || echo "")
293309
ipaddr=$(test ! -z "${ipaddr}" && echo "${ipaddr}" || echo "${host}")
294310
port=$(get_proxy_port "${address}")
295311
auth=$(test ! -z "${user}" && printf "http username = %s\nhttp password = %s\n" "${user}" "${password}" || echo "")
@@ -316,7 +332,7 @@ set_proxy_redirect_rules() {
316332
done
317333
# Do not redirect traffic to the proxy itself
318334
proxy_addr=$(get_proxy_address)
319-
proxy_host=$(get_proxy_host_from_address "${proxy_addr}")
335+
proxy_host=$(extract_host_from_proxy_address "${proxy_addr}")
320336
${IPT_CMD} -t nat -A PROXY-REDIRECT -d "${proxy_host}" -j RETURN
321337

322338
${IPT_CMD} -t nat -A PROXY-REDIRECT -p tcp -j REDIRECT --to-port "${proxy_port}"
@@ -409,24 +425,27 @@ start_userspace_agent() {
409425
}
410426

411427
scrub_secrets() {
412-
local text="${1}"
413-
local temp_file=$(mktemp)
414-
echo "${text}" > "${temp_file}"
415-
416-
for secret in FARCASTER_AGENT_TOKEN HTTP_PROXY HTTPS_PROXY SOCKS5_PROXY; do
417-
# Word boundary: (^|[^A-Za-z0-9_])
418-
sed -i -E \
419-
-e "s/(^|[^A-Za-z0-9_])${secret}[[:space:]]*=[[:space:]]*([^[:space:]\"']+)/\1${secret}=********/g" \
420-
-e "s/(^|[^A-Za-z0-9_])${secret}[[:space:]]*=[[:space:]]*\"([^\"]*)\"/\1${secret}=\"********\"/g" \
421-
-e "s/(^|[^A-Za-z0-9_])${secret}[[:space:]]*=[[:space:]]*'([^']*)'/\1${secret}='********'/g" \
422-
"${temp_file}"
428+
local text="$1"
429+
shift # Remove first argument, leaving only secrets
430+
431+
local result="$text"
432+
433+
# Replace each secret value with asterisks
434+
for secret in "$@"; do
435+
# Skip empty values
436+
[ -z "$secret" ] && continue
437+
438+
# Escape special regex characters in the secret
439+
local escaped_secret=$(printf '%s\n' "$secret" | sed 's/[[\.*^$()+?{|]/\\&/g')
440+
441+
# Replace all occurrences of the secret with asterisks
442+
result=$(echo "$result" | sed "s/${escaped_secret}/*******/g")
423443
done
424444

425-
cat "${temp_file}"
426-
rm -f "${temp_file}"
445+
echo "$result"
427446
}
428447

429-
function print_log() {
448+
function dump_log() {
430449
set +e
431450
echo
432451
echo
@@ -438,8 +457,14 @@ function print_log() {
438457
local content
439458
local scrubbed
440459
content=$(cat "${log_file}" 2>/dev/null)
441-
scrubbed=$(scrub_secrets "${content}" 2>/dev/null)
442-
echo "${scrubbed}" > "${log_file}"
460+
# Ensure the log file is removed.
461+
rm -f "${log_file}"
462+
scrubbed=$(scrub_secrets "${content}" \
463+
"${FARCASTER_AGENT_TOKEN:-}" \
464+
"${HTTP_PROXY:-}" \
465+
"${HTTPS_PROXY:-}" \
466+
"${SOCKS5_PROXY:-}")
467+
echo "${scrubbed}"
443468
fi
444469

445470
echo
@@ -452,6 +477,7 @@ function print_log() {
452477
echo
453478
echo "===================================================================="
454479
echo
480+
455481
sleep 120
456482
}
457483

scripts/run.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ if [ "${v2_config_success}" != "true" ]; then
6666
elif [ -f "${SECRETS_DIR_V2}/wg-tunnel.conf" ] && [ -f "${SECRETS_DIR_V2}/wg-gateway.conf" ]; then
6767
cp ${SECRETS_DIR_V2}/* "${WORK_DIR}/"
6868
else
69-
print_log "${LOG_FILE}"
69+
dump_log "${LOG_FILE}"
7070
echo "Could not find the configuration files and the agent token is not set."
7171
fi
7272
fi
7373

7474
if [ ! -f "${WORK_DIR}/wg-tunnel.conf" ] || [ ! -f "${WORK_DIR}/wg-gateway.conf" ]; then
7575
echo "Could not find the configuration files."
76-
print_log "${LOG_FILE}"
76+
dump_log "${LOG_FILE}"
7777
exit 1
7878
fi
7979

8080
if ! HUB_HOST="$(wg_get_endpoint "${WG_TUN_IF}")"; then
8181
echo "Could not find the hub host"
82-
print_log "${LOG_FILE}"
82+
dump_log "${LOG_FILE}"
8383
exit 1
8484
fi
8585

@@ -98,7 +98,7 @@ echo -ne "Starting local DNS resolver\t... "
9898
if ! start_dnsmasq; then
9999
echo "failed"
100100
echo "Could not start local DNS resolver"
101-
print_log "${LOG_FILE}"
101+
dump_log "${LOG_FILE}"
102102
exit 1
103103
fi
104104
echo "done"
@@ -111,7 +111,7 @@ if ! start_proxy_maybe "${TCP_PROXY_PORT}"; then
111111
echo -n "HTTP_PROXY defined, but could not set traffic redirection rules. "
112112
echo "Ensure HTTP_PROXY is correct and this container has NET_ADMIN capabilities."
113113
echo
114-
print_log "${LOG_FILE}"
114+
dump_log "${LOG_FILE}"
115115
exit 1
116116
fi
117117
echo "done"
@@ -140,7 +140,7 @@ if [ "${CONNECTED_UDP}" = "0" ]; then
140140
echo
141141
echo "Could not start fallback TCP tunnel."
142142
proxy_warning
143-
print_log "${LOG_FILE}"
143+
dump_log "${LOG_FILE}"
144144
exit 1
145145
fi
146146
echo "done"
@@ -154,7 +154,7 @@ if [ "${CONNECTED_UDP}" = "0" ]; then
154154
echo
155155
echo "Could not establish TCP tunnel."
156156
proxy_warning
157-
print_log "${LOG_FILE}"
157+
dump_log "${LOG_FILE}"
158158
exit 1
159159
fi
160160
echo "done"
@@ -170,7 +170,7 @@ if [ "${DISABLE_FIREWALL}" != "true" ] &&
170170
echo "failed"
171171
echo
172172
echo "Could not set network gateway filter rules."
173-
print_log "${LOG_FILE}"
173+
dump_log "${LOG_FILE}"
174174
exit 1
175175
else
176176
echo "done"
@@ -184,7 +184,7 @@ if ! set_gw_nat_rules; then
184184
echo "failed"
185185
echo
186186
echo "Could not set network gateway NAT rules."
187-
print_log "${LOG_FILE}"
187+
dump_log "${LOG_FILE}"
188188
exit 1
189189
fi
190190
echo "done"
@@ -194,7 +194,7 @@ if ! wg_start "${WG_GW_IF}"; then
194194
echo "failed"
195195
echo
196196
echo "Could not start WireGuard gateway."
197-
print_log "${LOG_FILE}"
197+
dump_log "${LOG_FILE}"
198198
exit 1
199199
fi
200200
echo "done"

tests/agent/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
- FARCASTER_API_URL
2424
- FARCASTER_FORCE_TCP
2525
cap_add:
26-
- ${NET_ADMIN}
26+
- ${NET_ADMIN:-SETUID}
2727
volumes:
2828
- shared-logs:/logs
2929
restart: "no"

tests/proxy/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
- /bin/bash
1212
- -c
1313
- set -eu
14-
&& proxy_ip=$$(dig +short proxy)
14+
&& proxy_ip=$$(dig +short proxy)
1515
&& iptables -t nat -I PREROUTING -p tcp -m multiport --dport 80,443 -j REDIRECT --to-port 1080
1616
&& iptables -t nat -I OUTPUT -p tcp -m multiport --dports 80,443 -j REDIRECT --to-port 1080
1717
&& { curl ifconfig.me >/dev/null 2>&1 && echo "FAILED" || echo "OK"; }

0 commit comments

Comments
 (0)