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.2KB

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