Created
October 29, 2015 12:13
-
-
Save hongruiqi/53379476f91923e68cc3 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 ( | |
"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