Created
January 22, 2023 20:02
-
-
Save whit3rabbit/b8f250b173dd2ae35d8229c9ba39d058 to your computer and use it in GitHub Desktop.
port knocker by chatgpt
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
| // Client | |
| package main | |
| import ( | |
| "fmt" | |
| "net" | |
| "time" | |
| ) | |
| func main() { | |
| ip := net.ParseIP("127.0.0.1") | |
| knock := []string{"1111", "2222", "3333"} | |
| for _, port := range knock { | |
| conn, err := net.DialTimeout("tcp", ip.String()+":"+port, 1*time.Second) | |
| if err != nil { | |
| fmt.Println("Port Knocking failed:", err) | |
| } | |
| if conn != nil { | |
| conn.Close() | |
| } | |
| } | |
| fmt.Println("Port Knocking successful!") | |
| } |
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
| // Server | |
| package main | |
| import ( | |
| "fmt" | |
| "net" | |
| ) | |
| var knockCounter int | |
| func main() { | |
| conn, err := net.Listen("tcp", ":1111") | |
| if err != nil { | |
| fmt.Println("Error listening:", err.Error()) | |
| return | |
| } | |
| defer conn.Close() | |
| fmt.Println("Listening on port 1111...") | |
| for { | |
| c, err := conn.Accept() | |
| if err != nil { | |
| fmt.Println("Error accepting:", err.Error()) | |
| return | |
| } | |
| go handleRequest(c) | |
| } | |
| } | |
| func handleRequest(c net.Conn) { | |
| defer c.Close() | |
| knockCounter++ | |
| fmt.Println("Connection opened. Knock counter:", knockCounter) | |
| if knockCounter == 3 { | |
| fmt.Println("All knocks received. Opening port 2222...") | |
| openPort("2222") | |
| knockCounter = 0 | |
| } | |
| } | |
| func openPort(port string) { | |
| listener, err := net.Listen("tcp", ":"+port) | |
| if err != nil { | |
| fmt.Println("Error listening on port", port, err.Error()) | |
| return | |
| } | |
| defer listener.Close() | |
| fmt.Println("Listening on port", port) | |
| for { | |
| conn, err := listener.Accept() | |
| if err != nil { | |
| fmt.Println("Error accepting: ", err.Error()) | |
| return | |
| } | |
| go handleConnection(conn) | |
| } | |
| } | |
| func handleConnection(conn net.Conn) { | |
| defer conn.Close() | |
| fmt.Println("Connection from: ", conn.RemoteAddr()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment