forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
113 lines (104 loc) · 2.58 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2016-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"github.com/mdp/qrterminal"
"github.com/txthinking/crypto"
)
func Link(kind, s, username, password string) string {
v := url.Values{}
v.Set(kind, s)
v.Set("username", username)
v.Set("password", password)
s = fmt.Sprintf("brook://%s?%s", kind, v.Encode())
return s
}
func LinkExtra(kind, s, username, password string, v url.Values) string {
v.Set(kind, s)
v.Set("username", username)
v.Set("password", password)
s = fmt.Sprintf("brook://%s?%s", kind, v.Encode())
return s
}
func QR(kind, s, username, password string) {
qrterminal.GenerateHalfBlock(Link(kind, s, username, password), qrterminal.L, os.Stdout)
}
func ParseLink(link string) (kind, s, username, password string, err error) {
var u *url.URL
u, err = url.Parse(link)
if err != nil {
return
}
kind = u.Host
s = u.Query().Get(kind)
username = u.Query().Get("username")
password = u.Query().Get("password")
return
}
func ParseLinkExtra(link string) (kind, s, username, password string, v url.Values, err error) {
var u *url.URL
u, err = url.Parse(link)
if err != nil {
return
}
kind = u.Host
s = u.Query().Get(kind)
username = u.Query().Get("username")
password = u.Query().Get("password")
v = u.Query()
return
}
func ParseLinkOld(link string) (kind, server, username, password string, err error) {
if !strings.HasPrefix(link, "brook://") {
err = errors.New("Invalid brook link")
return
}
s := link[8:]
s, err = crypto.URIUnescape(s)
if err != nil {
return
}
l := strings.Split(s, " ")
if len(l) == 1 {
kind = "socks5"
server = l[0]
return
}
if len(l) == 3 {
kind = "socks5"
server = l[0]
username = l[1]
password = l[2]
return
}
if len(l) == 2 {
kind = "server"
if strings.HasPrefix(l[0], "ws://") {
kind = "wsserver"
}
if strings.HasPrefix(l[0], "wss://") {
kind = "wssserver"
}
server = l[0]
password = l[1]
return
}
err = errors.New("Invalid brook link")
return
}