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

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