mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
Refactor if/else in core and firewall packages
This commit is contained in:
parent
191e8eacff
commit
35ef29f27b
4 changed files with 25 additions and 21 deletions
|
@ -185,13 +185,17 @@ func TestCoreExec(t *testing.T) {
|
|||
gotStdout := Trim(buf.String())
|
||||
if gotOut != u.out {
|
||||
t.Fatalf("expected output '%s', got '%s'", u.out, gotOut)
|
||||
} else if u.err == "" && gotErr != nil {
|
||||
}
|
||||
if u.err == "" && gotErr != nil {
|
||||
t.Fatalf("expected no error, got '%s'", gotErr)
|
||||
} else if u.err != "" && gotErr == nil {
|
||||
}
|
||||
if u.err != "" && gotErr == nil {
|
||||
t.Fatalf("expected error '%s', got none", u.err)
|
||||
} else if u.err != "" && gotErr != nil && gotErr.Error() != u.err {
|
||||
}
|
||||
if u.err != "" && gotErr != nil && gotErr.Error() != u.err {
|
||||
t.Fatalf("expected error '%s', got '%s'", u.err, gotErr)
|
||||
} else if gotStdout != "" {
|
||||
}
|
||||
if gotStdout != "" {
|
||||
t.Fatalf("expected empty stdout, got '%s'", gotStdout)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,11 +43,11 @@ func Make(iface *network.Endpoint) FirewallManager {
|
|||
func (f PfFirewall) sysCtlRead(param string) (string, error) {
|
||||
if out, err := core.ExecSilent("sysctl", []string{param}); err != nil {
|
||||
return "", err
|
||||
} else if m := sysCtlParser.FindStringSubmatch(out); len(m) == 3 && m[1] == param {
|
||||
return m[2], nil
|
||||
} else {
|
||||
return "", fmt.Errorf("Unexpected sysctl output: %s", out)
|
||||
}
|
||||
if m := sysCtlParser.FindStringSubmatch(out); len(m) == 3 && m[1] == param {
|
||||
return m[2], nil
|
||||
}
|
||||
return "", fmt.Errorf("Unexpected sysctl output: %s", out)
|
||||
}
|
||||
|
||||
func (f PfFirewall) sysCtlWrite(param string, value string) (string, error) {
|
||||
|
@ -60,11 +60,11 @@ func (f PfFirewall) sysCtlWrite(param string, value string) (string, error) {
|
|||
// make sure we actually wrote the value
|
||||
if out, err := f.sysCtlRead(param); err != nil {
|
||||
return "", err
|
||||
} else if out != value {
|
||||
return "", fmt.Errorf("Expected value for '%s' is %s, found %s", param, value, out)
|
||||
} else {
|
||||
return out, nil
|
||||
}
|
||||
if out != value {
|
||||
return "", fmt.Errorf("Expected value for '%s' is %s, found %s", param, value, out)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f PfFirewall) IsForwardingEnabled() bool {
|
||||
|
@ -87,9 +87,8 @@ func (f PfFirewall) enableParam(param string, enabled bool) error {
|
|||
|
||||
if _, err := f.sysCtlWrite(param, value); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f PfFirewall) EnableForwarding(enabled bool) error {
|
||||
|
|
|
@ -53,11 +53,11 @@ func (f LinuxFirewall) enableFeature(filename string, enable bool) error {
|
|||
|
||||
func (f LinuxFirewall) IsForwardingEnabled() bool {
|
||||
|
||||
if out, err := ioutil.ReadFile(IPV4ForwardingFile); err != nil {
|
||||
out, err := ioutil.ReadFile(IPV4ForwardingFile)
|
||||
if err != nil {
|
||||
return false
|
||||
} else {
|
||||
return str.Trim(string(out)) == "1"
|
||||
}
|
||||
return str.Trim(string(out)) == "1"
|
||||
}
|
||||
|
||||
func (f LinuxFirewall) EnableForwarding(enabled bool) error {
|
||||
|
@ -111,7 +111,8 @@ func (f *LinuxFirewall) EnableRedirection(r *Redirection, enabled bool) error {
|
|||
// accept all
|
||||
if _, err := core.Exec("iptables", []string{"-P", "FORWARD", "ACCEPT"}); err != nil {
|
||||
return err
|
||||
} else if _, err := core.Exec("iptables", cmdLine); err != nil {
|
||||
}
|
||||
if _, err := core.Exec("iptables", cmdLine); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -30,9 +30,8 @@ func (f WindowsFirewall) IsForwardingEnabled() bool {
|
|||
if out, err := core.Exec("netsh", []string{"interface", "ipv4", "dump"}); err != nil {
|
||||
fmt.Printf("%s\n", err)
|
||||
return false
|
||||
} else {
|
||||
return strings.Contains(out, "forwarding=enabled")
|
||||
}
|
||||
return strings.Contains(out, "forwarding=enabled")
|
||||
}
|
||||
|
||||
func (f WindowsFirewall) EnableForwarding(enabled bool) error {
|
||||
|
@ -88,7 +87,8 @@ func (f *WindowsFirewall) AllowPort(port int, address string, proto string, allo
|
|||
func (f *WindowsFirewall) EnableRedirection(r *Redirection, enabled bool) error {
|
||||
if err := f.AllowPort(r.SrcPort, r.DstAddress, r.Protocol, enabled); err != nil {
|
||||
return err
|
||||
} else if err := f.AllowPort(r.DstPort, r.DstAddress, r.Protocol, enabled); err != nil {
|
||||
}
|
||||
if err := f.AllowPort(r.DstPort, r.DstAddress, r.Protocol, enabled); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue