@@ -56,23 +56,25 @@ type Log struct {
5656 Level string `form:"level" json:"level,omitempty"`
5757 Limit Limit `form:"limit" json:"limit,omitempty"`
5858}
59+
60+ type Value struct {
61+ Value string `form:"value" json:"value,omitempty"`
62+ }
63+
5964type Limit struct {
6065 Value string `form:"value" json:"value,omitempty"`
6166}
6267type Audit struct {
6368 Limit Limit `form:"limit" json:"limit,omitempty"`
6469}
6570type Accept struct {
66- Flag bool
67- Limit Limit `form:"limit" json:"limit,omitempty"`
71+ Flag bool `form:"flag" json:"flag,omitempty"`
6872}
6973type Reject struct {
70- Type string `form:"type" json:"type,omitempty"`
71- Limit Limit `form:"limit" json:"limit,omitempty"`
74+ Flag bool `form:"flag" json:"flag,omitempty"`
7275}
7376type Drop struct {
74- Flag bool
75- Limit Limit `form:"limit" json:"limit,omitempty"`
77+ Flag bool
7678}
7779
7880type Mark struct {
@@ -86,11 +88,11 @@ type SourcePort struct {
8688}
8789
8890type Rule struct {
89- Family string `form:"family" json:"family,omitempty,default=ipv4 "`
91+ Family string `form:"family" json:"family,omitempty"`
9092 Source * Source `form:"source" json:"source,omitempty"`
9193 Destination * Destination `form:"destination" json:"destination,omitempty"`
9294 Service []string `form:"service" json:"service,omitempty"`
93- Port * Port `form:"protocol " json:"protocol ,omitempty"`
95+ Port * Port `form:"port " json:"port ,omitempty"`
9496 Protocol * Protocol `form:"protocol" json:"protocol,omitempty"`
9597 IcmpBlock * IcmpBlock `form:"icmpblock" json:"icmpblock,omitempty"`
9698 IcmpType * IcmpType `form:"icmptype" json:"icmptype,omitempty"`
@@ -101,6 +103,8 @@ type Rule struct {
101103 Reject * Reject `form:"reject" json:"reject,omitempty"`
102104 Drop * Drop `form:"drop" json:"drop,omitempty"`
103105 Mark * Mark `form:"mark" json:"mark,omitempty"`
106+ Limit * Limit `form:"limit" json:"limit,omitempty"`
107+ Value * Value `form:"value" json:"value,omitempty"`
104108}
105109
106110type Interface struct {
@@ -168,8 +172,8 @@ type Settings struct {
168172 IcmpBlockInversion bool `deepcopier:"field:IcmpBlockInversion" form:"icmp-block-inversion" json:"icmp-block-inversion",omitempty"`
169173}
170174
171- func (this * Source ) IsEmpty () bool {
172- return this == nil
175+ func (s * Source ) IsEmpty () bool {
176+ return s == nil
173177}
174178
175179func (this * Destination ) IsEmpty () bool {
@@ -224,13 +228,18 @@ func (this *Limit) IsEmpty() bool {
224228 return this == nil
225229}
226230
231+ func (this * Value ) IsEmpty () bool {
232+ return this == nil
233+ }
234+
227235func (this * Source ) ToString () string {
236+
228237 var str = " source "
229238 if this .Address != "" {
230239 str += "address=" + this .Address
231240 } else if this .Mac != "" {
232241 str += "mac=" + this .Mac
233- } else {
242+ } else if this . Ipset != "" {
234243 str += "ipset=" + this .Ipset
235244 }
236245 if this .Invert != "" {
@@ -257,18 +266,19 @@ func (this *Destination) ToString() string {
257266func (this * Port ) ToString () string {
258267 var str = "port "
259268 if this .Port != "" {
260- str += "name =" + this .Port
269+ str += "port =" + this .Port
261270 }
262271 if this .Protocol != "" {
263- str += "protocol=" + this .Protocol
272+
273+ str += " " + "protocol=" + this .Protocol
264274 }
265275
266276 str += " "
267277 return str
268278}
269279
270280func (this * Protocol ) ToString () string {
271- var str = "Protocol "
281+ var str = "protocol "
272282 if this .Value != "" {
273283 str += "value=" + this .Value
274284 }
@@ -337,7 +347,6 @@ func (this *Log) ToString() string {
337347 if ! this .Limit .IsEmpty () {
338348 str += " " + "limit value=" + this .Limit .Value
339349 }
340-
341350 str += " "
342351 return str
343352}
@@ -353,32 +362,42 @@ func (this *Audit) ToString() string {
353362 return str
354363}
355364
365+ func (this * Limit ) ToString () string {
366+ var str string
367+ if ! this .IsEmpty () {
368+ str += "limit value=" + this .Value
369+ }
370+ str += " "
371+ return str
372+ }
373+
356374func (this * Accept ) ToString () string {
357375 var str string
358376
359377 if this .Flag {
360378 str = "accept "
361379 }
362- if ! this .Limit .IsEmpty () {
363- str += "limit value=" + this .Limit .Value
364- }
365-
366380 str += " "
367381 return str
368382}
369383
370384func (this * Reject ) ToString () string {
371- var str = "reject "
385+ var str string
372386
373- if this .Type != "" {
374- str + = "type=" + this . Type
387+ if ! this .IsEmpty () {
388+ str = "reject "
375389 }
376390
377- if ! this .Limit .IsEmpty () {
378- str += " "
379- str += "limit value=" + this .Limit .Value
380- }
391+ str += " "
392+ return str
393+ }
381394
395+ func (this * Value ) ToString () string {
396+ var str = "value= "
397+
398+ if ! this .IsEmpty () {
399+ str += "type=" + this .Value
400+ }
382401 str += " "
383402 return str
384403}
@@ -389,9 +408,6 @@ func (this *Drop) ToString() string {
389408 if this .Flag {
390409 str = "drop "
391410 }
392- if ! this .Limit .IsEmpty () {
393- str += "limit value=" + this .Limit .Value
394- }
395411 str += " "
396412 return str
397413}
@@ -415,8 +431,9 @@ func (this *Mark) ToString() string {
415431
416432func (this * Rule ) ToString () (ruleString string ) {
417433 ruleString = "rule "
434+
418435 if this .Family != "" {
419- ruleString += "family=" + this .Family
436+ ruleString += "family=" + this .Family + " "
420437 }
421438
422439 if ! this .Source .IsEmpty () {
@@ -520,27 +537,21 @@ func SliceToStruct(array interface{}) (ForwardPort, error) {
520537 return ForwardPort {}, encounterError
521538}
522539
523- func stringToReject (slice []string ) (reject * Reject , ruleSlice []string ) {
524- Label:
525- for index , value := range slice {
526- tmp_slice := strings .Split (value , "=" )
527- switch tmp_slice [1 ] {
528- case "type" :
529- slice = removeSliceElement (slice , index )
530- reject .Type = slice [index ]
531- slice = removeSliceElement (slice , index )
532- goto Label
533- case "limit" :
534- slice = removeSliceElement (slice , index )
535- tmp_slice := strings .Split (slice [index ], "=" )
536- reject .Limit = Limit {Value : tmp_slice [1 ]}
537- slice = removeSliceElement (slice , index )
538- goto Label
539- }
540- }
541- ruleSlice = slice
542- return reject , ruleSlice
543- }
540+ //func stringToReject(slice []string) (reject *Reject, ruleSlice []string) {
541+ //Label:
542+ // for index, value := range slice {
543+ // tmp_slice := strings.Split(value, "=")
544+ // switch tmp_slice[1] {
545+ // case "type":
546+ // slice = removeSliceElement(slice, index)
547+ // reject.Type = slice[index]
548+ // slice = removeSliceElement(slice, index)
549+ // goto Label
550+ // }
551+ // }
552+ // ruleSlice = slice
553+ // return reject, ruleSlice
554+ //}
544555
545556func stringToMark (slice []string ) (mark * Mark , ruleSlice []string ) {
546557
@@ -619,18 +630,18 @@ Label:
619630}
620631
621632func StringToRule (str string ) (rule * Rule ) {
622-
623633 strslice := strings .Split (str , " " )
624634 rule = & Rule {}
625635Label:
626636 for index , value := range strslice {
627637 switch value {
628638 case "rule" :
629639 strslice = removeSliceElement (strslice , index )
640+ goto Label
641+ case `family="ipv4"` , `family="ipv6"` :
630642 tmp_str := strings .Split (strslice [index ], "=" )
631643 rule .Family = tmp_str [1 ]
632644 strslice = removeSliceElement (strslice , index )
633- goto Label
634645 case "source" :
635646 strslice = removeSliceElement (strslice , index )
636647 tmp_str := strings .Split (strslice [index ], "=" )
@@ -676,10 +687,11 @@ Label:
676687 Protocol : protocol [1 ],
677688 }
678689 strslice = removeSliceElement (strslice , index )
690+ strslice = removeSliceElement (strslice , index )
679691 goto Label
680692 case "protocol" :
681693 strslice = removeSliceElement (strslice , index )
682- tmp_str := strings .Split (strslice [index + 1 ], "=" )
694+ tmp_str := strings .Split (strslice [index ], "=" )
683695 rule .Protocol = & Protocol {Value : tmp_str [1 ]}
684696 strslice = removeSliceElement (strslice , index )
685697 goto Label
@@ -711,39 +723,37 @@ Label:
711723 goto Label
712724 case "accept" :
713725 strslice = removeSliceElement (strslice , index )
714- accept := & Accept {}
715- rule .Accept = accept
716- rule .Accept .Flag = true
717- var tmp_str []string
718- if len (strslice ) > 0 {
719- if strslice [index ] == "limit" {
720- strslice = removeSliceElement (strslice , index )
721- tmp_str = strings .Split (strslice [index ], "=" )
722- rule .Accept .Limit = Limit {Value : tmp_str [1 ]}
723- }
726+ rule .Accept = & Accept {
727+ Flag : true ,
724728 }
725729 goto Label
726730 case "drop" :
727- var tmp_str []string
728731 strslice = removeSliceElement (strslice , index )
729- rule .Drop .Flag = true
730- if len (strslice ) > 0 {
731- if strslice [index ] == "limit" {
732- strslice = removeSliceElement (strslice , index )
733- tmp_str = strings .Split (strslice [index ], "=" )
734- rule .Drop .Limit = Limit {Value : tmp_str [1 ]}
735- }
732+ rule .Drop = & Drop {
733+ Flag : true ,
736734 }
737735 goto Label
738736 case "reject" :
739737 strslice = removeSliceElement (strslice , index )
740- rule .Reject , strslice = stringToReject ( strslice )
738+ rule .Reject = & Reject {}
741739 goto Label
742740 case "mark" :
743741 strslice = removeSliceElement (strslice , index )
744742 rule .Mark , strslice = stringToMark (strslice )
745743 goto Label
744+ case "limit" :
745+ var tmp_str []string
746+ strslice = removeSliceElement (strslice , index )
747+ tmp_str = strings .Split (strslice [index ], "=" )
748+ rule .Limit = & Limit {Value : tmp_str [1 ]}
749+ goto Label
750+ case "value" :
751+ strslice = removeSliceElement (strslice , index )
752+ rule .Value = & Value {
753+ Value : strslice [index ],
754+ }
746755 }
756+
747757 }
748758 return rule
749759}
0 commit comments