Skip to content

Instantly share code, notes, and snippets.

@gerep
Forked from scalone/server.go
Created Jul 8, 2014
Embed
What would you like to do?
package main
import (
"fmt"
"bufio"
"net"
"io"
)
func main() {
service := "0.0.0.0:8080"
tcpAddr, err := net.ResolveTCPAddr("tcp", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
fmt.Println("Server listening")
for {
conn, err := listener.Accept()
fmt.Println("Server Accepted")
bufr := bufio.NewReader(conn)
header, err := bufr.Peek(6)
fmt.Println("Recv err [%s] header [%s]", err, header)
if header[4] == 0x17 && header[5] == 0x0a {
fmt.Println("Go Riak")
err = foward(conn, bufr, "192.168.0.20:8087")
}
if err == nil {
fmt.Println("Go Torrent")
err = foward(conn, bufr, "54.232.100.223:800")
}
fmt.Println("Err %s", err)
//conn.Close()
}
}
func foward(conn net.Conn, bufr *bufio.Reader, address string) error {
remote, err := net.Dial("tcp", address)
checkError(err)
if err == nil && remote != nil {
go io.Copy(remote, bufr)
go io.Copy(conn, remote)
go io.Copy(remote, conn)
}
return err
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment