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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package telegram
  2. import (
  3. "io"
  4. "math/rand"
  5. "github.com/juju/errors"
  6. "github.com/9seconds/mtg/mtproto"
  7. )
  8. // Telegram defines an interface to connect to Telegram. This
  9. // encapsulates logic of working with middleproxies or direct
  10. // connections.
  11. type Telegram interface {
  12. Dial(*mtproto.ConnectionOpts) (io.ReadWriteCloser, error)
  13. Init(*mtproto.ConnectionOpts, io.ReadWriteCloser) (io.ReadWriteCloser, error)
  14. }
  15. type baseTelegram struct {
  16. dialer tgDialer
  17. v4Addresses map[int16][]string
  18. v6Addresses map[int16][]string
  19. }
  20. func (b *baseTelegram) dial(dcIdx int16, proto mtproto.ConnectionProtocol) (io.ReadWriteCloser, error) {
  21. addrs := make([]string, 2)
  22. if proto&mtproto.ConnectionProtocolIPv6 != 0 {
  23. if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
  24. addrs = append(addrs, addr[rand.Intn(len(addr))])
  25. }
  26. }
  27. if proto&mtproto.ConnectionProtocolIPv4 != 0 {
  28. if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
  29. addrs = append(addrs, addr[rand.Intn(len(addr))])
  30. }
  31. }
  32. for _, addr := range addrs {
  33. if conn, err := b.dialer.dialRWC(addr); err == nil {
  34. return conn, err
  35. }
  36. }
  37. return nil, errors.New("Cannot connect to Telegram")
  38. }