fix(sms): inside Docker, send the server's public IP as the relay host, not the container's

Messages went out locally but not from the server. The relay gateway
identifies the sending server by `host`, and the owner's ruling (D26)
is that this is the server's IP. With SMS_RELAY_HOST unset, the driver
sent the machine's first external IPv4. On the bare PHP server that was
the real IP; inside the API container it is the container's (172.x).
Reproduced: a container on this machine reports 172.17.0.3.

- relay.driver: when SMS_RELAY_HOST is unset and the machine address is
  private (RFC 1918, CGNAT, link-local), the public IPv4 is looked up
  once (ipify, ifconfig.me, icanhazip in turn) and cached. If every
  lookup fails, it falls back to the old value. SMS_RELAY_HOST still wins.
- SmsService logs the host it will send at boot, and warns when it is
  still private, so a wrong value is visible instead of silent.
- Removed a debug console.log that wrote every recipient's mobile to the
  server log.
- .env.example and SERVER-SETUP-CHECKLIST: SMS_RELAY_HOST = this
  server's public IP.

Tests: relay-host.spec (5). All 254 API tests pass.

اپ: پیامک کد ورود دوباره به‌درستی ارسال می‌شود.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
s.esmaeili
2026-10-10 12:58:13 +03:30
co-authored by Claude Opus 5.5
parent c47d02d377
commit 7f851676b5
7 changed files with 201 additions and 14 deletions
@@ -39,12 +39,51 @@ export function serverIpv4(): string | null {
return null;
}
/** RFC 1918, carrier-grade NAT and link-local: addresses only a private network knows. */
export function isPrivateIpv4(ip: string | null): boolean {
return !!ip && /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.|169\.254\.)/.test(ip);
}
/**
* The server's public IPv4, looked up once. Only used when SMS_RELAY_HOST is
* unset and the machine's own address is private — inside Docker that address
* is the container's (172.x), which is not "the server" to the gateway. The
* PHP client ran on the bare server, where gethostbyname gave its real IP;
* this restores that value (owner ruling D26: host = the server's IP).
*/
let publicIpv4: string | null = null;
const PUBLIC_IP_SERVICES = ['https://api.ipify.org', 'https://ifconfig.me/ip', 'https://icanhazip.com'];
export async function resolveRelayHost(): Promise<string | null> {
if (envOrNull('SMS_RELAY_HOST') || publicIpv4) return relayConfig().host;
const own = serverIpv4();
if (!isPrivateIpv4(own)) return own;
for (const url of PUBLIC_IP_SERVICES) {
try {
const reply = await postWithTimeout(url, {}, null, 5_000, 'GET');
const ip = reply.body.trim();
if (reply.ok && /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) && !isPrivateIpv4(ip)) {
publicIpv4 = ip;
break;
}
} catch {
/* try the next service */
}
}
return relayConfig().host;
}
/** Tests only. */
export function resetRelayHostCache() {
publicIpv4 = null;
}
export function relayConfig() {
const platform = envOr('SMS_RELAY_PLATFORM', DEFAULT_RELAY_PLATFORM);
return {
url: envOr('SMS_RELAY_URL', DEFAULT_RELAY_URL),
secret: envOr('SMS_RELAY_SECRET', DEFAULT_RELAY_SECRET),
host: envOrNull('SMS_RELAY_HOST') ?? serverIpv4(),
host: envOrNull('SMS_RELAY_HOST') ?? publicIpv4 ?? serverIpv4(),
// "default" sends null, so the gateway chooses the line itself.
platform: platform.toLowerCase() === 'default' ? null : platform,
};
@@ -93,6 +132,7 @@ export const relayDriver: SmsDriver = {
},
async deliver(mobile, text) {
await resolveRelayHost();
const c = relayConfig();
const reply = await postWithTimeout(
c.url,