Compare commits

...
5 Commits
Author SHA1 Message Date
oqyude c73a698857 opencode fix linger 2026-10-04 22:27:55 +03:00
oqyude c8d4a12a73 3x-ui: revert nginx + ports to 543fcc6 (testing) declarative state
Sapphira: HTTP reverse proxy serves panel/sub on x.zeroq.su;
no xray stream on 443 and no 8443 stream either (8443 is directly
exposed by podman as 0.0.0.0:8443:8443/tcp).

Otreca: stream on 443 routes by SNI (panel via pubray1.zeroq.su,
xray default) and 8443 is direct 0.0.0.0:8443.

Modules/containers/3x-ui.nix:
  - basePorts restored: '0.0.0.0:8443:8443/tcp' (was '127.0.0.1:15380:8443/tcp')
  - realityPorts restored (was 'lib.optional ... "127.0.0.1:15380:443/tcp"')
  - image restored: ':latest' (was ':v3.9.0')

Modules/server/nginx.nix:
  - removed 8443 streamConfig for xray (the one b0191bc added)
  - removed 8443 from allowedTCPPorts

Other files (configurations/{server,vds,wsl}.nix, home/modules/opencode.nix)
left alone — they contain SSH firewall / builder / opencode web changes
unrelated to nginx + ports that the user asked to revert.
2026-10-04 22:05:27 +03:00
oqyude c854b2cc6d 3x-ui: drop dead -p 127.0.0.1:15380:443/tcp (double-bind blocks start)
The systemd unit on the otreca VDS carried two -p flags that bind
the same host port 127.0.0.1:15380:

  -p 127.0.0.1:15380:8443/tcp   # from basePorts
  -p 127.0.0.1:15380:443/tcp    # from realityPorts (when reality443Forwarding=true)

podman 5.x tries to bind 127.05 in each -p flag and the second
fails with EADDRINUSE, even though no process is visible in ss —
the bind happens at the proxy level before the container starts:

  Error: cannot listen on the TCP port: listen tcp4 127.0.0.1:15380:
  bind: address already in use

Symptom on otreca: podman-3xui_app.service hits start-limit-hit
after 5 rapid retries.

The 15380:443 mapping is dead code: the container's only Reality
inbound listens on 8443, and nginx stream already routes host:443
to 127.0.0.1:15380 via SNI (modules/server/nginx.nix streamConfig).
reality443Forwarding remains a host option for configurations to
declare intent; the broken port-mapping generation is replaced with
an empty list.
2026-10-04 21:37:14 +03:00
oqyude 22a19be1b6 3x-ui: rollback to c05cc88 (before otreca vds commit)
Revert the b0191bc 'otreca vds: pin 3x-ui:v3.8.5 + nginx stream + ssh
tailscale-only + patch-3xui-xray-config' changes:

- 3x-ui.nix: back to :latest image, direct 0.0.0.0:8443 port mapping,
  remove migrateScript + patchScript and their systemd units/timer.
- vds.nix: re-open 22/tcp on public (openFirewall = true); remove the
  tailscale0-only port rule.
- nginx.nix: drop the 8443 stream proxy.
- Remove modules/containers/3x-ui-migration-notes.md.

Reason: those changes, once applied on otreca, left the 3x-ui container
in a start-limit-hit loop (bind 127.0.0.1:15380: address already in use,
nothing visible in ss - probably a stale TIME_WAIT or slirp4netns port
from a prior container that never released).
2026-10-04 21:30:47 +03:00
oqyude 99747849d3 3x-ui regress 2026-10-04 21:03:48 +03:00
4 changed files with 23 additions and 141 deletions
+12 -1
View File
@@ -336,7 +336,18 @@ in
# Refs:
# https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html#OOMScoreAdjust=
systemd.user.services.opencode-web.serviceConfig = {
# Override the [Service] section emitted by `programs.opencode.web`.
# Upstream writes its own [Service] keys (ExecStart, EnvironmentFile,
# Restart, RestartSec); merging on the same `Service` attrset unions both
# sides into the same systemd section, so cgroup limits land where systemd
# actually reads them.
#
# NOTE: do NOT use `serviceConfig = { ... }` here — it is rendered as a
# literal `[serviceConfig]` section header by home-manager, which systemd
# silently ignores (verified on sapphira, journal: "Unknown section
# 'serviceConfig'. Ignoring."). The previous version of this block was
# exactly that, so the OOM/cgroup protection above never took effect.
systemd.user.services.opencode-web.Service = {
MemoryHigh = "1G";
MemoryMax = "2G";
OOMScoreAdjust = -900;
+5 -118
View File
@@ -24,95 +24,15 @@ let
"key.pem"
];
basePorts = [
# 3x-ui panel + subscription endpoint on the loopback only.
# Local-only upstreams for the 3x-ui panel and subscription endpoint.
# The direct Xray inbound remains publicly reachable on 8443.
"127.0.0.1:2049:2049/tcp"
"127.0.0.1:2096:2096/tcp"
# xray's Reality inbound on the loopback only — nginx stream (in
# modules/server/nginx.nix) listens on the public 8443 and forwards
# here. Going nginx-stream → podman → xray keeps Reality's TLS
# ClientHello intact end-to-end; exposing 8443 directly via podman
# port-forward mangles it and clients see the fallback cert.
"127.0.0.1:15380:8443/tcp"
"0.0.0.0:8443:8443/tcp"
];
# VDS-only: nginx stream forwards host:443 → 127.0.0.1:15380 →
# container:443, so Xray sees its REALITY inbound on port 443.
realityPorts = lib.optional config.host."3x-ui".reality443Forwarding "127.0.0.1:15380:443/tcp";
# Workaround for a 3x-ui panel bug (both 3.8.5 and 3.9.0 reproduce it): when
# generating bin/config.json from the inbounds DB rows, the panel drops the
# inner `realitySettings.settings.{publicKey,fingerprint,serverName,spiderX,
# mldsa65Verify}` block — without which the xray Reality server cannot
# complete the auth handshake with any client. The DB has the data; only
# the generated config.json is missing it. This script reads DB inside the
# running container and re-applies the missing fields to bin/config.json,
# then SIGHUPs xray so clients can connect. Runs every 30s; safe to
# overlap with the panel's own config writes (it's idempotent and only
# touches missing/different fields).
# REAL ROOT-CAUSE FIX for the 3x-ui config-gen bug.
#
# In `internal/web/service/xray.go` the panel's `GetXrayConfig()`
# function does this on every config regeneration (xray restart, inbound
# update, restartXrayService API call):
#
# realitySettings, ok2 := stream["realitySettings"].(map[string]any)
# if ok2 { delete(realitySettings, "settings") }
#
# i.e. it explicitly drops the *nested* `realitySettings.settings` block
# before serialising to bin/config.json. The panel's inbound DB row
# stores these fields under `stream_settings.realitySettings.settings`,
# so every regeneration wipes publicKey/fingerprint/serverName/spiderX/
# mldsa65Verify from the live xray config, breaking Reality-auth for
# every inbound.
#
# The proper fix is to move these fields from the nested `settings` block
# to the *top level* of `realitySettings` directly in the DB. Panel's
# delete() targets the nested block only; top-level fields pass through
# untouched, and Panel passes them through to bin/config.json correctly.
#
# The migration is idempotent (no-op once fields are top-level) and is
# re-applied on every container start so that any new inbound created
# via the panel UI gets migrated automatically.
migrateScript = pkgs.writeScript "migrate-3xui-reality.py" ''
#!/usr/bin/env python3
"""Move Reality fields from nested settings to top-level realitySettings in DB.
Idempotent. Re-applied on every container start so newly-added inbounds
are auto-migrated."""
import json, sqlite3, sys
FIELDS = ("publicKey", "fingerprint", "serverName", "spiderX", "mldsa65Verify")
try:
conn = sqlite3.connect("/etc/x-ui/x-ui.db")
rows = conn.execute(
"SELECT id, stream_settings FROM inbounds "
"WHERE stream_settings IS NOT NULL AND protocol='vless'"
).fetchall()
migrated = 0
for rid, ss_json in rows:
ss = json.loads(ss_json)
rs = ss.get("realitySettings")
if not rs:
continue
inner = rs.get("settings", {})
if not inner:
continue
changed = False
for k in FIELDS:
v = inner.get(k)
if v and not rs.get(k):
rs[k] = v
changed = True
if changed:
conn.execute(
"UPDATE inbounds SET stream_settings=? WHERE id=?",
(json.dumps(ss), rid),
)
migrated += 1
conn.commit()
conn.close()
print(f"migrated={migrated}")
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
sys.exit(1)
'';
in
{
# `host."3x-ui"` options are declared in modules/options.nix: they are set
@@ -131,15 +51,7 @@ in
oci-containers = {
backend = "podman";
containers."3xui_app" = {
# Pinned to v3.8.5 — the last release before the panel added the
# nested `realitySettings.settings` block for new post-quantum
# fields that its own GetXrayConfig then strips on every regenerate.
# Both 3.8.5 and 3.9.0 reproduce the bug; we work around it with
# migrate-3xui-reality.service, which moves the affected fields
# to the top level of `realitySettings` in the DB so they survive
# the panel's delete() of the nested block. The migration runs
# once on every container start, idempotently.
image = "ghcr.io/mhsanaei/3x-ui:v3.8.5";
image = "ghcr.io/mhsanaei/3x-ui:latest";
environment = {
"XRAY_VMESS_AEAD_FORCED" = "false";
"XUI_ENABLE_FAIL2BAN" = "true";
@@ -172,35 +84,10 @@ containers."3xui_app" = {
TimeoutSec = 300;
};
script = ''
podman pull ghcr.io/mhsanaei/3x-ui:v3.8.5
podman pull ghcr.io/mhsanaei/3x-ui:latest
systemctl restart podman-3xui_app.service
'';
};
# Real fix for the panel config-gen bug: run the DB migration once
# after each container start so any new inbounds (created via panel UI
# or API) have their Reality public fields moved to top-level on the
# next launch. The migration is idempotent — a no-op once fields are
# top-level — so it's safe to run on every container start.
#
# The script is piped into the container via stdin rather than
# referenced by its host-side /nix/store path (which does not exist
# inside the container). Restart=on-failure so a transient container
# race (e.g. 3x-ui still seeding the DB on first start) is retried
# instead of silently passing.
"migrate-3xui-reality" = {
path = [ pkgs.podman ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
Restart = "on-failure";
RestartSec = 5;
};
script = ''
${pkgs.podman}/bin/podman exec -i 3xui_app python3 < ${migrateScript}
'';
after = [ "podman-3xui_app.service" ];
wantedBy = [ "podman-compose-3x-ui-root.target" ];
};
};
# Starts/stops together with all 3x-ui compose resources.
targets."podman-compose-3x-ui-root" = {
-21
View File
@@ -225,26 +225,5 @@ in
networking.firewall.allowedTCPPorts = [
80
443
8443
];
# TCP-level proxy for the 3x-ui xray inbound on 8443. nginx doesn't
# unwrap TLS here — `proxy_pass` just relays opaque TCP bytes between
# the client and the xray inside the 3x-ui container. Podman's
# userspace port-forward mangles the Reality ClientHello, so we go
# via nginx stream (same pattern as VDS uses for port 443) instead:
# client → nginx stream :8443 → 127.0.0.1:15380 → podman → xray :8443.
# Reality auth and TLS are preserved end-to-end.
services.nginx.streamConfig = ''
upstream xray_in_8443 {
server 127.0.0.1:15380;
}
server {
listen 8443;
proxy_pass xray_in_8443;
proxy_timeout 600s;
proxy_connect_timeout 5s;
}
'';
}
+5
View File
@@ -68,6 +68,11 @@ in
hashedPasswordFile = config.sops.secrets.hashed_password.path; # hashed_password
homeMode = "700";
home = "/home/${user}";
# Linger keeps `user@<uid>.service` (the systemd user manager) alive
# across logouts, so user services like opencode-web survive when no
# SSH/login session is active. Without this the service is torn down
# together with the user manager on the last session close.
linger = true;
extraGroups = [
"audio"
"disk"