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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. type Telegram interface {
  9. Dial(string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
  10. Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
  11. }
  12. type baseTelegram struct {
  13. dialer tgDialer
  14. v4Addresses map[int16][]string
  15. v6Addresses map[int16][]string
  16. }
  17. func (b *baseTelegram) dial(dcIdx int16, connID string, proto mtproto.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
  18. addrs := make([]string, 2)
  19. if proto&mtproto.ConnectionProtocolIPv6 != 0 {
  20. if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
  21. addrs = append(addrs, addr[rand.Intn(len(addr))])
  22. }
  23. }
  24. if proto&mtproto.ConnectionProtocolIPv4 != 0 {
  25. if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
  26. addrs = append(addrs, addr[rand.Intn(len(addr))])
  27. }
  28. }
  29. for _, addr := range addrs {
  30. if conn, err := b.dialer.dialRWC(addr, connID); err == nil {
  31. return conn, err
  32. }
  33. }
  34. return nil, errors.New("Cannot connect to Telegram")
  35. }