소스 검색

Add shadowsocks 2022 relay config

yuhan6665 3 년 전
부모
커밋
b67314796f
1개의 변경된 파일63개의 추가작업 그리고 36개의 파일을 삭제
  1. 63 36
      infra/conf/shadowsocks.go

+ 63 - 36
infra/conf/shadowsocks.go

@@ -32,10 +32,12 @@ func cipherFromString(c string) shadowsocks.CipherType {
 }
 
 type ShadowsocksUserConfig struct {
-	Cipher   string `json:"method"`
-	Password string `json:"password"`
-	Level    byte   `json:"level"`
-	Email    string `json:"email"`
+	Cipher   string   `json:"method"`
+	Password string   `json:"password"`
+	Level    byte     `json:"level"`
+	Email    string   `json:"email"`
+	Address  *Address `json:"address"`
+	Port     uint16   `json:"port"`
 }
 
 type ShadowsocksServerConfig struct {
@@ -50,38 +52,7 @@ type ShadowsocksServerConfig struct {
 
 func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
 	if C.Contains(shadowaead_2022.List, v.Cipher) {
-		if len(v.Users) > 0 {
-			if v.Cipher == "" {
-				return nil, newError("shadowsocks 2022 (multi-user): missing server method")
-			}
-			if !strings.Contains(v.Cipher, "aes") {
-				return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
-			}
-
-			config := new(shadowsocks_2022.MultiUserServerConfig)
-			config.Method = v.Cipher
-
-			config.Key = v.Password
-			config.Network = v.NetworkList.Build()
-
-			for _, user := range v.Users {
-				if user.Cipher != "" {
-					return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
-				}
-				config.Users = append(config.Users, &shadowsocks_2022.User{
-					Key:   user.Password,
-					Email: user.Email,
-				})
-			}
-			return config, nil
-		}
-
-		config := new(shadowsocks_2022.ServerConfig)
-		config.Method = v.Cipher
-		config.Key = v.Password
-		config.Network = v.NetworkList.Build()
-		config.Email = v.Email
-		return config, nil
+		return buildShadowsocks2022(v)
 	}
 
 	config := new(shadowsocks.ServerConfig)
@@ -129,6 +100,62 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
 	return config, nil
 }
 
+func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
+	if len(v.Users) == 0 {
+		config := new(shadowsocks_2022.ServerConfig)
+		config.Method = v.Cipher
+		config.Key = v.Password
+		config.Network = v.NetworkList.Build()
+		config.Email = v.Email
+		return config, nil
+	}
+	
+	if v.Cipher == "" {
+		return nil, newError("shadowsocks 2022 (multi-user): missing server method")
+	}
+	if !strings.Contains(v.Cipher, "aes") {
+		return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
+	}
+
+	if v.Users[0].Address == nil {
+		config := new(shadowsocks_2022.MultiUserServerConfig)
+		config.Method = v.Cipher
+		config.Key = v.Password
+		config.Network = v.NetworkList.Build()
+	
+		for _, user := range v.Users {
+			if user.Cipher != "" {
+				return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
+			}
+			config.Users = append(config.Users, &shadowsocks_2022.User{
+				Key:   user.Password,
+				Email: user.Email,
+			})
+		}
+		return config, nil
+	}
+
+	config := new(shadowsocks_2022.RelayServerConfig)
+	config.Method = v.Cipher
+	config.Key = v.Password
+	config.Network = v.NetworkList.Build()
+	for _, user := range v.Users {
+		if user.Cipher != "" {
+			return nil, newError("shadowsocks 2022 (relay): users must have empty method")
+		}
+		if user.Address == nil {
+			return nil, newError("shadowsocks 2022 (relay): all users must have relay address")
+		}
+		config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
+			Key: user.Password,
+			Email: user.Email,
+			Address: user.Address.Build(),
+			Port: uint32(user.Port),
+		})
+	}
+	return config, nil
+}
+
 type ShadowsocksServerTarget struct {
 	Address  *Address `json:"address"`
 	Port     uint16   `json:"port"`