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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. )
  36. // ConnectionProtocol* define which connection protocols to use.
  37. // ConnectionProtocolAny means that any is suitable.
  38. const (
  39. ConnectionProtocolIPv4 ConnectionProtocol = 1
  40. ConnectionProtocolIPv6 = ConnectionProtocolIPv4 << 1
  41. ConnectionProtocolAny = ConnectionProtocolIPv4 | ConnectionProtocolIPv6
  42. )
  43. // Connection tags for mtproto handshakes.
  44. var (
  45. ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
  46. ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
  47. )
  48. // Tag maps connection type to the corresponding handshake tag.
  49. func (t ConnectionType) Tag() ([]byte, error) {
  50. switch t {
  51. case ConnectionTypeAbridged:
  52. return ConnectionTagAbridged, nil
  53. case ConnectionTypeIntermediate:
  54. return ConnectionTagIntermediate, nil
  55. default:
  56. return nil, errors.Errorf("Unknown connection type %d", t)
  57. }
  58. }
  59. // ConnectionTagFromHandshake maps magic bytes to the connection type.
  60. func ConnectionTagFromHandshake(magic []byte) (ConnectionType, error) {
  61. if bytes.Equal(magic, ConnectionTagIntermediate) {
  62. return ConnectionTypeIntermediate, nil
  63. }
  64. if bytes.Equal(magic, ConnectionTagAbridged) {
  65. return ConnectionTypeAbridged, nil
  66. }
  67. return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
  68. }