Skip to content

Commit

Permalink
fix: parse for the CipherType of Shadowsocks in simplified config (#2510
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AkinoKaede committed Jul 25, 2023
1 parent 580d33a commit d586497
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 84 deletions.
22 changes: 3 additions & 19 deletions proxy/shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"crypto/cipher"
"crypto/md5"
"crypto/sha1"
"encoding/json"
"io"
"strings"

"github.com/golang/protobuf/jsonpb"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"

Expand Down Expand Up @@ -212,27 +210,13 @@ func (NoneCipher) DecodePacket(key []byte, b *buf.Buffer) error {
return nil
}

func (c *CipherType) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
var method string

if err := json.Unmarshal(bytes, &method); err != nil {
return err
}

if *c = CipherFromString(method); *c == CipherType_UNKNOWN {
return newError("unknown cipher method: ", method)
}

return nil
}

func CipherFromString(c string) CipherType {
switch strings.ToLower(c) {
case "aes-128-gcm", "aead_aes_128_gcm":
case "aes-128-gcm", "aes_128_gcm", "aead_aes_128_gcm":
return CipherType_AES_128_GCM
case "aes-256-gcm", "aead_aes_256_gcm":
case "aes-256-gcm", "aes_256_gcm", "aead_aes_256_gcm":
return CipherType_AES_256_GCM
case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
case "chacha20-poly1305", "chacha20_poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
return CipherType_CHACHA20_POLY1305
case "none", "plain":
return CipherType_NONE
Expand Down
21 changes: 19 additions & 2 deletions proxy/shadowsocks/simplified/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,38 @@ package simplified

import (
"context"
"encoding/json"

"github.com/golang/protobuf/jsonpb"

"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
)

func (c *CipherTypeWrapper) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
var method string

if err := json.Unmarshal(bytes, &method); err != nil {
return err
}

if c.Value = shadowsocks.CipherFromString(method); c.Value == shadowsocks.CipherType_UNKNOWN {
return newError("unknown cipher method: ", method)
}

return nil
}

func init() {
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
simplifiedServer := config.(*ServerConfig)
fullServer := &shadowsocks.ServerConfig{
User: &protocol.User{
Account: serial.ToTypedMessage(&shadowsocks.Account{
Password: simplifiedServer.Password,
CipherType: simplifiedServer.Method,
CipherType: simplifiedServer.Method.Value,
}),
},
Network: simplifiedServer.Networks.GetNetwork(),
Expand All @@ -37,7 +54,7 @@ func init() {
{
Account: serial.ToTypedMessage(&shadowsocks.Account{
Password: simplifiedClient.Password,
CipherType: simplifiedClient.Method,
CipherType: simplifiedClient.Method.Value,
ExperimentReducedIvHeadEntropy: simplifiedClient.ExperimentReducedIvHeadEntropy,
}),
},
Expand Down
Loading

0 comments on commit d586497

Please sign in to comment.