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 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. type ConnectionProtocol uint8
  11. type Hacks struct {
  12. SimpleAck bool
  13. QuickAck bool
  14. }
  15. // ConnectionOpts presents an options, metadata on connection requested
  16. // by the user on handshake.
  17. type ConnectionOpts struct {
  18. DC int16
  19. ConnectionType ConnectionType
  20. ConnectionProto ConnectionProtocol
  21. ReadHacks Hacks
  22. WriteHacks Hacks
  23. ClientAddr *net.TCPAddr
  24. }
  25. // Different connection types which user requests from Telegram.
  26. const (
  27. ConnectionTypeUnknown ConnectionType = iota
  28. ConnectionTypeAbridged
  29. ConnectionTypeIntermediate
  30. )
  31. const (
  32. ConnectionProtocolIPv4 ConnectionProtocol = 1
  33. ConnectionProtocolIPv6 = ConnectionProtocolIPv4 << 1
  34. ConnectionProtocolAny = ConnectionProtocolIPv4 | ConnectionProtocolIPv6
  35. )
  36. // Connection tags for mtproto handshakes.
  37. var (
  38. ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
  39. ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
  40. )
  41. // Tag maps connection type to the corresponding handshake tag.
  42. func (t ConnectionType) Tag() ([]byte, error) {
  43. switch t {
  44. case ConnectionTypeAbridged:
  45. return ConnectionTagAbridged, nil
  46. case ConnectionTypeIntermediate:
  47. return ConnectionTagIntermediate, nil
  48. default:
  49. return nil, errors.Errorf("Unknown connection type %d", t)
  50. }
  51. }
  52. // ConnectionTagFromHandshake maps magic bytes to the connection type.
  53. func ConnectionTagFromHandshake(magic []byte) (ConnectionType, error) {
  54. if bytes.Equal(magic, ConnectionTagIntermediate) {
  55. return ConnectionTypeIntermediate, nil
  56. }
  57. if bytes.Equal(magic, ConnectionTagAbridged) {
  58. return ConnectionTypeAbridged, nil
  59. }
  60. return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
  61. }