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 kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

stream_context.go 1.4KB

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