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.

connection_options.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package mtproto
  2. import (
  3. "bytes"
  4. "net"
  5. "github.com/juju/errors"
  6. )
  7. // ConnectionType is a type of obfuscated2/mtproto connection requested
  8. // by the user.
  9. type ConnectionType uint8
  10. // ConnectionProtocol is a type of IP protocol to use.
  11. type ConnectionProtocol uint8
  12. // Hacks is a simple structure to store flags for packet transmission.
  13. type Hacks struct {
  14. SimpleAck bool
  15. QuickAck bool
  16. }
  17. // ConnectionOpts presents an options, metadata on connection requested
  18. // by the user on handshake.
  19. type ConnectionOpts struct {
  20. DC int16
  21. ConnectionType ConnectionType
  22. ConnectionProto ConnectionProtocol
  23. // Read and Write means direction related to the client.
  24. // ReadHacks are meant to be flushed on client read
  25. // WriteHacks are meant to be flushed on client write.
  26. ReadHacks Hacks
  27. WriteHacks Hacks
  28. ClientAddr *net.TCPAddr
  29. }
  30. // Different connection types which user requests from Telegram.
  31. const (
  32. ConnectionTypeUnknown ConnectionType = iota
  33. ConnectionTypeAbridged
  34. ConnectionTypeIntermediate
  35. ConnectionTypeSecure
  36. )
  37. // ConnectionProtocol* define which connection protocols to use.
  38. // ConnectionProtocolAny means that any is suitable.
  39. const (
  40. ConnectionProtocolIPv4 ConnectionProtocol = 1
  41. ConnectionProtocolIPv6 = ConnectionProtocolIPv4 << 1
  42. ConnectionProtocolAny = ConnectionProtocolIPv4 | ConnectionProtocolIPv6
  43. )
  44. // Connection tags for mtproto handshakes.
  45. var (
  46. ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
  47. ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
  48. ConnectionTagSecure = []byte{0xdd, 0xdd, 0xdd, 0xdd}
  49. )
  50. // Tag maps connection type to the corresponding handshake tag.
  51. func (t ConnectionType) Tag() ([]byte, error) {
  52. switch t {
  53. case ConnectionTypeAbridged:
  54. return ConnectionTagAbridged, nil
  55. case ConnectionTypeIntermediate:
  56. return ConnectionTagIntermediate, nil
  57. case ConnectionTypeSecure:
  58. return ConnectionTagSecure, nil
  59. default:
  60. return nil, errors.Errorf("Unknown connection type %d", t)
  61. }
  62. }
  63. // ConnectionTagFromHandshake maps magic bytes to the connection type.
  64. func ConnectionTagFromHandshake(magic []byte) (ConnectionType, error) {
  65. if bytes.Equal(magic, ConnectionTagIntermediate) {
  66. return ConnectionTypeIntermediate, nil
  67. }
  68. if bytes.Equal(magic, ConnectionTagAbridged) {
  69. return ConnectionTypeAbridged, nil
  70. }
  71. if bytes.Equal(magic, ConnectionTagSecure) {
  72. return ConnectionTypeSecure, nil
  73. }
  74. return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
  75. }