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

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