fix(inbounds): refuse 2022-blake3-chacha20-poly1305 on shadowsocks inbounds

xray-core builds a Shadowsocks-2022 inbound with users only for the AES
methods: the multi-user identity header is AES-only, so the config
builder and the multi-user server both reject chacha20. Every panel
inbound carries clients, so picking 2022-blake3-chacha20-poly1305 in the
inbound form kept the whole core from starting; its links also failed
on the client with "multi-key is not supported".

The inbound form no longer offers the method and AddInbound/UpdateInbound
reject it. Startup moves stored inbounds to 2022-blake3-aes-256-gcm,
which takes the same 32-byte keys, so every stored key stays valid.
Outbounds keep the method, since a single-key outbound to such a server
works. The share-link generator drops the single-user branch that only
this method reached.
This commit is contained in:
MHSanaei
2026-10-11 21:20:07 +02:00
parent b9e1d3afee
commit f1ed1a9b17
16 changed files with 203 additions and 45 deletions
+33
View File
@@ -181,6 +181,9 @@ func initModels() error {
if err := migrateShadowsocksRemovedCiphers(); err != nil {
return err
}
if err := migrateShadowsocks2022ChachaInbounds(); err != nil {
return err
}
if err := repairShadowsocks2022ServerKeys(); err != nil {
return err
}
@@ -990,6 +993,36 @@ func migrateShadowsocksRemovedCiphers() error {
return nil
}
// migrateShadowsocks2022ChachaInbounds moves 2022-chacha20 inbounds, which the core
// refuses with users, to aes-256-gcm: same 32-byte keys, so all keys stay valid.
func migrateShadowsocks2022ChachaInbounds() error {
var inbounds []model.Inbound
if err := db.Where("protocol = ?", model.Shadowsocks).Find(&inbounds).Error; err != nil {
return err
}
for _, inbound := range inbounds {
var settings map[string]any
if json.Unmarshal([]byte(inbound.Settings), &settings) != nil {
continue
}
method, _ := settings["method"].(string)
if !xray.Shadowsocks2022NoMultiUser(method) {
continue
}
settings["method"] = "2022-blake3-aes-256-gcm"
newSettings, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
}
if err := db.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
Update("settings", string(newSettings)).Error; err != nil {
return err
}
log.Printf("Inbound %q: switched %s to 2022-blake3-aes-256-gcm; its share links changed", inbound.Tag, method)
}
return nil
}
// repairShadowsocks2022ServerKeys replaces a stored server key xray-core cannot
// parse; that inbound never worked, as the core refuses to start with it.
func repairShadowsocks2022ServerKeys() error {
@@ -67,3 +67,55 @@ func TestRepairShadowsocks2022ServerKeys(t *testing.T) {
})
}
}
// A stored 2022-chacha20 inbound never let xray-core start; aes-256-gcm takes the
// same 32-byte keys, so switching keeps every stored key valid.
func TestMigrateShadowsocks2022ChachaInbounds(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
if err := InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = CloseDB() })
key := base64.StdEncoding.EncodeToString([]byte("0123456789abcdef0123456789abcdef"))
rows := map[string]struct{ settings, wantMethod string }{
"chacha": {`{"method":"2022-blake3-chacha20-poly1305","password":"` + key + `","clients":[{"email":"c","password":"` + key + `"}]}`, "2022-blake3-aes-256-gcm"},
"legacy chacha": {`{"method":"chacha20-ietf-poly1305","clients":[{"email":"l","password":"p"}]}`, "chacha20-ietf-poly1305"},
}
port := 31200
for tag, r := range rows {
port++
ib := model.Inbound{UserId: 1, Port: port, Protocol: model.Shadowsocks, Tag: tag, Settings: r.settings}
if err := GetDB().Create(&ib).Error; err != nil {
t.Fatalf("create %s: %v", tag, err)
}
}
if err := migrateShadowsocks2022ChachaInbounds(); err != nil {
t.Fatalf("migrateShadowsocks2022ChachaInbounds: %v", err)
}
for tag, r := range rows {
var ib model.Inbound
if err := GetDB().Where("tag = ?", tag).First(&ib).Error; err != nil {
t.Fatal(err)
}
var got struct {
Method string `json:"method"`
Password string `json:"password"`
Clients []struct {
Password string `json:"password"`
} `json:"clients"`
}
if err := json.Unmarshal([]byte(ib.Settings), &got); err != nil {
t.Fatal(err)
}
if got.Method != r.wantMethod {
t.Errorf("%s: method %q, want %q", tag, got.Method, r.wantMethod)
}
if tag == "chacha" && (got.Password != key || got.Clients[0].Password != key) {
t.Errorf("%s: keys changed to server=%q client=%q, want both kept", tag, got.Password, got.Clients[0].Password)
}
}
}
+6 -3
View File
@@ -509,9 +509,9 @@ func shadowsocksKeyBytes(method string) int {
return 0
}
// validateShadowsocks2022ServerKey refuses a server key xray-core cannot parse:
// the core then fails to build the inbound and does not start at all.
func validateShadowsocks2022ServerKey(protocol model.Protocol, settings string) error {
// validateShadowsocks2022Inbound refuses a method or server key xray-core cannot
// build an inbound from: the core then does not start at all.
func validateShadowsocks2022Inbound(protocol model.Protocol, settings string) error {
if protocol != model.Shadowsocks {
return nil
}
@@ -526,6 +526,9 @@ func validateShadowsocks2022ServerKey(protocol model.Protocol, settings string)
if n == 0 {
return nil
}
if xray.Shadowsocks2022NoMultiUser(parsed.Method) {
return common.NewErrorf("shadowsocks inbound method %s does not support multiple users, use a 2022-blake3-aes method", parsed.Method)
}
if !xray.ValidShadowsocks2022Key(parsed.Method, parsed.Password) {
return common.NewErrorf("shadowsocks inbound password: %s needs a %d-byte key, base64 encoded", parsed.Method, n)
}
+2 -2
View File
@@ -1178,7 +1178,7 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo
if err := validateFinalMaskSalamander(inbound.StreamSettings); err != nil {
return inbound, false, err
}
if err := validateShadowsocks2022ServerKey(inbound.Protocol, inbound.Settings); err != nil {
if err := validateShadowsocks2022Inbound(inbound.Protocol, inbound.Settings); err != nil {
return inbound, false, err
}
s.normalizeMtprotoSecret(inbound)
@@ -1826,7 +1826,7 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
if err := validateFinalMaskSalamander(inbound.StreamSettings); err != nil {
return inbound, false, err
}
if err := validateShadowsocks2022ServerKey(inbound.Protocol, inbound.Settings); err != nil {
if err := validateShadowsocks2022Inbound(inbound.Protocol, inbound.Settings); err != nil {
return inbound, false, err
}
s.normalizeMtprotoSecret(inbound)
@@ -75,3 +75,50 @@ func TestShadowsocks2022ServerKeyRejected(t *testing.T) {
}
})
}
// xray-core builds a Shadowsocks-2022 inbound with users only for the AES methods;
// a 2022-chacha20 one with clients keeps the whole core from starting.
func TestShadowsocks2022ChachaInboundRejected(t *testing.T) {
const chacha = "2022-blake3-chacha20-poly1305"
key32 := base64.StdEncoding.EncodeToString(make([]byte, 32))
key16 := base64.StdEncoding.EncodeToString(make([]byte, 16))
const wantErr = "shadowsocks inbound method 2022-blake3-chacha20-poly1305 does not support multiple users"
t.Run("add", func(t *testing.T) {
setupConflictDB(t)
nodeID, fake := setupNodeRuntime(t)
ib := &model.Inbound{
UserId: 1, NodeID: &nodeID, Tag: "ss-chacha-add", Enable: true, Port: 41811, Protocol: model.Shadowsocks,
Settings: ss2022Settings(chacha, key32),
}
_, _, err := (&InboundService{}).AddInbound(ib)
if err == nil || !strings.Contains(err.Error(), wantErr) {
t.Fatalf("AddInbound with %s: err = %v", chacha, err)
}
if fake.addInbound.Load() != 0 {
t.Fatal("rejected inbound dispatched to the runtime")
}
})
t.Run("update", func(t *testing.T) {
setupConflictDB(t)
nodeID, fake := setupNodeRuntime(t)
svc := &InboundService{}
ib := &model.Inbound{
UserId: 1, NodeID: &nodeID, Tag: "ss-chacha-update", Enable: true, Port: 41812, Protocol: model.Shadowsocks,
Settings: ss2022Settings("2022-blake3-aes-128-gcm", key16),
}
if _, _, err := svc.AddInbound(ib); err != nil {
t.Fatalf("AddInbound: %v", err)
}
update := *ib
update.Settings = ss2022Settings(chacha, key32)
_, _, err := svc.UpdateInbound(&update)
if err == nil || !strings.Contains(err.Error(), wantErr) {
t.Fatalf("UpdateInbound to %s: err = %v", chacha, err)
}
if fake.updateInbound.Load() != 0 {
t.Fatal("rejected update dispatched to the runtime")
}
})
}
+7
View File
@@ -18,3 +18,10 @@ func ValidShadowsocks2022Key(method, key string) bool {
_, err := shadowsocks_2022.ParseKey(key, Shadowsocks2022KeyBytes(method))
return err == nil
}
// Shadowsocks2022NoMultiUser reports a 2022 method xray-core refuses on an
// inbound with users: the multi-user identity header is AES-only.
func Shadowsocks2022NoMultiUser(method string) bool {
cipher, err := shadowsocks_2022.GetCipherMethod(method)
return err == nil && cipher.IsChaCha
}