|
|
@@ -5,6 +5,7 @@ import (
|
|
5
|
5
|
"errors"
|
|
6
|
6
|
"fmt"
|
|
7
|
7
|
"net"
|
|
|
8
|
+ "strconv"
|
|
8
|
9
|
"sync"
|
|
9
|
10
|
"time"
|
|
10
|
11
|
|
|
|
@@ -21,10 +22,11 @@ type Proxy struct {
|
|
21
|
22
|
ctxCancel context.CancelFunc
|
|
22
|
23
|
streamWaitGroup sync.WaitGroup
|
|
23
|
24
|
|
|
24
|
|
- idleTimeout time.Duration
|
|
25
|
|
- bufferSize int
|
|
26
|
|
- workerPool *ants.PoolWithFunc
|
|
27
|
|
- telegram *telegram.Telegram
|
|
|
25
|
+ idleTimeout time.Duration
|
|
|
26
|
+ bufferSize int
|
|
|
27
|
+ domainFrontAddress string
|
|
|
28
|
+ workerPool *ants.PoolWithFunc
|
|
|
29
|
+ telegram *telegram.Telegram
|
|
28
|
30
|
|
|
29
|
31
|
secret Secret
|
|
30
|
32
|
network Network
|
|
|
@@ -59,9 +61,7 @@ func (p *Proxy) ServeConn(conn net.Conn) {
|
|
59
|
61
|
ctx.logger.Info("Stream has been finished")
|
|
60
|
62
|
}()
|
|
61
|
63
|
|
|
62
|
|
- if err := p.doFakeTLSHandshake(ctx); err != nil {
|
|
63
|
|
- p.logger.InfoError("faketls handshake is failed", err)
|
|
64
|
|
-
|
|
|
64
|
+ if !p.doFakeTLSHandshake(ctx) {
|
|
65
|
65
|
return
|
|
66
|
66
|
}
|
|
67
|
67
|
|
|
|
@@ -77,7 +77,8 @@ func (p *Proxy) ServeConn(conn net.Conn) {
|
|
77
|
77
|
return
|
|
78
|
78
|
}
|
|
79
|
79
|
|
|
80
|
|
- rel := relay.AcquireRelay(ctx, p.logger.Named("relay"), p.bufferSize, p.idleTimeout)
|
|
|
80
|
+ rel := relay.AcquireRelay(ctx,
|
|
|
81
|
+ p.logger.Named("relay"), p.bufferSize, p.idleTimeout)
|
|
81
|
82
|
defer relay.ReleaseRelay(rel)
|
|
82
|
83
|
|
|
83
|
84
|
if err := rel.Process(ctx.clientConn, ctx.telegramConn); err != nil {
|
|
|
@@ -122,38 +123,52 @@ func (p *Proxy) Shutdown() {
|
|
122
|
123
|
p.workerPool.Release()
|
|
123
|
124
|
}
|
|
124
|
125
|
|
|
125
|
|
-func (p *Proxy) doFakeTLSHandshake(ctx *streamContext) error {
|
|
|
126
|
+func (p *Proxy) doFakeTLSHandshake(ctx *streamContext) bool {
|
|
126
|
127
|
rec := record.AcquireRecord()
|
|
127
|
128
|
defer record.ReleaseRecord(rec)
|
|
128
|
129
|
|
|
129
|
|
- if err := rec.Read(ctx.clientConn); err != nil {
|
|
130
|
|
- return fmt.Errorf("cannot read client hello: %w", err)
|
|
|
130
|
+ rewind := newConnRewind(ctx.clientConn)
|
|
|
131
|
+
|
|
|
132
|
+ if err := rec.Read(rewind); err != nil {
|
|
|
133
|
+ p.logger.InfoError("cannot read client hello", err)
|
|
|
134
|
+ p.doDomainFronting(ctx, rewind)
|
|
|
135
|
+
|
|
|
136
|
+ return false
|
|
131
|
137
|
}
|
|
132
|
138
|
|
|
133
|
139
|
hello, err := faketls.ParseClientHello(p.secret.Key[:], rec.Payload.Bytes())
|
|
134
|
140
|
if err != nil {
|
|
135
|
|
- return fmt.Errorf("cannot parse client hello: %w", err)
|
|
|
141
|
+ p.logger.InfoError("cannot parse client hello", err)
|
|
|
142
|
+ p.doDomainFronting(ctx, rewind)
|
|
|
143
|
+
|
|
|
144
|
+ return false
|
|
136
|
145
|
}
|
|
137
|
146
|
|
|
138
|
147
|
if err := p.timeAttackDetector.Valid(hello.Time); err != nil {
|
|
139
|
|
- return fmt.Errorf("invalid time: %w", err)
|
|
|
148
|
+ p.logger.InfoError("invalid faketls time", err)
|
|
|
149
|
+ p.doDomainFronting(ctx, rewind)
|
|
|
150
|
+
|
|
|
151
|
+ return false
|
|
140
|
152
|
}
|
|
141
|
153
|
|
|
142
|
154
|
if p.antiReplayCache.SeenBefore(hello.SessionID) {
|
|
143
|
|
- return errReplayAttackDetected
|
|
|
155
|
+ p.logger.Warning("replay attack has been detected!")
|
|
|
156
|
+ p.doDomainFronting(ctx, rewind)
|
|
|
157
|
+
|
|
|
158
|
+ return false
|
|
144
|
159
|
}
|
|
145
|
160
|
|
|
146
|
|
- if err := faketls.SendWelcomePacket(ctx.clientConn, p.secret.Key[:], hello); err != nil {
|
|
|
161
|
+ if err := faketls.SendWelcomePacket(rewind, p.secret.Key[:], hello); err != nil {
|
|
147
|
162
|
p.logger.InfoError("cannot send welcome packet", err)
|
|
148
|
163
|
|
|
149
|
|
- return errCannotSendWelcomePacket
|
|
|
164
|
+ return false
|
|
150
|
165
|
}
|
|
151
|
166
|
|
|
152
|
167
|
ctx.clientConn = &faketls.Conn{
|
|
153
|
168
|
Conn: ctx.clientConn,
|
|
154
|
169
|
}
|
|
155
|
170
|
|
|
156
|
|
- return nil
|
|
|
171
|
+ return true
|
|
157
|
172
|
}
|
|
158
|
173
|
|
|
159
|
174
|
func (p *Proxy) doObfuscated2Handshake(ctx *streamContext) error {
|
|
|
@@ -207,6 +222,25 @@ func (p *Proxy) doTelegramCall(ctx *streamContext) error {
|
|
207
|
222
|
return nil
|
|
208
|
223
|
}
|
|
209
|
224
|
|
|
|
225
|
+func (p *Proxy) doDomainFronting(ctx context.Context, conn *connRewind) {
|
|
|
226
|
+ conn.Rewind()
|
|
|
227
|
+
|
|
|
228
|
+ frontConn, err := p.network.DialContext(ctx, "tcp", p.domainFrontAddress)
|
|
|
229
|
+ if err != nil {
|
|
|
230
|
+ p.logger.WarningError("cannot dial to the fronting domain", err)
|
|
|
231
|
+
|
|
|
232
|
+ return
|
|
|
233
|
+ }
|
|
|
234
|
+
|
|
|
235
|
+ rel := relay.AcquireRelay(ctx,
|
|
|
236
|
+ p.logger.Named("domain-fronting"), p.bufferSize, p.idleTimeout)
|
|
|
237
|
+ defer relay.ReleaseRelay(rel)
|
|
|
238
|
+
|
|
|
239
|
+ if err := rel.Process(conn, frontConn); err != nil {
|
|
|
240
|
+ p.logger.DebugError("domain fronting relay has been finished", err)
|
|
|
241
|
+ }
|
|
|
242
|
+}
|
|
|
243
|
+
|
|
210
|
244
|
func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
|
|
211
|
245
|
switch {
|
|
212
|
246
|
case opts.Network == nil:
|
|
|
@@ -245,6 +279,11 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
|
|
245
|
279
|
bufferSize = DefaultBufferSize
|
|
246
|
280
|
}
|
|
247
|
281
|
|
|
|
282
|
+ domainFrontingPort := int(opts.DomainFrontingPort)
|
|
|
283
|
+ if domainFrontingPort == 0 {
|
|
|
284
|
+ domainFrontingPort = DefaultDomainFrontingPort
|
|
|
285
|
+ }
|
|
|
286
|
+
|
|
248
|
287
|
ctx, cancel := context.WithCancel(context.Background())
|
|
249
|
288
|
proxy := &Proxy{
|
|
250
|
289
|
ctx: ctx,
|
|
|
@@ -256,9 +295,11 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
|
|
256
|
295
|
ipBlocklist: opts.IPBlocklist,
|
|
257
|
296
|
eventStream: opts.EventStream,
|
|
258
|
297
|
logger: opts.Logger.Named("proxy"),
|
|
259
|
|
- idleTimeout: idleTimeout,
|
|
260
|
|
- bufferSize: int(bufferSize),
|
|
261
|
|
- telegram: tg,
|
|
|
298
|
+ domainFrontAddress: net.JoinHostPort(opts.Secret.Host,
|
|
|
299
|
+ strconv.Itoa(domainFrontingPort)),
|
|
|
300
|
+ idleTimeout: idleTimeout,
|
|
|
301
|
+ bufferSize: int(bufferSize),
|
|
|
302
|
+ telegram: tg,
|
|
262
|
303
|
}
|
|
263
|
304
|
|
|
264
|
305
|
pool, err := ants.NewPoolWithFunc(int(concurrency), func(arg interface{}) {
|