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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

telegram.go 1.2KB

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