diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fae1338..34e4b6c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,6 +11,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + - name: Unshallow + run: git fetch --prune --unshallow - name: Set up Go uses: actions/setup-go@v1 with: diff --git a/.goreleaser.yml b/.goreleaser.yml index 8d853a1..ec8ed08 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -21,7 +21,7 @@ before: builds: - id: default env: - - CGO_ENABLED=0 + - CGO_ENABLED=0 goos: - windows - linux diff --git a/go.mod b/go.mod index 09775a7..69d58f3 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/xtaci/kcp-go v5.4.20+incompatible golang.org/x/crypto v0.0.0-20200403201458-baeed622b8d8 golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect - golang.org/x/sys v0.0.0-20200331124033-c3d80250170d // indirect + golang.org/x/sys v0.0.0-20200406113430-c6e801f48ba2 // indirect ) // Replace dependency modules with local developing copy diff --git a/go.sum b/go.sum index c11d598..ba23a95 100644 --- a/go.sum +++ b/go.sum @@ -73,8 +73,8 @@ golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/nt golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191020212454-3e7259c5e7c2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200331124033-c3d80250170d h1:nc5K6ox/4lTFbMVSL9WRR81ixkcwXThoiF6yf+R9scA= -golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200406113430-c6e801f48ba2 h1:Z9pPywZscwuw0ijrLEbTzW9lppFgBY4HDgbvoDnreQs= +golang.org/x/sys v0.0.0-20200406113430-c6e801f48ba2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/ipset/ipset_linux.go b/ipset/ipset_linux.go index eb995e7..01ce51c 100644 --- a/ipset/ipset_linux.go +++ b/ipset/ipset_linux.go @@ -89,7 +89,8 @@ func NewManager(rules []*rule.Config) (*Manager, error) { Family: syscall.AF_NETLINK, } - if err = syscall.Bind(fd, &lsa); err != nil { + err = syscall.Bind(fd, &lsa) + if err != nil { log.F("%s", err) return nil, err } @@ -123,16 +124,18 @@ func NewManager(rules []*rule.Config) (*Manager, error) { // AddDomainIP implements the DNSAnswerHandler function, used to update ipset according to domainSet rule func (m *Manager) AddDomainIP(domain, ip string) error { - if ip != "" { - domainParts := strings.Split(domain, ".") - length := len(domainParts) - for i := length - 1; i >= 0; i-- { - domain := strings.Join(domainParts[i:length], ".") + if domain == "" || ip == "" { + return errors.New("please specify the domain and ip address") + } - // find in domainMap - if ipset, ok := m.domainSet.Load(domain); ok { - AddToSet(m.fd, m.lsa, ipset.(string), ip) - } + domainParts := strings.Split(domain, ".") + length := len(domainParts) + for i := length - 1; i >= 0; i-- { + domain := strings.Join(domainParts[i:length], ".") + + // find in domainMap + if ipset, ok := m.domainSet.Load(domain); ok { + AddToSet(m.fd, m.lsa, ipset.(string), ip) } } diff --git a/proxy/obfs/http.go b/proxy/obfs/http.go index 3942824..fd4c48b 100644 --- a/proxy/obfs/http.go +++ b/proxy/obfs/http.go @@ -42,7 +42,7 @@ func (p *HTTPObfs) NewConn(c net.Conn) (net.Conn, error) { } func (c *HTTPObfsConn) writeHeader() (int, error) { - buf := new(bytes.Buffer) + var buf bytes.Buffer 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") diff --git a/proxy/obfs/tls.go b/proxy/obfs/tls.go index ffb070a..7bab773 100644 --- a/proxy/obfs/tls.go +++ b/proxy/obfs/tls.go @@ -69,9 +69,9 @@ func (c *TLSObfsConn) Write(b []byte) (int, error) { end = n } - buf := new(bytes.Buffer) + var buf bytes.Buffer 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()) @@ -124,7 +124,7 @@ func (c *TLSObfsConn) Read(b []byte) (int, error) { } func (c *TLSObfsConn) handshake(b []byte) (int, error) { - buf := new(bytes.Buffer) + var buf bytes.Buffer // prepare extension & clientHello content bufExt, bufHello := extension(b, c.obfsHost), clientHello() @@ -142,7 +142,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,7 +156,7 @@ 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()) @@ -170,7 +170,7 @@ func (c *TLSObfsConn) handshake(b []byte) (int, error) { } func clientHello() *bytes.Buffer { - buf := new(bytes.Buffer) + var buf bytes.Buffer // Version: TLS 1.2 (0x0303) buf.Write([]byte{0x03, 0x03}) @@ -182,7 +182,7 @@ func clientHello() *bytes.Buffer { // clients do not send current time, and server do not check it, // golang tls client and chrome browser send random bytes instead. // - binary.Write(buf, binary.BigEndian, uint32(time.Now().Unix())) + binary.Write(&buf, binary.BigEndian, uint32(time.Now().Unix())) random := make([]byte, 28) // The above 2 lines of codes was added to make it compatible with some server implementation, // if we don't need the compatibility, just use the following code instead. @@ -200,7 +200,7 @@ func clientHello() *bytes.Buffer { // https://github.com/shadowsocks/simple-obfs/blob/7659eeccf473aa41eb294e92c32f8f60a8747325/src/obfs_tls.c#L57 // Cipher Suites Length: 56 - binary.Write(buf, binary.BigEndian, uint16(56)) + binary.Write(&buf, binary.BigEndian, uint16(56)) // Cipher Suites (28 suites) buf.Write([]byte{ 0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f, @@ -214,55 +214,55 @@ func clientHello() *bytes.Buffer { // Compression Methods (1 method) buf.WriteByte(0x00) - return buf + return &buf } func extension(b []byte, server string) *bytes.Buffer { - buf := new(bytes.Buffer) + var buf bytes.Buffer // Extension: SessionTicket TLS buf.Write([]byte{0x00, 0x23}) // type // NOTE: send some data in sessionticket, the server will treat it as data too - binary.Write(buf, binary.BigEndian, uint16(len(b))) // length + binary.Write(&buf, binary.BigEndian, uint16(len(b))) // length buf.Write(b) // Extension: server_name - buf.Write([]byte{0x00, 0x00}) // type - binary.Write(buf, binary.BigEndian, uint16(len(server)+5)) // length - 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{0x00, 0x00}) // type + binary.Write(&buf, binary.BigEndian, uint16(len(server)+5)) // length + 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)) // https://github.com/shadowsocks/simple-obfs/blob/7659eeccf473aa41eb294e92c32f8f60a8747325/src/obfs_tls.c#L88 // Extension: ec_point_formats (len=4) - buf.Write([]byte{0x00, 0x0b}) // type - binary.Write(buf, binary.BigEndian, uint16(4)) // length - buf.WriteByte(0x03) // format length + buf.Write([]byte{0x00, 0x0b}) // type + binary.Write(&buf, binary.BigEndian, uint16(4)) // length + buf.WriteByte(0x03) // format length buf.Write([]byte{0x01, 0x00, 0x02}) // Extension: supported_groups (len=10) - buf.Write([]byte{0x00, 0x0a}) // type - binary.Write(buf, binary.BigEndian, uint16(10)) // length - binary.Write(buf, binary.BigEndian, uint16(8)) // Supported Groups List Length: 8 + buf.Write([]byte{0x00, 0x0a}) // type + binary.Write(&buf, binary.BigEndian, uint16(10)) // length + binary.Write(&buf, binary.BigEndian, uint16(8)) // Supported Groups List Length: 8 buf.Write([]byte{0x00, 0x1d, 0x00, 0x17, 0x00, 0x19, 0x00, 0x18}) // Extension: signature_algorithms (len=32) - buf.Write([]byte{0x00, 0x0d}) // type - binary.Write(buf, binary.BigEndian, uint16(32)) // length - binary.Write(buf, binary.BigEndian, uint16(30)) // Signature Hash Algorithms Length: 30 + buf.Write([]byte{0x00, 0x0d}) // type + binary.Write(&buf, binary.BigEndian, uint16(32)) // length + binary.Write(&buf, binary.BigEndian, uint16(30)) // Signature Hash Algorithms Length: 30 buf.Write([]byte{ 0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05, 0x01, 0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03, }) // Extension: encrypt_then_mac (len=0) - buf.Write([]byte{0x00, 0x16}) // type - binary.Write(buf, binary.BigEndian, uint16(0)) // length + buf.Write([]byte{0x00, 0x16}) // type + binary.Write(&buf, binary.BigEndian, uint16(0)) // length // Extension: extended_master_secret (len=0) - buf.Write([]byte{0x00, 0x17}) // type - binary.Write(buf, binary.BigEndian, uint16(0)) // length + buf.Write([]byte{0x00, 0x17}) // type + binary.Write(&buf, binary.BigEndian, uint16(0)) // length - return buf + return &buf } diff --git a/proxy/vmess/client.go b/proxy/vmess/client.go index 0901a9b..8877bcd 100644 --- a/proxy/vmess/client.go +++ b/proxy/vmess/client.go @@ -39,7 +39,7 @@ const ( CmdUDP byte = 2 ) -// Client vmess client +// Client is a vmess client type Client struct { users []*User count int @@ -68,7 +68,7 @@ type Conn struct { dataWriter io.Writer } -// NewClient . +// NewClient returns a new vmess client. func NewClient(uuidStr, security string, alterID int) (*Client, error) { uuid, err := StrToUUID(uuidStr) if err != nil { @@ -105,7 +105,7 @@ func NewClient(uuidStr, security string, alterID int) (*Client, error) { return c, nil } -// NewConn . +// NewConn returns a new vmess conn. func (c *Client) NewConn(rc net.Conn, target string) (*Conn, error) { r := rand.Intn(c.count) conn := &Conn{user: c.users[r], opt: c.opt, security: c.security} @@ -158,9 +158,9 @@ func (c *Conn) EncodeAuthInfo() []byte { return h.Sum(nil) } -// EncodeRequest encodes requests to network bytes +// EncodeRequest encodes requests to network bytes. func (c *Conn) EncodeRequest() ([]byte, error) { - buf := new(bytes.Buffer) + var buf bytes.Buffer // Request buf.WriteByte(1) // Ver @@ -178,7 +178,7 @@ func (c *Conn) EncodeRequest() ([]byte, error) { buf.WriteByte(CmdTCP) // cmd // target - err := binary.Write(buf, binary.BigEndian, uint16(c.port)) // port + err := binary.Write(&buf, binary.BigEndian, uint16(c.port)) // port if err != nil { return nil, err } @@ -212,7 +212,7 @@ func (c *Conn) EncodeRequest() ([]byte, error) { return buf.Bytes(), nil } -// DecodeRespHeader . +// DecodeRespHeader decodes response header. func (c *Conn) DecodeRespHeader() error { block, err := aes.NewCipher(c.respBodyKey[:]) if err != nil { diff --git a/proxy/ws/ws.go b/proxy/ws/ws.go index 3c75151..e798659 100644 --- a/proxy/ws/ws.go +++ b/proxy/ws/ws.go @@ -39,8 +39,7 @@ func NewWS(s string, d proxy.Dialer) (*WS, error) { addr = d.Addr() } - query := u.Query() - host := query.Get("host") + host := u.Query().Get("host") if host == "" { colonPos := strings.LastIndex(addr, ":") if colonPos == -1 {