-
Notifications
You must be signed in to change notification settings - Fork 260
refactor: add receiver to iptables and create interface #2421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7b04b86
Move network utils functions with iptables to new file
QxBytes 263ea03
Add receiver to iptables and create interface
QxBytes 8c57dba
Resolve conflicts from rebasing
QxBytes d303cc7
Add changes for building on windows
QxBytes 80dc635
Address linter issues
QxBytes dd4c48c
Address windows linter issues
QxBytes b107304
Invert if condition for linter nesting
QxBytes 925011f
Scope iptables interfaces to package
QxBytes b79066c
Rename iptables client to avoid stuttering
QxBytes 848c50d
Move EnableIPForwarding to snat linux
QxBytes 8f02574
Rename ipTablesClientInterface to ipTablesClient
QxBytes 4a667da
Address linter issues from moving enable ip forwarding function
QxBytes d621c22
Rename after rebase
QxBytes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -87,8 +87,14 @@ type IPTableEntry struct { | |
| Params string | ||
| } | ||
|
|
||
| type Client struct{} | ||
|
|
||
| func NewClient() *Client { | ||
| return &Client{} | ||
| } | ||
|
Comment on lines
+90
to
+94
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are no fields (at all) being set in this struct, or other initialization magic - we don't need a constructor and can make the struct directly |
||
|
|
||
| // Run iptables command | ||
| func RunCmd(version, params string) error { | ||
| func (c *Client) RunCmd(version, params string) error { | ||
| var cmd string | ||
|
|
||
| p := platform.NewExecClient(logger) | ||
|
|
@@ -111,29 +117,29 @@ func RunCmd(version, params string) error { | |
| } | ||
|
|
||
| // check if iptable chain alreay exists | ||
| func ChainExists(version, tableName, chainName string) bool { | ||
| func (c *Client) ChainExists(version, tableName, chainName string) bool { | ||
| params := fmt.Sprintf("-t %s -L %s", tableName, chainName) | ||
| if err := RunCmd(version, params); err != nil { | ||
| if err := c.RunCmd(version, params); err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func GetCreateChainCmd(version, tableName, chainName string) IPTableEntry { | ||
| func (c *Client) GetCreateChainCmd(version, tableName, chainName string) IPTableEntry { | ||
| return IPTableEntry{ | ||
| Version: version, | ||
| Params: fmt.Sprintf("-t %s -N %s", tableName, chainName), | ||
| } | ||
| } | ||
|
|
||
| // create new iptable chain under specified table name | ||
| func CreateChain(version, tableName, chainName string) error { | ||
| func (c *Client) CreateChain(version, tableName, chainName string) error { | ||
| var err error | ||
|
|
||
| if !ChainExists(version, tableName, chainName) { | ||
| cmd := GetCreateChainCmd(version, tableName, chainName) | ||
| err = RunCmd(version, cmd.Params) | ||
| if !c.ChainExists(version, tableName, chainName) { | ||
| cmd := c.GetCreateChainCmd(version, tableName, chainName) | ||
| err = c.RunCmd(version, cmd.Params) | ||
| } else { | ||
| logger.Info("Chain exists in table", zap.String("chainName", chainName), zap.String("tableName", tableName)) | ||
| } | ||
|
|
@@ -142,52 +148,52 @@ func CreateChain(version, tableName, chainName string) error { | |
| } | ||
|
|
||
| // check if iptable rule alreay exists | ||
| func RuleExists(version, tableName, chainName, match, target string) bool { | ||
| func (c *Client) RuleExists(version, tableName, chainName, match, target string) bool { | ||
| params := fmt.Sprintf("-t %s -C %s %s -j %s", tableName, chainName, match, target) | ||
| if err := RunCmd(version, params); err != nil { | ||
| if err := c.RunCmd(version, params); err != nil { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func GetInsertIptableRuleCmd(version, tableName, chainName, match, target string) IPTableEntry { | ||
| func (c *Client) GetInsertIptableRuleCmd(version, tableName, chainName, match, target string) IPTableEntry { | ||
| return IPTableEntry{ | ||
| Version: version, | ||
| Params: fmt.Sprintf("-t %s -I %s 1 %s -j %s", tableName, chainName, match, target), | ||
| } | ||
| } | ||
|
|
||
| // Insert iptable rule at beginning of iptable chain | ||
| func InsertIptableRule(version, tableName, chainName, match, target string) error { | ||
| if RuleExists(version, tableName, chainName, match, target) { | ||
| func (c *Client) InsertIptableRule(version, tableName, chainName, match, target string) error { | ||
| if c.RuleExists(version, tableName, chainName, match, target) { | ||
| logger.Info("Rule already exists") | ||
| return nil | ||
| } | ||
|
|
||
| cmd := GetInsertIptableRuleCmd(version, tableName, chainName, match, target) | ||
| return RunCmd(version, cmd.Params) | ||
| cmd := c.GetInsertIptableRuleCmd(version, tableName, chainName, match, target) | ||
| return c.RunCmd(version, cmd.Params) | ||
| } | ||
|
|
||
| func GetAppendIptableRuleCmd(version, tableName, chainName, match, target string) IPTableEntry { | ||
| func (c *Client) GetAppendIptableRuleCmd(version, tableName, chainName, match, target string) IPTableEntry { | ||
| return IPTableEntry{ | ||
| Version: version, | ||
| Params: fmt.Sprintf("-t %s -A %s %s -j %s", tableName, chainName, match, target), | ||
| } | ||
| } | ||
|
|
||
| // Append iptable rule at end of iptable chain | ||
| func AppendIptableRule(version, tableName, chainName, match, target string) error { | ||
| if RuleExists(version, tableName, chainName, match, target) { | ||
| func (c *Client) AppendIptableRule(version, tableName, chainName, match, target string) error { | ||
| if c.RuleExists(version, tableName, chainName, match, target) { | ||
| logger.Info("Rule already exists") | ||
| return nil | ||
| } | ||
|
|
||
| cmd := GetAppendIptableRuleCmd(version, tableName, chainName, match, target) | ||
| return RunCmd(version, cmd.Params) | ||
| cmd := c.GetAppendIptableRuleCmd(version, tableName, chainName, match, target) | ||
| return c.RunCmd(version, cmd.Params) | ||
| } | ||
|
|
||
| // Delete matched iptable rule | ||
| func DeleteIptableRule(version, tableName, chainName, match, target string) error { | ||
| func (c *Client) DeleteIptableRule(version, tableName, chainName, match, target string) error { | ||
| params := fmt.Sprintf("-t %s -D %s %s -j %s", tableName, chainName, match, target) | ||
| return RunCmd(version, params) | ||
| return c.RunCmd(version, params) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.