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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

telegram.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package proxy
  2. import (
  3. "net"
  4. "time"
  5. "github.com/juju/errors"
  6. )
  7. // TelegramAddress presents a pair of v4 and v6 addresses. This pairization
  8. // is required because we want to use DC indexes.
  9. type TelegramAddress struct {
  10. v4 string
  11. v6 string
  12. }
  13. // IPv4 returns v4 address.
  14. func (t *TelegramAddress) IPv4() string {
  15. return net.JoinHostPort(t.v4, telegramPort)
  16. }
  17. // IPv6 returns v4 address.
  18. func (t *TelegramAddress) IPv6() string {
  19. return net.JoinHostPort(t.v6, telegramPort)
  20. }
  21. // TelegramAddresses is a list of all known Telegram addresses for DC indexes.
  22. var TelegramAddresses = []TelegramAddress{
  23. TelegramAddress{v4: "149.154.175.50", v6: "2001:b28:f23d:f001::a"},
  24. TelegramAddress{v4: "149.154.167.51", v6: "2001:67c:04e8:f002::a"},
  25. TelegramAddress{v4: "149.154.175.100", v6: "2001:b28:f23d:f003::a"},
  26. TelegramAddress{v4: "149.154.167.91", v6: "2001:67c:04e8:f004::a"},
  27. TelegramAddress{v4: "149.154.171.5", v6: "2001:b28:f23f:f005::a"},
  28. }
  29. const telegramPort = "443"
  30. const telegramKeepAlive = 30 * time.Second
  31. func dialToTelegram(ipv6 bool, dcIdx int16, timeout time.Duration) (net.Conn, error) {
  32. if dcIdx < 0 || dcIdx >= 5 {
  33. return nil, errors.New("Incorrect DC IDX")
  34. }
  35. conn, err := doDial(ipv6, dcIdx, timeout)
  36. if err != nil {
  37. return nil, errors.Annotate(err, "Cannot dial")
  38. }
  39. if err := conn.SetKeepAlive(true); err != nil {
  40. return nil, errors.Annotate(err, "Cannot establish keepalive connection")
  41. }
  42. if err := conn.SetKeepAlivePeriod(telegramKeepAlive); err != nil {
  43. return nil, errors.Annotate(err, "Cannot set keepalive timeout")
  44. }
  45. return conn, nil
  46. }
  47. func doDial(ipv6 bool, dcIdx int16, timeout time.Duration) (*net.TCPConn, error) {
  48. dialer := net.Dialer{Timeout: timeout}
  49. addr := TelegramAddresses[dcIdx]
  50. if ipv6 {
  51. if conn, err := dialer.Dial("tcp", addr.IPv6()); err == nil {
  52. return conn.(*net.TCPConn), nil
  53. }
  54. }
  55. conn, err := dialer.Dial("tcp", addr.IPv4())
  56. if err == nil {
  57. return conn.(*net.TCPConn), nil
  58. }
  59. return nil, err
  60. }