Skip to content

Commit

Permalink
feat: allow unencoded userinfo as per SIP002
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Jul 15, 2024
1 parent 78384c0 commit adf5a6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 12 additions & 0 deletions x/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ func TestParseShadowsocksSIP002URLSuccessful(t *testing.T) {
require.Equal(t, "example.com:1234", config.serverAddress)
require.Equal(t, "HTTP/1.1 ", string(config.prefix))
}

func TestParseShadowsocksSIP002URLSuccessfulWithoutEncoding(t *testing.T) {
configString := "ss://aes-256-gcm:[email protected]:1234"
urls, err := parseConfig(configString)
require.NoError(t, err)
require.Equal(t, 1, len(urls))

config, err := parseShadowsocksSIP002URL(urls[0])

require.NoError(t, err)
require.Equal(t, "example.com:1234", config.serverAddress)
}
13 changes: 8 additions & 5 deletions x/config/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ func parseShadowsocksSIP002URL(url *url.URL) (*shadowsocksConfig, error) {
return nil, errors.New("host not specified")
}
config.serverAddress = url.Host
cipherInfoBytes, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(url.User.String())
if err != nil {
return nil, fmt.Errorf("failed to decode cipher info [%v]: %w", url.User.String(), err)
}
cipherName, secret, found := strings.Cut(string(cipherInfoBytes), ":")
cipherInfo := url.User.String()
// Cipher info can be optionally encoded with Base64URL.
encoding := base64.URLEncoding.WithPadding(base64.NoPadding)
decodedCipherInfo, err := encoding.DecodeString(cipherInfo)
if err == nil && encoding.EncodeToString(decodedCipherInfo) == cipherInfo {
cipherInfo = string(decodedCipherInfo)
}
cipherName, secret, found := strings.Cut(cipherInfo, ":")
if !found {
return nil, errors.New("invalid cipher info: no ':' separator")
}
Expand Down

0 comments on commit adf5a6a

Please sign in to comment.