Skip to content

Commit 4d65c97

Browse files
authored
Added support of vmess and trojan keys.
Now support vmess and trojan keys. Changed code structer
1 parent a2b13b5 commit 4d65c97

File tree

5 files changed

+568
-231
lines changed

5 files changed

+568
-231
lines changed

shadowsocks.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type XrSsServers struct {
11+
SsServers []SsServerConf `json:"servers"`
12+
}
13+
14+
type SsServerConf struct {
15+
Address string `json:"address"`
16+
Port int `json:"port"`
17+
Method string `json:"method"`
18+
Password string `json:"password"`
19+
UoT bool `json:"uot,omitempty"`
20+
}
21+
22+
func decodeSsServerConfig(str string) {
23+
var datastr string
24+
index := strings.IndexByte(str, '@')
25+
if index == -1 { // fully encoded string
26+
data, err := base64.StdEncoding.DecodeString(str)
27+
if err != nil {
28+
fmt.Println("error:", err)
29+
return
30+
}
31+
datastr = string(data[:])
32+
} else { // encoded only method:password
33+
shortstr := str[:index]
34+
data, err := base64.StdEncoding.DecodeString(shortstr)
35+
if err != nil {
36+
fmt.Println("error:", err)
37+
return
38+
}
39+
datastr = string(data[:]) + str[index:]
40+
}
41+
errstr := createSsServerConfig(datastr)
42+
if errstr != "" {
43+
fmt.Println(errstr)
44+
}
45+
}
46+
47+
func createSsServerConfig(str string) (errstr string) {
48+
var index int
49+
ind := strings.IndexByte(str, '@')
50+
if ind == -1 {
51+
errString := "Invalid format of string " + str
52+
return errString //, 1
53+
} else {
54+
mpstr := str[:ind]
55+
conf := new(SsServerConf)
56+
index = strings.IndexByte(mpstr, ':')
57+
if index == -1 {
58+
errString := "Invalid format of string " + mpstr
59+
return errString //, 2
60+
} else {
61+
conf.Method = mpstr[:index]
62+
conf.Password = mpstr[index+1:]
63+
}
64+
spstr := str[ind+1:]
65+
// find '?'
66+
indx := strings.IndexByte(spstr, '/')
67+
if indx != -1 {
68+
spstr = spstr[:indx]
69+
} else {
70+
indx := strings.IndexByte(spstr, '?')
71+
if indx != -1 {
72+
spstr = spstr[:indx]
73+
}
74+
}
75+
index = strings.IndexByte(spstr, ':')
76+
if index == -1 {
77+
errString := "Invalid format of string " + spstr
78+
return errString //, 3
79+
} else {
80+
conf.Address = spstr[:index]
81+
i, err := strconv.Atoi(spstr[index+1:])
82+
if err != nil {
83+
errString := "Invalid format of port " + spstr
84+
return errString //, 4
85+
}
86+
conf.Port = i
87+
}
88+
xrconf := new(XrayConf)
89+
xrconf.Protocol = "shadowsocks"
90+
servers := new(XrSsServers)
91+
servers.SsServers = append(servers.SsServers, *conf)
92+
xrconf.Settings = servers
93+
confToSave++
94+
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //config.SsTag + strconv.Itoa(len(xrSsConfigs)+1)
95+
xrSsConfigs = append(xrSsConfigs, *xrconf)
96+
ssConfToSave = ssConfToSave + 1
97+
}
98+
return "" //, 0
99+
}

trojan.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type XrTrojanServerConfig struct {
10+
Trojan []TrojanServerConfig `json:"servers"`
11+
}
12+
13+
type TrojanServerConfig struct {
14+
Address string `json:"address"`
15+
Port int `json:"port"`
16+
Password string `json:"password"`
17+
}
18+
19+
func decodeTrojanServerConfig(str string) {
20+
var ser_passw string
21+
var params string
22+
index := strings.IndexByte(str, '?')
23+
if index == -1 { // no params
24+
index = strings.IndexByte(str, '@')
25+
if index == -1 {
26+
fmt.Println("Can not decode config")
27+
return
28+
} else {
29+
ser_passw = str
30+
params = ""
31+
}
32+
} else {
33+
ser_passw = str[:index]
34+
params = str[index+1:]
35+
}
36+
createTrojanServerConfig(ser_passw, params)
37+
}
38+
39+
func createTrojanServerConfig(ser_passw string, params string) (errstr string) {
40+
ind := strings.IndexByte(ser_passw, '@')
41+
if ind == -1 {
42+
errString := "Invalid format of string " + ser_passw
43+
return errString //, 1
44+
} else {
45+
conf := new(TrojanServerConfig)
46+
passw := ser_passw[:ind]
47+
conf.Password = passw
48+
ser := ser_passw[ind+1:]
49+
portInd := strings.IndexByte(ser, ':')
50+
conf.Address = ser[:portInd]
51+
i, err := strconv.Atoi(ser[portInd+1:])
52+
if err != nil {
53+
errString := "Invalid format of port " + ser
54+
return errString //, 4
55+
}
56+
conf.Port = i
57+
streamSettings := new(XrStreamSettings)
58+
if len(params) > 0 {
59+
paramsMap := createParamsMap(params)
60+
netType, ok := paramsMap["type"]
61+
if ok {
62+
streamSettings.Network = netType
63+
switch netType {
64+
case "tcp":
65+
tcppar := createTcpParam(paramsMap)
66+
streamSettings.TcpSettings = tcppar
67+
case "ws":
68+
wspar := createWsParams(paramsMap)
69+
streamSettings.WsSettings = wspar
70+
case "grpc":
71+
grpspar := createGrpcParams(paramsMap)
72+
streamSettings.GrpcSettings = grpspar
73+
case "xhttp":
74+
//
75+
}
76+
}
77+
sec, ok := paramsMap["security"]
78+
if ok {
79+
streamSettings.Security = sec
80+
switch sec {
81+
case "tls":
82+
tlsset := createTlsParams(paramsMap)
83+
streamSettings.TlsSettings = tlsset
84+
}
85+
}
86+
}
87+
xrconf := new(XrayConf)
88+
xrconf.Protocol = "trojan"
89+
servers := new(XrTrojanServerConfig)
90+
servers.Trojan = append(servers.Trojan, *conf)
91+
xrconf.Settings = servers
92+
xrconf.StreamSet = *streamSettings
93+
confToSave++
94+
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //
95+
xrTrConfigs = append(xrTrConfigs, *xrconf)
96+
trojanConfToSave = trojanConfToSave + 1
97+
}
98+
return ""
99+
}

vless.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type XrVlessServerConfig struct {
10+
Vless []VlessServerConfig `json:"vnext"`
11+
}
12+
13+
type VlessServerConfig struct {
14+
Address string `json:"address"`
15+
Port int `json:"port"`
16+
Users []VlessUser `json:"users"`
17+
}
18+
19+
type VlessUser struct {
20+
Id string `json:"id"`
21+
Encryption string `json:"encryption"`
22+
Flow string `json:"flow,omitempty"`
23+
Level int `json:"level,omitempty"`
24+
}
25+
26+
func decodeVlessServerConfig(str string) {
27+
var uid_ser string
28+
var params string
29+
index := strings.IndexByte(str, '?')
30+
if index == -1 { // no params
31+
index = strings.IndexByte(str, '@')
32+
if index == -1 {
33+
fmt.Println("Can not decode config")
34+
return
35+
} else {
36+
uid_ser = str
37+
params = ""
38+
}
39+
} else {
40+
uid_ser = str[:index]
41+
params = str[index+1:]
42+
}
43+
createVlessServerConfig(uid_ser, params)
44+
}
45+
46+
func createVlessServerConfig(uid_ser string, params string) (errstr string) {
47+
//var index int
48+
ind := strings.IndexByte(uid_ser, '@')
49+
if ind == -1 {
50+
errString := "Invalid format of string " + uid_ser
51+
return errString //, 1
52+
} else {
53+
conf := new(VlessServerConfig)
54+
uid := uid_ser[:ind]
55+
ser := uid_ser[ind+1:]
56+
portInd := strings.IndexByte(ser, ':')
57+
conf.Address = ser[:portInd]
58+
i, err := strconv.Atoi(ser[portInd+1:])
59+
if err != nil {
60+
errString := "Invalid format of port " + ser
61+
return errString //, 4
62+
}
63+
conf.Port = i
64+
user := new(VlessUser)
65+
user.Id = uid
66+
user.Encryption = "none"
67+
streamSettings := new(XrStreamSettings)
68+
if len(params) > 0 {
69+
paramsMap := createParamsMap(params)
70+
netType, ok := paramsMap["type"]
71+
if ok {
72+
streamSettings.Network = netType
73+
switch netType {
74+
case "tcp":
75+
tcppar := createTcpParam(paramsMap)
76+
streamSettings.TcpSettings = tcppar
77+
case "ws":
78+
wspar := createWsParams(paramsMap)
79+
streamSettings.WsSettings = wspar
80+
case "grpc":
81+
grpspar := createGrpcParams(paramsMap)
82+
streamSettings.GrpcSettings = grpspar
83+
case "xhttp":
84+
//
85+
}
86+
}
87+
sec, ok := paramsMap["security"]
88+
if ok {
89+
streamSettings.Security = sec
90+
switch sec {
91+
case "tls":
92+
tlsset := createTlsParams(paramsMap)
93+
streamSettings.TlsSettings = tlsset
94+
case "reality":
95+
realset := createRealityParams(paramsMap)
96+
streamSettings.RealitySettings = realset
97+
flow, ok := paramsMap["flow"]
98+
if ok {
99+
user.Flow = flow
100+
}
101+
}
102+
}
103+
}
104+
conf.Users = append(conf.Users, *user)
105+
xrconf := new(XrayConf)
106+
xrconf.Protocol = "vless"
107+
servers := new(XrVlessServerConfig)
108+
servers.Vless = append(servers.Vless, *conf)
109+
xrconf.Settings = servers
110+
xrconf.StreamSet = *streamSettings
111+
confToSave++
112+
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //config.VlessTag + strconv.Itoa(len(xrVlConfigs)+1)
113+
xrVlConfigs = append(xrVlConfigs, *xrconf)
114+
vlessConfToSave = vlessConfToSave + 1
115+
}
116+
return ""
117+
}

0 commit comments

Comments
 (0)