Skip to content

Commit

Permalink
Refactor: Fragmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
RPRX authored Jul 6, 2023
1 parent 6d41944 commit f0f3b41
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 218 deletions.
134 changes: 66 additions & 68 deletions infra/conf/freedom.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,91 +39,89 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
}

if c.Fragment != nil {
if len(c.Fragment.Interval) == 0 || len(c.Fragment.Length) == 0 {
return nil, newError("Invalid interval or length")
}
intervalMinMax := strings.Split(c.Fragment.Interval, "-")
var minInterval, maxInterval int64
config.Fragment = new(freedom.Fragment)
var err, err2 error
if len(intervalMinMax) == 2 {
minInterval, err = strconv.ParseInt(intervalMinMax[0], 10, 64)
maxInterval, err2 = strconv.ParseInt(intervalMinMax[1], 10, 64)
} else {
minInterval, err = strconv.ParseInt(intervalMinMax[0], 10, 64)
maxInterval = minInterval
}
if err != nil {
return nil, newError("Invalid minimum interval: ", err).Base(err)
}
if err2 != nil {
return nil, newError("Invalid maximum interval: ", err2).Base(err2)
}

lengthMinMax := strings.Split(c.Fragment.Length, "-")
var minLength, maxLength int64
if len(lengthMinMax) == 2 {
minLength, err = strconv.ParseInt(lengthMinMax[0], 10, 64)
maxLength, err2 = strconv.ParseInt(lengthMinMax[1], 10, 64)

} else {
minLength, err = strconv.ParseInt(lengthMinMax[0], 10, 64)
maxLength = minLength
}
if err != nil {
return nil, newError("Invalid minimum length: ", err).Base(err)
}
if err2 != nil {
return nil, newError("Invalid maximum length: ", err2).Base(err2)
}

if minInterval > maxInterval {
minInterval, maxInterval = maxInterval, minInterval
}
if minLength > maxLength {
minLength, maxLength = maxLength, minLength
}

config.Fragment = &freedom.Fragment{
MinInterval: int32(minInterval),
MaxInterval: int32(maxInterval),
MinLength: int32(minLength),
MaxLength: int32(maxLength),
}

switch strings.ToLower(c.Fragment.Packets) {
case "tlshello":
// TLS Hello Fragmentation (into multiple handshake messages)
config.Fragment.StartPacket = 0
config.Fragment.EndPacket = 1
config.Fragment.PacketsFrom = 0
config.Fragment.PacketsTo = 1
case "":
// TCP Segmentation (all packets)
config.Fragment.StartPacket = 0
config.Fragment.EndPacket = 0
config.Fragment.PacketsFrom = 0
config.Fragment.PacketsTo = 0
default:
// TCP Segmentation (range)
packetRange := strings.Split(c.Fragment.Packets, "-")
var startPacket, endPacket int64
if len(packetRange) == 2 {
startPacket, err = strconv.ParseInt(packetRange[0], 10, 64)
endPacket, err2 = strconv.ParseInt(packetRange[1], 10, 64)
packetsFromTo := strings.Split(c.Fragment.Packets, "-")
if len(packetsFromTo) == 2 {
config.Fragment.PacketsFrom, err = strconv.ParseUint(packetsFromTo[0], 10, 64)
config.Fragment.PacketsTo, err2 = strconv.ParseUint(packetsFromTo[1], 10, 64)
} else {
startPacket, err = strconv.ParseInt(packetRange[0], 10, 64)
endPacket = startPacket
config.Fragment.PacketsFrom, err = strconv.ParseUint(packetsFromTo[0], 10, 64)
config.Fragment.PacketsTo = config.Fragment.PacketsFrom
}
if err != nil {
return nil, newError("Invalid start packet: ", err).Base(err)
return nil, newError("Invalid PacketsFrom").Base(err)
}
if err2 != nil {
return nil, newError("Invalid end packet: ", err2).Base(err2)
return nil, newError("Invalid PacketsTo").Base(err2)
}
if config.Fragment.PacketsFrom > config.Fragment.PacketsTo {
config.Fragment.PacketsFrom, config.Fragment.PacketsTo = config.Fragment.PacketsTo, config.Fragment.PacketsFrom
}
if startPacket > endPacket {
return nil, newError("Invalid packet range: ", c.Fragment.Packets)
if config.Fragment.PacketsFrom == 0 {
return nil, newError("PacketsFrom can't be 0")
}
}

{
if c.Fragment.Length == "" {
return nil, newError("Length can't be empty")
}
lengthMinMax := strings.Split(c.Fragment.Length, "-")
if len(lengthMinMax) == 2 {
config.Fragment.LengthMin, err = strconv.ParseUint(lengthMinMax[0], 10, 64)
config.Fragment.LengthMax, err2 = strconv.ParseUint(lengthMinMax[1], 10, 64)
} else {
config.Fragment.LengthMin, err = strconv.ParseUint(lengthMinMax[0], 10, 64)
config.Fragment.LengthMax = config.Fragment.LengthMin
}
if err != nil {
return nil, newError("Invalid LengthMin").Base(err)
}
if err2 != nil {
return nil, newError("Invalid LengthMax").Base(err2)
}
if config.Fragment.LengthMin > config.Fragment.LengthMax {
config.Fragment.LengthMin, config.Fragment.LengthMax = config.Fragment.LengthMax, config.Fragment.LengthMin
}
if config.Fragment.LengthMin == 0 {
return nil, newError("LengthMin can't be 0")
}
}

{
if c.Fragment.Interval == "" {
return nil, newError("Interval can't be empty")
}
intervalMinMax := strings.Split(c.Fragment.Interval, "-")
if len(intervalMinMax) == 2 {
config.Fragment.IntervalMin, err = strconv.ParseUint(intervalMinMax[0], 10, 64)
config.Fragment.IntervalMax, err2 = strconv.ParseUint(intervalMinMax[1], 10, 64)
} else {
config.Fragment.IntervalMin, err = strconv.ParseUint(intervalMinMax[0], 10, 64)
config.Fragment.IntervalMax = config.Fragment.IntervalMin
}
if err != nil {
return nil, newError("Invalid IntervalMin").Base(err)
}
if err2 != nil {
return nil, newError("Invalid IntervalMax").Base(err2)
}
if startPacket < 1 {
return nil, newError("Cannot start from packet 0")
if config.Fragment.IntervalMin > config.Fragment.IntervalMax {
config.Fragment.IntervalMin, config.Fragment.IntervalMax = config.Fragment.IntervalMax, config.Fragment.IntervalMin
}
config.Fragment.StartPacket = int32(startPacket)
config.Fragment.EndPacket = int32(endPacket)
}
}

Expand Down
62 changes: 31 additions & 31 deletions proxy/freedom/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions proxy/freedom/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ message DestinationOverride {
}

message Fragment {
int32 min_interval = 1;
int32 max_interval = 2;
int32 min_length = 3;
int32 max_length = 4;
int32 start_packet = 5;
int32 end_packet = 6;
uint64 packets_from = 1;
uint64 packets_to = 2;
uint64 length_min = 3;
uint64 length_max = 4;
uint64 interval_min = 5;
uint64 interval_max = 6;
}

message Config {
Expand Down
Loading

0 comments on commit f0f3b41

Please sign in to comment.