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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package dc
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. )
  8. type Telegram struct {
  9. ctx context.Context
  10. lock sync.RWMutex
  11. view dcView
  12. preferIP preferIP
  13. }
  14. func (t *Telegram) GetAddresses(dc int) []Addr {
  15. t.lock.RLock()
  16. defer t.lock.RUnlock()
  17. switch t.preferIP {
  18. case preferIPOnlyIPv4:
  19. return t.view.getV4(dc)
  20. case preferIPOnlyIPv6:
  21. return t.view.getV4(dc)
  22. case preferIPPreferIPv4:
  23. return append(t.view.getV4(dc), t.view.getV6(dc)...)
  24. }
  25. return append(t.view.getV6(dc), t.view.getV4(dc)...)
  26. }
  27. func New(ipPreference string) (*Telegram, error) {
  28. var pref preferIP
  29. switch strings.ToLower(ipPreference) {
  30. case "prefer-ipv4":
  31. pref = preferIPPreferIPv4
  32. case "prefer-ipv6":
  33. pref = preferIPPreferIPv6
  34. case "only-ipv4":
  35. pref = preferIPOnlyIPv4
  36. case "only-ipv6":
  37. pref = preferIPOnlyIPv6
  38. default:
  39. return nil, fmt.Errorf("unknown ip preference %s", ipPreference)
  40. }
  41. return &Telegram{
  42. preferIP: pref,
  43. }, nil
  44. }