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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

stream_context.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. logger Logger
  15. }
  16. func (s *streamContext) Deadline() (time.Time, bool) {
  17. return s.ctx.Deadline()
  18. }
  19. func (s *streamContext) Done() <-chan struct{} {
  20. return s.ctx.Done()
  21. }
  22. func (s *streamContext) Err() error {
  23. return s.ctx.Err()
  24. }
  25. func (s *streamContext) Value(key interface{}) interface{} {
  26. return s.ctx.Value(key)
  27. }
  28. func (s *streamContext) Close() {
  29. s.ctxCancel()
  30. s.clientConn.Close()
  31. }
  32. func (s *streamContext) ClientIP() net.IP {
  33. return s.clientConn.RemoteAddr().(*net.TCPAddr).IP
  34. }
  35. func newStreamContext(ctx context.Context, logger Logger, clientConn net.Conn) *streamContext {
  36. connIDBytes := make([]byte, 16)
  37. if _, err := rand.Read(connIDBytes); err != nil {
  38. panic(err)
  39. }
  40. ctx, cancel := context.WithCancel(ctx)
  41. streamCtx := &streamContext{
  42. ctx: ctx,
  43. ctxCancel: cancel,
  44. clientConn: clientConn,
  45. connID: base64.RawURLEncoding.EncodeToString(connIDBytes),
  46. }
  47. streamCtx.logger = logger.
  48. BindStr("stream-id", streamCtx.connID).
  49. BindStr("client-ip", streamCtx.ClientIP().String())
  50. return streamCtx
  51. }