Skip to content

Instantly share code, notes, and snippets.

@hongruiqi
Created October 29, 2015 12:13
Show Gist options
  • Save hongruiqi/53379476f91923e68cc3 to your computer and use it in GitHub Desktop.
Save hongruiqi/53379476f91923e68cc3 to your computer and use it in GitHub Desktop.
package main
import (
"net"
"fmt"
"io"
"os"
)
func handle(source net.Conn) {
defer source.Close();
target, err := net.Dial("tcp", os.Args[2])
if (err != nil) {
fmt.Println(err)
return
}
defer target.Close()
c := make(chan int, 2)
go func() {
io.Copy(target, source)
c <- 1
}()
go func() {
io.Copy(source, target)
c <- 2
}()
<-c
}
func main() {
if len(os.Args) < 3 {
fmt.Println(os.Args[0], "<listen port> <target addr>")
return
}
ln, err := net.Listen("tcp", os.Args[1])
if (err != nil) {
fmt.Println(err);
return;
}
for {
conn, err := ln.Accept()
if (err != nil) {
fmt.Println(err)
continue
}
go handle(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment