mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Remove support for the base64 WebSocket subprotocol.
This was only needed for very very old Firefox before WebSockets were properly standardized.
This commit is contained in:
parent
3e78251715
commit
15963688c2
1 changed files with 9 additions and 40 deletions
|
@ -10,7 +10,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -31,8 +30,7 @@ import (
|
||||||
const ptMethodName = "snowflake"
|
const ptMethodName = "snowflake"
|
||||||
const requestTimeout = 10 * time.Second
|
const requestTimeout = 10 * time.Second
|
||||||
|
|
||||||
// "4/3+1" accounts for possible base64 encoding.
|
const maxMessageSize = 64*1024
|
||||||
const maxMessageSize = 64*1024*4/3 + 1
|
|
||||||
|
|
||||||
var ptInfo pt.ServerInfo
|
var ptInfo pt.ServerInfo
|
||||||
|
|
||||||
|
@ -50,11 +48,9 @@ func usage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// An abstraction that makes an underlying WebSocket connection look like an
|
// An abstraction that makes an underlying WebSocket connection look like an
|
||||||
// io.ReadWriteCloser. It internally takes care of things like base64 encoding
|
// io.ReadWriteCloser.
|
||||||
// and decoding.
|
|
||||||
type webSocketConn struct {
|
type webSocketConn struct {
|
||||||
Ws *websocket.WebSocket
|
Ws *websocket.WebSocket
|
||||||
Base64 bool
|
|
||||||
messageBuf []byte
|
messageBuf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,25 +66,11 @@ func (conn *webSocketConn) Read(b []byte) (n int, err error) {
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if conn.Base64 {
|
if m.Opcode != 2 {
|
||||||
if m.Opcode != 1 {
|
err = errors.New(fmt.Sprintf("got non-binary opcode %d", m.Opcode))
|
||||||
err = errors.New(fmt.Sprintf("got non-text opcode %d with the base64 subprotocol", m.Opcode))
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.messageBuf = make([]byte, base64.StdEncoding.DecodedLen(len(m.Payload)))
|
|
||||||
var num int
|
|
||||||
num, err = base64.StdEncoding.Decode(conn.messageBuf, m.Payload)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.messageBuf = conn.messageBuf[:num]
|
|
||||||
} else {
|
|
||||||
if m.Opcode != 2 {
|
|
||||||
err = errors.New(fmt.Sprintf("got non-binary opcode %d with no subprotocol", m.Opcode))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.messageBuf = m.Payload
|
|
||||||
}
|
}
|
||||||
|
conn.messageBuf = m.Payload
|
||||||
}
|
}
|
||||||
|
|
||||||
n = copy(b, conn.messageBuf)
|
n = copy(b, conn.messageBuf)
|
||||||
|
@ -98,20 +80,9 @@ func (conn *webSocketConn) Read(b []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements io.Writer.
|
// Implements io.Writer.
|
||||||
func (conn *webSocketConn) Write(b []byte) (n int, err error) {
|
func (conn *webSocketConn) Write(b []byte) (int, error) {
|
||||||
if conn.Base64 {
|
err := conn.Ws.WriteMessage(2, b)
|
||||||
buf := make([]byte, base64.StdEncoding.EncodedLen(len(b)))
|
return len(b), err
|
||||||
base64.StdEncoding.Encode(buf, b)
|
|
||||||
err = conn.Ws.WriteMessage(1, buf)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
n = len(b)
|
|
||||||
} else {
|
|
||||||
err = conn.Ws.WriteMessage(2, b)
|
|
||||||
n = len(b)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements io.Closer.
|
// Implements io.Closer.
|
||||||
|
@ -125,7 +96,6 @@ func (conn *webSocketConn) Close() error {
|
||||||
func newWebSocketConn(ws *websocket.WebSocket) webSocketConn {
|
func newWebSocketConn(ws *websocket.WebSocket) webSocketConn {
|
||||||
var conn webSocketConn
|
var conn webSocketConn
|
||||||
conn.Ws = ws
|
conn.Ws = ws
|
||||||
conn.Base64 = (ws.Subprotocol == "base64")
|
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +203,6 @@ func startServer(ln net.Listener) (net.Listener, error) {
|
||||||
go func() {
|
go func() {
|
||||||
defer ln.Close()
|
defer ln.Close()
|
||||||
var config websocket.Config
|
var config websocket.Config
|
||||||
config.Subprotocols = []string{"base64"}
|
|
||||||
config.MaxMessageSize = maxMessageSize
|
config.MaxMessageSize = maxMessageSize
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Handler: config.Handler(webSocketHandler),
|
Handler: config.Handler(webSocketHandler),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue