mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
pool: return specified size of buffer
This commit is contained in:
1 parent
5326c0a901
commit
a0542a028e
16 files changed
+158
-139
No files matched your search
+9
-5
@@ -2,11 +2,12 @@ package obfs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
)
|
||||
|
||||
// HTTPObfs struct
|
||||
@@ -42,16 +43,19 @@ func (p *HTTPObfs) NewConn(c net.Conn) (net.Conn, error) {
|
||||
}
|
||||
|
||||
func (c *HTTPObfsConn) writeHeader() (int, error) {
|
||||
var buf bytes.Buffer
|
||||
buf := pool.GetWriteBuffer()
|
||||
defer pool.PutWriteBuffer(buf)
|
||||
|
||||
buf.WriteString("GET " + c.obfsURI + " HTTP/1.1\r\n")
|
||||
buf.WriteString("Host: " + c.obfsHost + "\r\n")
|
||||
buf.WriteString("User-Agent: " + c.obfsUA + "\r\n")
|
||||
buf.WriteString("Upgrade: websocket\r\n")
|
||||
buf.WriteString("Connection: Upgrade\r\n")
|
||||
|
||||
p := make([]byte, 16)
|
||||
rand.Read(p)
|
||||
buf.WriteString("Sec-WebSocket-Key: " + base64.StdEncoding.EncodeToString(p) + "\r\n")
|
||||
b := pool.GetBuffer(16)
|
||||
rand.Read(b)
|
||||
buf.WriteString("Sec-WebSocket-Key: " + base64.StdEncoding.EncodeToString(b) + "\r\n")
|
||||
pool.PutBuffer(b)
|
||||
|
||||
buf.WriteString("\r\n")
|
||||
|
||||
|
||||
+9
-5
@@ -17,6 +17,8 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -69,12 +71,13 @@ func (c *TLSObfsConn) Write(b []byte) (int, error) {
|
||||
end = n
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf := pool.GetWriteBuffer()
|
||||
buf.Write([]byte{0x17, 0x03, 0x03})
|
||||
binary.Write(&buf, binary.BigEndian, uint16(len(b[i:end])))
|
||||
binary.Write(buf, binary.BigEndian, uint16(len(b[i:end])))
|
||||
buf.Write(b[i:end])
|
||||
|
||||
_, err := c.Conn.Write(buf.Bytes())
|
||||
pool.PutWriteBuffer(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -124,7 +127,7 @@ func (c *TLSObfsConn) Read(b []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (c *TLSObfsConn) handshake(b []byte) (int, error) {
|
||||
var buf bytes.Buffer
|
||||
buf := pool.GetWriteBuffer()
|
||||
|
||||
// prepare extension & clientHello content
|
||||
bufExt, bufHello := extension(b, c.obfsHost), clientHello()
|
||||
@@ -142,7 +145,7 @@ func (c *TLSObfsConn) handshake(b []byte) (int, error) {
|
||||
buf.Write([]byte{0x03, 0x01})
|
||||
|
||||
// length
|
||||
binary.Write(&buf, binary.BigEndian, uint16(handshakeLen))
|
||||
binary.Write(buf, binary.BigEndian, uint16(handshakeLen))
|
||||
|
||||
// Handshake Begin
|
||||
// Handshake Type: Client Hello (1)
|
||||
@@ -156,12 +159,13 @@ func (c *TLSObfsConn) handshake(b []byte) (int, error) {
|
||||
|
||||
// Extension Begin
|
||||
// ext content length
|
||||
binary.Write(&buf, binary.BigEndian, uint16(extLen))
|
||||
binary.Write(buf, binary.BigEndian, uint16(extLen))
|
||||
|
||||
// ext content
|
||||
buf.Write(bufExt.Bytes())
|
||||
|
||||
_, err := c.Conn.Write(buf.Bytes())
|
||||
pool.PutWriteBuffer(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user