Skip to content

Commit

Permalink
add link export for vmess
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Sep 4, 2020
1 parent 0e88bb6 commit 9de327f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soveran/redisurl v0.0.0-20180322091936-eb325bc7a4b8/go.mod h1:FVJ8jbHu7QrNFs3bZEsv/L5JjearIAY9N0oXh2wk+6Y=
github.com/spf13/cobra v0.0.2/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
Expand Down
1 change: 1 addition & 0 deletions pkg/proxy/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Proxy interface {
String() string
ToClash() string
ToSurge() string
Link() string
Identifier() string
SetName(name string)
SetIP(ip string)
Expand Down
14 changes: 14 additions & 0 deletions pkg/proxy/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ func TestTrojanLink(t *testing.T) {
}
fmt.Println(trojan)
}

func TestVmessLink(t *testing.T) {
v, err := ParseVmessLink("vmess://ew0KICAidiI6ICIyIiwNCiAgInBzIjogIuW+ruS/oeWFrOS8l+WPtyDlpJrlvannmoTlpKfljYPkuJbnlYwiLA0KICAiYWRkIjogInMyNzEuc25vZGUueHl6IiwNCiAgInBvcnQiOiAiNDQzIiwNCiAgImlkIjogIjZhOTAwZDYzLWNiOTItMzVhMC1hZWYwLTNhMGMxMWFhODUyMyIsDQogICJhaWQiOiAiMSIsDQogICJuZXQiOiAid3MiLA0KICAidHlwZSI6ICJub25lIiwNCiAgImhvc3QiOiAiczI3MS5zbm9kZS54eXoiLA0KICAicGF0aCI6ICIvcGFuZWwiLA0KICAidGxzIjogInRscyINCn0=")
if err != nil {
t.Error(err)
}
fmt.Println(v)
fmt.Println(v.Link())
v, err = ParseVmessLink(v.Link())
if err != nil {
t.Error(err)
}
fmt.Println(v)
}
1 change: 1 addition & 0 deletions pkg/proxy/shadowsocksr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (ssr ShadowsocksR) Clone() Proxy {
return &ssr
}

// https://github.com/HMBSbige/ShadowsocksR-Windows/wiki/SSR-QRcode-scheme
func (ssr ShadowsocksR) Link() (link string) {
payload := fmt.Sprintf("%s:%d:%s:%s:%s:%s",
ssr.Server, ssr.Port, ssr.Protocol, ssr.Cipher, ssr.Obfs, tool.Base64EncodeString(ssr.Password))
Expand Down
38 changes: 34 additions & 4 deletions pkg/proxy/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func (v Vmess) Clone() Proxy {
return &v
}

func (v Vmess) Link() (link string) {
vjv, err := json.Marshal(v.toLinkJson())
if err != nil {
return
}
return fmt.Sprintf("vmess://%s", tool.Base64EncodeBytes(vjv))
}

type vmessLinkJson struct {
Add string `json:"add"`
V string `json:"v"`
Expand All @@ -100,6 +108,27 @@ type vmessLinkJson struct {
Tls string `json:"tls"`
}

func (v Vmess) toLinkJson() vmessLinkJson {
vj := vmessLinkJson{
Add: v.Server,
Ps: v.Name,
Port: v.Port,
Id: v.UUID,
Aid: strconv.Itoa(v.AlterID),
Net: v.Network,
Path: v.WSPath,
Host: v.ServerName,
V: "2",
}
if v.TLS {
vj.Tls = "tls"
}
if host, ok := v.WSHeaders["HOST"]; ok && host != "" {
vj.Host = host
}
return vj
}

func ParseVmessLink(link string) (*Vmess, error) {
if !strings.HasPrefix(link, "vmess") {
return nil, ErrorNotVmessLink
Expand Down Expand Up @@ -190,10 +219,11 @@ func ParseVmessLink(link string) (*Vmess, error) {
}
port := 443
portInterface := vmessJson.Port
if i, ok := portInterface.(int); ok {
port = i
} else if s, ok := portInterface.(string); ok {
port, _ = strconv.Atoi(s)
switch portInterface.(type) {
case int:
port = portInterface.(int)
case string:
port, _ = strconv.Atoi(portInterface.(string))
}

alterId, err := strconv.Atoi(vmessJson.Aid)
Expand Down
4 changes: 4 additions & 0 deletions pkg/tool/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ func Base64DecodeString(src string) (dst string, err error) {
func Base64EncodeString(origin string) (result string) {
return base64.StdEncoding.EncodeToString([]byte(origin))
}

func Base64EncodeBytes(origin []byte) (result string) {
return base64.StdEncoding.EncodeToString([]byte(origin))
}

0 comments on commit 9de327f

Please sign in to comment.