mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:14:02 +03:00
Add client abridged protocol
This commit is contained in:
+20
-20
@@ -5,53 +5,53 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type proxyRequestFlags uint32
|
||||
type ProxyRequestFlags uint32
|
||||
|
||||
const (
|
||||
proxyRequestFlagsHasAdTag proxyRequestFlags = 0x8
|
||||
proxyRequestFlagsEncrypted proxyRequestFlags = 0x2
|
||||
proxyRequestFlagsMagic proxyRequestFlags = 0x1000
|
||||
proxyRequestFlagsExtMode2 proxyRequestFlags = 0x20000
|
||||
proxyRequestFlagsIntermediate proxyRequestFlags = 0x20000000
|
||||
proxyRequestFlagsAbdridged proxyRequestFlags = 0x40000000
|
||||
proxyRequestFlagsQuickAck proxyRequestFlags = 0x80000000
|
||||
proxyRequestFlagsPad proxyRequestFlags = 0x8000000
|
||||
ProxyRequestFlagsHasAdTag ProxyRequestFlags = 0x8
|
||||
ProxyRequestFlagsEncrypted ProxyRequestFlags = 0x2
|
||||
ProxyRequestFlagsMagic ProxyRequestFlags = 0x1000
|
||||
ProxyRequestFlagsExtMode2 ProxyRequestFlags = 0x20000
|
||||
ProxyRequestFlagsIntermediate ProxyRequestFlags = 0x20000000
|
||||
ProxyRequestFlagsAbdridged ProxyRequestFlags = 0x40000000
|
||||
ProxyRequestFlagsQuickAck ProxyRequestFlags = 0x80000000
|
||||
ProxyRequestFlagsPad ProxyRequestFlags = 0x8000000
|
||||
)
|
||||
|
||||
var proxyRequestFlagsEncryptedPrefix [8]byte
|
||||
var ProxyRequestFlagsEncryptedPrefix [8]byte
|
||||
|
||||
func (r proxyRequestFlags) Bytes() []byte {
|
||||
func (r ProxyRequestFlags) Bytes() []byte {
|
||||
converted := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(converted, uint32(r))
|
||||
|
||||
return converted
|
||||
}
|
||||
|
||||
func (r proxyRequestFlags) String() string {
|
||||
func (r ProxyRequestFlags) String() string {
|
||||
flags := make([]string, 0, 7)
|
||||
|
||||
if r&proxyRequestFlagsHasAdTag != 0 {
|
||||
if r&ProxyRequestFlagsHasAdTag != 0 {
|
||||
flags = append(flags, "HAS_AD_TAG")
|
||||
}
|
||||
if r&proxyRequestFlagsEncrypted != 0 {
|
||||
if r&ProxyRequestFlagsEncrypted != 0 {
|
||||
flags = append(flags, "ENCRYPTED")
|
||||
}
|
||||
if r&proxyRequestFlagsMagic != 0 {
|
||||
if r&ProxyRequestFlagsMagic != 0 {
|
||||
flags = append(flags, "MAGIC")
|
||||
}
|
||||
if r&proxyRequestFlagsExtMode2 != 0 {
|
||||
if r&ProxyRequestFlagsExtMode2 != 0 {
|
||||
flags = append(flags, "EXT_MODE_2")
|
||||
}
|
||||
if r&proxyRequestFlagsIntermediate != 0 {
|
||||
if r&ProxyRequestFlagsIntermediate != 0 {
|
||||
flags = append(flags, "INTERMEDIATE")
|
||||
}
|
||||
if r&proxyRequestFlagsAbdridged != 0 {
|
||||
if r&ProxyRequestFlagsAbdridged != 0 {
|
||||
flags = append(flags, "ABRIDGED")
|
||||
}
|
||||
if r&proxyRequestFlagsQuickAck != 0 {
|
||||
if r&ProxyRequestFlagsQuickAck != 0 {
|
||||
flags = append(flags, "QUICK_ACK")
|
||||
}
|
||||
if r&proxyRequestFlagsPad != 0 {
|
||||
if r&ProxyRequestFlagsPad != 0 {
|
||||
flags = append(flags, "PAD")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/9seconds/mtg/conntypes"
|
||||
)
|
||||
|
||||
type ProxyResponseType uint8
|
||||
|
||||
const (
|
||||
ProxyResponseTypeAns ProxyResponseType = iota
|
||||
ProxyResponseTypeSimpleAck
|
||||
ProxyResponseTypeCloseExt
|
||||
)
|
||||
|
||||
type ProxyResponse struct {
|
||||
Type ProxyResponseType
|
||||
ConnID conntypes.ConnID
|
||||
Payload conntypes.Packet
|
||||
}
|
||||
|
||||
func ParseProxyResponse(packet conntypes.Packet) (*ProxyResponse, error) {
|
||||
var response ProxyResponse
|
||||
|
||||
if len(packet) < 4 {
|
||||
return nil, fmt.Errorf("incorrect packet length: %d", len(packet))
|
||||
}
|
||||
|
||||
tag := packet[:4]
|
||||
switch {
|
||||
case bytes.Equal(tag, TagProxyAns):
|
||||
response.Type = ProxyResponseTypeAns
|
||||
copy(response.ConnID[:], packet[8:16])
|
||||
response.Payload = packet[16:]
|
||||
return &response, nil
|
||||
|
||||
case bytes.Equal(tag, TagSimpleAck):
|
||||
response.Type = ProxyResponseTypeSimpleAck
|
||||
copy(response.ConnID[:], packet[4:12])
|
||||
response.Payload = packet[12:]
|
||||
return &response, nil
|
||||
|
||||
case bytes.Equal(tag, TagCloseExt):
|
||||
response.Type = ProxyResponseTypeCloseExt
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unknown response type %x", tag)
|
||||
}
|
||||
Reference in New Issue
Block a user