fix: public IP detection with validation and fallbacks

This commit is contained in:
SamNet-dev
2026-02-11 12:07:00 -06:00
parent 051d238df8
commit baee4cbfdb

View File

@@ -1741,9 +1741,21 @@ is_mtproto_running() {
docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^mtproto-proxy$" docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^mtproto-proxy$"
} }
get_public_ip() {
local _ip _url
for _url in "https://api.ipify.org" "https://ifconfig.me" "https://icanhazip.com"; do
_ip=$(curl -4 -s --max-time 5 "$_url" 2>/dev/null | tr -d '[:space:]')
if echo "$_ip" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then
echo "$_ip"
return 0
fi
done
return 1
}
get_mtproto_link() { get_mtproto_link() {
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -z "$_mt_ip" ]; then if [ -z "$_mt_ip" ]; then
echo "Could not detect server IP." echo "Could not detect server IP."
return 1 return 1
@@ -1768,7 +1780,7 @@ show_mtproto_status() {
fi fi
if is_mtproto_running; then if is_mtproto_running; then
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
[ -n "$_mt_ip" ] && echo -e " Link: tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}" [ -n "$_mt_ip" ] && echo -e " Link: tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}"
fi fi
} }
@@ -1796,7 +1808,7 @@ show_mtproto_menu() {
echo -e " Traffic: ↓ $(format_bytes ${_mt_dl:-0}) ↑ $(format_bytes ${_mt_ul:-0})" echo -e " Traffic: ↓ $(format_bytes ${_mt_dl:-0}) ↑ $(format_bytes ${_mt_ul:-0})"
if is_mtproto_running; then if is_mtproto_running; then
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -n "$_mt_ip" ]; then if [ -n "$_mt_ip" ]; then
echo "" echo ""
echo -e " ${BOLD}Share link:${NC}" echo -e " ${BOLD}Share link:${NC}"
@@ -1901,7 +1913,7 @@ show_mtproto_menu() {
7) 7)
echo "" echo ""
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -n "$_mt_ip" ]; then if [ -n "$_mt_ip" ]; then
local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}" local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}"
echo -e " ${BOLD}Share this link with Telegram users:${NC}" echo -e " ${BOLD}Share this link with Telegram users:${NC}"
@@ -1924,7 +1936,7 @@ show_mtproto_menu() {
echo -e " ${DIM}Enable it in Settings → t. Telegram notifications${NC}" echo -e " ${DIM}Enable it in Settings → t. Telegram notifications${NC}"
else else
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -n "$_mt_ip" ]; then if [ -n "$_mt_ip" ]; then
local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}" local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}"
echo -e " Sending to Telegram..." echo -e " Sending to Telegram..."
@@ -2080,7 +2092,7 @@ Domain: ${MTPROTO_DOMAIN}
echo -e " ${GREEN}✓ MTProto proxy enabled and running!${NC}" echo -e " ${GREEN}✓ MTProto proxy enabled and running!${NC}"
echo "" echo ""
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -n "$_mt_ip" ]; then if [ -n "$_mt_ip" ]; then
local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}" local _mt_link="tg://proxy?server=${_mt_ip}&port=${MTPROTO_PORT}&secret=${MTPROTO_SECRET}"
echo -e " ${BOLD}Share link:${NC}" echo -e " ${BOLD}Share link:${NC}"
@@ -6227,7 +6239,7 @@ telegram_send_message() {
# Prepend server label + IP (escape for Markdown) # Prepend server label + IP (escape for Markdown)
local label="${TELEGRAM_SERVER_LABEL:-$(hostname 2>/dev/null || echo 'unknown')}" local label="${TELEGRAM_SERVER_LABEL:-$(hostname 2>/dev/null || echo 'unknown')}"
label=$(escape_telegram_markdown "$label") label=$(escape_telegram_markdown "$label")
local _ip=$(curl -s --max-time 3 https://api.ipify.org 2>/dev/null || echo "") local _ip=$(get_public_ip)
if [ -n "$_ip" ]; then if [ -n "$_ip" ]; then
message="[${label} | ${_ip}] ${message}" message="[${label} | ${_ip}] ${message}"
else else
@@ -6567,9 +6579,12 @@ INSTALL_DIR="/opt/conduit"
[ -z "$TELEGRAM_CHAT_ID" ] && exit 0 [ -z "$TELEGRAM_CHAT_ID" ] && exit 0
# Cache server IP once at startup # Cache server IP once at startup
_server_ip=$(curl -s --max-time 5 https://api.ipify.org 2>/dev/null \ _server_ip=""
|| curl -s --max-time 5 https://ifconfig.me 2>/dev/null \ for _ip_url in "https://api.ipify.org" "https://ifconfig.me" "https://icanhazip.com"; do
|| echo "") _server_ip=$(curl -4 -s --max-time 5 "$_ip_url" 2>/dev/null | tr -d '[:space:]')
echo "$_server_ip" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' && break
_server_ip=""
done
telegram_send() { telegram_send() {
local message="$1" local message="$1"
@@ -7606,7 +7621,7 @@ Instances: ${_sf_count}"
telegram_send "❌ MTProto proxy is not configured." telegram_send "❌ MTProto proxy is not configured."
else else
local _mt_ip local _mt_ip
_mt_ip=$(curl -4 -s --max-time 5 ifconfig.me 2>/dev/null) _mt_ip=$(get_public_ip)
if [ -z "$_mt_ip" ]; then if [ -z "$_mt_ip" ]; then
telegram_send "❌ Could not detect server IP." telegram_send "❌ Could not detect server IP."
else else