Skip to content

Commit

Permalink
feat: 支持根据订阅链接读取 vmess 格式的配置
Browse files Browse the repository at this point in the history
close #2

(cherry picked from commit 26b6448)
  • Loading branch information
Bpazy committed Aug 24, 2021
1 parent 25d19ca commit 082a0bd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
12 changes: 10 additions & 2 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,22 @@ func parseLinks(uris []string) []*Link {
SsCfg: cfg,
})
case protocol.Vmess:

cfg, err := protocol.ParseVmessUri(uri)
if err != nil {
log.Warn("illegal shadowsocks uri schema: " + uri)
continue
}
links = append(links, &Link{
VmessCfg: cfg,
})
}
}
return links
}

type Link struct {
SsCfg *protocol.ShadowsocksConfig
SsCfg *protocol.ShadowsocksConfig
VmessCfg *protocol.VmessConfig
}

// build xray-core config
Expand Down
6 changes: 3 additions & 3 deletions xray/protocol/shadowsocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestParseShadowsocksUri(t *testing.T) {
wantErr bool
}{
{
name: "without comment",
name: "shadowsocks without comment",
args: args{"ss://YWVzLTI1Ni1nY206dGVzdHBhc3N3b3Jk@127.0.0.1:51507"},
want: &ShadowsocksConfig{
Method: "aes-256-gcm",
Expand All @@ -26,7 +26,7 @@ func TestParseShadowsocksUri(t *testing.T) {
},
wantErr: false,
}, {
name: "with comment",
name: "shadowsocks with comment",
args: args{"ss://YWVzLTI1Ni1nY206dGVzdHBhc3N3b3Jk@127.0.0.1:51507#test%20comment"},
want: &ShadowsocksConfig{
Method: "aes-256-gcm",
Expand All @@ -37,7 +37,7 @@ func TestParseShadowsocksUri(t *testing.T) {
},
wantErr: false,
}, {
name: "incorrect",
name: "shadowsocks incorrect",
args: args{"ss://illegalbase64@127.0.0.1:51507"},
wantErr: true,
},
Expand Down
40 changes: 40 additions & 0 deletions xray/protocol/vmess.go
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
}
50 changes: 50 additions & 0 deletions xray/protocol/vmess_test.go
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)
}
})
}
}

0 comments on commit 082a0bd

Please sign in to comment.