Skip to content

Instantly share code, notes, and snippets.

@ChaosJohn
Forked from qhwa/go_port_forwarding.go
Created December 20, 2022 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaosJohn/cea2c018493fd78d194d4c2ed37285e6 to your computer and use it in GitHub Desktop.
Save ChaosJohn/cea2c018493fd78d194d4c2ed37285e6 to your computer and use it in GitHub Desktop.
network port forwarding in go lang
package main
import (
"fmt"
"io"
"net"
)
func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
fmt.Println("new client")
proxy, err := net.Dial("tcp", "127.0.0.1:80")
if err != nil {
panic(err)
}
fmt.Println("proxy connected")
go copyIO(conn, proxy)
go copyIO(proxy, conn)
}
func copyIO(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