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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

base.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package telegram
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "net"
  7. "time"
  8. "github.com/9seconds/mtg/conntypes"
  9. "github.com/9seconds/mtg/utils"
  10. "github.com/9seconds/mtg/wrappers"
  11. )
  12. const telegramDialTimeout = 10 * time.Second
  13. type baseTelegram struct {
  14. dialer net.Dialer
  15. v4DefaultDC conntypes.DC
  16. V6DefaultDC conntypes.DC
  17. v4Addresses map[conntypes.DC][]string
  18. v6Addresses map[conntypes.DC][]string
  19. }
  20. func (b *baseTelegram) dialToAddress(ctx context.Context,
  21. cancel context.CancelFunc,
  22. addr string) (wrappers.StreamReadWriteCloser, error) {
  23. conn, err := b.dialer.Dial("tcp", addr)
  24. if err != nil {
  25. return nil, fmt.Errorf("dial has failed: %w", err)
  26. }
  27. if err := utils.InitTCP(conn); err != nil {
  28. return nil, fmt.Errorf("cannot initialize tcp socket: %w", err)
  29. }
  30. return wrappers.NewTelegramConn(ctx, cancel, conn), nil
  31. }
  32. func (b *baseTelegram) dial(ctx context.Context,
  33. cancel context.CancelFunc,
  34. dc conntypes.DC,
  35. protocol conntypes.ConnectionProtocol) (wrappers.StreamReadWriteCloser, error) {
  36. addr := ""
  37. switch protocol {
  38. case conntypes.ConnectionProtocolIPv4:
  39. addr = b.chooseAddress(b.v4Addresses, dc, b.v4DefaultDC)
  40. default:
  41. addr = b.chooseAddress(b.v6Addresses, dc, b.V6DefaultDC)
  42. }
  43. return b.dialToAddress(ctx, cancel, addr)
  44. }
  45. func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
  46. dc, defaultDC conntypes.DC) string {
  47. addrs, ok := addresses[dc]
  48. if !ok {
  49. addrs, _ = addresses[defaultDC]
  50. }
  51. if len(addrs) > 0 {
  52. return addrs[rand.Intn(len(addrs))]
  53. }
  54. return ""
  55. }