Skip to content

Commit d61c845

Browse files
committed
support for masquerade configuration
1 parent 4783d89 commit d61c845

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

cmd/create/create_firewall.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package create
1717

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"loxicmd/pkg/api"
2223
"net/http"
@@ -31,6 +32,7 @@ import (
3132
type CreateFirewallOptions struct {
3233
FirewallRule []string
3334
Redirect []string
35+
SnatArgs []string
3436
Allow bool
3537
Drop bool
3638
Trap bool
@@ -64,6 +66,8 @@ ex) loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.
6466
loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.3.1.2/32,preference:200" --drop
6567
loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.3.1.2/32,preference:200" --trap
6668
loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.3.1.2/32,preference:200" --redirect=hs1
69+
loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.3.1.2/32,preference:200" --snat=10.10.10.1,3030
70+
loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.3.1.2/32,preference:200" --snat=10.10.10.1 (Do not change sourceport)
6771
`,
6872
Aliases: []string{"Firewall", "fw", "firewalls"},
6973
PreRun: func(cmd *cobra.Command, args []string) {
@@ -107,6 +111,7 @@ ex) loxicmd create firewall --firewallRule="sourceIP:1.2.3.2/32,destinationIP:2.
107111
createFirewallCmd.Flags().BoolVarP(&o.Record, "record", "", false, "Record/Dump any matching rule")
108112
createFirewallCmd.Flags().BoolVarP(&o.Trap, "trap", "", false, " Trap anything matching rule")
109113
createFirewallCmd.Flags().IntVarP(&o.Mark, "setmark", "", 0, " Add a fw mark")
114+
createFirewallCmd.Flags().StringSliceVar(&o.SnatArgs, "snat", o.SnatArgs, "SNAT any matching rule")
110115
createFirewallCmd.MarkFlagRequired("firewallRule")
111116
return createFirewallCmd
112117
}
@@ -175,6 +180,21 @@ func GetFWOptionPairList(FirewallMods *api.FwRuleMod, o CreateFirewallOptions) e
175180
} else if len(o.Redirect) != 0 {
176181
FirewallMods.Opts.Rdr = true
177182
FirewallMods.Opts.RdrPort = o.Redirect[0]
183+
} else if len(o.SnatArgs) != 0 {
184+
if len(o.SnatArgs) > 2 {
185+
return errors.New("invalid snat-args")
186+
}
187+
FirewallMods.Opts.DoSnat = true
188+
FirewallMods.Opts.ToIP = o.SnatArgs[0]
189+
if len(o.SnatArgs) > 1 {
190+
sPort, err := strconv.Atoi(o.SnatArgs[1])
191+
if err != nil {
192+
return errors.New("invalid snat-args")
193+
}
194+
FirewallMods.Opts.ToPort = uint16(sPort)
195+
} else {
196+
FirewallMods.Opts.ToPort = 0
197+
}
178198
}
179199
FirewallMods.Opts.Record = o.Record
180200
FirewallMods.Opts.Mark = o.Mark

cmd/get/get_firewall.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func MakeFirewallOptionToString(t api.FwOptArg) (ret string) {
111111
ret = "Trap"
112112
} else if t.Rdr {
113113
ret = fmt.Sprintf("Redirect(%s)", t.RdrPort)
114+
} else if t.DoSnat {
115+
ret = fmt.Sprintf("Snat(%s:%d)", t.ToIP, t.ToPort)
114116
}
115117
if t.Record {
116118
ret += fmt.Sprintf(",Record")

cmd/get/get_loadbalancer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func PrintGetLbResult(resp *http.Response, o api.RESTOptions) {
156156

157157
// Making load balance data
158158
for _, lbrule := range lbresp.LbRules {
159-
if o.ServiceName != "" && o.ServiceName != lbrule.Service.Name {
159+
if o.ServiceName != "" && o.ServiceName != lbrule.Service.Name || lbrule.Service.Snat {
160160
continue
161161
}
162162
protocolStr := lbrule.Service.Protocol
@@ -258,7 +258,7 @@ func Lbdump(restOptions *api.RESTOptions, path string) (string, error) {
258258
}
259259

260260
for _, lbrule := range lbresp.LbRules {
261-
if !lbrule.Service.Managed && !strings.Contains(lbrule.Service.Name, "ipvs") {
261+
if !lbrule.Service.Managed && !lbrule.Service.Snat && !strings.Contains(lbrule.Service.Name, "ipvs") {
262262
for i := range lbrule.Endpoints {
263263
lbacts := &lbrule.Endpoints[i]
264264
lbacts.Counter = ""

pkg/api/firewall.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ type FwOptArg struct {
4242
Mark int `json:"fwMark" yaml:"fwMark"`
4343
// Record - Record packets matching rule
4444
Record bool `json:"record" yaml:"record"`
45-
45+
// DoSNAT - Do snat on matching rule
46+
DoSnat bool `json:"doSnat"`
47+
ToIP string `json:"toIP"`
48+
ToPort uint16 `json:"toPort"`
4649
// Counter - Traffic counter
4750
Counter string `json:"counter"`
4851
}

pkg/api/loadBalancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type LoadBalancerService struct {
5151
Block uint16 `json:"block" yaml:"block"`
5252
Managed bool `json:"managed,omitempty" yaml:"managed"`
5353
Name string `json:"name,omitempty" yaml:"name"`
54+
Snat bool `json:"snat,omitempty"`
5455
Oper LbOP `json:"oper,omitempty"`
5556
Security LbSec `json:"security,omitempty" yaml:"security"`
5657
}

0 commit comments

Comments
 (0)