mirror of
https://github.com/MetaCubeX/Clash.Meta.git
synced 2025-04-19 08:40:57 +00:00
fix: panic in ssr packet
This commit is contained in:
parent
d0d0c392d7
commit
b21b8ee046
1 changed files with 14 additions and 2 deletions
|
@ -29,6 +29,18 @@ func Pack(dst, plaintext []byte, s Cipher) ([]byte, error) {
|
|||
return dst[:len(iv)+len(plaintext)], nil
|
||||
}
|
||||
|
||||
// UnpackInplace decrypts pkt using stream cipher s.
|
||||
// Returns a slice of pkt containing decrypted plaintext.
|
||||
// Note: The data in the input dst will be changed
|
||||
func UnpackInplace(pkt []byte, s Cipher) ([]byte, error) {
|
||||
if len(pkt) < s.IVSize() {
|
||||
return nil, ErrShortPacket
|
||||
}
|
||||
iv, dst := pkt[:s.IVSize()], pkt[s.IVSize():]
|
||||
s.Decrypter(iv).XORKeyStream(dst, dst)
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// Unpack decrypts pkt using stream cipher s.
|
||||
// Returns a slice of dst containing decrypted plaintext.
|
||||
func Unpack(dst, pkt []byte, s Cipher) ([]byte, error) {
|
||||
|
@ -71,7 +83,7 @@ func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|||
if err != nil {
|
||||
return n, addr, err
|
||||
}
|
||||
bb, err := Unpack(b[c.IVSize():], b[:n], c.Cipher)
|
||||
bb, err := UnpackInplace(b[:n], c.Cipher)
|
||||
if err != nil {
|
||||
return n, addr, err
|
||||
}
|
||||
|
@ -84,7 +96,7 @@ func (c *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
data, err = Unpack(data[c.IVSize():], data, c)
|
||||
data, err = UnpackInplace(data, c.Cipher)
|
||||
if err != nil {
|
||||
if put != nil {
|
||||
put()
|
||||
|
|
Loading…
Add table
Reference in a new issue