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 символов.

global_ips.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package config2
  2. import (
  3. "context"
  4. "io"
  5. "io/ioutil"
  6. "net"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "github.com/juju/errors"
  11. "go.uber.org/zap"
  12. )
  13. const (
  14. ifconfigAddress = "https://ifconfig.co/ip"
  15. ifconfigTimeout = 10 * time.Second
  16. )
  17. func getGlobalIPv4(ctx context.Context, cancel context.CancelFunc) {
  18. ip, err := fetchIP(ctx, "tcp4")
  19. if err != nil || ip.To4() == nil {
  20. cancel()
  21. zap.S().Errorw("Cannot find public ipv4 address", "error", err)
  22. return
  23. }
  24. C.PublicIPv4Addr.IP = ip
  25. }
  26. func getGlobalIPv6(ctx context.Context, cancel context.CancelFunc) {
  27. ip, err := fetchIP(ctx, "tcp6")
  28. if err != nil || ip.To4() != nil {
  29. cancel()
  30. zap.S().Errorw("Cannot find public ipv6 address", "error", err)
  31. return
  32. }
  33. C.PublicIPv6Addr.IP = ip
  34. }
  35. func fetchIP(ctx context.Context, network string) (net.IP, error) {
  36. dialer := &net.Dialer{FallbackDelay: -1}
  37. client := &http.Client{
  38. Jar: nil,
  39. Timeout: ifconfigTimeout,
  40. Transport: &http.Transport{
  41. DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
  42. return dialer.DialContext(ctx, network, addr)
  43. },
  44. },
  45. }
  46. req, err := http.NewRequest("GET", ifconfigAddress, nil)
  47. if err != nil {
  48. panic(err)
  49. }
  50. resp, err := client.Do(req.WithContext(ctx))
  51. if err != nil {
  52. if resp.Body != nil {
  53. io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
  54. }
  55. return nil, errors.Annotate(err, "Cannot perform a request")
  56. }
  57. defer resp.Body.Close() // nolint: errcheck
  58. respDataBytes, err := ioutil.ReadAll(resp.Body)
  59. if err != nil {
  60. return nil, errors.Annotate(err, "Cannot read response body")
  61. }
  62. respData := strings.TrimSpace(string(respDataBytes))
  63. ip := net.ParseIP(respData)
  64. if ip == nil {
  65. return nil, errors.Errorf("ifconfig.co returns incorrect IP %s", respData)
  66. }
  67. return ip, nil
  68. }