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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

telegram.go 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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) (io.ReadWriteCloser, error) {
  21. addrs := make([]string, 2)
  22. if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
  23. addrs = append(addrs, addr[rand.Intn(len(addr))])
  24. }
  25. if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
  26. addrs = append(addrs, addr[rand.Intn(len(addr))])
  27. }
  28. for _, addr := range addrs {
  29. if conn, err := b.dialer.dialRWC(addr); err == nil {
  30. return conn, err
  31. }
  32. }
  33. return nil, errors.New("Cannot connect to Telegram")
  34. }