Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzack99s committed Sep 26, 2020
2 parents 31a1fe5 + 032229c commit 4653254
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 17 deletions.
98 changes: 81 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"crypto/sha256"
"encoding/json"
"errors"
Expand Down Expand Up @@ -86,6 +87,22 @@ func main() {
os.Exit(0)
}

out, _ := exec.Command("bash", "-c", "ip route | grep default | awk '{print $3}'").Output()
winIp := strings.TrimSpace(string(out))

udpAddr, err := net.ResolveUDPAddr("udp", winIp+":40123")
if err != nil {
fmt.Println("Wrong Address")
return
}

//Create the connection
udpConn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
fmt.Println(err)
return
}

configs.ParseForwardingTable(filename)

var protoPtr, portPtr *string
Expand All @@ -106,22 +123,6 @@ func main() {
}
}

out, _ := exec.Command("bash", "-c", "ip route | grep default | awk '{print $3}'").Output()
winIp := strings.TrimSpace(string(out))

udpAddr, err := net.ResolveUDPAddr("udp", winIp+":40123")
if err != nil {
fmt.Println("Wrong Address")
return
}

//Create the connection
udpConn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
fmt.Println(err)
return
}

switch os.Args[1] {
case "create":
createCommand := flag.NewFlagSet("create", flag.ExitOnError)
Expand Down Expand Up @@ -233,10 +234,73 @@ func main() {
}
}

case "purge":
reader := bufio.NewReader(os.Stdin)
fmt.Print("Are you sure to purge? (Y,n): ")
ans, _ := reader.ReadString('\n')
if strings.TrimSpace(ans) == "Y" {
message := []byte("purge@Y")
_, err = udpConn.Write(message)

status := false
//Keep calling this function
for {
buffer := make([]byte, 2048)
n, _, err := udpConn.ReadFromUDP(buffer)
if err != nil {
break
} else {
if strings.TrimSpace(string(buffer[0:n])) == "SUCCESS" {
status = true
break
} else {
break
}
}
}
if status {
configs.RulesTable.Rules = nil
writeFile(filename, configs.RulesTable)
fmt.Println("Purge success")
}

}

case "ls":
cmds.Lists()
case "version":
fmt.Println("WSL2-Forwarding-port-cli version 2.0.0")
cliVersion := "2.1.0"
if len(os.Args) < 3 {
supports.VersionHelp()
} else {
if os.Args[2] == "--client" {
fmt.Println("WSL2-Forwarding-port-cli version 2.0.1")
} else if os.Args[2] == "--all" {
message := []byte("get@engine_version")
_, err = udpConn.Write(message)

status := false
//Keep calling this function
engineVersion := ""
for {
buffer := make([]byte, 2048)
n, _, err := udpConn.ReadFromUDP(buffer)
if err != nil {
break
} else {
status = true
engineVersion = strings.TrimSpace(string(buffer[0:n]))
break
}
}
if status {
fmt.Println("WSL2-Forwarding-port-cli version " + cliVersion)
fmt.Println("WSL2-Forwarding-port-engine version " + engineVersion)
}
} else {
supports.VersionHelp()
}
}
default:
supports.Help()
os.Exit(0)
Expand Down
10 changes: 10 additions & 0 deletions supports/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func Help() {
fmt.Println(" ls List all of port")
fmt.Println(" create Create a forwarding port rule to wsl2")
fmt.Println(" delete Delete a forwarding port rule to wsl2")
fmt.Println(" purge Purge a forwarding port rule to wsl2")
fmt.Print(" version Display release version\n\n")
}

Expand All @@ -19,6 +20,15 @@ func CreateHelp() {
fmt.Println(" wfp-cli create --proto=<TCP|UDP> --port=<window port>:<wsl2 port> ")
}

func VersionHelp() {
fmt.Print("WSL2 Forwarding Port Management \n\n")
fmt.Println("Usage:")
fmt.Println(" wfp-cli version <arguments> ")
fmt.Print("\nThe arguments are:\n\n")
fmt.Println(" --client Get client version")
fmt.Println(" --all Get client and engine version")
}

func DeleteHelp() {
fmt.Print("WSL2 Forwarding Port Management \n\n")
fmt.Println("Usage:")
Expand Down

0 comments on commit 4653254

Please sign in to comment.