Skip to content

Commit 022a4c7

Browse files
committed
Refactored
1 parent d75cca0 commit 022a4c7

8 files changed

+150
-52
lines changed

xray/protocols.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
package xray
22

3-
import "github.com/xtls/xray-core/infra/conf"
3+
import (
4+
"errors"
5+
"github.com/xtls/xray-core/infra/conf"
6+
"strings"
7+
)
8+
9+
const (
10+
vmessIdentifier = "vmess://"
11+
vlessIdentifier = "vless://"
12+
trojanIdentifier = "trojan://"
13+
ShadowsocksIdentifier = "ss://"
14+
wireguardIdentifier = "wireguard://"
15+
socksIdentifier = "socks://"
16+
)
417

518
type Protocol interface {
619
Parse(configLink string) error
@@ -10,6 +23,25 @@ type Protocol interface {
1023
ConvertToGeneralConfig() GeneralConfig
1124
}
1225

26+
func CreateProtocol(configLink string) (Protocol, error) {
27+
switch {
28+
case strings.HasPrefix(configLink, vmessIdentifier):
29+
return NewVmess(), nil
30+
case strings.HasPrefix(configLink, vlessIdentifier):
31+
return NewVless(), nil
32+
case strings.HasPrefix(configLink, ShadowsocksIdentifier):
33+
return NewShadowsocks(), nil
34+
case strings.HasPrefix(configLink, trojanIdentifier):
35+
return NewTrojan(), nil
36+
case strings.HasPrefix(configLink, socksIdentifier):
37+
return NewSocks(), nil
38+
case strings.HasPrefix(configLink, wireguardIdentifier):
39+
return NewWireguard(), nil
40+
default:
41+
return nil, errors.New("invalid protocol type")
42+
}
43+
}
44+
1345
type GeneralConfig struct {
1446
Protocol string
1547
Address string
@@ -106,7 +138,13 @@ type Trojan struct {
106138
Remark string // Config's name
107139
ServiceName string `json:"serviceName"` // GRPC
108140
Mode string `json:"mode"` // GRPC
109-
OrigLink string `json:"-"` // Original link
141+
142+
// Yes, Trojan can have reality too xD
143+
PublicKey string `json:"pbk"`
144+
ShortIds string `json:"sid"` // Mandatory, the shortId list available to the client, which can be used to distinguish different clients
145+
SpiderX string `json:"spx"` // Reality path
146+
147+
OrigLink string `json:"-"` // Original link
110148
}
111149

112150
type Wireguard struct {

xray/shadowsocks.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ import (
1313
"github.com/xtls/xray-core/infra/conf"
1414
)
1515

16+
func NewShadowsocks() Protocol {
17+
return &Shadowsocks{}
18+
}
19+
20+
func (s *Shadowsocks) Name() string {
21+
return "shadowsocks"
22+
}
23+
1624
func (s *Shadowsocks) Parse(configLink string) error {
17-
if !strings.HasPrefix(configLink, "ss://") {
25+
if !strings.HasPrefix(configLink, ShadowsocksIdentifier) {
1826
return fmt.Errorf("shadowsocks unreconized: %s", configLink)
1927
}
2028

@@ -78,8 +86,8 @@ func (s *Shadowsocks) Parse(configLink string) error {
7886
}
7987

8088
func (s *Shadowsocks) DetailsStr() string {
81-
info := fmt.Sprintf("%s: Shadowsocks\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
82-
color.RedString("Protocol"),
89+
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
90+
color.RedString("Protocol"), s.Name(),
8391
color.RedString("Remark"), s.Remark,
8492
color.RedString("IP"), s.Address,
8593
color.RedString("Port"), s.Port,
@@ -90,7 +98,7 @@ func (s *Shadowsocks) DetailsStr() string {
9098

9199
func (s *Shadowsocks) ConvertToGeneralConfig() GeneralConfig {
92100
var g GeneralConfig
93-
g.Protocol = "Shadowsocks"
101+
g.Protocol = s.Name()
94102
g.Address = s.Address
95103
g.ID = s.Password
96104
g.Port = s.Port
@@ -103,7 +111,7 @@ func (s *Shadowsocks) ConvertToGeneralConfig() GeneralConfig {
103111
func (s *Shadowsocks) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDetourConfig, error) {
104112
out := &conf.OutboundDetourConfig{}
105113
out.Tag = "proxy"
106-
out.Protocol = "shadowsocks"
114+
out.Protocol = s.Name()
107115

108116
streamConf := &conf.StreamConfig{}
109117

xray/socks.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ import (
1515
"github.com/xtls/xray-core/infra/conf"
1616
)
1717

18+
func NewSocks() Protocol {
19+
return &Socks{}
20+
}
21+
22+
func (s *Socks) Name() string {
23+
return "socks"
24+
}
25+
1826
func (s *Socks) Parse(configLink string) error {
19-
if !strings.HasPrefix(configLink, "socks://") {
27+
if !strings.HasPrefix(configLink, socksIdentifier) {
2028
return fmt.Errorf("socks unreconized: %s", configLink)
2129
}
2230

@@ -47,8 +55,8 @@ func (s *Socks) Parse(configLink string) error {
4755
func (s *Socks) DetailsStr() string {
4856
copyV := *s
4957

50-
info := fmt.Sprintf("%s: Socks\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n",
51-
color.RedString("Protocol"),
58+
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n",
59+
color.RedString("Protocol"), s.Name(),
5260
color.RedString("Remark"), copyV.Remark,
5361
color.RedString("Network"), "tcp",
5462
color.RedString("Address"), copyV.Address,
@@ -66,7 +74,7 @@ func (s *Socks) DetailsStr() string {
6674

6775
func (s *Socks) ConvertToGeneralConfig() GeneralConfig {
6876
var g GeneralConfig
69-
g.Protocol = "socks"
77+
g.Protocol = s.Name()
7078
g.Address = s.Address
7179
g.Port = fmt.Sprintf("%v", s.Port)
7280
g.Remark = s.Remark
@@ -112,8 +120,8 @@ func (s *Socks) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDet
112120
func (s *Socks) BuildInboundDetourConfig() (*conf.InboundDetourConfig, error) {
113121
p := conf.TransportProtocol("tcp")
114122
in := &conf.InboundDetourConfig{
115-
Protocol: "socks",
116-
Tag: "socks",
123+
Protocol: s.Name(),
124+
Tag: s.Name(),
117125
Settings: nil,
118126
StreamSetting: &conf.StreamConfig{
119127
Network: &p,

xray/trojan.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import (
1212
"strings"
1313
)
1414

15+
func NewTrojan() Protocol {
16+
return &Trojan{}
17+
}
18+
19+
func (t *Trojan) Name() string {
20+
return "trojan"
21+
}
22+
1523
func (t *Trojan) Parse(configLink string) error {
16-
if !strings.HasPrefix(configLink, "trojan://") {
24+
if !strings.HasPrefix(configLink, trojanIdentifier) {
1725
return fmt.Errorf("trojan unreconized: %s", configLink)
1826
}
1927
uri, err := url.Parse(configLink)
@@ -87,8 +95,8 @@ func (t *Trojan) DetailsStr() string {
8795
if copyV.Flow == "" || copyV.Type == "grpc" {
8896
copyV.Flow = "none"
8997
}
90-
info := fmt.Sprintf("%s: Trojan\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
91-
color.RedString("Protocol"),
98+
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
99+
color.RedString("Protocol"), t.Name(),
92100
color.RedString("Remark"), t.Remark,
93101
color.RedString("Network"), t.Type,
94102
color.RedString("Address"), t.Address,
@@ -112,7 +120,19 @@ func (t *Trojan) DetailsStr() string {
112120
info += fmt.Sprintf("%s: %s\n", color.RedString("ServiceName"), copyV.ServiceName)
113121
}
114122

115-
if copyV.Security == "tls" {
123+
if copyV.Security == "reality" {
124+
info += fmt.Sprintf("%s: reality\n", color.RedString("TLS"))
125+
if copyV.SpiderX == "" {
126+
copyV.SpiderX = "none"
127+
}
128+
info += fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %s\n",
129+
color.RedString("Public key"), copyV.PublicKey,
130+
color.RedString("SNI"), copyV.SNI,
131+
color.RedString("ShortID"), copyV.ShortIds,
132+
color.RedString("SpiderX"), copyV.SpiderX,
133+
color.RedString("Fingerprint"), copyV.TlsFingerprint,
134+
)
135+
} else if copyV.Security == "tls" {
116136
info += fmt.Sprintf("%s: tls\n", color.RedString("TLS"))
117137
if len(copyV.SNI) == 0 {
118138
if copyV.Host != "" {
@@ -139,7 +159,7 @@ func (t *Trojan) DetailsStr() string {
139159

140160
func (t *Trojan) ConvertToGeneralConfig() GeneralConfig {
141161
var g GeneralConfig
142-
g.Protocol = "trojan"
162+
g.Protocol = t.Name()
143163
g.Address = t.Address
144164
g.Host = t.Host
145165
g.ID = t.Password
@@ -165,7 +185,7 @@ func (t *Trojan) ConvertToGeneralConfig() GeneralConfig {
165185
func (t *Trojan) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDetourConfig, error) {
166186
out := &conf.OutboundDetourConfig{}
167187
out.Tag = "proxy"
168-
out.Protocol = "trojan"
188+
out.Protocol = t.Name()
169189

170190
p := conf.TransportProtocol(t.Type)
171191
s := &conf.StreamConfig{
@@ -280,6 +300,15 @@ func (t *Trojan) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDe
280300
if t.ALPN != "" {
281301
s.TLSSettings.ALPN = &conf.StringList{t.ALPN}
282302
}
303+
} else if t.Security == "reality" {
304+
s.REALITYSettings = &conf.REALITYConfig{
305+
Show: false,
306+
Fingerprint: t.TlsFingerprint,
307+
ServerName: t.SNI,
308+
PublicKey: t.PublicKey,
309+
ShortId: t.ShortIds,
310+
SpiderX: t.SpiderX,
311+
}
283312
}
284313

285314
out.StreamSetting = s
@@ -299,6 +328,6 @@ func (t *Trojan) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDe
299328
return out, nil
300329
}
301330

302-
func (v *Trojan) BuildInboundDetourConfig() (*conf.InboundDetourConfig, error) {
331+
func (t *Trojan) BuildInboundDetourConfig() (*conf.InboundDetourConfig, error) {
303332
return nil, nil
304333
}

xray/vless.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import (
1212
"strings"
1313
)
1414

15+
func NewVless() Protocol {
16+
return &Vless{}
17+
}
18+
19+
func (v *Vless) Name() string {
20+
return "vless"
21+
}
22+
1523
func (v *Vless) Parse(configLink string) error {
16-
if !strings.HasPrefix(configLink, "vless://") {
24+
if !strings.HasPrefix(configLink, vlessIdentifier) {
1725
return fmt.Errorf("vless unreconized: %s", configLink)
1826
}
1927

@@ -86,8 +94,8 @@ func (v *Vless) DetailsStr() string {
8694
if copyV.Flow == "" || copyV.Type == "grpc" {
8795
copyV.Flow = "none"
8896
}
89-
info := fmt.Sprintf("%s: Vless\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
90-
color.RedString("Protocol"),
97+
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
98+
color.RedString("Protocol"), v.Name(),
9199
color.RedString("Remark"), v.Remark,
92100
color.RedString("Network"), v.Type,
93101
color.RedString("Address"), v.Address,
@@ -148,7 +156,7 @@ func (v *Vless) DetailsStr() string {
148156

149157
func (v *Vless) ConvertToGeneralConfig() GeneralConfig {
150158
var g GeneralConfig
151-
g.Protocol = "vless"
159+
g.Protocol = v.Name()
152160
g.Address = v.Address
153161
g.Host = v.Host
154162
g.ID = v.ID
@@ -174,7 +182,7 @@ func (v *Vless) ConvertToGeneralConfig() GeneralConfig {
174182
func (v *Vless) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDetourConfig, error) {
175183
out := &conf.OutboundDetourConfig{}
176184
out.Tag = "proxy"
177-
out.Protocol = "vless"
185+
out.Protocol = v.Name()
178186

179187
p := conf.TransportProtocol(v.Type)
180188
s := &conf.StreamConfig{

xray/vmess.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"github.com/xtls/xray-core/infra/conf"
1313
)
1414

15+
func NewVmess() Protocol {
16+
return &Vmess{}
17+
}
18+
19+
func (v *Vmess) Name() string {
20+
return "vmess"
21+
}
22+
1523
func method1(v *Vmess, link string) error {
1624
b64encoded := link[8:]
1725
decoded, err := utils.Base64Decode(b64encoded)
@@ -37,7 +45,7 @@ func method2(v *Vmess, link string) error {
3745
if err != nil {
3846
return err
3947
}
40-
link = "vmess://" + string(decoded) + "?" + uri.RawQuery
48+
link = vmessIdentifier + string(decoded) + "?" + uri.RawQuery
4149

4250
uri, err = url.Parse(link)
4351
if err != nil {
@@ -100,7 +108,7 @@ func method2(v *Vmess, link string) error {
100108
}
101109

102110
func (v *Vmess) Parse(configLink string) error {
103-
if !strings.HasPrefix(configLink, "vmess://") {
111+
if !strings.HasPrefix(configLink, vmessIdentifier) {
104112
return fmt.Errorf("vmess unreconized: %s", configLink)
105113
}
106114

@@ -125,8 +133,8 @@ func (v *Vmess) Parse(configLink string) error {
125133

126134
func (v *Vmess) DetailsStr() string {
127135
copyV := *v
128-
info := fmt.Sprintf("%s: Vmess\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n",
129-
color.RedString("Protocol"),
136+
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n",
137+
color.RedString("Protocol"), v.Name(),
130138
color.RedString("Remark"), copyV.Remark,
131139
color.RedString("Network"), copyV.Network,
132140
color.RedString("Address"), copyV.Address,
@@ -184,7 +192,7 @@ func (v *Vmess) DetailsStr() string {
184192

185193
func (v *Vmess) ConvertToGeneralConfig() GeneralConfig {
186194
var g GeneralConfig
187-
g.Protocol = "vmess"
195+
g.Protocol = v.Name()
188196
g.Address = v.Address
189197
g.Aid = fmt.Sprintf("%v", v.Aid)
190198
g.Host = v.Host
@@ -211,7 +219,7 @@ func (v *Vmess) ConvertToGeneralConfig() GeneralConfig {
211219
func (v *Vmess) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDetourConfig, error) {
212220
out := &conf.OutboundDetourConfig{}
213221
out.Tag = "proxy"
214-
out.Protocol = "vmess"
222+
out.Protocol = v.Name()
215223

216224
p := conf.TransportProtocol(v.Network)
217225
s := &conf.StreamConfig{

0 commit comments

Comments
 (0)