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.

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package telegram
  2. import (
  3. "io"
  4. "math/rand"
  5. "github.com/juju/errors"
  6. )
  7. // Telegram defines an interface to connect to Telegram. This
  8. // encapsulates logic of working with middleproxies or direct
  9. // connections.
  10. type Telegram interface {
  11. Dial(int16) (io.ReadWriteCloser, error)
  12. Init(io.ReadWriteCloser) (io.ReadWriteCloser, 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(dcIdx int16) (io.ReadWriteCloser, error) {
  20. addrs := make([]string, 2)
  21. if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
  22. addrs = append(addrs, addr[rand.Intn(len(addr))])
  23. }
  24. if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
  25. addrs = append(addrs, addr[rand.Intn(len(addr))])
  26. }
  27. for _, addr := range addrs {
  28. if conn, err := b.dialer.dialRWC(addr); err == nil {
  29. return conn, err
  30. }
  31. }
  32. return nil, errors.New("Cannot connect to Telegram")
  33. }