mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 05:54:01 +03:00
Support different client connection types
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package mtproto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
// ConnectionType is a type of obfuscated2/mtproto connection requested
|
||||
// by the user.
|
||||
type ConnectionType uint8
|
||||
|
||||
// ConnectionOpts presents an options, metadata on connection requested
|
||||
// by the user on handshake.
|
||||
type ConnectionOpts struct {
|
||||
DC int16
|
||||
ConnectionType ConnectionType
|
||||
}
|
||||
|
||||
// Different connection types which user requests from Telegram.
|
||||
const (
|
||||
ConnectionTypeUnknown ConnectionType = iota
|
||||
ConnectionTypeAbridged
|
||||
ConnectionTypeIntermediate
|
||||
)
|
||||
|
||||
// Connection tags for mtproto handshakes.
|
||||
var (
|
||||
ConnectionTagAbridged = []byte{0xef, 0xef, 0xef, 0xef}
|
||||
ConnectionTagIntermediate = []byte{0xee, 0xee, 0xee, 0xee}
|
||||
)
|
||||
|
||||
// Tag maps connection type to the corresponding handshake tag.
|
||||
func (t ConnectionType) Tag() ([]byte, error) {
|
||||
switch t {
|
||||
case ConnectionTypeAbridged:
|
||||
return ConnectionTagAbridged, nil
|
||||
case ConnectionTypeIntermediate:
|
||||
return ConnectionTagIntermediate, nil
|
||||
default:
|
||||
return nil, errors.Errorf("Unknown connection type %d", t)
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectionTagFromHandshake maps magic bytes to the connection type.
|
||||
func ConnectionTagFromHandshake(magic []byte) (ConnectionType, error) {
|
||||
if bytes.Equal(magic, ConnectionTagIntermediate) {
|
||||
return ConnectionTypeIntermediate, nil
|
||||
}
|
||||
if bytes.Equal(magic, ConnectionTagAbridged) {
|
||||
return ConnectionTypeAbridged, nil
|
||||
}
|
||||
|
||||
return ConnectionTypeUnknown, errors.New("Unknown handshake protocol")
|
||||
}
|
||||
Reference in New Issue
Block a user