Proxy is working in a simple mode now

This commit is contained in:
9seconds
2021-03-23 15:14:36 +03:00
parent 66c45dc83b
commit a3362c7ea4
9 changed files with 220 additions and 26 deletions
+15
View File
@@ -9,6 +9,7 @@ import (
"time"
"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
"github.com/9seconds/mtg/v2/mtglib/internal/relay"
"github.com/9seconds/mtg/v2/mtglib/internal/telegram"
"github.com/panjf2000/ants/v2"
)
@@ -19,6 +20,7 @@ type Proxy struct {
streamWaitGroup sync.WaitGroup
idleTimeout time.Duration
bufferSize int
workerPool *ants.PoolWithFunc
telegram *telegram.Telegram
@@ -64,6 +66,13 @@ func (p *Proxy) ServeConn(conn net.Conn) {
return
}
rel := relay.AcquireRelay(ctx, p.logger.Named("relay"), p.bufferSize, p.idleTimeout)
defer relay.ReleaseRelay(rel)
if err := rel.Process(ctx.clientConn, ctx.telegramConn); err != nil {
p.logger.DebugError("relay has been finished", err)
}
}
func (p *Proxy) Serve(listener net.Listener) error {
@@ -185,6 +194,11 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop
idleTimeout = DefaultIdleTimeout
}
bufferSize := opts.BufferSize
if bufferSize < 1 {
bufferSize = DefaultBufferSize
}
ctx, cancel := context.WithCancel(context.Background())
proxy := &Proxy{
ctx: ctx,
@@ -195,6 +209,7 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop
eventStream: opts.EventStream,
logger: opts.Logger.Named("proxy"),
idleTimeout: idleTimeout,
bufferSize: int(bufferSize),
telegram: tg,
}