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ů.

cloak.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package faketls
  2. import (
  3. "context"
  4. "io"
  5. "sync"
  6. "time"
  7. "github.com/9seconds/mtg/wrappers/rwc"
  8. )
  9. const (
  10. cloakLastActivityTimeout = 5 * time.Second
  11. cloakMaxTimeout = 30 * time.Second
  12. )
  13. func cloak(one, another io.ReadWriteCloser) {
  14. defer func() {
  15. one.Close()
  16. another.Close()
  17. }()
  18. channelPing := make(chan struct{}, 1)
  19. ctx, cancel := context.WithCancel(context.Background())
  20. one = rwc.NewPing(ctx, one, channelPing)
  21. another = rwc.NewPing(ctx, another, channelPing)
  22. wg := &sync.WaitGroup{}
  23. wg.Add(2)
  24. go func() {
  25. defer wg.Done()
  26. io.Copy(one, another) // nolint: errcheck
  27. }()
  28. go func() {
  29. defer wg.Done()
  30. io.Copy(another, one) // nolint: errcheck
  31. }()
  32. go func() {
  33. wg.Wait()
  34. cancel()
  35. }()
  36. go func() {
  37. lastActivityTimer := time.NewTimer(cloakLastActivityTimeout)
  38. defer lastActivityTimer.Stop()
  39. maxTimer := time.NewTimer(cloakMaxTimeout)
  40. defer maxTimer.Stop()
  41. for {
  42. select {
  43. case <-channelPing:
  44. lastActivityTimer.Stop()
  45. lastActivityTimer = time.NewTimer(cloakLastActivityTimeout)
  46. case <-ctx.Done():
  47. return
  48. case <-lastActivityTimer.C:
  49. cancel()
  50. return
  51. case <-maxTimer.C:
  52. cancel()
  53. return
  54. }
  55. }
  56. }()
  57. <-ctx.Done()
  58. }