-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alessio Greggi <[email protected]>
- Loading branch information
Showing
9 changed files
with
156 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package extractor | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ExtractRuleInfo extract forward information from rule | ||
// if it matches the requirements | ||
func ExtractRuleInfo(rule string) ([]string, error) { | ||
// extract rules info: | ||
// -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 3000 -j DNAT --to-destination 192.168.199.105:80 | ||
// result: | ||
// slice ruleInfo: [ , eth0, tcp, 3000, 192.168.199.105, 80] | ||
ruleSplit := strings.Split(rule, " ") | ||
ruleInfo := make([]string, 6) | ||
for id, arg := range ruleSplit { | ||
switch arg { | ||
case "-i": | ||
ruleInfo[1] = ruleSplit[id+1] | ||
case "-p": | ||
ruleInfo[2] = ruleSplit[id+1] | ||
case "--dport": | ||
ruleInfo[3] = ruleSplit[id+1] | ||
case "--to-destination": | ||
ruleInfo[4] = strings.Split(ruleSplit[id+1], ":")[0] | ||
ruleInfo[5] = strings.Split(ruleSplit[id+1], ":")[1] | ||
} | ||
} | ||
|
||
for i := 1; i < len(ruleInfo); i++ { | ||
if ruleInfo[i] == "" { | ||
return []string{}, fmt.Errorf("unable to retrieve elements from rule") | ||
} | ||
} | ||
return ruleInfo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package extractor | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestExtractRuleInfo(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rule string | ||
want []string | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "example_1", | ||
rule: "-A PREROUTING -i lo -p tcp -m tcp --dport 3001 -m comment --comment fwdctl -j DNAT --to-destination 127.0.0.1:80", | ||
want: []string{"", "lo", "tcp", "3001", "127.0.0.1", "80"}, | ||
}, | ||
{ | ||
name: "example_2", | ||
rule: "-A PREROUTING -i eth0 -p tcp -m tcp --dport 3001 -m comment --comment fwdctl -j DNAT --to-destination 127.0.0.1:80", | ||
want: []string{"", "eth0", "tcp", "3001", "127.0.0.1", "80"}, | ||
}, | ||
{ | ||
name: "example_3", | ||
rule: "-A PREROUTING -i eth0 -p tcp -m tcp --dport 3001 -m comment --comment fwdctl -j DNAT", | ||
want: []string{""}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ExtractRuleInfo(tt.rule) | ||
if (err != nil) tt.wantErr { | ||
Check failure on line 36 in internal/extractor/extract_test.go GitHub Actions / integration-test
Check failure on line 36 in internal/extractor/extract_test.go GitHub Actions / lint
|
||
t.Errorf("ExtractRuleInfo() error = %v", err) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ExtractRuleInfo() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters