Add more lint

This commit is contained in:
世界 2022-06-29 12:26:13 +08:00
parent beeeba3388
commit 849d16c37b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
13 changed files with 118 additions and 64 deletions

View file

@ -2,5 +2,43 @@ run:
timeout: 5m
linters:
enable-all: true
disable:
- errcheck
- errcheck
- wrapcheck
- varnamelen
- stylecheck
- nonamedreturns
- nlreturn
- ireturn
- gomnd
- exhaustivestruct
- ifshort
- goerr113
- gochecknoglobals
- forcetypeassert
- exhaustruct
- exhaustive
- cyclop
- containedctx
- wsl
- nestif
- lll
- funlen
- goconst
- godot
- gocognit
- golint
- goimports
linters-settings:
revive:
rules:
- name: var-naming
disabled: true
govet:
enable-all: true
disable:
- composites
- fieldalignment
- shadow

View file

@ -10,29 +10,40 @@ func Put(buf []byte) error {
func Make(size int) []byte {
var buffer []byte
if size <= 16 {
switch {
case size <= 2:
buffer = make([]byte, 2)
case size <= 4:
buffer = make([]byte, 4)
case size <= 8:
buffer = make([]byte, 8)
case size <= 16:
buffer = make([]byte, 16)
} else if size <= 32 {
case size <= 32:
buffer = make([]byte, 32)
} else if size <= 64 {
case size <= 64:
buffer = make([]byte, 64)
} else if size <= 128 {
case size <= 128:
buffer = make([]byte, 128)
} else if size <= 256 {
case size <= 256:
buffer = make([]byte, 256)
} else if size <= 512 {
case size <= 512:
buffer = make([]byte, 512)
} else if size <= 1024 {
case size <= 1024:
buffer = make([]byte, 1024)
} else if size <= 4*1024 {
buffer = make([]byte, 4*1024)
} else if size <= 16*1024 {
buffer = make([]byte, 16*1024)
} else if size <= 20*1024 {
buffer = make([]byte, 20*1024)
} else if size <= 65535 {
case size <= 2048:
buffer = make([]byte, 2048)
case size <= 4096:
buffer = make([]byte, 4096)
case size <= 8192:
buffer = make([]byte, 8192)
case size <= 16384:
buffer = make([]byte, 16384)
case size <= 32768:
buffer = make([]byte, 32768)
case size <= 65535:
buffer = make([]byte, 65535)
} else {
default:
return make([]byte, size)
}
return buffer[:size]

View file

@ -17,6 +17,7 @@ var dbgvars any
// go.info.runtime.dbgvars: relocation target go.info.[]github.com/sagernet/sing/common/buf.dbgVar not defined
// var dbgvars []dbgVar
//nolint:gochecknoinits
func init() {
debugVars := *(*[]dbgVar)(unsafe.Pointer(&dbgvars))
for _, v := range debugVars {

View file

@ -191,7 +191,7 @@ func (c *LruCache[K, V]) StoreWithExpire(key K, value V, expires time.Time) {
c.cache[key] = c.lru.PushBack(e)
if c.maxSize > 0 {
if len := c.lru.Len(); len > c.maxSize {
if n := c.lru.Len(); n > c.maxSize {
c.deleteElement(c.lru.Front())
}
}

View file

@ -140,7 +140,7 @@ func Must1(_ any, err error) {
}
}
func Must2(_ any, _ any, err error) {
func Must2(_, _ any, err error) {
if err != nil {
panic(err)
}
@ -168,8 +168,6 @@ func Close(closers ...any) error {
retErr = err
}
continue
}
switch c := closer.(type) {
case WithUpstream:
err := Close(c.Upstream())
if err != nil {

View file

@ -1,3 +1,4 @@
//nolint:errorlint
package exceptions
import (

View file

@ -5,10 +5,10 @@
// Package list implements a doubly linked list.
//
// To iterate over a list (where l is a *List[T]):
//
// for e := l.Front(); e != nil; e = e.Next() {
// // do something with e.Value
// }
//
package list
// Element is an element of a linked list.

View file

@ -1,6 +1,7 @@
package sing
package shadowsocks
//go:generate go install -v mvdan.cc/gofumpt@latest
//go:generate go install -v github.com/daixiang0/gci@latest
//go:generate gofumpt -l -w .
//go:generate gofmt -s -w .
//go:generate gci write .

10
protocol/http/link.go Normal file
View file

@ -0,0 +1,10 @@
package http
import (
"bufio"
"net/http"
_ "unsafe" // for linkname
)
//go:linkname ReadRequest net/http.readRequest
func ReadRequest(b *bufio.Reader) (req *http.Request, err error)

View file

@ -1,14 +1,12 @@
package http
import (
"bufio"
"context"
"encoding/base64"
"net"
"net/http"
"strings"
"time"
_ "unsafe"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
@ -131,9 +129,6 @@ func HandleRequest(ctx context.Context, request *http.Request, conn net.Conn, au
}
}
//go:linkname ReadRequest net/http.readRequest
func ReadRequest(b *bufio.Reader) (req *http.Request, err error)
func removeHopByHopHeaders(header http.Header) {
// Strip hop-by-hop header based on RFC:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1

View file

@ -10,11 +10,11 @@ import (
N "github.com/sagernet/sing/common/network"
)
//+----+------+------+----------+----------+----------+
//|RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
//+----+------+------+----------+----------+----------+
//| 2 | 1 | 1 | Variable | 2 | Variable |
//+----+------+------+----------+----------+----------+
// +----+------+------+----------+----------+----------+
// |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
// +----+------+------+----------+----------+----------+
// | 2 | 1 | 1 | Variable | 2 | Variable |
// +----+------+------+----------+----------+----------+
type AssociatePacketConn struct {
N.PacketConn

View file

@ -35,11 +35,11 @@ const (
ReplyCodeAddressTypeUnsupported byte = 8
)
//+----+----------+----------+
//|VER | NMETHODS | METHODS |
//+----+----------+----------+
//| 1 | 1 | 1 to 255 |
//+----+----------+----------+
// +----+----------+----------+
// |VER | NMETHODS | METHODS |
// +----+----------+----------+
// | 1 | 1 | 1 to 255 |
// +----+----------+----------+
type AuthRequest struct {
Methods []byte
@ -78,11 +78,11 @@ func ReadAuthRequest0(reader io.Reader) (request AuthRequest, err error) {
return
}
//+----+--------+
//|VER | METHOD |
//+----+--------+
//| 1 | 1 |
//+----+--------+
// +----+--------+
// |VER | METHOD |
// +----+--------+
// | 1 | 1 |
// +----+--------+
type AuthResponse struct {
Method byte
@ -105,11 +105,11 @@ func ReadAuthResponse(reader io.Reader) (response AuthResponse, err error) {
return
}
//+----+------+----------+------+----------+
//|VER | ULEN | UNAME | PLEN | PASSWD |
//+----+------+----------+------+----------+
//| 1 | 1 | 1 to 255 | 1 | 1 to 255 |
//+----+------+----------+------+----------+
// +----+------+----------+------+----------+
// |VER | ULEN | UNAME | PLEN | PASSWD |
// +----+------+----------+------+----------+
// | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
// +----+------+----------+------+----------+
type UsernamePasswordAuthRequest struct {
Username string
@ -148,11 +148,11 @@ func ReadUsernamePasswordAuthRequest(reader io.Reader) (request UsernamePassword
return
}
//+----+--------+
//|VER | STATUS |
//+----+--------+
//| 1 | 1 |
//+----+--------+
// +----+--------+
// |VER | STATUS |
// +----+--------+
// | 1 | 1 |
// +----+--------+
type UsernamePasswordAuthResponse struct {
Status byte
@ -179,11 +179,11 @@ func ReadUsernamePasswordAuthResponse(reader io.Reader) (response UsernamePasswo
return
}
//+----+-----+-------+------+----------+----------+
//|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
//+----+-----+-------+------+----------+----------+
//| 1 | 1 | X'00' | 1 | Variable | 2 |
//+----+-----+-------+------+----------+----------+
// +----+-----+-------+------+----------+----------+
// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
type Request struct {
Command byte
@ -227,11 +227,11 @@ func ReadRequest(reader io.Reader) (request Request, err error) {
return
}
//+----+-----+-------+------+----------+----------+
//|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
//+----+-----+-------+------+----------+----------+
//| 1 | 1 | X'00' | 1 | Variable | 2 |
//+----+-----+-------+------+----------+----------+
// +----+-----+-------+------+----------+----------+
// |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
type Response struct {
ReplyCode byte

View file

@ -59,9 +59,8 @@ func (s *Service[K]) RemoveUser(user K) bool {
delete(s.users, user)
delete(s.keys, key)
return true
} else {
return false
}
return false
}
func (s *Service[K]) ResetUsers() {