forked from zu1k/proxypool
/
trojan.go
165 lines (142 loc) · 3.37 KB
/
trojan.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package proxy
import (
"encoding/json"
"errors"
"math/rand"
"net"
"net/url"
"regexp"
"strconv"
"strings"
)
var (
ErrorNotTrojanink = errors.New("not a correct trojan link")
)
type Trojan struct {
Base
Password string `yaml:"password" json:"password"`
ALPN []string `yaml:"alpn,omitempty" json:"alpn,omitempty"`
SNI string `yaml:"sni,omitempty" json:"sni,omitempty"`
SkipCertVerify bool `yaml:"skip-cert-verify,omitempty" json:"skip-cert-verify,omitempty"`
UDP bool `yaml:"udp,omitempty" json:"udp,omitempty"`
}
/**
- name: "trojan"
type: trojan
server: server
port: 443
password: yourpsk
# udp: true
# sni: example.com # aka server name
# alpn:
# - h2
# - http/1.1
# skip-cert-verify: true
*/
func (t Trojan) Identifier() string {
return net.JoinHostPort(t.Server, strconv.Itoa(t.Port)) + t.Password
}
func (t Trojan) String() string {
data, err := json.Marshal(t)
if err != nil {
return ""
}
return string(data)
}
func (t Trojan) ToClash() string {
data, err := json.Marshal(t)
if err != nil {
return ""
}
return "- " + string(data)
}
func (t Trojan) ToSurge() string {
return ""
}
func (t Trojan) Clone() Proxy {
return &t
}
// https://p4gefau1t.github.io/trojan-go/developer/url/
func (t Trojan) Link() (link string) {
query := url.Values{}
if t.SNI != "" {
query.Set("sni", url.QueryEscape(t.SNI))
}
uri := url.URL{
Scheme: "trojan",
User: url.User(url.QueryEscape(t.Password)),
Host: net.JoinHostPort(t.Server, strconv.Itoa(t.Port)),
RawQuery: query.Encode(),
Fragment: t.Name,
}
return uri.String()
}
func ParseTrojanLink(link string) (*Trojan, error) {
if !strings.HasPrefix(link, "trojan://") && !strings.HasPrefix(link, "trojan-go://") {
return nil, ErrorNotTrojanink
}
/**
trojan-go://
$(trojan-password)
@
trojan-host
:
port
/?
sni=$(tls-sni.com)&
type=$(original|ws|h2|h2+ws)&
host=$(websocket-host.com)&
path=$(/websocket/path)&
encryption=$(ss;aes-256-gcm;ss-password)&
plugin=$(...)
#$(descriptive-text)
*/
uri, err := url.Parse(link)
if err != nil {
return nil, ErrorNotSSLink
}
password := uri.User.Username()
password, _ = url.QueryUnescape(password)
server := uri.Hostname()
port, _ := strconv.Atoi(uri.Port())
moreInfos := uri.Query()
sni := moreInfos.Get("sni")
sni, _ = url.QueryUnescape(sni)
transformType := moreInfos.Get("type")
transformType, _ = url.QueryUnescape(transformType)
host := moreInfos.Get("host")
host, _ = url.QueryUnescape(host)
path := moreInfos.Get("path")
path, _ = url.QueryUnescape(path)
alpn := make([]string, 0)
if transformType == "h2" {
alpn = append(alpn, "h2")
}
if port == 0 {
return nil, ErrorNotTrojanink
}
return &Trojan{
Base: Base{
Name: strconv.Itoa(rand.Int()),
Server: server,
Port: port,
Type: "trojan",
},
Password: password,
ALPN: alpn,
UDP: true,
SNI: host,
SkipCertVerify: true,
}, nil
}
var (
trojanPlainRe = regexp.MustCompile("trojan(-go)?://([A-Za-z0-9+/_&?=@:%.-])+")
)
func GrepTrojanLinkFromString(text string) []string {
results := make([]string, 0)
texts := strings.Split(text, "trojan://")
for _, text := range texts {
results = append(results, trojanPlainRe.FindAllString("trojan://"+text, -1)...)
}
return results
}