mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 03:54:47 +03:00
fix(amneziawg): check forwarded ports against the port the panel listens on (#7090)
The web server listens on XUI_PORT when it is set and valid, but the AmneziaWG forwardedPorts conflict check read only the stored webPort. With XUI_PORT set, a forward on the real panel port passed the check and could never bind, while a forward on the unused stored port was refused. The XUI_PORT-over-webPort rule now lives in SettingService.GetListenPort, which both the web server and the conflict check use.
This commit is contained in:
@@ -376,7 +376,7 @@ func (s *InboundService) loadPortConflictContext(db *gorm.DB, nodeID *int) (port
|
||||
ctx.onNode = true
|
||||
q = q.Where("node_id = ?", *nodeID)
|
||||
} else {
|
||||
if webPort, err := (&SettingService{}).GetPort(); err == nil {
|
||||
if webPort, err := (&SettingService{}).GetListenPort(); err == nil {
|
||||
ctx.webPort = webPort
|
||||
}
|
||||
q = q.Where("node_id IS NULL")
|
||||
|
||||
@@ -94,6 +94,21 @@ func TestCheckForwardedPortsConflict_CollidesWithPanelPort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// With XUI_PORT set the panel listens there, not on the stored webPort, so a
|
||||
// forward on that port could never bind.
|
||||
func TestCheckForwardedPortsConflict_CollidesWithXUIPort(t *testing.T) {
|
||||
setupConflictDB(t)
|
||||
t.Setenv("XUI_PORT", "8443")
|
||||
svc := &InboundService{}
|
||||
ctx, err := svc.loadPortConflictContext(database.GetDB(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("loadPortConflictContext: %v", err)
|
||||
}
|
||||
if hit := svc.checkForwardedPortsConflict(ctx, "8443"); !strings.Contains(hit, "panel") {
|
||||
t.Fatalf("expected a collision naming the port XUI_PORT makes the panel listen on, got %q", hit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckForwardedPortsConflict_CollidesWithEnabledInboundPort(t *testing.T) {
|
||||
setupConflictDB(t)
|
||||
seedInboundConflict(t, "vless-8080", "0.0.0.0", 8080, model.VLESS, `{"network":"tcp"}`, `{}`)
|
||||
|
||||
@@ -713,6 +713,15 @@ func (s *SettingService) GetPort() (int, error) {
|
||||
return s.getInt("webPort")
|
||||
}
|
||||
|
||||
// GetListenPort returns the port the panel actually listens on: a valid
|
||||
// XUI_PORT overrides the stored webPort.
|
||||
func (s *SettingService) GetListenPort() (int, error) {
|
||||
if port, configured, err := config.GetPortOverride(); configured && err == nil {
|
||||
return port, nil
|
||||
}
|
||||
return s.GetPort()
|
||||
}
|
||||
|
||||
func (s *SettingService) SetPort(port int) error {
|
||||
return s.setInt("webPort", port)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package service
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetListenPort(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
xuiPort string
|
||||
want int
|
||||
}{
|
||||
// 2053 is the default webPort on a fresh database.
|
||||
{"XUI_PORT unset", "", 2053},
|
||||
{"valid XUI_PORT", "8443", 8443},
|
||||
{"XUI_PORT not a number", "abc", 2053},
|
||||
{"XUI_PORT out of range", "70000", 2053},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
setupConflictDB(t)
|
||||
t.Setenv("XUI_PORT", tc.xuiPort)
|
||||
got, err := (&SettingService{}).GetListenPort()
|
||||
if err != nil {
|
||||
t.Fatalf("GetListenPort: %v", err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Fatalf("GetListenPort() = %d, want %d", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -589,15 +589,14 @@ func (s *Server) start(restartXray bool, startTgBot bool) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
port, err := s.settingService.GetPort()
|
||||
port, err := s.settingService.GetListenPort()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if envPort, configured, envErr := config.GetPortOverride(); configured {
|
||||
if _, configured, envErr := config.GetPortOverride(); configured {
|
||||
if envErr != nil {
|
||||
logger.Warning("Ignoring invalid XUI_PORT; using configured web port:", port, envErr)
|
||||
} else {
|
||||
port = envPort
|
||||
logger.Info("Using XUI_PORT override for web panel port:", port)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user