-
Notifications
You must be signed in to change notification settings - Fork 5k
UDP noises: Add base64 and multi-packet support #3794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RPRX
merged 10 commits into
XTLS:main
from
dragonbreath2000:udp-noise-base64_and_mulipacket_support
Sep 16, 2024
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5970a78
add base 64 and multiPacket support
dragonbreath2000 227228a
add optional list and some clean up
dragonbreath2000 6dfa6a3
fix a bug
dragonbreath2000 7a5c03b
change Noise to Noises and remove listable type
dragonbreath2000 85eb3fc
backward compat and a clean up
dragonbreath2000 7d50016
Use separated "type" instead of split ":"
Fangliding aaa74d0
Do not allow to set "noise"{}
dragonbreath2000 8fdd4e2
apply review feedback
mmmray 340b82a
Merge remote-tracking branch 'origin/main' into udp-noise-base64_and_…
mmmray cdb18c1
fix import
mmmray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package conf | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "net" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -19,6 +20,7 @@ type FreedomConfig struct { | |
| UserLevel uint32 `json:"userLevel"` | ||
| Fragment *Fragment `json:"fragment"` | ||
| Noise *Noise `json:"noise"` | ||
| Noises []*Noise `json:"noises"` | ||
| ProxyProtocol uint32 `json:"proxyProtocol"` | ||
| } | ||
|
|
||
|
|
@@ -29,8 +31,8 @@ type Fragment struct { | |
| } | ||
|
|
||
| type Noise struct { | ||
| Packet string `json:"packet"` | ||
| Delay string `json:"delay"` | ||
| Packet string `json:"packet"` | ||
| Delay *Int32Range `json:"delay"` | ||
| } | ||
|
|
||
| // Build implements Buildable | ||
|
|
@@ -150,73 +152,15 @@ 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 | ||
| } | ||
| return nil, errors.New(`Freedom settings: please use "noises":[{}] instead of "noise":{}`) | ||
| } | ||
| if c.Noises != nil { | ||
| for _, n := range c.Noises { | ||
| NConfig, err := ParseNoise(n) | ||
| 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) | ||
| return nil, errors.New(err) | ||
|
||
| } | ||
| 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 | ||
| config.Noises = append(config.Noises, NConfig) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -248,3 +192,71 @@ func (c *FreedomConfig) Build() (proto.Message, error) { | |
| } | ||
| return config, nil | ||
| } | ||
|
|
||
| func ParseNoise(noise *Noise) (*freedom.Noise, error) { | ||
| var err, err2 error | ||
| NConfig := new(freedom.Noise) | ||
|
|
||
| packet := strings.SplitN(noise.Packet, ":", 2) | ||
| if len(packet) != 2 { | ||
| return nil, errors.New("invalid type for packet") | ||
| } | ||
| switch strings.ToLower(packet[0]) { | ||
| case "rand": | ||
| randValue := strings.Split(packet[1], "-") | ||
| if len(randValue) > 2 { | ||
| return nil, errors.New("Only 2 values are allowed for rand") | ||
| } | ||
| if len(randValue) == 2 { | ||
| NConfig.LengthMin, err = strconv.ParseUint(randValue[0], 10, 64) | ||
| NConfig.LengthMax, err2 = strconv.ParseUint(randValue[1], 10, 64) | ||
| } | ||
| if len(randValue) == 1 { | ||
| NConfig.LengthMin, err = strconv.ParseUint(randValue[0], 10, 64) | ||
| NConfig.LengthMax = NConfig.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 NConfig.LengthMin > NConfig.LengthMax { | ||
| NConfig.LengthMin, NConfig.LengthMax = NConfig.LengthMax, NConfig.LengthMin | ||
| } | ||
| if NConfig.LengthMin == 0 { | ||
| return nil, errors.New("rand lengthMin or lengthMax cannot be 0") | ||
| } | ||
|
|
||
| case "str": | ||
| //user input string | ||
| NConfig.StrNoise = []byte(strings.TrimSpace(packet[1])) | ||
|
|
||
| case "base64": | ||
| //user input base64 | ||
| NConfig.StrNoise, err = base64.StdEncoding.DecodeString(strings.TrimSpace(packet[1])) | ||
| if err != nil { | ||
| return nil, errors.New("Invalid base64 string") | ||
| } | ||
|
|
||
| default: | ||
| return nil, errors.New("Invalid packet,only rand,str,base64 are supported") | ||
| } | ||
|
|
||
| if noise.Delay != nil { | ||
| if noise.Delay.From != 0 && noise.Delay.To != 0 { | ||
| NConfig.DelayMin = uint64(noise.Delay.From) | ||
| NConfig.DelayMax = uint64(noise.Delay.To) | ||
| if NConfig.DelayMin > NConfig.LengthMax { | ||
| NConfig.DelayMin, NConfig.DelayMax = NConfig.LengthMax, NConfig.DelayMin | ||
| } | ||
| } else { | ||
| return nil, errors.New("DelayMin or DelayMax cannot be zero") | ||
| } | ||
|
|
||
| } else { | ||
| NConfig.DelayMin = 0 | ||
| NConfig.DelayMax = 0 | ||
| } | ||
| return NConfig, nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to maintain backwards compat, I suggest to append to
config.Noisesinstead. The behavior when both are defined can be an error, I think.