This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
229 lines (202 loc) · 5.17 KB
/
main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"io"
"math/rand"
"os"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/juju/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"mtgs/config"
"mtgs/ntp"
"mtgs/proxy"
"mtgs/users"
)
var version = "dev" // this has to be set by build ld flags
var (
app = kingpin.New("mtgs", "Multiuser MTPROTO proxy")
debug = app.Flag("debug",
"Run in debug mode.").
Short('d').
Envar("MTGS_DEBUG").
Bool()
verbose = app.Flag("verbose",
"Run in verbose mode.").
Short('v').
Envar("MTGS_VERBOSE").
Bool()
consulHost = app.Flag("consul-host",
"Which host use for consul.").
Envar("MTGS_CONSUL_HOST").
Default("127.0.0.1").
String()
consulPort = app.Flag("consul-port",
"Which port use for consul.").
Envar("MTGS_CONSUL_PORT").
Default("8500").
Uint16()
bindIP = app.Flag("bind-ip",
"Which IP to bind to.").
Short('b').
Envar("MTGS_IP").
Default("0.0.0.0").
IP()
bindPort = app.Flag("port",
"Which port to bind to.").
Short('P').
Envar("MTGS_PORT").
Default("3128").
Uint16()
bindAPIPort = app.Flag("api-port",
"Which port to bind to.").
Short('p').
Envar("MTGS_API_PORT").
Default("8080").
Uint16()
apiBasepath = app.Flag("api-path",
"Which path use for API.").
Envar("MTGS_API_PATH").
Default("/mtgs").
String()
apiToken = app.Flag("api-token",
"Which token use for API authorization.").
Envar("MTGS_API_TOKEN").
Short('t').
Default("").
String()
publicIPv4 = app.Flag("public-ipv4",
"Which IPv4 address is public.").
Short('4').
Envar("MTGS_IPV4").
IP()
publicIPv4Port = app.Flag("public-ipv4-port",
"Which IPv4 port is public. Default is 'bind-port' value.").
Envar("MTGS_IPV4_PORT").
Uint16()
publicIPv6 = app.Flag("public-ipv6",
"Which IPv6 address is public.").
Short('6').
Envar("MTGS_IPV6").
IP()
publicIPv6Port = app.Flag("public-ipv6-port",
"Which IPv6 port is public. Default is 'bind-port' value.").
Envar("MTGS_IPV6_PORT").
Uint16()
writeBufferSize = app.Flag("write-buffer",
"Write buffer size in bytes. You can think about it as a buffer from client to Telegram.").
Short('w').
Envar("MTGS_BUFFER_WRITE").
Default("65536").
Uint32()
readBufferSize = app.Flag("read-buffer",
"Read buffer size in bytes. You can think about it as a buffer from Telegram to client.").
Short('r').
Envar("MTGS_BUFFER_READ").
Default("131072").
Uint32()
secureOnly = app.Flag("secure-only",
"Support clients with dd-secrets only.").
Short('s').
Envar("MTGS_SECURE_ONLY").
Bool()
antiReplayMaxSize = app.Flag("anti-replay-max-size",
"Max size of antireplay cache in megabytes.").
Envar("MTGS_ANTIREPLAY_MAXSIZE").
Default("128").
Int()
antiReplayEvictionTime = app.Flag("anti-replay-eviction-time",
"Eviction time period for obfuscated2 handshakes").
Envar("MTGS_ANTIREPLAY_EVICTIONTIME").
Default("168h").
Duration()
adtag = app.Flag("adtag",
"ADTag of the proxy.").
Short('a').
Envar("MTGS_ADTAG").
HexBytes()
)
func main() { // nolint: gocyclo
rand.Seed(time.Now().UTC().UnixNano())
app.Version(version)
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
err := setRLimit()
if err != nil {
zap.S().Infow(err.Error())
}
conf, err := config.NewConfig(*debug, *verbose,
*writeBufferSize, *readBufferSize,
*bindIP, *publicIPv4, *publicIPv6,
*bindPort, *bindAPIPort, *publicIPv4Port, *publicIPv6Port,
*apiBasepath, *apiToken,
*consulHost, *consulPort,
*secureOnly, *antiReplayMaxSize, *antiReplayEvictionTime,
*adtag,
)
if err != nil {
usage(err.Error())
}
atom := zap.NewAtomicLevel()
switch {
case conf.Debug:
atom.SetLevel(zapcore.DebugLevel)
case conf.Verbose:
atom.SetLevel(zapcore.InfoLevel)
default:
atom.SetLevel(zapcore.ErrorLevel)
gin.SetMode(gin.ReleaseMode)
}
encoderCfg := zap.NewProductionEncoderConfig()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(encoderCfg),
zapcore.Lock(os.Stderr),
atom,
))
zap.ReplaceGlobals(logger)
defer logger.Sync() // nolint: errcheck
zap.S().Debugw("Configuration", "config", conf)
if conf.UseMiddleProxy() {
zap.S().Infow("Use middle proxy connection to Telegram")
if diff, err := ntp.Fetch(); err != nil {
zap.S().Warnw("Could not fetch time data from NTP")
} else {
if diff >= time.Second {
usage(fmt.Sprintf("You choose to use middle proxy but your clock drift (%s) "+
"is bigger than 1 second. Please, sync your time", diff))
}
go ntp.AutoUpdate()
}
} else {
zap.S().Infow("Use direct connection to Telegram")
}
users.StartAPI(conf)
server, err := proxy.NewProxy(conf)
if err != nil {
panic(err)
}
if err := server.Serve(); err != nil {
zap.S().Fatalw("Server stopped", "error", err)
}
}
func setRLimit() (err error) {
rLimit := syscall.Rlimit{}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
err = errors.Annotate(err, "Cannot get rlimit")
return
}
rLimit.Cur = rLimit.Max
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
err = errors.Annotate(err, "Cannot set rlimit")
}
return
}
func usage(msg string) {
io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck, gosec
os.Exit(1)
}