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文字以内のものにしてください。

telegram.go 1.7KB

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