Created
November 4, 2023 07:14
-
-
Save biningo/4b044be89d1b0e62ece80c4b519ddbe3 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"flag" | |
"fmt" | |
"io" | |
"net" | |
) | |
var ( | |
localAddr = flag.String("l", ":7001", "local address") | |
remoteAddr = flag.String("r", "localhost:80", "remote address") | |
) | |
func pipe(src, dst io.ReadWriter) { | |
buff := make([]byte, 0xffff) | |
for { | |
n, err := src.Read(buff) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
data := buff[:n] | |
n, err = dst.Write(data) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
} | |
} | |
func handle(localConn net.Conn) { | |
rAddr, err := net.ResolveTCPAddr("tcp", *remoteAddr) | |
if err != nil { | |
panic(err) | |
} | |
remoteConn, err := net.DialTCP("tcp", nil, rAddr) | |
if err != nil { | |
panic(err) | |
} | |
defer localConn.Close() | |
defer remoteConn.Close() | |
//go io.Copy(localConn, remoteConn) | |
//io.Copy(remoteConn, localConn) | |
go pipe(localConn, remoteConn) | |
pipe(remoteConn, localConn) | |
fmt.Println("done!", localConn.RemoteAddr()) | |
} | |
func main() { | |
flag.Parse() | |
lAddr, err := net.ResolveTCPAddr("tcp", *localAddr) | |
if err != nil { | |
panic(err) | |
} | |
listener, err := net.ListenTCP("tcp", lAddr) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("listen", localAddr) | |
for { | |
localConn, err := listener.Accept() | |
fmt.Println("accept", localConn.RemoteAddr()) | |
if err != nil { | |
panic(err) | |
} | |
go handle(localConn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment