-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package protocol | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"regexp" | ||
) | ||
|
||
type VmessConfig struct { | ||
V string `json:"v"` | ||
Ps string `json:"ps"` | ||
Add string `json:"add"` | ||
Port string `json:"port"` | ||
Id string `json:"id"` | ||
Aid string `json:"aid"` | ||
Scy string `json:"scy"` | ||
Net string `json:"net"` | ||
Type string `json:"type"` | ||
Host string `json:"host"` | ||
Path string `json:"path"` | ||
Tls string `json:"tls"` | ||
Sni string `json:"sni"` | ||
} | ||
|
||
var vmessUriRe = regexp.MustCompile(`(?m)vmess://(.+)`) | ||
|
||
func ParseVmessUri(uri string) (*VmessConfig, error) { | ||
sm := vmessUriRe.FindStringSubmatch(uri) | ||
cfgBytes, err := base64.StdEncoding.DecodeString(sm[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cfg := new(VmessConfig) | ||
if err = json.Unmarshal(cfgBytes, cfg); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package protocol | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseVmessUri(t *testing.T) { | ||
type args struct { | ||
uri string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *VmessConfig | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "vmess correct", | ||
args: args{"vmess://eyJ2IjoiMiIsInBzIjoiIOWkh+azqOaIluWIq+WQjSAgIiwiYWRkIjoiMTExLjExMS4xMTEuMTExIiwicG9ydCI6IjMyMDAwIiwiaWQiOiIxMzg2Zjg1ZS02NTdiLTRkNmUtOWQ1Ni03OGJhZGI3NWUxZmQiLCJhaWQiOiIxMDAiLCJzY3kiOiJ6ZXJvIiwibmV0IjoidGNwIiwidHlwZSI6Im5vbmUiLCJob3N0Ijoid3d3LmJiYi5jb20iLCJwYXRoIjoiLyIsInRscyI6InRscyIsInNuaSI6Ind3dy5jY2MuY29tIn0="}, | ||
want: &VmessConfig{ | ||
V: "2", | ||
Ps: " 备注或别名 ", | ||
Add: "111.111.111.111", | ||
Port: "32000", | ||
Id: "1386f85e-657b-4d6e-9d56-78badb75e1fd", | ||
Aid: "100", | ||
Scy: "zero", | ||
Net: "tcp", | ||
Type: "none", | ||
Host: "www.bbb.com", | ||
Path: "/", | ||
Tls: "tls", | ||
Sni: "www.ccc.com", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ParseVmessUri(tt.args.uri) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ParseVmessUri() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ParseVmessUri() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |