-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshadowsocks.go
More file actions
107 lines (102 loc) · 2.56 KB
/
shadowsocks.go
File metadata and controls
107 lines (102 loc) · 2.56 KB
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
package main
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
)
type XrSsServers struct {
SsServers []SsServerConf `json:"servers"`
}
type SsServerConf struct {
Address string `json:"address"`
Port int `json:"port"`
Method string `json:"method"`
Password string `json:"password"`
UoT bool `json:"uot,omitempty"`
}
func decodeSsServerConfig(str string) bool {
var datastr string
index := strings.IndexByte(str, '@')
if index == -1 { // fully encoded string
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
fmt.Println("error:", err)
return false
}
datastr = string(data[:])
} else { // encoded only method:password
shortstr := str[:index]
data, err := base64.StdEncoding.DecodeString(shortstr)
if err != nil {
fmt.Println("error:", err)
return false
}
datastr = string(data[:]) + str[index:]
}
errstr := createSsServerConfig(datastr)
if errstr != "" {
fmt.Println(errstr)
return false
}
return true
}
func createSsServerConfig(str string) (errstr string) {
var index int
ind := strings.IndexByte(str, '@')
if ind == -1 {
errString := "Invalid format of string " + str
return errString //, 1
} else {
mpstr := str[:ind]
conf := new(SsServerConf)
index = strings.IndexByte(mpstr, ':')
if index == -1 {
errString := "Invalid format of string " + mpstr
return errString //, 2
} else {
conf.Method = mpstr[:index]
conf.Password = mpstr[index+1:]
}
spstr := str[ind+1:]
// find '?'
indx := strings.IndexByte(spstr, '/')
if indx != -1 {
spstr = spstr[:indx]
} else {
indx := strings.IndexByte(spstr, '?')
if indx != -1 {
spstr = spstr[:indx]
}
}
index = strings.IndexByte(spstr, ':')
if index == -1 {
errString := "SS Invalid format of string " + spstr
return errString //, 3
} else {
conf.Address = spstr[:index]
//check ip
if !isIpValid(config.IpCheckServer, conf.Address, config.IpCheckKey,
config.IpCheckValue, config.IpCheckBlackList) {
return "SS Ip is invalid"
}
//
i, err := strconv.Atoi(spstr[index+1:])
if err != nil {
errString := "SS Invalid format of port " + spstr
return errString //, 4
}
conf.Port = i
}
xrconf := new(XrayConf)
xrconf.Protocol = "shadowsocks"
servers := new(XrSsServers)
servers.SsServers = append(servers.SsServers, *conf)
xrconf.Settings = servers
confToSave++
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //config.SsTag + strconv.Itoa(len(xrSsConfigs)+1)
xrSsConfigs = append(xrSsConfigs, *xrconf)
ssConfToSave = ssConfToSave + 1
}
return "" //, 0
}