Skip to content

Instantly share code, notes, and snippets.

@elazarl
Created June 6, 2011 08:45
Show Gist options
  • Save elazarl/1009958 to your computer and use it in GitHub Desktop.
Save elazarl/1009958 to your computer and use it in GitHub Desktop.
redirect network stream
package main
import (
"log"
"io"
"os"
"crypto/tls"
"strings"
"net"
"flag"
"time"
)
func redCon(r io.ReadCloser,w io.Writer) {
defer r.Close()
buf := make([]byte,100)
for {
nr,err := r.Read(buf)
if err == os.EOF {break}
if err != nil {log.Println("Read:",err);break}
w.Write(buf[:nr])
}
}
func redirect(sock net.Listener,mkCon func() (net.Conn,os.Error)) {
for {
conn, err := sock.Accept()
if err != nil {
log.Fatal(err)
}
red_to,err := mkCon()
if err != nil {log.Println("Error connecting",err);continue}
go redCon(conn, red_to)
go redCon(red_to, conn)
}
}
func mkTls(addr string) func() (net.Conn,os.Error) {
if !strings.Contains(addr,":") {
addr += ":443"
}
return func() (net.Conn,os.Error) {
return tls.Dial("tcp",addr,nil)
}
}
func mkTcp(addr string) func() (net.Conn,os.Error) {
if !strings.Contains(addr,":") {
addr += ":80"
}
return func() (net.Conn,os.Error) {
return net.Dial("tcp",addr)
}
}
var SERVER_CERT = []byte(`-----BEGIN CERTIFICATE-----
MIICATCCAWoCCQD/6eUeFn3yRDANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB
VTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTExMDYwMzEzMjEwMVoXDTEyMDYwMjEzMjEwMVowRTELMAkG
A1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0OwS
N2h83OXkzdv8azyvAwFsAWJZPyV8oLvL05OIUb+RgvzeA1FL3YXsRR1dIBLqD7H8
OmS1ZctpQ61N8dOKISTohGdkK0l3X1ZKN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nLmQLa+JWCQJAPxvmm5VLT0lFh6gYswvEJtUnJ++x1axbGlNxx+cg+vbT
QSD5/EcAsiDP5HgX2BQ8VubV+cruuuOew56wcLjS/Q==
-----END RSA PRIVATE KEY-----`)
func mkSSLListener(addr string) (l net.Listener, err os.Error) {
config := &tls.Config{
//Rand: rand.Reader,
Time: time.Nanoseconds,
}
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.X509KeyPair(SERVER_CERT,
SERVER_KEY)
if err != nil {
log.Printf("Certificates: ", err.String())
return
}
l, e := tls.Listen("tcp", addr, config)
if e != nil {
log.Printf("Listen error : ", e)
return
}
return
}
func mkTCPListener(addr string) (l net.Listener,err os.Error) {
tcpaddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {log.Fatal("Resolve:",err)}
l,err = net.ListenTCP("tcp", tcpaddr)
return
}
func main() {
var dialWithTls bool
var listenWithTls bool
var listenPort string
flag.StringVar(&listenPort, "l", ":8080", "Address we'll listen to")
flag.BoolVar(&dialWithTls, "rtls", false, "connect to target with SSL")
flag.BoolVar(&listenWithTls, "ltls", false, "Listen to connections with SSL")
flag.Parse()
if flag.NArg() == 0 {log.Fatal("No address to connect")}
lmap := map[bool]func(string)(net.Listener,os.Error){true:mkSSLListener,false:mkTCPListener}
lsn, err := lmap[listenWithTls](listenPort)
if err != nil {log.Fatal("Listen:",err)}
maker := mkTcp(flag.Arg(0))
if dialWithTls {
log.Println("Dialing with TLS")
maker = mkTls(flag.Arg(0))
}
redirect(lsn,maker)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment