diff --git a/README.md b/README.md index 2d31e70..2097924 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ WSL2-forwarding-port-cli is command line tools for wsl2 forwarding port configur 1. Open WSL2 with Run Administrator 2. Download the binary with the command ``` - curl -LO https://github.com/mrzack99s/wsl2-forwarding-port-cli/releases/download/v1.0.0/wfp-cli + curl -LO https://github.com/mrzack99s/wsl2-forwarding-port-cli/releases/download/v1.1.0/wfp-cli ``` 3. Make the kubectl binary executable. ``` diff --git a/cmds/lists.go b/cmds/lists.go index 07d453b..1cb330d 100644 --- a/cmds/lists.go +++ b/cmds/lists.go @@ -7,11 +7,12 @@ import ( ) func Lists() { - fmt.Printf("%-10s%-15s%-10s%-10s%-10s\n", "ID", "WSL2 IPADDR", "PROTOCOL", "SPORT", "DPORT") - fmt.Println("-------------------------------------------------------") + fmt.Println("--------------------------------------------------------------------") + fmt.Printf("%-10s%-22s%-12s%-12s%-12s\n", "ID", "WSL2 IPADDR", "PROTOCOL", "SPORT", "DPORT") + fmt.Println("--------------------------------------------------------------------") if len(configs.RulesTable.Rules) > 0 { for _, rule := range configs.RulesTable.Rules { - fmt.Printf("%-10s%-15s%-10s%-10s%-10s\n", rule.Id, rule.IpAddress, rule.Protocol, rule.SourcePort, rule.DestinationPort) + fmt.Printf("%-10s%-22s%-10s%-10s%-10s\n", rule.Id, rule.IpAddress, rule.Protocol, rule.SourcePort, rule.DestinationPort) } } diff --git a/main.go b/main.go index 96844ff..a865280 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ func asSha256(o interface{}) string { return fmt.Sprintf("%x", h.Sum(nil)) } -func checkAlreadyRule(id string) bool { +func checkAlreadyRuleByID(id string) bool { for _, rule := range configs.RulesTable.Rules { if id == rule.Id { return true @@ -52,6 +52,15 @@ func checkAlreadyRule(id string) bool { return false } +func checkAlreadyRuleBySPortAndProto(port string, proto string) bool { + for _, rule := range configs.RulesTable.Rules { + if port == rule.SourcePort && proto == rule.Protocol { + return true + } + } + return false +} + func main() { if len(os.Args) < 2 { @@ -105,7 +114,7 @@ func main() { hash := asSha256(rule) substringhash := hash[:8] - if !checkAlreadyRule(substringhash) { + if !checkAlreadyRuleByID(substringhash) && !checkAlreadyRuleBySPortAndProto(rule.SourcePort, rule.Protocol) { status := cmds.CreateRule(createArgs, ip) if status { rule.Id = substringhash @@ -123,7 +132,7 @@ func main() { id := os.Args[2] - if checkAlreadyRule(id) { + if checkAlreadyRuleByID(id) { index, status := cmds.DeleteRule(id) if status { length := len(configs.RulesTable.Rules)