general: optimize code

This commit is contained in:
nadoo 2019-09-07 17:17:38 +08:00
parent 69bbaa809c
commit d36b8fa394
12 changed files with 65 additions and 59 deletions

View File

@ -1,5 +1,5 @@
language: go language: go
go: go:
- "1.12.x" - "1.13.x"
- master - master

View File

@ -4,4 +4,4 @@ forward=reject://
ipset=glider ipset=glider
domain=pornhub.com domain=pornhub.com
domain=amazon.com domain=amazon.com

15
go.mod
View File

@ -1,6 +1,6 @@
module github.com/nadoo/glider module github.com/nadoo/glider
go 1.12 go 1.13
require ( require (
github.com/Yawning/chacha20 v0.0.0-20170904085104-e3b1f968fc63 // indirect github.com/Yawning/chacha20 v0.0.0-20170904085104-e3b1f968fc63 // indirect
@ -9,8 +9,8 @@ require (
github.com/dgryski/go-idea v0.0.0-20170306091226-d2fb45a411fb // indirect github.com/dgryski/go-idea v0.0.0-20170306091226-d2fb45a411fb // indirect
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 // indirect github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 // indirect
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521 // indirect github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521 // indirect
github.com/klauspost/cpuid v1.2.0 // indirect github.com/klauspost/cpuid v1.2.1 // indirect
github.com/klauspost/reedsolomon v1.9.1 // indirect github.com/klauspost/reedsolomon v1.9.2 // indirect
github.com/nadoo/conflag v0.2.0 github.com/nadoo/conflag v0.2.0
github.com/nadoo/go-shadowsocks2 v0.1.0 github.com/nadoo/go-shadowsocks2 v0.1.0
github.com/pkg/errors v0.8.1 // indirect github.com/pkg/errors v0.8.1 // indirect
@ -18,10 +18,11 @@ require (
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect
github.com/tjfoc/gmsm v1.0.1 // indirect github.com/tjfoc/gmsm v1.0.1 // indirect
github.com/xtaci/kcp-go v5.0.7+incompatible github.com/xtaci/kcp-go v5.4.4+incompatible
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 // indirect golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect
) )
// Replace dependency modules with local developing copy // Replace dependency modules with local developing copy

29
go.sum
View File

@ -10,10 +10,10 @@ github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152 h1:ED31mPIxDJnrLt9W
github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152/go.mod h1:I9fhc/EvSg88cDxmfQ47v35Ssz9rlFunL/KY0A1JAYI= github.com/dgryski/go-rc2 v0.0.0-20150621095337-8a9021637152/go.mod h1:I9fhc/EvSg88cDxmfQ47v35Ssz9rlFunL/KY0A1JAYI=
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521 h1:fBHFH+Y/GPGFGo7LIrErQc3p2MeAhoIQNgaxPWYsSxk= github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521 h1:fBHFH+Y/GPGFGo7LIrErQc3p2MeAhoIQNgaxPWYsSxk=
github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:ucvhdsUCE3TH0LoLRb6ShHiJl8e39dGlx6A4g/ujlow= github.com/ebfe/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:ucvhdsUCE3TH0LoLRb6ShHiJl8e39dGlx6A4g/ujlow=
github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE= github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/reedsolomon v1.9.1 h1:kYrT1MlR4JH6PqOpC+okdb9CDTcwEC/BqpzK4WFyXL8= github.com/klauspost/reedsolomon v1.9.2 h1:E9CMS2Pqbv+C7tsrYad4YC9MfhnMVWhMRsTi7U0UB18=
github.com/klauspost/reedsolomon v1.9.1/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4= github.com/klauspost/reedsolomon v1.9.2/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ/pcNv7fu+8Un4=
github.com/nadoo/conflag v0.2.0 h1:xao13tYqfD+5bjQ1A/jT2kBL8tUcVpFhq3seuN5kpeM= github.com/nadoo/conflag v0.2.0 h1:xao13tYqfD+5bjQ1A/jT2kBL8tUcVpFhq3seuN5kpeM=
github.com/nadoo/conflag v0.2.0/go.mod h1:Ayl83klaw7fagwYaI6luTmbOi4psAf7FqJNRRv5YMvU= github.com/nadoo/conflag v0.2.0/go.mod h1:Ayl83klaw7fagwYaI6luTmbOi4psAf7FqJNRRv5YMvU=
github.com/nadoo/go-shadowsocks2 v0.1.0 h1:NkdUrZrI8uYq8R0YDmHLttLqKt0Z9i7dUKtGvBqZQl8= github.com/nadoo/go-shadowsocks2 v0.1.0 h1:NkdUrZrI8uYq8R0YDmHLttLqKt0Z9i7dUKtGvBqZQl8=
@ -28,15 +28,20 @@ github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b h1:mnG1fcsIB1d/3vbkB
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4= github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/tjfoc/gmsm v1.0.1 h1:R11HlqhXkDospckjZEihx9SW/2VW0RgdwrykyWMFOQU= github.com/tjfoc/gmsm v1.0.1 h1:R11HlqhXkDospckjZEihx9SW/2VW0RgdwrykyWMFOQU=
github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc= github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc=
github.com/xtaci/kcp-go v5.0.7+incompatible h1:zs9tc8XRID0m+aetu3qPWZFyRt2UIMqbXIBgw+vcnlE= github.com/xtaci/kcp-go v5.4.4+incompatible h1:QIJ0a0Q0N1G20yLHL2+fpdzyy2v/Cb3PI+xiwx/KK9c=
github.com/xtaci/kcp-go v5.0.7+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE= github.com/xtaci/kcp-go v5.4.4+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae h1:J0GxkO96kL4WF+AIT3M4mfUVinOCPgf2uUWYFUzN0sM=
github.com/xtaci/lossyconn v0.0.0-20190602105132-8df528c0c9ae/go.mod h1:gXtu8J62kEgmN++bm9BVICuT/e8yiLI2KFobd/TRFsE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 h1:aUX/1G2gFSs4AsJJg2cL3HuoRhCSCz733FE5GUSuaT4= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 h1:kcXqo9vE6fsZY5X5Rd7R1l7fTgnWaDCVmln65REefiE= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 h1:xe1/2UUJRmA9iDglQSlkx8c5n3twv58+K0mPpC2zmhA= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View File

@ -30,7 +30,7 @@ import (
) )
// VERSION . // VERSION .
const VERSION = "0.7.0" const VERSION = "0.7.1"
func main() { func main() {
// TODO: remove this line when Go1.13 is released. // TODO: remove this line when Go1.13 is released.

View File

@ -43,17 +43,17 @@ func (p *HTTPObfs) NewConn(c net.Conn) (net.Conn, error) {
func (c *HTTPObfsConn) writeHeader() (int, error) { func (c *HTTPObfsConn) writeHeader() (int, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.Write([]byte("GET " + c.obfsURI + " HTTP/1.1\r\n")) buf.WriteString("GET " + c.obfsURI + " HTTP/1.1\r\n")
buf.Write([]byte("Host: " + c.obfsHost + "\r\n")) buf.WriteString("Host: " + c.obfsHost + "\r\n")
buf.Write([]byte("User-Agent: " + c.obfsUA + "\r\n")) buf.WriteString("User-Agent: " + c.obfsUA + "\r\n")
buf.Write([]byte("Upgrade: websocket\r\n")) buf.WriteString("Upgrade: websocket\r\n")
buf.Write([]byte("Connection: Upgrade\r\n")) buf.WriteString("Connection: Upgrade\r\n")
p := make([]byte, 16) p := make([]byte, 16)
rand.Read(p) 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()) return c.Conn.Write(buf.Bytes())
} }

View File

@ -30,7 +30,7 @@ func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHea
if ctrlConn != nil { if ctrlConn != nil {
go func() { go func() {
buf := []byte{} buf := make([]byte, 1)
for { for {
_, err := ctrlConn.Read(buf) _, err := ctrlConn.Read(buf)
if err, ok := err.(net.Error); ok && err.Timeout() { if err, ok := err.(net.Error); ok && err.Timeout() {

View File

@ -9,6 +9,7 @@
// socks5 server: // socks5 server:
// https://github.com/shadowsocks/go-shadowsocks2/tree/master/socks // https://github.com/shadowsocks/go-shadowsocks2/tree/master/socks
// Package socks5 implements a socks5 proxy.
package socks5 package socks5
import ( import (
@ -114,7 +115,7 @@ func (s *SOCKS5) Serve(c net.Conn) {
if err != nil { if err != nil {
// UDP: keep the connection until disconnect then free the UDP socket // UDP: keep the connection until disconnect then free the UDP socket
if err == socks.Errors[9] { if err == socks.Errors[9] {
buf := []byte{} buf := make([]byte, 1)
// block here // block here
for { for {
_, err := c.Read(buf) _, err := c.Read(buf)

View File

@ -15,20 +15,20 @@ import (
var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") var keyGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
// Client ws client // Client is ws client struct.
type Client struct { type Client struct {
host string host string
path string path string
} }
// Conn is a connection to ws server // Conn is a connection to ws server.
type Conn struct { type Conn struct {
net.Conn net.Conn
reader io.Reader reader io.Reader
writer io.Writer writer io.Writer
} }
// NewClient . // NewClient creates a new ws client.
func NewClient(host, path string) (*Client, error) { func NewClient(host, path string) (*Client, error) {
if path == "" { if path == "" {
path = "/" path = "/"
@ -37,33 +37,38 @@ func NewClient(host, path string) (*Client, error) {
return c, nil return c, nil
} }
// NewConn . // NewConn creates a new ws client connection.
func (c *Client) NewConn(rc net.Conn, target string) (*Conn, error) { func (c *Client) NewConn(rc net.Conn, target string) (*Conn, error) {
conn := &Conn{Conn: rc} conn := &Conn{Conn: rc}
return conn, conn.Handshake(c.host, c.path) 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 { func (c *Conn) Handshake(host, path string) error {
clientKey := generateClientKey() clientKey := generateClientKey()
var buf bytes.Buffer var buf bytes.Buffer
buf.Write([]byte("GET " + path + " HTTP/1.1\r\n")) buf.WriteString("GET " + path + " HTTP/1.1\r\n")
buf.Write([]byte("Host: " + host + "\r\n")) buf.WriteString("Host: " + host + "\r\n")
buf.Write([]byte("Upgrade: websocket\r\n")) buf.WriteString("Upgrade: websocket\r\n")
buf.Write([]byte("Connection: Upgrade\r\n")) buf.WriteString("Connection: Upgrade\r\n")
buf.Write([]byte("Origin: http://" + host + "\r\n")) buf.WriteString("Origin: http://" + host + "\r\n")
buf.Write([]byte("Sec-WebSocket-Key: " + clientKey + "\r\n")) buf.WriteString("Sec-WebSocket-Key: " + clientKey + "\r\n")
buf.Write([]byte("Sec-WebSocket-Protocol: binary\r\n")) buf.WriteString("Sec-WebSocket-Protocol: binary\r\n")
buf.Write([]byte("Sec-WebSocket-Version: 13\r\n")) buf.WriteString("Sec-WebSocket-Version: 13\r\n")
buf.Write([]byte("\r\n")) buf.WriteString(("\r\n"))
if _, err := c.Conn.Write(buf.Bytes()); err != nil { if _, err := c.Conn.Write(buf.Bytes()); err != nil {
return err return err
} }
tpr := textproto.NewReader(bufio.NewReader(c.Conn)) 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" { if !ok || code != "101" {
return errors.New("[ws] error in ws handshake parseFirstLine") 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. // 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) // 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) { func parseFirstLine(line string) (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
}
s1 := strings.Index(line, " ") s1 := strings.Index(line, " ")
s2 := strings.Index(line[s1+1:], " ") s2 := strings.Index(line[s1+1:], " ")
if s1 < 0 || s2 < 0 { if s1 < 0 || s2 < 0 {

View File

@ -45,7 +45,7 @@ type frameWriter struct {
maskKey []byte maskKey []byte
} }
// FrameWriter returns a frame writer // FrameWriter returns a frame writer.
func FrameWriter(w io.Writer) io.Writer { func FrameWriter(w io.Writer) io.Writer {
n := rand.Uint32() n := rand.Uint32()
return &frameWriter{ return &frameWriter{
@ -130,7 +130,7 @@ type frameReader struct {
leftBytes int64 leftBytes int64
} }
// FrameReader returns a chunked reader // FrameReader returns a chunked reader.
func FrameReader(r io.Reader) io.Reader { func FrameReader(r io.Reader) io.Reader {
return &frameReader{ return &frameReader{
Reader: r, Reader: r,

View File

@ -1,3 +1,4 @@
// Package ws implements a simple websocket client.
package ws package ws
import ( import (
@ -10,7 +11,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// WS . // WS is the base ws proxy struct.
type WS struct { type WS struct {
dialer proxy.Dialer dialer proxy.Dialer
addr string addr string
@ -63,7 +64,7 @@ func NewWSDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
return NewWS(s, dialer) return NewWS(s, dialer)
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *WS) Addr() string { func (s *WS) Addr() string {
if s.addr == "" { if s.addr == "" {
return s.dialer.Addr() return s.dialer.Addr()
@ -71,7 +72,7 @@ func (s *WS) Addr() string {
return s.addr 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) } 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. // Dial connects to the address addr on the network net via the proxy.