Skip to content

Instantly share code, notes, and snippets.

@gerep
Forked from scalone/server.go
Created July 8, 2014 13:06
Show Gist options
  • Save gerep/c8b2655e5b8c622d0f7b to your computer and use it in GitHub Desktop.
Save gerep/c8b2655e5b8c622d0f7b to your computer and use it in GitHub Desktop.
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