Last active
January 1, 2022 15:52
-
-
Save ghost-ng/26a5e1744939df6d208aecb9a3eb6667 to your computer and use it in GitHub Desktop.
tcpforward.go
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
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"os" | |
"github.com/akamensky/argparse" | |
) | |
var defaultListen = "0.0.0.0:9999" | |
var defaultConnect = "127.0.0.1:9999" | |
func main() { | |
parser := argparse.NewParser("TCP Forwarding", "Simple TCP Forwarder") | |
Listen := parser.String("l", "listen", &argparse.Options{Required: false, Help: "IP Address and Port", Default: defaultListen}) | |
Connect := parser.String("c", "connect", &argparse.Options{Required: false, Help: "IP Address and Port", Default: defaultConnect}) | |
errs := parser.Parse(os.Args) | |
if errs != nil { | |
// In case of error print error and print usage | |
// This can also be done by passing -h or --help flags | |
fmt.Print(parser.Usage(errs)) | |
os.Exit(0) | |
} | |
ln, err := net.Listen("tcp", *Listen) | |
msg := "[*] L:" + *Listen + " --> C:" + *Connect | |
fmt.Println(msg) | |
if err != nil { | |
panic(err) | |
} | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
panic(err) | |
} | |
go handleRequest(conn, *Connect) | |
} | |
} | |
func handleRequest(conn net.Conn, connect string) { | |
proxy, err := net.Dial("tcp", connect) | |
if err != nil { | |
log.Println("[-] Unable to connect") | |
} else { | |
log.Println(proxy.LocalAddr().String() + " <--> {Proxy} <--> " + proxy.RemoteAddr().String()) | |
go forward(conn, proxy) | |
go forward(proxy, conn) | |
} | |
} | |
func forward(src, dest net.Conn) { | |
defer src.Close() | |
defer dest.Close() | |
io.Copy(src, dest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment