-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrojan.go
More file actions
110 lines (105 loc) · 2.61 KB
/
trojan.go
File metadata and controls
110 lines (105 loc) · 2.61 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
108
109
110
package main
import (
"fmt"
"strconv"
"strings"
)
type XrTrojanServerConfig struct {
Trojan []TrojanServerConfig `json:"servers"`
}
type TrojanServerConfig struct {
Address string `json:"address"`
Port int `json:"port"`
Password string `json:"password"`
}
func decodeTrojanServerConfig(str string) bool {
var ser_passw string
var params string
index := strings.IndexByte(str, '?')
if index == -1 { // no params
index = strings.IndexByte(str, '@')
if index == -1 {
fmt.Println("Can not decode config")
return false
} else {
ser_passw = str
params = ""
}
} else {
ser_passw = str[:index]
params = str[index+1:]
}
errstr := createTrojanServerConfig(ser_passw, params)
if errstr != "" {
fmt.Println(errstr)
return false
}
return true
}
func createTrojanServerConfig(ser_passw string, params string) (errstr string) {
ind := strings.IndexByte(ser_passw, '@')
if ind == -1 {
errString := "TR Invalid format of string " + ser_passw
return errString //, 1
} else {
conf := new(TrojanServerConfig)
passw := ser_passw[:ind]
conf.Password = passw
ser := ser_passw[ind+1:]
portInd := strings.IndexByte(ser, ':')
conf.Address = ser[:portInd]
//check ip
if !isIpValid(config.IpCheckServer, conf.Address, config.IpCheckKey,
config.IpCheckValue, config.IpCheckBlackList) {
return "TR Ip is invalid"
}
//
i, err := strconv.Atoi(ser[portInd+1:])
if err != nil {
errString := "TR Invalid format of port " + ser
return errString //, 4
}
conf.Port = i
streamSettings := new(XrStreamSettings)
if len(params) > 0 {
paramsMap := createParamsMap(params)
netType, ok := paramsMap["type"]
if ok {
streamSettings.Network = netType
switch netType {
case "tcp":
tcppar := createTcpParam(paramsMap)
streamSettings.TcpSettings = tcppar
case "ws":
wspar := createWsParams(paramsMap)
streamSettings.WsSettings = wspar
case "grpc":
grpspar := createGrpcParams(paramsMap)
streamSettings.GrpcSettings = grpspar
case "xhttp":
//
}
}
sec, ok := paramsMap["security"]
if ok {
streamSettings.Security = sec
switch sec {
case "tls":
tlsset := createTlsParams(paramsMap)
streamSettings.TlsSettings = tlsset
}
}
}
xrconf := new(XrayConf)
xrconf.Protocol = "trojan"
servers := new(XrTrojanServerConfig)
servers.Trojan = append(servers.Trojan, *conf)
xrconf.Settings = servers
xrconf.StreamSet = *streamSettings
confToSave++
xrconf.Tag = config.Tag + strconv.Itoa(confToSave) //
xrTrConfigs = append(xrTrConfigs, *xrconf)
trojanConfToSave = trojanConfToSave + 1
}
return ""
}