mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Add packet padding container for packet length shaping
This commit is contained in:
parent
c1ac2aa577
commit
9e1cc35878
3 changed files with 194 additions and 0 deletions
47
common/packetPaddingContainer/container.go
Normal file
47
common/packetPaddingContainer/container.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package packetPaddingContainer
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
func New() PacketPaddingContainer {
|
||||
return packetPaddingContainer{}
|
||||
}
|
||||
|
||||
type packetPaddingContainer struct {
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Pack(data_OWNERSHIP_RELINQUISHED []byte, padding int) []byte {
|
||||
data := append(data_OWNERSHIP_RELINQUISHED, make([]byte, padding)...)
|
||||
data_length := len(data_OWNERSHIP_RELINQUISHED)
|
||||
data = append(data, byte(data_length>>8), byte(data_length))
|
||||
return data
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Pad(padding int) []byte {
|
||||
if assertPaddingLengthIsNotNegative := padding < 0; assertPaddingLengthIsNotNegative {
|
||||
return nil
|
||||
}
|
||||
switch padding {
|
||||
case 0:
|
||||
return []byte{}
|
||||
case 1:
|
||||
return []byte{0}
|
||||
case 2:
|
||||
return []byte{0, 0}
|
||||
default:
|
||||
return append(make([]byte, padding-2), byte(padding>>8), byte(padding))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c packetPaddingContainer) Unpack(wrappedData_OWNERSHIP_RELINQUISHED []byte) ([]byte, int) {
|
||||
if len(wrappedData_OWNERSHIP_RELINQUISHED) < 2 {
|
||||
return nil, len(wrappedData_OWNERSHIP_RELINQUISHED)
|
||||
}
|
||||
wrappedData_tail := wrappedData_OWNERSHIP_RELINQUISHED[len(wrappedData_OWNERSHIP_RELINQUISHED)-2:]
|
||||
dataLength := int(binary.BigEndian.Uint16(wrappedData_tail))
|
||||
paddingLength := len(wrappedData_OWNERSHIP_RELINQUISHED) - dataLength - 2
|
||||
if paddingLength < 0 {
|
||||
return nil, paddingLength
|
||||
}
|
||||
return wrappedData_OWNERSHIP_RELINQUISHED[:dataLength], paddingLength
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue