11package conf
22
33import (
4+ "encoding/base64"
45 "net"
56 "strconv"
67 "strings"
@@ -19,6 +20,7 @@ type FreedomConfig struct {
1920 UserLevel uint32 `json:"userLevel"`
2021 Fragment * Fragment `json:"fragment"`
2122 Noise * Noise `json:"noise"`
23+ Noises []* Noise `json:"noises"`
2224 ProxyProtocol uint32 `json:"proxyProtocol"`
2325}
2426
@@ -29,8 +31,9 @@ type Fragment struct {
2931}
3032
3133type Noise struct {
32- Packet string `json:"packet"`
33- Delay string `json:"delay"`
34+ Type string `json:"type"`
35+ Packet string `json:"packet"`
36+ Delay * Int32Range `json:"delay"`
3437}
3538
3639// Build implements Buildable
@@ -149,74 +152,18 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
149152 }
150153 }
151154 }
152- if c .Noise != nil {
153- config .Noise = new (freedom.Noise )
154- var err , err2 error
155- p := strings .Split (strings .ToLower (c .Noise .Packet ), ":" )
156- if len (p ) != 2 {
157- return nil , errors .New ("invalid type for packet" )
158- }
159- switch p [0 ] {
160- case "rand" :
161- randValue := strings .Split (p [1 ], "-" )
162- if len (randValue ) > 2 {
163- return nil , errors .New ("Only 2 values are allowed for rand" )
164- }
165- if len (randValue ) == 2 {
166- config .Noise .LengthMin , err = strconv .ParseUint (randValue [0 ], 10 , 64 )
167- config .Noise .LengthMax , err2 = strconv .ParseUint (randValue [1 ], 10 , 64 )
168- }
169- if len (randValue ) == 1 {
170- config .Noise .LengthMin , err = strconv .ParseUint (randValue [0 ], 10 , 64 )
171- config .Noise .LengthMax = config .Noise .LengthMin
172- }
173- if err != nil {
174- return nil , errors .New ("invalid value for rand LengthMin" ).Base (err )
175- }
176- if err2 != nil {
177- return nil , errors .New ("invalid value for rand LengthMax" ).Base (err2 )
178- }
179- if config .Noise .LengthMin > config .Noise .LengthMax {
180- config .Noise .LengthMin , config .Noise .LengthMax = config .Noise .LengthMax , config .Noise .LengthMin
181- }
182- if config .Noise .LengthMin == 0 {
183- return nil , errors .New ("rand lengthMin or lengthMax cannot be 0" )
184- }
185-
186- case "str" :
187- //user input string
188- config .Noise .StrNoise = strings .TrimSpace (p [1 ])
189155
190- default :
191- return nil , errors .New ("Invalid packet,only rand and str are supported" )
192- }
193- if c .Noise .Delay != "" {
194- d := strings .Split (strings .ToLower (c .Noise .Delay ), "-" )
195- if len (d ) > 2 {
196- return nil , errors .New ("Invalid delay value" )
197- }
198- if len (d ) == 2 {
199- config .Noise .DelayMin , err = strconv .ParseUint (d [0 ], 10 , 64 )
200- config .Noise .DelayMax , err2 = strconv .ParseUint (d [1 ], 10 , 64 )
156+ if c .Noise != nil {
157+ return nil , errors .PrintRemovedFeatureError ("noise = { ... }" , "noises = [ { ... } ]" )
158+ }
201159
202- } else {
203- config .Noise .DelayMin , err = strconv .ParseUint (d [0 ], 10 , 64 )
204- config .Noise .DelayMax = config .Noise .DelayMin
205- }
160+ if c .Noises != nil {
161+ for _ , n := range c .Noises {
162+ NConfig , err := ParseNoise (n )
206163 if err != nil {
207- return nil , errors .New ("Invalid value for DelayMin" ).Base (err )
208- }
209- if err2 != nil {
210- return nil , errors .New ("Invalid value for DelayMax" ).Base (err2 )
164+ return nil , err
211165 }
212- if config .Noise .DelayMin > config .Noise .DelayMax {
213- config .Noise .DelayMin , config .Noise .DelayMax = config .Noise .DelayMax , config .Noise .DelayMin
214- }
215- if config .Noise .DelayMin == 0 {
216- return nil , errors .New ("DelayMin or DelayMax cannot be 0" )
217- }
218- } else {
219- config .Noise .DelayMin = 0
166+ config .Noises = append (config .Noises , NConfig )
220167 }
221168 }
222169
@@ -248,3 +195,67 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
248195 }
249196 return config , nil
250197}
198+
199+ func ParseNoise (noise * Noise ) (* freedom.Noise , error ) {
200+ var err , err2 error
201+ NConfig := new (freedom.Noise )
202+
203+ switch strings .ToLower (noise .Type ) {
204+ case "rand" :
205+ randValue := strings .Split (noise .Packet , "-" )
206+ if len (randValue ) > 2 {
207+ return nil , errors .New ("Only 2 values are allowed for rand" )
208+ }
209+ if len (randValue ) == 2 {
210+ NConfig .LengthMin , err = strconv .ParseUint (randValue [0 ], 10 , 64 )
211+ NConfig .LengthMax , err2 = strconv .ParseUint (randValue [1 ], 10 , 64 )
212+ }
213+ if len (randValue ) == 1 {
214+ NConfig .LengthMin , err = strconv .ParseUint (randValue [0 ], 10 , 64 )
215+ NConfig .LengthMax = NConfig .LengthMin
216+ }
217+ if err != nil {
218+ return nil , errors .New ("invalid value for rand LengthMin" ).Base (err )
219+ }
220+ if err2 != nil {
221+ return nil , errors .New ("invalid value for rand LengthMax" ).Base (err2 )
222+ }
223+ if NConfig .LengthMin > NConfig .LengthMax {
224+ NConfig .LengthMin , NConfig .LengthMax = NConfig .LengthMax , NConfig .LengthMin
225+ }
226+ if NConfig .LengthMin == 0 {
227+ return nil , errors .New ("rand lengthMin or lengthMax cannot be 0" )
228+ }
229+
230+ case "str" :
231+ //user input string
232+ NConfig .StrNoise = []byte (strings .TrimSpace (noise .Packet ))
233+
234+ case "base64" :
235+ //user input base64
236+ NConfig .StrNoise , err = base64 .StdEncoding .DecodeString (strings .TrimSpace (noise .Packet ))
237+ if err != nil {
238+ return nil , errors .New ("Invalid base64 string" )
239+ }
240+
241+ default :
242+ return nil , errors .New ("Invalid packet,only rand,str,base64 are supported" )
243+ }
244+
245+ if noise .Delay != nil {
246+ if noise .Delay .From != 0 && noise .Delay .To != 0 {
247+ NConfig .DelayMin = uint64 (noise .Delay .From )
248+ NConfig .DelayMax = uint64 (noise .Delay .To )
249+ if NConfig .DelayMin > NConfig .LengthMax {
250+ NConfig .DelayMin , NConfig .DelayMax = NConfig .LengthMax , NConfig .DelayMin
251+ }
252+ } else {
253+ return nil , errors .New ("DelayMin or DelayMax cannot be zero" )
254+ }
255+
256+ } else {
257+ NConfig .DelayMin = 0
258+ NConfig .DelayMax = 0
259+ }
260+ return NConfig , nil
261+ }
0 commit comments