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
+26 -4
View File
@@ -1,8 +1,9 @@
import { Injectable, Logger } from '@nestjs/common';
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { MessageAudience } from '@prisma/client';
import { PrismaService } from '../../prisma/prisma.service';
import { envOr } from '../../common/env';
import { envOr, envOrNull } from '../../common/env';
import { DEFAULT_SMS_PROVIDER, SMS_DRIVERS, SmsDriver, SmsSendResult } from './drivers';
import { isPrivateIpv4, resolveRelayHost } from './drivers/relay.driver';
/**
* SMS delivery — port of henza_server/app/Models/Sms.php, with the gateway
@@ -96,11 +97,33 @@ export function assertSmsConfigured(logger: Logger = new Logger('SmsConfig')): v
}
@Injectable()
export class SmsService {
export class SmsService implements OnApplicationBootstrap {
private readonly logger = new Logger(SmsService.name);
constructor(private readonly prisma: PrismaService) {}
/**
* Reports the `host` the relay gateway will receive, so a wrong one shows in
* the boot log instead of as silent failures. Never throws and never delays
* the boot: the lookup runs in the background.
*/
onApplicationBootstrap() {
if (smsDriver()?.name !== 'relay') return;
void resolveRelayHost()
.then((host) => {
const source = envOrNull('SMS_RELAY_HOST') ? 'SMS_RELAY_HOST' : 'detected';
if (isPrivateIpv4(host)) {
this.logger.warn(
`SMS relay host is a private address (${host}, ${source}); the gateway may not accept it. ` +
"Set SMS_RELAY_HOST to this server's public IP.",
);
} else {
this.logger.log(`SMS relay host: ${host ?? '(none)'} (${source})`);
}
})
.catch(() => undefined);
}
/**
* Sends one message. Never throws — a failed SMS must not roll back the
* business operation that triggered it, exactly as in the reference.
@@ -110,7 +133,6 @@ export class SmsService {
// The reference trims before sending (`Sms.php:77`).
const text = body.trim();
console.log("mobile ==>" ,mobile)
const result = await this.deliver(mobile, text);
if (persist) {