commit 12340199b5c80b1d97f928d42b42a6f01ae8988d Author: David Fifield david@bamsoftware.com Date: Sat Dec 7 21:58:20 2013 -0800
Fuller client and server examples. --- examples/dummy-client/dummy-client.go | 4 +- examples/dummy-server/dummy-server.go | 4 +- pt.go | 98 ++++++++++++++++++++++++--------- 3 files changed, 77 insertions(+), 29 deletions(-)
diff --git a/examples/dummy-client/dummy-client.go b/examples/dummy-client/dummy-client.go index f619e7c..876fc87 100644 --- a/examples/dummy-client/dummy-client.go +++ b/examples/dummy-client/dummy-client.go @@ -43,7 +43,7 @@ func copyLoop(a, b net.Conn) { wg.Wait() }
-func handleConnection(conn *pt.SocksConn) error { +func handler(conn *pt.SocksConn) error { handlerChan <- 1 defer func() { handlerChan <- -1 @@ -72,7 +72,7 @@ func acceptLoop(ln *pt.SocksListener) error { if err != nil { return err } - go handleConnection(conn) + go handler(conn) } return nil } diff --git a/examples/dummy-server/dummy-server.go b/examples/dummy-server/dummy-server.go index 7341b17..0ca901c 100644 --- a/examples/dummy-server/dummy-server.go +++ b/examples/dummy-server/dummy-server.go @@ -44,7 +44,7 @@ func copyLoop(a, b net.Conn) { wg.Wait() }
-func handleConnection(conn net.Conn) error { +func handler(conn net.Conn) error { defer conn.Close()
handlerChan <- 1 @@ -69,7 +69,7 @@ func acceptLoop(ln net.Listener) error { if err != nil { return err } - go handleConnection(conn) + go handler(conn) } return nil } diff --git a/pt.go b/pt.go index 45dc795..bd715c0 100644 --- a/pt.go +++ b/pt.go @@ -2,47 +2,95 @@ // // Sample client usage: // var ptInfo pt.ClientInfo -// var err error -// ptInfo, err = pt.ClientSetup([]string{"foo"}) -// if err != nil { -// os.Exit(1) +// ... +// func handler(conn *pt.SocksConn) error { +// defer conn.Close() +// remote, err := net.Dial("tcp", conn.Req.Target) +// if err != nil { +// conn.Reject() +// return err +// } +// defer remote.Close() +// err = conn.Grant(remote.RemoteAddr().(*net.TCPAddr)) +// if err != nil { +// return err +// } +// // do something with conn and or. +// return nil // } -// for _, methodName := range ptInfo.MethodNames { -// ln, err := pt.ListenSocks("tcp", "127.0.0.1:0") +// func acceptLoop(ln *pt.SocksListener) error { +// for { +// conn, err := ln.AcceptSocks() +// if err != nil { +// return err +// } +// go handler(conn) +// } +// return nil +// } +// ... +// func main() { +// var err error +// ptInfo, err = pt.ClientSetup([]string{"foo"}) // if err != nil { -// pt.CmethodError(methodName, err.Error()) -// continue +// os.Exit(1) +// } +// for _, methodName := range ptInfo.MethodNames { +// ln, err := pt.ListenSocks("tcp", "127.0.0.1:0") +// if err != nil { +// pt.CmethodError(methodName, err.Error()) +// continue +// } +// go acceptLoop(ln) +// pt.Cmethod(methodName, ln.Version(), ln.Addr()) // } -// go acceptLoop(ln) -// pt.Cmethod(methodName, ln.Version(), ln.Addr()) +// pt.CmethodsDone() // } -// pt.CmethodsDone() // // Sample server usage: -// func handler(conn net.Conn) { +// var ptInfo pt.ServerInfo +// ... +// func handler(conn net.Conn) error { +// defer conn.Close() // or, err := pt.ConnectOr(&ptInfo, conn.RemoteAddr(), "foo") // if err != nil { // return // } +// defer or.Close() // // do something with or and conn +// return nil // } -// ... -// var ptInfo pt.ServerInfo -// var err error -// ptInfo, err = pt.ServerSetup([]string{"foo"}) -// if err != nil { -// os.Exit(1) +// func acceptLoop(ln net.Listener) error { +// for { +// conn, err := ln.Accept() +// if err != nil { +// return err +// } +// go handler(conn) +// } +// return nil // } -// for _, bindaddr := range ptInfo.Bindaddrs { -// ln, err := net.ListenTCP("tcp", bindaddr.Addr) +// ... +// func main() { +// var err error +// ptInfo, err = pt.ServerSetup([]string{"foo"}) // if err != nil { -// pt.SmethodError(bindaddr.MethodName, err.Error()) -// continue +// os.Exit(1) // } -// go acceptLoop(ln) -// pt.Smethod(bindaddr.MethodName, ln.Addr()) +// for _, bindaddr := range ptInfo.Bindaddrs { +// ln, err := net.ListenTCP("tcp", bindaddr.Addr) +// if err != nil { +// pt.SmethodError(bindaddr.MethodName, err.Error()) +// continue +// } +// go acceptLoop(ln) +// pt.Smethod(bindaddr.MethodName, ln.Addr()) +// } +// pt.SmethodsDone() // } -// pt.SmethodsDone() +// +// Some additional care is needed to handle SIGINT and shutdown properly. See +// the example programs dummy-client and dummy-server. // // Tor pluggable transports specification: // https://gitweb.torproject.org/torspec.git/blob/HEAD:/pt-spec.txt.
tor-commits@lists.torproject.org