conn: change TCPBufSize to 32K as the golang standard lib did

This commit is contained in:
nadoo committed 2020-10-21 20:00:43 +08:00
1 parent cd78995cd4
commit 904757052d
7 files changed
+31 -52

No files matched your search

+3 -18
View File
@@ -14,7 +14,7 @@ import (
const (
// TCPBufSize is the size of tcp buffer.
TCPBufSize = 16 << 10
TCPBufSize = 32 << 10
// UDPBufSize is the size of udp buffer.
UDPBufSize = 64 << 10
@@ -65,7 +65,7 @@ func Relay(left, right net.Conn) error {
left.SetReadDeadline(time.Now().Add(wait)) // unblock read on left
wg.Wait()
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) { // requires Go 1.15+
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) {
return err1
}
@@ -78,9 +78,7 @@ func Relay(left, right net.Conn) error {
func worthReadFrom(src io.Reader) bool {
switch v := src.(type) {
case *net.TCPConn:
return true
case *net.UnixConn:
case *net.TCPConn, *net.UnixConn:
return true
case *os.File:
fi, err := v.Stat()
@@ -102,11 +100,9 @@ func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
if wt, ok := src.(io.WriterTo); ok {
return wt.WriteTo(dst)
}
if rt, ok := dst.(io.ReaderFrom); ok && worthReadFrom(src) {
return rt.ReadFrom(src)
}
return CopyBuffer(dst, src)
}
@@ -181,14 +177,3 @@ func RelayUDP(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout t
}
}
}
// OutboundIP returns preferred outbound ip of this machine.
func OutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ""
}
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP.String()
}
+1 -1
View File
@@ -85,7 +85,7 @@ func (c *ClientConn) Handshake(host, path string) error {
_, code, _, ok := parseFirstLine(line)
if !ok || code != "101" {
return errors.New("[ws] error in ws handshake parseFirstLine")
return errors.New("[ws] error in ws handshake parseFirstLine: " + line)
}
respHeader, err := tpr.ReadMIMEHeader()
+12 -17
View File
@@ -31,8 +31,7 @@ import (
)
const (
defaultFrameSize = 4096
maxHeaderSize = 2 + 8 + 4 // Fixed header + length + mask
hdrSize = 2 + 8 // Fixed header + length
// byte 0
finalBit byte = 1 << 7
@@ -44,10 +43,9 @@ const (
type frameWriter struct {
io.Writer
header [maxHeaderSize]byte
server bool
maskKey [4]byte
maskOffset int
header [hdrSize]byte
server bool
maskKey [4]byte
}
// FrameWriter returns a frame writer.
@@ -67,22 +65,22 @@ func (w *frameWriter) Write(b []byte) (int, error) {
hdr[1] = maskBit
}
nPayload, lenFieldLen := len(b), 0
nPayload, nLenField := len(b), 0
switch {
case nPayload <= 125:
hdr[1] |= byte(nPayload)
case nPayload < 65536:
hdr[1] |= 126
lenFieldLen = 2
binary.BigEndian.PutUint16(hdr[2:2+lenFieldLen], uint16(nPayload))
nLenField = 2
binary.BigEndian.PutUint16(hdr[2:2+nLenField], uint16(nPayload))
default:
hdr[1] |= 127
lenFieldLen = 8
binary.BigEndian.PutUint64(hdr[2:2+lenFieldLen], uint64(nPayload))
nLenField = 8
binary.BigEndian.PutUint64(hdr[2:2+nLenField], uint64(nPayload))
}
// header and length
_, err := w.Writer.Write(hdr[:2+lenFieldLen])
_, err := w.Writer.Write(hdr[:2+nLenField])
if err != nil {
return 0, err
}
@@ -94,12 +92,13 @@ func (w *frameWriter) Write(b []byte) (int, error) {
buf := pool.GetBuffer(nPayload)
pool.PutBuffer(buf)
// mask
_, err = w.Writer.Write(w.maskKey[:])
if err != nil {
return 0, err
}
// payload mask
// payload with mask
for i := 0; i < nPayload; i++ {
buf[i] = b[i] ^ w.maskKey[i%4]
}
@@ -129,10 +128,6 @@ func (r *frameReader) Read(b []byte) (int, error) {
return 0, err
}
// final := r.buf[0]&finalBit == finalBit
// frameType := int(r.buf[0] & 0xf)
// r.mask = r.buf[1]&maskBit == maskBit
r.left = int64(r.buf[1] & 0x7f)
switch r.left {
case 126:
+5 -5
View File
@@ -3,7 +3,6 @@ package ws
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"net/textproto"
@@ -102,7 +101,7 @@ func (c *ServerConn) Handshake(host, path string) error {
_, path, _, ok := parseFirstLine(line)
if !ok || path != path {
return errors.New("[ws] error in ws handshake parseFirstLine")
return errors.New("[ws] error in ws handshake parseFirstLine: " + line)
}
reqHeader, err := tpr.ReadMIMEHeader()
@@ -110,9 +109,10 @@ func (c *ServerConn) Handshake(host, path string) error {
return err
}
if reqHeader.Get("Host") != host {
return fmt.Errorf("[ws] got wrong host: %s, expected: %s", reqHeader.Get("Host"), host)
}
// NOTE: in server mode, we do not validate the request Host now, check it.
// if reqHeader.Get("Host") != host {
// return fmt.Errorf("[ws] got wrong host: %s, expected: %s", reqHeader.Get("Host"), host)
// }
clientKey := reqHeader.Get("Sec-WebSocket-Key")
serverKey := computeServerKey(clientKey)
+1 -2
View File
@@ -5,7 +5,6 @@ import (
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"net"
"net/url"
"strings"
@@ -51,7 +50,7 @@ func NewWS(s string, d proxy.Dialer, p proxy.Proxy) (*WS, error) {
}
if w.host == "" {
w.host, _, _ = net.SplitHostPort(addr)
w.host = addr
}
if w.path == "" {