direct.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package firewalld
  15. import (
  16. "encoding/xml"
  17. )
  18. type Direct struct {
  19. Rules []*Rule
  20. XMLName struct{} `xml:"direct"`
  21. }
  22. func (d *Direct) String() string {
  23. data, _ := xml.MarshalIndent(d, "", " ")
  24. return string(data)
  25. }
  26. type Rule struct {
  27. // required, smaller the number more front the rule in chain
  28. Priority int `xml:"priority,attr"`
  29. // required, netfilter table: "nat", "mangle", etc.
  30. Table string `xml:"table,attr"`
  31. // required, ip family: "ipv4", "ipv6", "eb"
  32. IPv string `xml:"ipv,attr"`
  33. // required, netfilter chain: "FORWARD", custom chain names
  34. Chain string `xml:"chain,attr"`
  35. // match and action command line options for {ip,ip6,eb}tables
  36. Body string `xml:",chardata"`
  37. XMLName struct{} `xml:"rule"`
  38. }
  39. func NewIP4Rule(prio int, table, chain, body string) *Rule {
  40. r := &Rule{
  41. Priority: prio,
  42. IPv: "ipv4",
  43. Table: table,
  44. Chain: chain,
  45. Body: body,
  46. }
  47. return r
  48. }
  49. func (r *Rule) String() string {
  50. data, _ := xml.MarshalIndent(r, "", " ")
  51. return string(data)
  52. }
  53. func NewDirect(rules ...*Rule) *Direct {
  54. d := &Direct{
  55. Rules: rules,
  56. }
  57. return d
  58. }