forked from p4gefau1t/trojan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy.go
172 lines (162 loc) · 4.4 KB
/
easy.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package easy
import (
"encoding/json"
"flag"
"net"
"strconv"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/log"
"github.com/p4gefau1t/trojan-go/option"
"github.com/p4gefau1t/trojan-go/proxy"
)
type easy struct {
server *bool
client *bool
password *string
local *string
remote *string
cert *string
key *string
}
type ClientConfig struct {
RunType string `json:"run_type"`
LocalAddr string `json:"local_addr"`
LocalPort int `json:"local_port"`
RemoteAddr string `json:"remote_addr"`
RemotePort int `json:"remote_port"`
Password []string `json:"password"`
}
type TLS struct {
SNI string `json:"sni"`
Cert string `json:"cert"`
Key string `json:"key"`
}
type ServerConfig struct {
RunType string `json:"run_type"`
LocalAddr string `json:"local_addr"`
LocalPort int `json:"local_port"`
RemoteAddr string `json:"remote_addr"`
RemotePort int `json:"remote_port"`
Password []string `json:"password"`
TLS `json:"ssl"`
}
func (o *easy) Name() string {
return "easy"
}
func (o *easy) Handle() error {
if !*o.server && !*o.client {
return common.NewError("empty")
}
if *o.password == "" {
log.Fatal("empty password is not allowed")
}
log.Info("easy mode enabled, trojan-go will NOT use the config file")
if *o.client {
if *o.local == "" {
log.Warn("client local addr is unspecified, using 127.0.0.1:1080")
*o.local = "127.0.0.1:1080"
}
localHost, localPortStr, err := net.SplitHostPort(*o.local)
if err != nil {
log.Fatal(common.NewError("invalid local addr format:" + *o.local).Base(err))
}
remoteHost, remotePortStr, err := net.SplitHostPort(*o.remote)
if err != nil {
log.Fatal(common.NewError("invalid remote addr format:" + *o.remote).Base(err))
}
localPort, err := strconv.Atoi(localPortStr)
if err != nil {
log.Fatal(err)
}
remotePort, err := strconv.Atoi(remotePortStr)
if err != nil {
log.Fatal(err)
}
clientConfig := ClientConfig{
RunType: "client",
LocalAddr: localHost,
LocalPort: localPort,
RemoteAddr: remoteHost,
RemotePort: remotePort,
Password: []string{
*o.password,
},
}
clientConfigJSON, err := json.Marshal(&clientConfig)
common.Must(err)
log.Info("generated config:")
log.Info(string(clientConfigJSON))
proxy, err := proxy.NewProxyFromConfigData(clientConfigJSON, true)
if err != nil {
log.Fatal(err)
}
if err := proxy.Run(); err != nil {
log.Fatal(err)
}
} else if *o.server {
if *o.remote == "" {
log.Warn("server remote addr is unspecified, using 127.0.0.1:80")
*o.remote = "127.0.0.1:80"
}
if *o.local == "" {
log.Warn("server local addr is unspecified, using 0.0.0.0:443")
*o.local = "0.0.0.0:443"
}
localHost, localPortStr, err := net.SplitHostPort(*o.local)
if err != nil {
log.Fatal(common.NewError("invalid local addr format:" + *o.local).Base(err))
}
remoteHost, remotePortStr, err := net.SplitHostPort(*o.remote)
if err != nil {
log.Fatal(common.NewError("invalid remote addr format:" + *o.remote).Base(err))
}
localPort, err := strconv.Atoi(localPortStr)
if err != nil {
log.Fatal(err)
}
remotePort, err := strconv.Atoi(remotePortStr)
if err != nil {
log.Fatal(err)
}
serverConfig := ServerConfig{
RunType: "server",
LocalAddr: localHost,
LocalPort: localPort,
RemoteAddr: remoteHost,
RemotePort: remotePort,
Password: []string{
*o.password,
},
TLS: TLS{
Cert: *o.cert,
Key: *o.key,
},
}
serverConfigJSON, err := json.Marshal(&serverConfig)
common.Must(err)
log.Info("generated json config:")
log.Info(string(serverConfigJSON))
proxy, err := proxy.NewProxyFromConfigData(serverConfigJSON, true)
if err != nil {
log.Fatal(err)
}
if err := proxy.Run(); err != nil {
log.Fatal(err)
}
}
return nil
}
func (o *easy) Priority() int {
return 50
}
func init() {
option.RegisterHandler(&easy{
server: flag.Bool("server", false, "Run a trojan-go server"),
client: flag.Bool("client", false, "Run a trojan-go client"),
password: flag.String("password", "", "Password for authentication"),
remote: flag.String("remote", "", "Remote address, e.g. 127.0.0.1:12345"),
local: flag.String("local", "", "Local address, e.g. 127.0.0.1:12345"),
key: flag.String("key", "server.key", "Key of the server"),
cert: flag.String("cert", "server.crt", "Certificates of the server"),
})
}