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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package cli
  2. import (
  3. "fmt"
  4. "net"
  5. "net/url"
  6. "os"
  7. "github.com/9seconds/mtg/v2/internal/config2"
  8. "github.com/9seconds/mtg/v2/mtglib"
  9. "github.com/9seconds/mtg/v2/network"
  10. )
  11. func readTOMLConfig(path string) (*config2.Config, error) {
  12. content, err := os.ReadFile(path)
  13. if err != nil {
  14. return nil, fmt.Errorf("cannot read config file: %w", err)
  15. }
  16. conf, err := config2.Parse(content)
  17. if err != nil {
  18. return nil, fmt.Errorf("cannot parse config: %w", err)
  19. }
  20. return conf, nil
  21. }
  22. func makeNetwork(conf *config2.Config, version string) (mtglib.Network, error) {
  23. tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
  24. httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
  25. dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
  26. bufferSize := conf.TCPBuffer.Get(network.DefaultBufferSize)
  27. userAgent := "mtg/" + version
  28. baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
  29. if err != nil {
  30. return nil, fmt.Errorf("cannot build a default dialer: %w", err)
  31. }
  32. if len(conf.Network.Proxies) == 0 {
  33. return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
  34. }
  35. proxyURLs := make([]*url.URL, 0, len(conf.Network.Proxies))
  36. for _, v := range conf.Network.Proxies {
  37. if value := v.Get(nil); value != nil {
  38. proxyURLs = append(proxyURLs, value)
  39. }
  40. }
  41. if len(proxyURLs) == 1 {
  42. socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
  43. if err != nil {
  44. return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
  45. }
  46. return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
  47. }
  48. socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
  49. if err != nil {
  50. return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
  51. }
  52. return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout) // nolint: wrapcheck
  53. }