Skip to content

Commit

Permalink
UDP noise (#3711)
Browse files Browse the repository at this point in the history
* added udp noise

* adding protobuf settings

* freedom json parser and clean up

* resolve confict

* fix and clean up

* use net.conn instead of packetconnwrapper

* avoid constructing SequentialWriter directly

---------

Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>
  • Loading branch information
dragonbreath2000 and mmmray authored Aug 28, 2024
1 parent 8674ed5 commit 002d08b
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 37 deletions.
76 changes: 76 additions & 0 deletions infra/conf/freedom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type FreedomConfig struct {
Redirect string `json:"redirect"`
UserLevel uint32 `json:"userLevel"`
Fragment *Fragment `json:"fragment"`
Noise *Noise `json:"noise"`
ProxyProtocol uint32 `json:"proxyProtocol"`
}

Expand All @@ -27,6 +28,11 @@ type Fragment struct {
Interval string `json:"interval"`
}

type Noise struct {
Packet string `json:"packet"`
Delay string `json:"delay"`
}

// Build implements Buildable
func (c *FreedomConfig) Build() (proto.Message, error) {
config := new(freedom.Config)
Expand Down Expand Up @@ -143,6 +149,76 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
}
}
}
if c.Noise != nil {
config.Noise = new(freedom.Noise)
var err, err2 error
p := strings.Split(strings.ToLower(c.Noise.Packet), ":")
if len(p) != 2 {
return nil, errors.New("invalid type for packet")
}
switch p[0] {
case "rand":
randValue := strings.Split(p[1], "-")
if len(randValue) > 2 {
return nil, errors.New("Only 2 values are allowed for rand")
}
if len(randValue) == 2 {
config.Noise.LengthMin, err = strconv.ParseUint(randValue[0], 10, 64)
config.Noise.LengthMax, err2 = strconv.ParseUint(randValue[1], 10, 64)
}
if len(randValue) == 1 {
config.Noise.LengthMin, err = strconv.ParseUint(randValue[0], 10, 64)
config.Noise.LengthMax = config.Noise.LengthMin
}
if err != nil {
return nil, errors.New("invalid value for rand LengthMin").Base(err)
}
if err2 != nil {
return nil, errors.New("invalid value for rand LengthMax").Base(err2)
}
if config.Noise.LengthMin > config.Noise.LengthMax {
config.Noise.LengthMin, config.Noise.LengthMax = config.Noise.LengthMax, config.Noise.LengthMin
}
if config.Noise.LengthMin == 0 {
return nil, errors.New("rand lengthMin or lengthMax cannot be 0")
}

case "str":
//user input string
config.Noise.StrNoise = strings.TrimSpace(p[1])

default:
return nil, errors.New("Invalid packet,only rand and str are supported")
}
if c.Noise.Delay != "" {
d := strings.Split(strings.ToLower(c.Noise.Delay), "-")
if len(d) > 2 {
return nil, errors.New("Invalid delay value")
}
if len(d) == 2 {
config.Noise.DelayMin, err = strconv.ParseUint(d[0], 10, 64)
config.Noise.DelayMax, err2 = strconv.ParseUint(d[1], 10, 64)

} else {
config.Noise.DelayMin, err = strconv.ParseUint(d[0], 10, 64)
config.Noise.DelayMax = config.Noise.DelayMin
}
if err != nil {
return nil, errors.New("Invalid value for DelayMin").Base(err)
}
if err2 != nil {
return nil, errors.New("Invalid value for DelayMax").Base(err2)
}
if config.Noise.DelayMin > config.Noise.DelayMax {
config.Noise.DelayMin, config.Noise.DelayMax = config.Noise.DelayMax, config.Noise.DelayMin
}
if config.Noise.DelayMin == 0 {
return nil, errors.New("DelayMin or DelayMax cannot be 0")
}
} else {
config.Noise.DelayMin = 0
}
}

if c.Timeout != nil {
config.Timeout = *c.Timeout
Expand Down
188 changes: 151 additions & 37 deletions proxy/freedom/config.pb.go

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

8 changes: 8 additions & 0 deletions proxy/freedom/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ message Fragment {
uint64 interval_min = 5;
uint64 interval_max = 6;
}
message Noise {
uint64 length_min = 1;
uint64 length_max = 2;
uint64 delay_min = 3;
uint64 delay_max = 4;
string str_noise = 5;
}

message Config {
enum DomainStrategy {
Expand All @@ -41,4 +48,5 @@ message Config {
uint32 user_level = 4;
Fragment fragment = 5;
uint32 proxy_protocol = 6;
Noise noise = 7;
}
Loading

0 comments on commit 002d08b

Please sign in to comment.