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 kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cloak.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 cloakPipe(one, another, wg)
  25. go cloakPipe(another, one, wg)
  26. go func() {
  27. wg.Wait()
  28. cancel()
  29. }()
  30. go func() {
  31. lastActivityTimer := time.NewTimer(cloakLastActivityTimeout)
  32. defer lastActivityTimer.Stop()
  33. maxTimer := time.NewTimer(cloakMaxTimeout)
  34. defer maxTimer.Stop()
  35. for {
  36. select {
  37. case <-channelPing:
  38. lastActivityTimer.Stop()
  39. lastActivityTimer = time.NewTimer(cloakLastActivityTimeout)
  40. case <-ctx.Done():
  41. return
  42. case <-lastActivityTimer.C:
  43. cancel()
  44. return
  45. case <-maxTimer.C:
  46. cancel()
  47. return
  48. }
  49. }
  50. }()
  51. <-ctx.Done()
  52. }
  53. func cloakPipe(one io.Writer, another io.Reader, wg *sync.WaitGroup) {
  54. defer wg.Done()
  55. io.Copy(one, another) // nolint: errcheck
  56. }