-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshadowsocks.go
140 lines (115 loc) · 3.05 KB
/
shadowsocks.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
package xray
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"strings"
"github.com/fatih/color"
"github.com/lilendian0x00/xray-knife/v2/utils"
"github.com/xtls/xray-core/infra/conf"
)
func NewShadowsocks() Protocol {
return &Shadowsocks{}
}
func (s *Shadowsocks) Name() string {
return "shadowsocks"
}
func (s *Shadowsocks) Parse(configLink string) error {
if !strings.HasPrefix(configLink, ShadowsocksIdentifier) {
return fmt.Errorf("shadowsocks unreconized: %s", configLink)
}
uri, err := url.Parse(configLink)
if err != nil {
return err
}
secondPart := strings.SplitN(configLink[5:], "@", 2)
var decoded []byte
// Encryption part - b64 encoded (EncryptionType : Password)
if len(secondPart) > 1 {
decoded, err = utils.Base64Decode(secondPart[0])
if err != nil {
return errors.New("error when decoding secret part")
}
} else {
return errors.New("invalid config link")
}
//link := "ss://" + string(decoded) + "@" + secondPart[1]
//uri, err := url.Parse(link)
//if err != nil {
// return err
//}
creds := strings.SplitN(string(decoded), ":", 2)
if len(creds) != 2 {
return errors.New("error when decoding secret part")
}
s.Encryption = creds[0] // Encryption Type
s.Password = creds[1] // Encryption Password
//hostPortRemark := strings.SplitN(secondPart[1], ":", 2)
s.Address, s.Port, err = net.SplitHostPort(uri.Host)
if err != nil {
return err
}
if utils.IsIPv6(s.Address) {
s.Address = "[" + s.Address + "]"
}
s.Remark, err = url.PathUnescape(uri.Fragment)
if err != nil {
s.Remark = uri.Fragment
}
//s.Address = hostPortRemark[0]
//
//PortRemark := strings.SplitN(hostPortRemark[1], "#", 2)
//s.Port = PortRemark[0]
//remarkStr, _, _ := strings.Cut(PortRemark[1], "\n")
//s.Remark, err = url.PathUnescape(remarkStr)
//if err != nil {
// s.Remark = remarkStr
//}
s.OrigLink = configLink
return nil
}
func (s *Shadowsocks) DetailsStr() string {
info := fmt.Sprintf("%s: %s\n%s: %s\n%s: %s\n%s: %v\n%s: %s\n%s: %s\n",
color.RedString("Protocol"), s.Name(),
color.RedString("Remark"), s.Remark,
color.RedString("IP"), s.Address,
color.RedString("Port"), s.Port,
color.RedString("Encryption"), s.Encryption,
color.RedString("Password"), s.Password)
return info
}
func (s *Shadowsocks) ConvertToGeneralConfig() GeneralConfig {
var g GeneralConfig
g.Protocol = s.Name()
g.Address = s.Address
g.ID = s.Password
g.Port = s.Port
g.Remark = s.Remark
g.OrigLink = s.OrigLink
return g
}
func (s *Shadowsocks) BuildOutboundDetourConfig(allowInsecure bool) (*conf.OutboundDetourConfig, error) {
out := &conf.OutboundDetourConfig{}
out.Tag = "proxy"
out.Protocol = s.Name()
streamConf := &conf.StreamConfig{}
out.StreamSetting = streamConf
oset := json.RawMessage([]byte(fmt.Sprintf(`{
"servers": [
{
"address": "%s",
"port": %v,
"password": "%s",
"method": "%s",
"uot": true
}
]
}`, s.Address, s.Port, s.Password, s.Encryption)))
out.Settings = &oset
return out, nil
}
func (s *Shadowsocks) BuildInboundDetourConfig() (*conf.InboundDetourConfig, error) {
return nil, nil
}