-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Synchronously update the firewall when modifying connection inf… #7536
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package firewall | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/1Panel-dev/1Panel/core/utils/cmd" | ||
| ) | ||
|
|
||
| func UpdatePort(oldPort, newPort string) error { | ||
| firewalld := cmd.Which("firewalld") | ||
| if firewalld { | ||
| status, _ := cmd.Exec("LANGUAGE=en_US:en firewall-cmd --state") | ||
| isRunning := status == "running\n" | ||
| if isRunning { | ||
| return firewallUpdatePort(oldPort, newPort) | ||
| } | ||
| } | ||
|
|
||
| ufw := cmd.Which("ufw") | ||
| if !ufw { | ||
| return nil | ||
| } | ||
| status, _ := cmd.Exec("LANGUAGE=en_US:en ufw status | grep Status") | ||
| isRuning := status == "Status: active\n" | ||
| if isRuning { | ||
| return ufwUpdatePort(oldPort, newPort) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func firewallUpdatePort(oldPort, newPort string) error { | ||
| stdout, err := cmd.Execf("firewall-cmd --zone=public --add-port=%s/tcp --permanent", newPort) | ||
| if err != nil { | ||
| return fmt.Errorf("add (port: %s/tcp) failed, err: %s", newPort, stdout) | ||
| } | ||
|
|
||
| _, _ = cmd.Execf("firewall-cmd --zone=public --remove-port=%s/tcp --permanent", oldPort) | ||
| _, _ = cmd.Exec("firewall-cmd --reload") | ||
| return nil | ||
| } | ||
|
|
||
| func ufwUpdatePort(oldPort, newPort string) error { | ||
| stdout, err := cmd.Execf("ufw allow %s", newPort) | ||
| if err != nil { | ||
| return fmt.Errorf("add (port: %s/tcp) failed, err: %s", newPort, stdout) | ||
| } | ||
|
|
||
| _, _ = cmd.Execf("ufw delete allow %s", oldPort) | ||
| return nil | ||
| } | ||
|
Member
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 major issues with the given snippet. The code seems to correctly uses However, minor improvements like considering the return types of the executables ( // Define constants here if needed
const (
oldPort = "80" // Change to your old port here
newPort = "443" // Example new port from above example
... // rest similar
)
func UpdatePort(oldPort string, newPort string) error {
switch f := &foo{nil}: {f.func1(); }
return fooUpdateNewOrOldPort(oldPort, newPort)
}This small change could make the function more readable since it doesn't rely on hard-coded values and can easily maintain multiple ports. Also, this kind of refactoring helps prevent future errors related to changing variables. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,12 @@ import ( | |
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func Proxy(c *gin.Context, currentNode string) { | ||
| return | ||
| } | ||
| func Proxy(c *gin.Context, currentNode string) { return } | ||
|
|
||
| func UpdateGroup(name string, group, newGroup uint) error { | ||
| return nil | ||
| } | ||
| func UpdateGroup(name string, group, newGroup uint) error { return nil } | ||
|
|
||
| func CheckBackupUsed(id uint) error { | ||
| return nil | ||
| } | ||
| func CheckBackupUsed(id uint) error { return nil } | ||
|
|
||
| func InitAgentRouter(Router *gin.RouterGroup) {} | ||
|
|
||
| func UpdateMasterAddr(newAddr string) error { return nil } | ||
|
Member
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 known abnormalities or issues with the provided code snippet. All function definitions appear to be correctly formatted for Golang and there are no obvious errors in variable declarations or usage patterns. However, due to the limitations of this platform, we can only analyze these files based on their current state at September 1st, 2021. For more detailed analyses, consider checking the Go source files from the time period you specified. Optimization Suggestions: Keep the code up-to-date as newer versions often improve performance and add features that might not have been included in older releases; regularly update packages used in the project, where applicable. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code seems to be incomplete with certain sections missing, including the
LoadFromCert, etc. However, based on what you've shared, there seem to be some regular coding standards violated like using strings directly instead of a struct for error handling and variable names that don't follow Go conventions.Here's a basic outline:
Check for Regular Irregularities