Files
3x-ui/internal/web/job/amneziawg_job.go
T
MHSanaei 75f3702dd3 fix(amneziawg): fall back to a free egress port when 64900 is refused
The panel's SOCKS5 egress for AmneziaWG outbounds bound the fixed
127.0.0.1:64900, and every generated socks bridge dialed that constant.
64900 sits inside Windows' dynamic port range, where the OS can reserve
whole blocks (this host excludes 64885-64984), so on the Windows builds
release.yml ships the listener could stay down and every AmneziaWG
outbound with it. Listen now tries 64900 first and falls back to any
free loopback port; bridges, the outbound probe and the port-conflict
check use EgressPort(), the port actually held. Bridges are generated
apart from the listener, so BuildSocksBridge records the port it wrote
and the AmneziaWG job requests an Xray restart while the listener holds
a different one. Where 64900 is free nothing changes.

The job's restart request is two lines of wiring no test reaches; the
staleness it acts on is pinned by
TestBridgesStaleUntilRegeneratedForTheBoundPort.
2026-09-27 16:09:54 +02:00

117 lines
3.8 KiB
Go

package job
import (
"encoding/json"
"github.com/mhsanaei/3x-ui/v3/internal/amneziawg"
"github.com/mhsanaei/3x-ui/v3/internal/amneziawgnet"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
// AmneziaWGJob converges embedded AmneziaWG interfaces (inbounds AND the
// template's "amneziawg" outbounds) every 10s; stats stay with Xray.
type AmneziaWGJob struct {
inboundService service.InboundService
settingService service.SettingService
xrayService service.XrayService
}
// NewAmneziaWGJob creates a new AmneziaWG reconcile job instance.
func NewAmneziaWGJob() *AmneziaWGJob {
return new(AmneziaWGJob)
}
// Run reconciles desired AmneziaWG inbounds with running embedded interfaces.
func (j *AmneziaWGJob) Run() {
desired, err := j.inboundService.DesiredAmneziaWGInstances()
if err != nil {
logger.Warning("amneziawg job: get desired instances failed:", err)
return
}
wanted := make([]amneziawgnet.Desired, 0, len(desired))
for _, inst := range desired {
wanted = append(wanted, amneziawgnet.Desired{
Instance: inst,
Options: amneziawgnet.DeviceOptions{
HeaderProtectionKey: inst.Obfuscation.HeaderProtectionKey,
ContentPaddingAddition: inst.Obfuscation.ContentPaddingAddition,
RekeyAfterTime: inst.Obfuscation.RekeyAfterTime,
RekeyTimeout: inst.Obfuscation.RekeyTimeout,
RejectAfterTime: inst.Obfuscation.RejectAfterTime,
KeepaliveTimeout: inst.Obfuscation.KeepaliveTimeout,
MaxHandshakeAttempts: inst.Obfuscation.MaxHandshakeAttempts,
RandomTrailers: inst.Obfuscation.RandomTrailers,
DisableCookies: inst.Obfuscation.DisableCookies,
},
})
}
amneziawgnet.GetManager().Reconcile(wanted)
outboundDesired, err := j.desiredOutboundInstances()
if err != nil {
logger.Warning("amneziawg job: get desired outbound instances failed:", err)
return
}
amneziawgnet.GetOutboundManager().Reconcile(outboundDesired)
// Xray's bridges are generated apart from the listener; one that moved needs them regenerated.
if amneziawgnet.BridgesStale() {
j.xrayService.SetToNeedRestart()
}
}
// desiredOutboundInstances derives client instances per template "amneziawg" outbound.
func (j *AmneziaWGJob) desiredOutboundInstances() ([]amneziawgnet.OutboundDesired, error) {
template, err := j.settingService.GetXrayConfigTemplate()
if err != nil {
return nil, err
}
if template == "" {
return nil, nil
}
cfg := &xray.Config{}
if err := json.Unmarshal([]byte(template), cfg); err != nil {
return nil, err
}
if len(cfg.OutboundConfigs) == 0 {
return nil, nil
}
var raws []json.RawMessage
if err := json.Unmarshal(cfg.OutboundConfigs, &raws); err != nil {
return nil, err
}
out := make([]amneziawgnet.OutboundDesired, 0, len(raws))
for _, raw := range raws {
if !amneziawg.IsAmneziaWGOutbound(raw) {
continue
}
var probe struct {
Tag string `json:"tag"`
}
if err := json.Unmarshal(raw, &probe); err != nil || probe.Tag == "" {
continue
}
inst, ok := amneziawg.InstanceFromOutbound(probe.Tag, raw)
if !ok {
continue
}
out = append(out, amneziawgnet.OutboundDesired{
Instance: inst,
Options: amneziawgnet.DeviceOptions{
HeaderProtectionKey: inst.Obfuscation.HeaderProtectionKey,
ContentPaddingAddition: inst.Obfuscation.ContentPaddingAddition,
RekeyAfterTime: inst.Obfuscation.RekeyAfterTime,
RekeyTimeout: inst.Obfuscation.RekeyTimeout,
RejectAfterTime: inst.Obfuscation.RejectAfterTime,
KeepaliveTimeout: inst.Obfuscation.KeepaliveTimeout,
MaxHandshakeAttempts: inst.Obfuscation.MaxHandshakeAttempts,
RandomTrailers: inst.Obfuscation.RandomTrailers,
DisableCookies: inst.Obfuscation.DisableCookies,
},
})
}
return out, nil
}