mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:54:01 +03:00
Refactor some configuration to proxy_opts
This commit is contained in:
+13
-50
@@ -262,50 +262,12 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
|
func NewProxy(opts ProxyOpts) (*Proxy, error) {
|
||||||
switch {
|
if err := opts.valid(); err != nil {
|
||||||
case opts.Network == nil:
|
return nil, fmt.Errorf("invalid settings: %w", err)
|
||||||
return nil, ErrNetworkIsNotDefined
|
|
||||||
case opts.AntiReplayCache == nil:
|
|
||||||
return nil, ErrAntiReplayCacheIsNotDefined
|
|
||||||
case opts.IPBlocklist == nil:
|
|
||||||
return nil, ErrIPBlocklistIsNotDefined
|
|
||||||
case opts.EventStream == nil:
|
|
||||||
return nil, ErrEventStreamIsNotDefined
|
|
||||||
case opts.TimeAttackDetector == nil:
|
|
||||||
return nil, ErrTimeAttackDetectorIsNotDefined
|
|
||||||
case opts.Logger == nil:
|
|
||||||
return nil, ErrLoggerIsNotDefined
|
|
||||||
case !opts.Secret.Valid():
|
|
||||||
return nil, ErrSecretInvalid
|
|
||||||
}
|
}
|
||||||
|
|
||||||
preferIP := opts.PreferIP
|
tg, err := telegram.New(opts.Network, opts.getPreferIP())
|
||||||
if preferIP == "" {
|
|
||||||
preferIP = DefaultPreferIP
|
|
||||||
}
|
|
||||||
|
|
||||||
concurrency := opts.Concurrency
|
|
||||||
if concurrency == 0 {
|
|
||||||
concurrency = DefaultConcurrency
|
|
||||||
}
|
|
||||||
|
|
||||||
idleTimeout := opts.IdleTimeout
|
|
||||||
if idleTimeout < 1 {
|
|
||||||
idleTimeout = DefaultIdleTimeout
|
|
||||||
}
|
|
||||||
|
|
||||||
bufferSize := opts.BufferSize
|
|
||||||
if bufferSize < 1 {
|
|
||||||
bufferSize = DefaultBufferSize
|
|
||||||
}
|
|
||||||
|
|
||||||
domainFrontingPort := int(opts.DomainFrontingPort)
|
|
||||||
if domainFrontingPort == 0 {
|
|
||||||
domainFrontingPort = DefaultDomainFrontingPort
|
|
||||||
}
|
|
||||||
|
|
||||||
tg, err := telegram.New(opts.Network, preferIP)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot build telegram dialer: %w", err)
|
return nil, fmt.Errorf("cannot build telegram dialer: %w", err)
|
||||||
}
|
}
|
||||||
@@ -320,17 +282,18 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
|
|||||||
timeAttackDetector: opts.TimeAttackDetector,
|
timeAttackDetector: opts.TimeAttackDetector,
|
||||||
ipBlocklist: opts.IPBlocklist,
|
ipBlocklist: opts.IPBlocklist,
|
||||||
eventStream: opts.EventStream,
|
eventStream: opts.EventStream,
|
||||||
logger: opts.Logger.Named("proxy"),
|
logger: opts.getLogger("proxy"),
|
||||||
domainFrontingPort: domainFrontingPort,
|
domainFrontingPort: opts.getDomainFrontingPort(),
|
||||||
idleTimeout: idleTimeout,
|
idleTimeout: opts.getIdleTimeout(),
|
||||||
bufferSize: int(bufferSize),
|
bufferSize: opts.getBufferSize(),
|
||||||
telegram: tg,
|
telegram: tg,
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := ants.NewPoolWithFunc(int(concurrency), func(arg interface{}) {
|
pool, err := ants.NewPoolWithFunc(opts.getConcurrency(),
|
||||||
proxy.ServeConn(arg.(net.Conn))
|
func(arg interface{}) {
|
||||||
},
|
proxy.ServeConn(arg.(net.Conn))
|
||||||
ants.WithLogger(opts.Logger.Named("ants")),
|
},
|
||||||
|
ants.WithLogger(opts.getLogger("ants")),
|
||||||
ants.WithNonblocking(true))
|
ants.WithNonblocking(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -17,3 +17,68 @@ type ProxyOpts struct {
|
|||||||
IdleTimeout time.Duration
|
IdleTimeout time.Duration
|
||||||
PreferIP string
|
PreferIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) valid() error {
|
||||||
|
switch {
|
||||||
|
case p.Network == nil:
|
||||||
|
return ErrNetworkIsNotDefined
|
||||||
|
case p.AntiReplayCache == nil:
|
||||||
|
return ErrAntiReplayCacheIsNotDefined
|
||||||
|
case p.IPBlocklist == nil:
|
||||||
|
return ErrIPBlocklistIsNotDefined
|
||||||
|
case p.EventStream == nil:
|
||||||
|
return ErrEventStreamIsNotDefined
|
||||||
|
case p.TimeAttackDetector == nil:
|
||||||
|
return ErrTimeAttackDetectorIsNotDefined
|
||||||
|
case p.Logger == nil:
|
||||||
|
return ErrLoggerIsNotDefined
|
||||||
|
case !p.Secret.Valid():
|
||||||
|
return ErrSecretInvalid
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getBufferSize() int {
|
||||||
|
if p.BufferSize < 1 {
|
||||||
|
return DefaultBufferSize
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(p.BufferSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getConcurrency() int {
|
||||||
|
if p.Concurrency == 0 {
|
||||||
|
return DefaultConcurrency
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(p.Concurrency)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getDomainFrontingPort() int {
|
||||||
|
if p.DomainFrontingPort == 0 {
|
||||||
|
return DefaultDomainFrontingPort
|
||||||
|
}
|
||||||
|
|
||||||
|
return int(p.DomainFrontingPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getIdleTimeout() time.Duration {
|
||||||
|
if p.IdleTimeout == 0 {
|
||||||
|
return DefaultIdleTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.IdleTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getPreferIP() string {
|
||||||
|
if p.PreferIP == "" {
|
||||||
|
return DefaultPreferIP
|
||||||
|
}
|
||||||
|
|
||||||
|
return p.PreferIP
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ProxyOpts) getLogger(name string) Logger {
|
||||||
|
return p.Logger.Named(name)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user