commit 6553c94c3132e22f09bbf8ddb2d806be6cbc0514
Author: David Fifield <david(a)bamsoftware.com>
Date: Mon Nov 26 21:24:42 2012 -0800
Capitalization for scope.
---
websocket-transport/pt.go | 88 +++++++++++++++---------------
websocket-transport/socks.go | 12 ++--
websocket-transport/websocket-client.go | 18 +++---
websocket-transport/websocket-server.go | 20 ++++----
websocket-transport/websocket.go | 36 ++++++------
5 files changed, 87 insertions(+), 87 deletions(-)
diff --git a/websocket-transport/pt.go b/websocket-transport/pt.go
index e63dd6a..f8760d3 100644
--- a/websocket-transport/pt.go
+++ b/websocket-transport/pt.go
@@ -12,7 +12,7 @@ import (
func getenvRequired(key string) string {
value := os.Getenv(key)
if value == "" {
- ptEnvError(fmt.Sprintf("no %s environment variable", key))
+ PtEnvError(fmt.Sprintf("no %s environment variable", key))
}
return value
}
@@ -35,7 +35,7 @@ func escape(s string) string {
return buf.String()
}
-func ptLine(keyword string, v ...string) {
+func PtLine(keyword string, v ...string) {
var buf bytes.Buffer
buf.WriteString(keyword)
for _, x := range v {
@@ -44,22 +44,22 @@ func ptLine(keyword string, v ...string) {
fmt.Println(buf.String())
}
-func ptEnvError(msg string) {
- ptLine("ENV-ERROR", msg)
+func PtEnvError(msg string) {
+ PtLine("ENV-ERROR", msg)
os.Exit(1)
}
-func ptVersionError(msg string) {
- ptLine("VERSION-ERROR", msg)
+func PtVersionError(msg string) {
+ PtLine("VERSION-ERROR", msg)
os.Exit(1)
}
-func ptCmethodError(methodName, msg string) {
- ptLine("CMETHOD-ERROR", methodName, msg)
+func PtCmethodError(methodName, msg string) {
+ PtLine("CMETHOD-ERROR", methodName, msg)
os.Exit(1)
}
-func ptGetManagedTransportVer() string {
+func PtGetManagedTransportVer() string {
const transportVersion = "1"
for _, offered := range strings.Split(getenvRequired("TOR_PT_MANAGED_TRANSPORT_VER"), ",") {
if offered == transportVersion {
@@ -69,7 +69,7 @@ func ptGetManagedTransportVer() string {
return ""
}
-func ptGetClientTransports(supported []string) []string {
+func PtGetClientTransports(supported []string) []string {
clientTransports := getenvRequired("TOR_PT_CLIENT_TRANSPORTS")
if clientTransports == "*" {
return supported
@@ -86,42 +86,42 @@ func ptGetClientTransports(supported []string) []string {
return result
}
-func ptCmethod(name string, socks string, addr net.Addr) {
- ptLine("CMETHOD", name, socks, addr.String())
+func PtCmethod(name string, socks string, addr net.Addr) {
+ PtLine("CMETHOD", name, socks, addr.String())
}
-func ptCmethodsDone() {
- ptLine("CMETHODS", "DONE")
+func PtCmethodsDone() {
+ PtLine("CMETHODS", "DONE")
}
-func ptClientSetup(methodNames []string) []string {
- ver := ptGetManagedTransportVer()
+func PtClientSetup(methodNames []string) []string {
+ ver := PtGetManagedTransportVer()
if ver == "" {
- ptVersionError("no-version")
+ PtVersionError("no-version")
} else {
- ptLine("VERSION", ver)
+ PtLine("VERSION", ver)
}
- methods := ptGetClientTransports(methodNames)
+ methods := PtGetClientTransports(methodNames)
if len(methods) == 0 {
- ptCmethodsDone()
+ PtCmethodsDone()
os.Exit(1)
}
return methods
}
-func ptSmethodError(methodName, msg string) {
- ptLine("SMETHOD-ERROR", methodName, msg)
+func PtSmethodError(methodName, msg string) {
+ PtLine("SMETHOD-ERROR", methodName, msg)
os.Exit(1)
}
-func ptSmethod(name string, addr net.Addr) {
- ptLine("SMETHOD", name, addr.String())
+func PtSmethod(name string, addr net.Addr) {
+ PtLine("SMETHOD", name, addr.String())
}
-func ptSmethodsDone() {
- ptLine("SMETHODS", "DONE")
+func PtSmethodsDone() {
+ PtLine("SMETHODS", "DONE")
}
func resolveBindAddr(bindAddr string) (*net.TCPAddr, error) {
@@ -140,13 +140,13 @@ func resolveBindAddr(bindAddr string) (*net.TCPAddr, error) {
return net.ResolveTCPAddr("tcp", bindAddr)
}
-type ptBindAddr struct {
+type PtBindAddr struct {
MethodName string
Addr *net.TCPAddr
}
-func filterBindAddrs(addrs []ptBindAddr, supported []string) []ptBindAddr {
- var result []ptBindAddr
+func filterBindAddrs(addrs []PtBindAddr, supported []string) []PtBindAddr {
+ var result []PtBindAddr
for _, ba := range addrs {
for _, methodName := range supported {
@@ -163,22 +163,22 @@ func filterBindAddrs(addrs []ptBindAddr, supported []string) []ptBindAddr {
// Return a map from method names to bind addresses. The map is the contents of
// TOR_PT_SERVER_BINDADDR, with keys filtered by TOR_PT_SERVER_TRANSPORTS, and
// further filtered by methods that we know.
-func ptGetServerBindAddrs(supported []string) []ptBindAddr {
- var result []ptBindAddr
+func PtGetServerBindAddrs(supported []string) []PtBindAddr {
+ var result []PtBindAddr
// Get the list of all requested bindaddrs.
var serverBindAddr = getenvRequired("TOR_PT_SERVER_BINDADDR")
for _, spec := range strings.Split(serverBindAddr, ",") {
- var bindAddr ptBindAddr
+ var bindAddr PtBindAddr
parts := strings.SplitN(spec, "-", 2)
if len(parts) != 2 {
- ptEnvError(fmt.Sprintf("TOR_PT_SERVER_BINDADDR: %q: doesn't contain \"-\"", spec))
+ PtEnvError(fmt.Sprintf("TOR_PT_SERVER_BINDADDR: %q: doesn't contain \"-\"", spec))
}
bindAddr.MethodName = parts[0]
addr, err := resolveBindAddr(parts[1])
if err != nil {
- ptEnvError(fmt.Sprintf("TOR_PT_SERVER_BINDADDR: %q: %s", spec, err.Error()))
+ PtEnvError(fmt.Sprintf("TOR_PT_SERVER_BINDADDR: %q: %s", spec, err.Error()))
}
bindAddr.Addr = addr
result = append(result, bindAddr)
@@ -196,31 +196,31 @@ func ptGetServerBindAddrs(supported []string) []ptBindAddr {
return result
}
-type ptServerInfo struct {
- BindAddrs []ptBindAddr
+type PtServerInfo struct {
+ BindAddrs []PtBindAddr
OrAddr *net.TCPAddr
}
-func ptServerSetup(methodNames []string) ptServerInfo {
- var info ptServerInfo
+func PtServerSetup(methodNames []string) PtServerInfo {
+ var info PtServerInfo
var err error
- ver := ptGetManagedTransportVer()
+ ver := PtGetManagedTransportVer()
if ver == "" {
- ptVersionError("no-version")
+ PtVersionError("no-version")
} else {
- ptLine("VERSION", ver)
+ PtLine("VERSION", ver)
}
var orPort = getenvRequired("TOR_PT_ORPORT")
info.OrAddr, err = net.ResolveTCPAddr("tcp", orPort)
if err != nil {
- ptEnvError(fmt.Sprintf("cannot resolve TOR_PT_ORPORT %q: %s", orPort, err.Error()))
+ PtEnvError(fmt.Sprintf("cannot resolve TOR_PT_ORPORT %q: %s", orPort, err.Error()))
}
- info.BindAddrs = ptGetServerBindAddrs(methodNames)
+ info.BindAddrs = PtGetServerBindAddrs(methodNames)
if len(info.BindAddrs) == 0 {
- ptSmethodsDone()
+ PtSmethodsDone()
os.Exit(1)
}
diff --git a/websocket-transport/socks.go b/websocket-transport/socks.go
index 33df918..061869e 100644
--- a/websocket-transport/socks.go
+++ b/websocket-transport/socks.go
@@ -16,7 +16,7 @@ const (
socksRequestFailed = 0x5b
)
-func readSocks4aConnect(s io.Reader) (string, error) {
+func ReadSocks4aConnect(s io.Reader) (string, error) {
r := bufio.NewReader(s)
var h [8]byte
@@ -57,7 +57,7 @@ func readSocks4aConnect(s io.Reader) (string, error) {
return fmt.Sprintf("%s:%d", host, port), nil
}
-func sendSocks4aResponse(w io.Writer, code byte, addr *net.TCPAddr) error {
+func SendSocks4aResponse(w io.Writer, code byte, addr *net.TCPAddr) error {
var resp [8]byte
resp[0] = socksResponseVersion
resp[1] = code
@@ -73,10 +73,10 @@ func sendSocks4aResponse(w io.Writer, code byte, addr *net.TCPAddr) error {
var emptyAddr = net.TCPAddr{net.IPv4(0, 0, 0, 0), 0}
-func sendSocks4aResponseGranted(w io.Writer, addr *net.TCPAddr) error {
- return sendSocks4aResponse(w, socksRequestGranted, addr)
+func SendSocks4aResponseGranted(w io.Writer, addr *net.TCPAddr) error {
+ return SendSocks4aResponse(w, socksRequestGranted, addr)
}
-func sendSocks4aResponseFailed(w io.Writer) error {
- return sendSocks4aResponse(w, socksRequestFailed, &emptyAddr)
+func SendSocks4aResponseFailed(w io.Writer) error {
+ return SendSocks4aResponse(w, socksRequestFailed, &emptyAddr)
}
diff --git a/websocket-transport/websocket-client.go b/websocket-transport/websocket-client.go
index 7e483f6..a8d437b 100644
--- a/websocket-transport/websocket-client.go
+++ b/websocket-transport/websocket-client.go
@@ -97,9 +97,9 @@ func handleConnection(conn *net.TCPConn) error {
}()
conn.SetDeadline(time.Now().Add(socksTimeout * time.Second))
- dest, err := readSocks4aConnect(conn)
+ dest, err := ReadSocks4aConnect(conn)
if err != nil {
- sendSocks4aResponseFailed(conn)
+ SendSocks4aResponseFailed(conn)
return err
}
// Disable deadline.
@@ -109,20 +109,20 @@ func handleConnection(conn *net.TCPConn) error {
// We need the parsed IP and port for the SOCKS reply.
destAddr, err := net.ResolveTCPAddr("tcp", dest)
if err != nil {
- sendSocks4aResponseFailed(conn)
+ SendSocks4aResponseFailed(conn)
return err
}
wsUrl := url.URL{Scheme: "ws", Host: dest}
ws, err := websocket.Dial(wsUrl.String(), "", wsUrl.String())
if err != nil {
- sendSocks4aResponseFailed(conn)
+ SendSocks4aResponseFailed(conn)
return err
}
defer ws.Close()
logDebug("WebSocket connection to %s", ws.Config().Location.String())
- sendSocks4aResponseGranted(conn, destAddr)
+ SendSocks4aResponseGranted(conn, destAddr)
proxy(conn, ws)
@@ -176,18 +176,18 @@ func main() {
socksAddrStrs = defaultSocksAddrStrs
}
- ptClientSetup([]string{ptMethodName})
+ PtClientSetup([]string{ptMethodName})
listeners := make([]*net.TCPListener, 0)
for _, socksAddrStr := range socksAddrStrs {
ln, err := startListener(socksAddrStr)
if err != nil {
- ptCmethodError(ptMethodName, err.Error())
+ PtCmethodError(ptMethodName, err.Error())
}
- ptCmethod(ptMethodName, "socks4", ln.Addr())
+ PtCmethod(ptMethodName, "socks4", ln.Addr())
listeners = append(listeners, ln)
}
- ptCmethodsDone()
+ PtCmethodsDone()
var numHandlers int = 0
diff --git a/websocket-transport/websocket-server.go b/websocket-transport/websocket-server.go
index 90a2a8d..aadd602 100644
--- a/websocket-transport/websocket-server.go
+++ b/websocket-transport/websocket-server.go
@@ -16,7 +16,7 @@ import (
var defaultPort int
-var ptInfo ptServerInfo
+var ptInfo PtServerInfo
// When a connection handler starts, +1 is written to this channel; when it
// ends, -1 is written.
@@ -27,14 +27,14 @@ func logDebug(format string, v ...interface{}) {
}
type websocketConn struct {
- Ws *websocket
+ Ws *Websocket
Base64 bool
messageBuf []byte
}
func (conn *websocketConn) Read(b []byte) (n int, err error) {
for len(conn.messageBuf) == 0 {
- var m websocketMessage
+ var m WebsocketMessage
m, err = conn.Ws.ReadMessage()
if err != nil {
return
@@ -112,7 +112,7 @@ func (conn *websocketConn) SetWriteDeadline(t time.Time) error {
return conn.Ws.Conn.SetWriteDeadline(t)
}
-func NewWebsocketConn(ws *websocket) websocketConn {
+func NewWebsocketConn(ws *Websocket) websocketConn {
var conn websocketConn
conn.Ws = ws
conn.Base64 = (ws.Subprotocol == "base64")
@@ -147,7 +147,7 @@ func proxy(local *net.TCPConn, conn *websocketConn) {
wg.Wait()
}
-func websocketHandler(ws *websocket) {
+func websocketHandler(ws *Websocket) {
conn := NewWebsocketConn(ws)
handlerChan <- 1
@@ -170,7 +170,7 @@ func startListener(addr *net.TCPAddr) (*net.TCPListener, error) {
return nil, err
}
go func() {
- var config websocketConfig
+ var config WebsocketConfig
config.Subprotocols = []string{"base64"}
config.MaxMessageSize = 2500
http.Handle("/", config.Handler(websocketHandler))
@@ -188,7 +188,7 @@ func main() {
flag.IntVar(&defaultPort, "port", 0, "port to listen on if unspecified by Tor")
flag.Parse()
- ptInfo = ptServerSetup([]string{ptMethodName})
+ ptInfo = PtServerSetup([]string{ptMethodName})
listeners := make([]*net.TCPListener, 0)
for _, bindAddr := range ptInfo.BindAddrs {
@@ -200,12 +200,12 @@ func main() {
ln, err := startListener(bindAddr.Addr)
if err != nil {
- ptSmethodError(bindAddr.MethodName, err.Error())
+ PtSmethodError(bindAddr.MethodName, err.Error())
}
- ptSmethod(bindAddr.MethodName, ln.Addr())
+ PtSmethod(bindAddr.MethodName, ln.Addr())
listeners = append(listeners, ln)
}
- ptSmethodsDone()
+ PtSmethodsDone()
var numHandlers int = 0
diff --git a/websocket-transport/websocket.go b/websocket-transport/websocket.go
index e401640..cdb4b5d 100644
--- a/websocket-transport/websocket.go
+++ b/websocket-transport/websocket.go
@@ -15,19 +15,19 @@ import (
"strings"
)
-type websocketConfig struct {
+type WebsocketConfig struct {
Subprotocols []string
MaxMessageSize uint64
}
-func (config *websocketConfig) maxMessageSize() uint64 {
+func (config *WebsocketConfig) maxMessageSize() uint64 {
if config.MaxMessageSize == 0 {
return 64000
}
return config.MaxMessageSize
}
-type websocket struct {
+type Websocket struct {
Conn net.Conn
Bufrw *bufio.ReadWriter
// Whether we are a client or a server implications for masking.
@@ -37,17 +37,17 @@ type websocket struct {
messageBuf bytes.Buffer
}
-type websocketFrame struct {
+type WebsocketFrame struct {
Fin bool
Opcode byte
Payload []byte
}
-func (frame *websocketFrame) IsControl() bool {
+func (frame *WebsocketFrame) IsControl() bool {
return (frame.Opcode & 0x08) != 0
}
-type websocketMessage struct {
+type WebsocketMessage struct {
Opcode byte
Payload []byte
}
@@ -58,7 +58,7 @@ func applyMask(payload []byte, maskKey [4]byte) {
}
}
-func (ws *websocket) ReadFrame() (frame websocketFrame, err error) {
+func (ws *Websocket) ReadFrame() (frame WebsocketFrame, err error) {
var b byte
err = binary.Read(ws.Bufrw, binary.BigEndian, &b)
if err != nil {
@@ -122,10 +122,10 @@ func (ws *websocket) ReadFrame() (frame websocketFrame, err error) {
return frame, nil
}
-func (ws *websocket) ReadMessage() (message websocketMessage, err error) {
+func (ws *Websocket) ReadMessage() (message WebsocketMessage, err error) {
var opcode byte = 0
for {
- var frame websocketFrame
+ var frame WebsocketFrame
frame, err = ws.ReadFrame()
if err != nil {
return
@@ -169,7 +169,7 @@ func (ws *websocket) ReadMessage() (message websocketMessage, err error) {
}
// Destructively masks payload in place if ws.IsClient.
-func (ws *websocket) WriteFrame(opcode byte, payload []byte) (err error) {
+func (ws *Websocket) WriteFrame(opcode byte, payload []byte) (err error) {
if opcode >= 16 {
err = errors.New(fmt.Sprintf("opcode %d is >= 16", opcode))
return
@@ -212,7 +212,7 @@ func (ws *websocket) WriteFrame(opcode byte, payload []byte) (err error) {
return
}
-func (ws *websocket) WriteMessage(opcode byte, payload []byte) (err error) {
+func (ws *Websocket) WriteMessage(opcode byte, payload []byte) (err error) {
return ws.WriteFrame(opcode, payload)
}
@@ -251,8 +251,8 @@ func httpError(w http.ResponseWriter, bufrw *bufio.ReadWriter, code int) {
}
type WebSocketHTTPHandler struct {
- config *websocketConfig
- websocketCallback func(*websocket)
+ Config *WebsocketConfig
+ WebsocketCallback func(*Websocket)
}
func (handler *WebSocketHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@@ -319,11 +319,11 @@ func (handler *WebSocketHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.
// 9. Optionally, a |Sec-WebSocket-Extensions| header field...
// 10. Optionally, other header fields...
- var ws websocket
+ var ws Websocket
ws.Conn = conn
ws.Bufrw = bufrw
ws.IsClient = false
- ws.MaxMessageSize = handler.config.MaxMessageSize
+ ws.MaxMessageSize = handler.Config.MaxMessageSize
// See RFC 6455 section 4.2.2, item 5 for these steps.
@@ -345,7 +345,7 @@ func (handler *WebSocketHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.
// 5. Optionally, a |Sec-WebSocket-Protocol| header field, with a value
// /subprotocol/ as defined in step 4 in Section 4.2.2.
for _, clientProto := range clientProtocols {
- for _, serverProto := range handler.config.Subprotocols {
+ for _, serverProto := range handler.Config.Subprotocols {
if clientProto == serverProto {
ws.Subprotocol = clientProto
w.Header().Set("Sec-WebSocket-Protocol", clientProto)
@@ -359,9 +359,9 @@ func (handler *WebSocketHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.
bufrw.Flush()
// Call the WebSocket-specific handler.
- handler.websocketCallback(&ws)
+ handler.WebsocketCallback(&ws)
}
-func (config *websocketConfig) Handler(f func(*websocket)) http.Handler {
+func (config *WebsocketConfig) Handler(f func(*Websocket)) http.Handler {
return &WebSocketHTTPHandler{config, f}
}