Last active
July 19, 2016 14:33
-
-
Save unixpickle/638ae05184fdf330bf7f73fa3d5b6303 to your computer and use it in GitHub Desktop.
Tiny TCP proxy
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 ( | |
"io" | |
"log" | |
"net" | |
"time" | |
) | |
var ListenHost = ":1234" | |
var TargetHost = "192.168.1.108:22" | |
var Timeout = time.Second * 5 | |
func main() { | |
listener, _ := net.Listen("tcp", ListenHost) | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
go func() { | |
localConn, err := net.DialTimeout("tcp", TargetHost, Timeout) | |
if err != nil { | |
log.Println(err) | |
conn.Close() | |
return | |
} | |
go func() { | |
io.Copy(conn, localConn) | |
conn.Close() | |
}() | |
go func() { | |
io.Copy(localConn, conn) | |
localConn.Close() | |
}() | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment