|
|
@ -4,6 +4,7 @@ import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"hash/crc32"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
|
@ -236,19 +237,26 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DecodeUDPPacket(validator *Validator, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
|
|
|
|
func DecodeUDPPacket(validator *Validator, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
|
|
|
|
bs := payload.Bytes()
|
|
|
|
rawPayload := payload.Bytes()
|
|
|
|
if len(bs) <= 32 {
|
|
|
|
user, _, d, _, err := validator.Get(rawPayload, protocol.RequestCommandUDP)
|
|
|
|
return nil, nil, newError("len(bs) <= 32")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user, _, d, _, err := validator.Get(bs, protocol.RequestCommandUDP)
|
|
|
|
if errors.Is(err, ErrIVNotUnique) {
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
case ErrIVNotUnique:
|
|
|
|
|
|
|
|
return nil, nil, newError("failed iv check").Base(err)
|
|
|
|
return nil, nil, newError("failed iv check").Base(err)
|
|
|
|
case ErrNotFound:
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if errors.Is(err, ErrNotFound) {
|
|
|
|
return nil, nil, newError("failed to match an user").Base(err)
|
|
|
|
return nil, nil, newError("failed to match an user").Base(err)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
account := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, nil, newError("unexpected error").Base(err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
account, ok := user.Account.(*MemoryAccount)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return nil, nil, newError("expected MemoryAccount returned from validator")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if account.Cipher.IsAEAD() {
|
|
|
|
if account.Cipher.IsAEAD() {
|
|
|
|
payload.Clear()
|
|
|
|
payload.Clear()
|
|
|
|
payload.Write(d)
|
|
|
|
payload.Write(d)
|
|
|
@ -261,13 +269,6 @@ func DecodeUDPPacket(validator *Validator, payload *buf.Buffer) (*protocol.Reque
|
|
|
|
return nil, nil, newError("failed to decrypt UDP payload").Base(err)
|
|
|
|
return nil, nil, newError("failed to decrypt UDP payload").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
|
|
|
|
Version: Version,
|
|
|
|
|
|
|
|
User: user,
|
|
|
|
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
payload.SetByte(0, payload.Byte(0)&0x0F)
|
|
|
|
payload.SetByte(0, payload.Byte(0)&0x0F)
|
|
|
|
|
|
|
|
|
|
|
@ -276,8 +277,13 @@ func DecodeUDPPacket(validator *Validator, payload *buf.Buffer) (*protocol.Reque
|
|
|
|
return nil, nil, newError("failed to parse address").Base(err)
|
|
|
|
return nil, nil, newError("failed to parse address").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
request.Address = addr
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
request.Port = port
|
|
|
|
Version: Version,
|
|
|
|
|
|
|
|
User: user,
|
|
|
|
|
|
|
|
Command: protocol.RequestCommandUDP,
|
|
|
|
|
|
|
|
Address: addr,
|
|
|
|
|
|
|
|
Port: port,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return request, payload, nil
|
|
|
|
return request, payload, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|