Skip to content

Commit

Permalink
add support for parsing some shadowsocks links (#3169)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang authored Oct 1, 2024
1 parent 5ff7c71 commit b921dca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions app/subscription/entries/nonnative/definitions/shadowsocks.jsont
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
{{if assertExists . "root_!kind" | not}} Unknown environment {{end}}
{{if assertIsOneOf . "root_!kind" "json" | not}} This template only works for json input. {{end}}
{{if assertIsOneOf . "root_!kind" "json" "link" | not}} This template only works for json input. {{end}}

{{ $methodName := tryGet . "root_!json_method_!unquoted" "root_!json_protocol_!unquoted" "root_!json_cipher_!unquoted"}}
{{ $type := tryGet . "root_!kind"}}

{{ $methodName := tryGet . "root_!json_method_!unquoted" "root_!json_protocol_!unquoted" "root_!json_cipher_!unquoted" "root_!link_userinfo_!value_!rawContent"}}
{{ if eq $type "link" }}
{{ $methodName = splitAndGetNth ":" 0 $methodName}}
{{end}}
{{if assertValueIsOneOf $methodName "chacha20-ietf-poly1305" "chacha20-poly1305" "aes-128-gcm" "aes-256-gcm" | not}}
This template only works for ss. {{end}}

{{ $server_address := tryGet . "root_!json_server" "root_!json_address" "root_!json_endpoint"}}
{{ $server_port := tryGet . "root_!json_port" "root_!json_server_port" "root_!json_endpoint"}}
{{if $server_address | splitAndGetAfterNth ":" 0 | len | gt 1}}
{{ $server_addressport_unquoted := tryGet . "root_!json_endpoint_!unquoted"}}
{{ $server_port = $server_addressport_unquoted | splitAndGetAfterNth ":" -1}}
{{ $server_address := tryGet . "root_!json_server" "root_!json_address" "root_!json_endpoint" "root_!link_host"}}
{{ $server_port := tryGet . "root_!json_port" "root_!json_server_port" "root_!json_endpoint" "root_!link_host"}}
{{if $server_address | splitAndGetAfterNth ":" 0 | len | lt 1}}
{{ $server_addressport_unquoted := tryGet . "root_!json_endpoint_!unquoted" "root_!link_host"}}
{{ $server_port = $server_addressport_unquoted | splitAndGetNth ":" -1}}

{{ $server_portWithSep := printf ":%v" $server_port}}
{{ $server_address = $server_addressport_unquoted | stringCutSuffix $server_portWithSep | jsonEncode}}
{{end}}

{{ $name_annotation := tryGet . "root_!json_name_!unquoted" "root_!json_id_!unquoted" "root_!json_tag_!unquoted" "root_!json_remarks_!unquoted" "<default>"}}
{{ $name_annotation := tryGet . "root_!json_name_!unquoted" "root_!json_id_!unquoted" "root_!json_tag_!unquoted" "root_!json_remarks_!unquoted" "root_!link_fragment" "<default>"}}

{{$password := tryGet . "root_!json_password" "root_!json_psk"}}
{{$password := tryGet . "root_!json_password" "root_!json_psk" "root_!link_userinfo_!value_!rawContent"}}
{{ if eq $type "link" }}
{{ $password = splitAndGetNth ":" 1 $password | jsonEncode}}
{{end}}

{
"protocol": "shadowsocks",
Expand Down
3 changes: 3 additions & 0 deletions app/subscription/entries/nonnative/nonnative.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (a *AbstractNonNativeLink) extractValue(content, prefix string) {
decoded, err := base64.RawStdEncoding.DecodeString(content)
if err == nil {
a.Values[prefix+"_!kind"] = "base64"
a.Values[prefix+"_!rawContent"] = string(decoded)
a.extractValue(string(decoded), prefix+"_!base64")
return
}
Expand All @@ -63,6 +64,7 @@ func (a *AbstractNonNativeLink) extractValue(content, prefix string) {
decoded, err := base64.RawURLEncoding.DecodeString(content)
if err == nil {
a.Values[prefix+"_!kind"] = "base64url"
a.Values[prefix+"_!rawContent"] = string(decoded)
a.extractValue(string(decoded), prefix+"_!base64")
return
}
Expand All @@ -87,6 +89,7 @@ func (a *AbstractNonNativeLink) extractLink(content *url.URL, prefix string) {
a.Values[prefix+"_!link_query"] = content.RawQuery
a.Values[prefix+"_!link_fragment"] = content.Fragment
a.Values[prefix+"_!link_userinfo"] = content.User.String()
a.extractValue(content.User.String(), prefix+"_!link_userinfo_!value")
a.Values[prefix+"_!link_opaque"] = content.Opaque
}

Expand Down

0 comments on commit b921dca

Please sign in to comment.