general: optimize code

This commit is contained in:
nadoo committed 2019-09-07 20:25:00 +08:00
1 parent 69bbaa809c
commit d36b8fa394
12 files changed
+65 -59

No files matched your search

+7 -7
View File
@@ -43,17 +43,17 @@ func (p *HTTPObfs) NewConn(c net.Conn) (net.Conn, error) {
func (c *HTTPObfsConn) writeHeader() (int, error) {
buf := new(bytes.Buffer)
buf.Write([]byte("GET " + c.obfsURI + " HTTP/1.1\r\n"))
buf.Write([]byte("Host: " + c.obfsHost + "\r\n"))
buf.Write([]byte("User-Agent: " + c.obfsUA + "\r\n"))
buf.Write([]byte("Upgrade: websocket\r\n"))
buf.Write([]byte("Connection: Upgrade\r\n"))
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.Write([]byte("Sec-WebSocket-Key: " + base64.StdEncoding.EncodeToString(p) + "\r\n"))
buf.WriteString("Sec-WebSocket-Key: " + base64.StdEncoding.EncodeToString(p) + "\r\n")
buf.Write([]byte("\r\n"))
buf.WriteString("\r\n")
return c.Conn.Write(buf.Bytes())
}
+1 -1
View File
@@ -30,7 +30,7 @@ func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHea
if ctrlConn != nil {
go func() {
buf := []byte{}
buf := make([]byte, 1)
for {
_, err := ctrlConn.Read(buf)
if err, ok := err.(net.Error); ok && err.Timeout() {
+2 -1
View File
@@ -9,6 +9,7 @@
// socks5 server:
// https://github.com/shadowsocks/go-shadowsocks2/tree/master/socks
// Package socks5 implements a socks5 proxy.
package socks5
import (
@@ -114,7 +115,7 @@ func (s *SOCKS5) Serve(c net.Conn) {
if err != nil {
// UDP: keep the connection until disconnect then free the UDP socket
if err == socks.Errors[9] {
buf := []byte{}
buf := make([]byte, 1)
// block here
for {
_, err := c.Read(buf)
+21 -23
View File
@@ -15,20 +15,20 @@ import (
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
// Client ws client
// Client is ws client struct.
type Client struct {
host string
path string
}
// Conn is a connection to ws server
// Conn is a connection to ws server.
type Conn struct {
net.Conn
reader io.Reader
writer io.Writer
}
// NewClient .
// NewClient creates a new ws client.
func NewClient(host, path string) (*Client, error) {
if path == "" {
path = "/"
@@ -37,33 +37,38 @@ func NewClient(host, path string) (*Client, error) {
return c, nil
}
// NewConn .
// NewConn creates a new ws client connection.
func (c *Client) NewConn(rc net.Conn, target string) (*Conn, error) {
conn := &Conn{Conn: rc}
return conn, conn.Handshake(c.host, c.path)
}
// Handshake handshakes with the server using HTTP to request a protocol upgrade
// Handshake handshakes with the server using HTTP to request a protocol upgrade.
func (c *Conn) Handshake(host, path string) error {
clientKey := generateClientKey()
var buf bytes.Buffer
buf.Write([]byte("GET " + path + " HTTP/1.1\r\n"))
buf.Write([]byte("Host: " + host + "\r\n"))
buf.Write([]byte("Upgrade: websocket\r\n"))
buf.Write([]byte("Connection: Upgrade\r\n"))
buf.Write([]byte("Origin: http://" + host + "\r\n"))
buf.Write([]byte("Sec-WebSocket-Key: " + clientKey + "\r\n"))
buf.Write([]byte("Sec-WebSocket-Protocol: binary\r\n"))
buf.Write([]byte("Sec-WebSocket-Version: 13\r\n"))
buf.Write([]byte("\r\n"))
buf.WriteString("GET " + path + " HTTP/1.1\r\n")
buf.WriteString("Host: " + host + "\r\n")
buf.WriteString("Upgrade: websocket\r\n")
buf.WriteString("Connection: Upgrade\r\n")
buf.WriteString("Origin: http://" + host + "\r\n")
buf.WriteString("Sec-WebSocket-Key: " + clientKey + "\r\n")
buf.WriteString("Sec-WebSocket-Protocol: binary\r\n")
buf.WriteString("Sec-WebSocket-Version: 13\r\n")
buf.WriteString(("\r\n"))
if _, err := c.Conn.Write(buf.Bytes()); err != nil {
return err
}
tpr := textproto.NewReader(bufio.NewReader(c.Conn))
_, code, _, ok := parseFirstLine(tpr)
line, err := tpr.ReadLine()
if err != nil {
return err
}
_, code, _, ok := parseFirstLine(line)
if !ok || code != "101" {
return errors.New("[ws] error in ws handshake parseFirstLine")
}
@@ -99,14 +104,7 @@ func (c *Conn) Read(b []byte) (n int, err error) {
// parseFirstLine parses "GET /foo HTTP/1.1" OR "HTTP/1.1 200 OK" into its three parts.
// TODO: move to separate http lib package for reuse(also for http proxy module)
func parseFirstLine(tp *textproto.Reader) (r1, r2, r3 string, ok bool) {
line, err := tp.ReadLine()
// log.F("first line: %s", line)
if err != nil {
// log.F("[ws] read first line error:%s", err)
return
}
func parseFirstLine(line string) (r1, r2, r3 string, ok bool) {
s1 := strings.Index(line, " ")
s2 := strings.Index(line[s1+1:], " ")
if s1 < 0 || s2 < 0 {
+2 -2
View File
@@ -45,7 +45,7 @@ type frameWriter struct {
maskKey []byte
}
// FrameWriter returns a frame writer
// FrameWriter returns a frame writer.
func FrameWriter(w io.Writer) io.Writer {
n := rand.Uint32()
return &frameWriter{
@@ -130,7 +130,7 @@ type frameReader struct {
leftBytes int64
}
// FrameReader returns a chunked reader
// FrameReader returns a chunked reader.
func FrameReader(r io.Reader) io.Reader {
return &frameReader{
Reader: r,
+4 -3
View File
@@ -1,3 +1,4 @@
// Package ws implements a simple websocket client.
package ws
import (
@@ -10,7 +11,7 @@ import (
"github.com/nadoo/glider/proxy"
)
// WS .
// WS is the base ws proxy struct.
type WS struct {
dialer proxy.Dialer
addr string
@@ -63,7 +64,7 @@ func NewWSDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
return NewWS(s, dialer)
}
// Addr returns forwarder's address
// Addr returns forwarder's address.
func (s *WS) Addr() string {
if s.addr == "" {
return s.dialer.Addr()
@@ -71,7 +72,7 @@ func (s *WS) Addr() string {
return s.addr
}
// NextDialer returns the next dialer
// NextDialer returns the next dialer.
func (s *WS) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy.