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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

stream_context.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package mtglib
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "encoding/base64"
  6. "net"
  7. "time"
  8. "github.com/9seconds/mtg/v2/essentials"
  9. )
  10. type streamContext struct {
  11. ctx context.Context
  12. ctxCancel context.CancelFunc
  13. clientConn essentials.Conn
  14. telegramConn essentials.Conn
  15. streamID string
  16. dc int
  17. matchedSecretKey []byte
  18. secretName string
  19. logger Logger
  20. }
  21. func (s *streamContext) Deadline() (time.Time, bool) {
  22. return s.ctx.Deadline()
  23. }
  24. func (s *streamContext) Done() <-chan struct{} {
  25. return s.ctx.Done()
  26. }
  27. func (s *streamContext) Err() error {
  28. return s.ctx.Err() //nolint: wrapcheck
  29. }
  30. func (s *streamContext) Value(key any) any {
  31. return s.ctx.Value(key)
  32. }
  33. func (s *streamContext) Close() {
  34. s.ctxCancel()
  35. if s.clientConn != nil {
  36. s.clientConn.Close() //nolint: errcheck
  37. }
  38. if s.telegramConn != nil {
  39. s.telegramConn.Close() //nolint: errcheck
  40. }
  41. }
  42. func (s *streamContext) ClientIP() net.IP {
  43. return s.clientConn.RemoteAddr().(*net.TCPAddr).IP //nolint: forcetypeassert
  44. }
  45. func newStreamContext(ctx context.Context, logger Logger, clientConn essentials.Conn) *streamContext {
  46. connIDBytes := make([]byte, ConnectionIDBytesLength)
  47. if _, err := rand.Read(connIDBytes); err != nil {
  48. panic(err)
  49. }
  50. ctx, cancel := context.WithCancel(ctx)
  51. streamCtx := &streamContext{
  52. ctx: ctx,
  53. ctxCancel: cancel,
  54. clientConn: clientConn,
  55. streamID: base64.RawURLEncoding.EncodeToString(connIDBytes),
  56. }
  57. streamCtx.logger = logger.
  58. BindStr("stream-id", streamCtx.streamID).
  59. BindStr("client-ip", streamCtx.ClientIP().String())
  60. return streamCtx
  61. }