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 812B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package telegram
  2. import (
  3. "io"
  4. "math/rand"
  5. "github.com/juju/errors"
  6. )
  7. type Telegram interface {
  8. Dial(int16) (io.ReadWriteCloser, error)
  9. Init(io.ReadWriteCloser) (io.ReadWriteCloser, error)
  10. }
  11. type baseTelegram struct {
  12. dialer *tgDialer
  13. v4Addresses map[int16][]string
  14. v6Addresses map[int16][]string
  15. }
  16. func (b *baseTelegram) Dial(dcIdx int16) (io.ReadWriteCloser, error) {
  17. addrs := make([]string, 2)
  18. if addr, ok := b.v6Addresses[dcIdx]; ok && len(addr) > 0 {
  19. addrs = append(addrs, addr[rand.Intn(len(addr))])
  20. }
  21. if addr, ok := b.v4Addresses[dcIdx]; ok && len(addr) > 0 {
  22. addrs = append(addrs, addr[rand.Intn(len(addr))])
  23. }
  24. for _, addr := range addrs {
  25. if conn, err := b.dialer.dialRWC(addr); err == nil {
  26. return conn, err
  27. }
  28. }
  29. return nil, errors.New("Cannot connect to Telegram")
  30. }