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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package telegram
  2. import (
  3. "context"
  4. "math/rand"
  5. "github.com/juju/errors"
  6. "github.com/9seconds/mtg/mtproto"
  7. "github.com/9seconds/mtg/wrappers"
  8. )
  9. // Telegram is an interface for different Telegram work modes.
  10. type Telegram interface {
  11. Dial(context.Context, context.CancelFunc, string, *mtproto.ConnectionOpts) (wrappers.StreamReadWriteCloser, error)
  12. Init(*mtproto.ConnectionOpts, wrappers.StreamReadWriteCloser) (wrappers.Wrap, error)
  13. }
  14. type baseTelegram struct {
  15. dialer tgDialer
  16. v4Addresses map[int16][]string
  17. v6Addresses map[int16][]string
  18. }
  19. func (b *baseTelegram) dial(ctx context.Context, cancel context.CancelFunc, dcIdx int16, connID string,
  20. proto mtproto.ConnectionProtocol) (wrappers.StreamReadWriteCloser, 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(ctx, cancel, addr, connID); err == nil {
  34. return conn, err
  35. }
  36. }
  37. return nil, errors.New("Cannot connect to Telegram")
  38. }