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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package mtglib
  2. import (
  3. "context"
  4. "errors"
  5. "net"
  6. "net/http"
  7. "time"
  8. )
  9. var (
  10. ErrSecretEmpty = errors.New("secret is empty")
  11. ErrSecretInvalid = errors.New("secret is invalid")
  12. ErrNetworkIsNotDefined = errors.New("network is not defined")
  13. ErrAntiReplayCacheIsNotDefined = errors.New("anti-replay cache is not defined")
  14. ErrTimeAttackDetectorIsNotDefined = errors.New("time attack detector is not defined")
  15. ErrIPBlocklistIsNotDefined = errors.New("ip blocklist is not defined")
  16. ErrEventStreamIsNotDefined = errors.New("event stream is not defined")
  17. ErrLoggerIsNotDefined = errors.New("logger is not defined")
  18. )
  19. const (
  20. DefaultConcurrency = 4096
  21. DefaultBufferSize = 16 * 1024 // 16 kib
  22. DefaultDomainFrontingPort = 443
  23. DefaultIdleTimeout = time.Minute
  24. DefaultPreferIP = "prefer-ipv6"
  25. )
  26. type Network interface {
  27. Dial(network, address string) (net.Conn, error)
  28. DialContext(ctx context.Context, network, address string) (net.Conn, error)
  29. MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
  30. }
  31. // AntiReplayCache is an interface that is used to detect replay attacks
  32. // based on some traffic fingerprints.
  33. //
  34. // Replay attacks are probe attacks whose main goal is to identify if
  35. // server software can be classified in some way. For example, if you
  36. // send some HTTP request to a web server, then you can expect that this
  37. // server will respond with HTTP response back.
  38. //
  39. // There is a problem though. Let's imagine, that connection is
  40. // encrypted. Let's imagine, that it is encrypted with some static key
  41. // like ShadowSocks (https://shadowsocks.org/assets/whitepaper.pdf).
  42. // In that case, in theory, if you repeat the same bytes, you can get
  43. // the same responses. Let's imagine, that you've cracked the key. then
  44. // if you send the same bytes, you can decrypt a response and see its
  45. // structure. Based on its structure you can identify if this server is
  46. // SOCKS5, MTPROTO proxy etc.
  47. //
  48. // This is just one example, maybe not the best or not the most
  49. // relevant. In real life, different organizations use such replay
  50. // attacks to perform some reverse engineering of the proxy, do some
  51. // statical analysis to identify server software.
  52. //
  53. // There are many ways how to protect your proxy against them. One
  54. // is domain fronting which is a core part of mtg. Another one is to
  55. // collect some 'handshake fingerprints' and forbid duplication.
  56. //
  57. // So, it one is sending the same byte flow right after you (or a couple
  58. // of hours after), mtg should detect that and reject this connection
  59. // (or redirect to fronting domain).
  60. type AntiReplayCache interface {
  61. // Seen before checks if this set of bytes was observed before or
  62. // not. If it is required to store this information somewhere else,
  63. // then it has to do that.
  64. SeenBefore(data []byte) bool
  65. }
  66. type IPBlocklist interface {
  67. Contains(net.IP) bool
  68. }
  69. type Event interface {
  70. StreamID() string
  71. Timestamp() time.Time
  72. }
  73. type EventStream interface {
  74. Send(context.Context, Event)
  75. }
  76. type TimeAttackDetector interface {
  77. Valid(time.Time) error
  78. }
  79. type Logger interface {
  80. Named(name string) Logger
  81. BindInt(name string, value int) Logger
  82. BindStr(name, value string) Logger
  83. Printf(format string, args ...interface{})
  84. Info(msg string)
  85. InfoError(msg string, err error)
  86. Warning(msg string)
  87. WarningError(msg string, err error)
  88. Debug(msg string)
  89. DebugError(msg string, err error)
  90. }