mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 01:15:41 +08:00
general: optimize codes
This commit is contained in:
parent
3c509f8b7a
commit
8f661a67a4
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -10,7 +10,7 @@ jobs:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.14.x
|
||||
- name: Test
|
||||
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -14,7 +14,7 @@ jobs:
|
||||
- name: Unshallow
|
||||
run: git fetch --prune --unshallow
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.14.x
|
||||
- name: Run GoReleaser
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -30,3 +30,4 @@ config/rules.d/*.list
|
||||
glider
|
||||
/bak/
|
||||
/rules.d/
|
||||
*/dev/*
|
@ -7,7 +7,8 @@ import (
|
||||
|
||||
const (
|
||||
// number of pools.
|
||||
num = 17 // pool sizes: 1<<0 ~ 1<<16 bytes, (1Byte~64KBytes).
|
||||
// pool sizes: [1<<0 ~ 1<<(num-1)] bytes, [1B~64KB].
|
||||
num = 17
|
||||
maxsize = 1 << (num - 1)
|
||||
)
|
||||
|
||||
@ -26,9 +27,10 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// GetBuffer gets a buffer from pool, size should in range: [1, 65536], otherwise, this function will call make([]byte, size) directly.
|
||||
// GetBuffer gets a buffer from pool, size should in range: [1, 65536],
|
||||
// otherwise, this function will call make([]byte, size) directly.
|
||||
func GetBuffer(size int) []byte {
|
||||
if size >= 1 && size < maxsize {
|
||||
if size >= 1 && size <= maxsize {
|
||||
i := bits.Len32(uint32(size)) - 1
|
||||
if sizes[i] < size {
|
||||
i += 1
|
||||
|
2
dev.go
2
dev.go
@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
|
||||
_ "github.com/nadoo/glider/proxy/dev/rtcp"
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -221,7 +221,7 @@ func (c *Client) exchangeUDP(rc net.Conn, reqBytes []byte) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
respBytes := make([]byte, 2+UDPMaxLen)
|
||||
respBytes := make([]byte, UDPMaxLen)
|
||||
n, err := rc.Read(respBytes[2:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
// Messages carried by UDP are restricted to 512 bytes (not counting the IP
|
||||
// or UDP headers). Longer messages are truncated and the TC bit is set in
|
||||
// the header.
|
||||
const UDPMaxLen = 510
|
||||
const UDPMaxLen = 512
|
||||
|
||||
// HeaderLen is the length of dns msg header.
|
||||
const HeaderLen = 12
|
||||
|
@ -60,7 +60,7 @@ func (s *Server) ListenAndServeUDP(wg *sync.WaitGroup) {
|
||||
log.F("[dns] listening UDP on %s", s.addr)
|
||||
|
||||
for {
|
||||
reqBytes := pool.GetBuffer(2 + UDPMaxLen)
|
||||
reqBytes := pool.GetBuffer(UDPMaxLen)
|
||||
|
||||
n, caddr, err := c.ReadFrom(reqBytes[2:])
|
||||
if err != nil {
|
||||
@ -76,7 +76,7 @@ func (s *Server) ListenAndServeUDP(wg *sync.WaitGroup) {
|
||||
binary.BigEndian.PutUint16(reqBytes[:2], reqLen)
|
||||
|
||||
go func() {
|
||||
respBytes, err := s.Client.Exchange(reqBytes[:2+n], caddr.String(), false)
|
||||
respBytes, err := s.Exchange(reqBytes[:2+n], caddr.String(), false)
|
||||
defer pool.PutBuffer(reqBytes)
|
||||
|
||||
if err != nil {
|
||||
|
14
go.mod
14
go.mod
@ -3,21 +3,13 @@ module github.com/nadoo/glider
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/klauspost/cpuid v1.3.1 // indirect
|
||||
github.com/klauspost/reedsolomon v1.9.9 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/mmcloughlin/avo v0.0.0-20200803215136-443f81d77104 // indirect
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200722151714-4f4abd8a2d94
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200809233203-ce9fb439e579
|
||||
github.com/nadoo/conflag v0.2.3
|
||||
github.com/nadoo/go-shadowsocks2 v0.1.2
|
||||
github.com/templexxx/cpu v0.0.7 // indirect
|
||||
github.com/tjfoc/gmsm v1.3.2 // indirect
|
||||
github.com/xtaci/kcp-go/v5 v5.5.14
|
||||
github.com/xtaci/kcp-go/v5 v5.5.15
|
||||
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
|
||||
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 // indirect
|
||||
golang.org/x/tools v0.0.0-20200806234136-990129eca547 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
golang.org/x/tools v0.0.0-20200809012840-6f4f008689da // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
)
|
||||
|
||||
|
17
go.sum
17
go.sum
@ -30,8 +30,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mmcloughlin/avo v0.0.0-20200803215136-443f81d77104 h1:ULR/QWMgcgRiZLUjSSJMU+fW+RDMstRdmnDWj9Q+AsA=
|
||||
github.com/mmcloughlin/avo v0.0.0-20200803215136-443f81d77104/go.mod h1:wqKykBG2QzQDJEzvRkcS8x6MiSJkF52hXZsXcjaB3ls=
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200126130347-721f53a7b15a/go.mod h1:1SJEvxD2Y+N7SK2NpCC4wSatvfGGTUo2rhPdthUFsCU=
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200722151714-4f4abd8a2d94 h1:pIyGakpliJhz8sFTsaSx9j9SuRmqYMp3wUgWttyDZ5k=
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200722151714-4f4abd8a2d94/go.mod h1:5A4hA1y7oP4SoAqcc7gZvjFF63KpKpI5aCUIMrXV1UI=
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200809233203-ce9fb439e579 h1:vz7wDwSpkurhD36VzgObl/t0IZR6PRGQsh20zcE68dA=
|
||||
github.com/mzz2017/shadowsocksR v0.0.0-20200809233203-ce9fb439e579/go.mod h1:5A4hA1y7oP4SoAqcc7gZvjFF63KpKpI5aCUIMrXV1UI=
|
||||
github.com/nadoo/conflag v0.2.2 h1:xywuyaevdBnA3+4g9S11ng+Nby725WN1LXargWnAXpM=
|
||||
github.com/nadoo/conflag v0.2.2/go.mod h1:dzFfDUpXdr2uS2oV+udpy5N2vfNOu/bFzjhX1WI52co=
|
||||
github.com/nadoo/conflag v0.2.3 h1:/+rTaN0bHTIiQbPl1WZK78JRoqjlNqJ9Zf05ep0o5jI=
|
||||
@ -66,8 +66,8 @@ github.com/xtaci/kcp-go v5.4.11+incompatible h1:tJbtarpmOoOD74cZ41uvvF5Hyt1nvctH
|
||||
github.com/xtaci/kcp-go v5.4.11+incompatible/go.mod h1:bN6vIwHQbfHaHtFpEssmWsN45a+AZwO7eyRCmEIbtvE=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.12 h1:iALGyvti/oBbl1TbVoUpHEUHCorDEb3tEKl1CPY3KXM=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.12/go.mod h1:H0T/EJ+lPNytnFYsKLH0JHUtiwZjG3KXlTM6c+Q4YUo=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.14 h1:YODIwvTyZmOTj3SduJeIcQPxthDoHllMm8YIBlK44Ik=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.14/go.mod h1:H0T/EJ+lPNytnFYsKLH0JHUtiwZjG3KXlTM6c+Q4YUo=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.15 h1:I/T1Mf1xWYJd7jjHlCcP+FsBxuPJJ3VPP/vfTgUX8lk=
|
||||
github.com/xtaci/kcp-go/v5 v5.5.15/go.mod h1:pVx3jb4LT5edTmPayc77tIU9nRsjGck8wep5ZV/RBO0=
|
||||
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=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@ -111,14 +111,15 @@ golang.org/x/sys v0.0.0-20191020212454-3e7259c5e7c2/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82 h1:6cBnXxYO+CiRVrChvCosSv7magqTPbyAgz1M8iOv5wM=
|
||||
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 h1:yi1hN8dcqI9l8klZfy4B8mJvFmmAxJEePIQQFNSd7Cs=
|
||||
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c h1:iHhCR0b26amDCiiO+kBguKZom9aMF+NrFxh9zeKR/XU=
|
||||
golang.org/x/tools v0.0.0-20200425043458-8463f397d07c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20200806234136-990129eca547 h1:HjPd370wmFEFsBBdDEikoje9tDY3OHE7UB2erLJBmss=
|
||||
golang.org/x/tools v0.0.0-20200806234136-990129eca547/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200808161706-5bf02b21f123/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200809012840-6f4f008689da h1:ml5G98G4/tdKT1XNq+ky5iSRdKKux0TANlLAzmXT/hg=
|
||||
golang.org/x/tools v0.0.0-20200809012840-6f4f008689da/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
|
@ -40,7 +40,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
||||
|
||||
if s.user != "" && s.password != "" {
|
||||
auth := s.user + ":" + s.password
|
||||
buf.Write([]byte("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n"))
|
||||
buf.WriteString("Proxy-Authorization: Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + "\r\n")
|
||||
}
|
||||
|
||||
// header ended
|
||||
|
@ -82,16 +82,16 @@ func cleanHeaders(header textproto.MIMEHeader) {
|
||||
}
|
||||
|
||||
func writeStartLine(w io.Writer, s1, s2, s3 string) {
|
||||
w.Write([]byte(s1 + " " + s2 + " " + s3 + "\r\n"))
|
||||
io.WriteString(w, s1+" "+s2+" "+s3+"\r\n")
|
||||
}
|
||||
|
||||
func writeHeaders(w io.Writer, header textproto.MIMEHeader) {
|
||||
for key, values := range header {
|
||||
for _, v := range values {
|
||||
w.Write([]byte(key + ": " + v + "\r\n"))
|
||||
io.WriteString(w, key+": "+v+"\r\n")
|
||||
}
|
||||
}
|
||||
w.Write([]byte("\r\n"))
|
||||
io.WriteString(w, "\r\n")
|
||||
}
|
||||
|
||||
func extractUserPass(auth string) (username, password string, ok bool) {
|
||||
|
@ -75,7 +75,7 @@ func (s *HTTP) servRequest(req *request, c *conn.Conn) {
|
||||
// Auth
|
||||
if s.user != "" && s.password != "" {
|
||||
if user, pass, ok := extractUserPass(req.auth); !ok || user != s.user || pass != s.password {
|
||||
c.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n"))
|
||||
io.WriteString(c, "HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n")
|
||||
log.F("[http] auth failed from %s, auth info: %s:%s", c.RemoteAddr(), user, pass)
|
||||
return
|
||||
}
|
||||
@ -92,13 +92,13 @@ func (s *HTTP) servRequest(req *request, c *conn.Conn) {
|
||||
func (s *HTTP) servHTTPS(r *request, c net.Conn) {
|
||||
rc, dialer, err := s.proxy.Dial("tcp", r.uri)
|
||||
if err != nil {
|
||||
c.Write([]byte(r.proto + " 502 ERROR\r\n\r\n"))
|
||||
io.WriteString(c, r.proto+" 502 ERROR\r\n\r\n")
|
||||
log.F("[http] %s <-> %s [c] via %s, error in dial: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
|
||||
io.WriteString(c, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||||
|
||||
log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), r.uri, dialer.Addr())
|
||||
|
||||
|
@ -238,7 +238,7 @@ func extension(b []byte, server string) *bytes.Buffer {
|
||||
binary.Write(&buf, binary.BigEndian, uint16(len(server)+3)) // Server Name list length
|
||||
buf.WriteByte(0x00) // Server Name Type: host_name (0)
|
||||
binary.Write(&buf, binary.BigEndian, uint16(len(server))) // Server Name length
|
||||
buf.Write([]byte(server))
|
||||
buf.WriteString(server)
|
||||
|
||||
// https://github.com/shadowsocks/simple-obfs/blob/7659eeccf473aa41eb294e92c32f8f60a8747325/src/obfs_tls.c#L88
|
||||
// Extension: ec_point_formats (len=4)
|
||||
|
@ -260,7 +260,7 @@ func checkWebSite(fwdr *Forwarder, website string, timeout time.Duration, buf []
|
||||
rc.SetDeadline(time.Now().Add(timeout))
|
||||
}
|
||||
|
||||
_, err = rc.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
|
||||
_, err = io.WriteString(rc, "GET / HTTP/1.0\r\n\r\n")
|
||||
if err != nil {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. error in write: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
|
Loading…
Reference in New Issue
Block a user