Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package cli
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "time"
  7. "github.com/9seconds/mtg/v2/internal/config"
  8. )
  9. type SimpleRun struct {
  10. BindTo string `kong:"arg,required,name='bind-to',help='A host:port to bind proxy to.'"`
  11. Secret string `kong:"arg,required,name='secret',help='Proxy secret.'"`
  12. Debug bool `kong:"name='debug',short='d',help='Run in debug mode.'"` //nolint: lll
  13. Concurrency uint64 `kong:"name='concurrency',short='c',default='8192',help='Max number of concurrent connection to proxy.'"` //nolint: lll
  14. TCPBuffer string `kong:"name='tcp-buffer',short='b',default='4KB',help='Deprecated and ignored'"` //nolint: lll
  15. PreferIP string `kong:"name='prefer-ip',short='i',default='prefer-ipv6',help='IP preference. By default we prefer IPv6 with fallback to IPv4.'"` //nolint: lll
  16. DomainFrontingPort uint64 `kong:"name='domain-fronting-port',short='p',default='443',help='A port to access for domain fronting.'"` //nolint: lll
  17. DOHIP net.IP `kong:"name='doh-ip',short='n',default='9.9.9.9',help='IP address of DNS-over-HTTP to use.'"` //nolint: lll
  18. Timeout time.Duration `kong:"name='timeout',short='t',default='10s',help='Network timeout to use'"` //nolint: lll
  19. Socks5Proxies []string `kong:"name='socks5-proxy',short='s',help='Socks5 proxies to use for network access.'"` //nolint: lll
  20. AntiReplayCacheSize string `kong:"name='antireplay-cache-size',short='a',default='1MB',help='A size of anti-replay cache to use.'"` //nolint: lll
  21. }
  22. func (s *SimpleRun) Run(cli *CLI, version string) error { //nolint: cyclop,funlen
  23. conf := &config.Config{}
  24. if err := conf.BindTo.Set(s.BindTo); err != nil {
  25. return fmt.Errorf("incorrect bind-to parameter: %w", err)
  26. }
  27. if err := conf.Secret.Set(s.Secret); err != nil {
  28. return fmt.Errorf("incorrect secret: %w", err)
  29. }
  30. if err := conf.Concurrency.Set(strconv.FormatUint(s.Concurrency, 10)); err != nil { //nolint: gomnd
  31. return fmt.Errorf("incorrect concurrency: %w", err)
  32. }
  33. if err := conf.PreferIP.Set(s.PreferIP); err != nil {
  34. return fmt.Errorf("incorrect prefer-ip: %w", err)
  35. }
  36. if err := conf.DomainFrontingPort.Set(strconv.FormatUint(s.DomainFrontingPort, 10)); err != nil { //nolint: gomnd
  37. return fmt.Errorf("incorrect domain-fronting-port: %w", err)
  38. }
  39. if err := conf.Network.DOHIP.Set(s.DOHIP.String()); err != nil {
  40. return fmt.Errorf("incorrect doh-ip: %w", err)
  41. }
  42. if err := conf.Network.Timeout.TCP.Set(s.Timeout.String()); err != nil {
  43. return fmt.Errorf("incorrect timeout: %w", err)
  44. }
  45. if err := conf.Network.Timeout.HTTP.Set(s.Timeout.String()); err != nil {
  46. return fmt.Errorf("incorrect timeout: %w", err)
  47. }
  48. if err := conf.Network.Timeout.Idle.Set(s.Timeout.String()); err != nil {
  49. return fmt.Errorf("incorrect timeout: %w", err)
  50. }
  51. if err := conf.Defense.AntiReplay.MaxSize.Set(s.AntiReplayCacheSize); err != nil {
  52. return fmt.Errorf("incorrect antireplay-cache-size: %w", err)
  53. }
  54. for _, v := range s.Socks5Proxies {
  55. proxyURL := config.TypeProxyURL{}
  56. if err := proxyURL.Set(v); err != nil {
  57. return fmt.Errorf("incorrect socks5 proxy URL: %w", err)
  58. }
  59. conf.Network.Proxies = append(conf.Network.Proxies, proxyURL)
  60. }
  61. conf.Debug.Value = s.Debug
  62. conf.AllowFallbackOnUnknownDC.Value = true
  63. conf.Defense.AntiReplay.Enabled.Value = true
  64. if err := conf.Validate(); err != nil {
  65. return fmt.Errorf("invalid result configuration: %w", err)
  66. }
  67. return runProxy(conf, version)
  68. }