diff --git a/.travis.yml b/.travis.yml index c9ed246..8d76147 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ script: - make prepare - make all - make lint + - make critic - make test cache: diff --git a/Makefile b/Makefile index 2f4f1e5..04a7219 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,10 @@ test: vendor version.go lint: version.go @golangci-lint run +.PHONY: critic +critic: version.go + @gocritic check-project "$(ROOT_DIR)" + .PHONY: clean clean: @git clean -xfd && \ @@ -69,7 +73,7 @@ docker: @docker build --pull -t "$(IMAGE_NAME)" "$(ROOT_DIR)" .PHONY: prepare -prepare: install-dep install-lint +prepare: install-dep install-lint install-critic .PHONY: install-dep install-dep: @@ -79,3 +83,7 @@ install-dep: install-lint: @curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh \ | bash -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION) + +.PHONY: install-critic +install-critic: + @go get -u github.com/go-critic/go-critic/... diff --git a/config/config.go b/config/config.go index d595ee1..7c6c7f3 100644 --- a/config/config.go +++ b/config/config.go @@ -114,13 +114,10 @@ func getAddr(host fmt.Stringer, port uint16) string { // fetches data from external sources. Parameters passed to this // function, should come from command line arguments. func NewConfig(debug, verbose bool, // nolint: gocyclo - bindIP net.IP, bindPort uint16, - publicIPv4 net.IP, PublicIPv4Port uint16, - publicIPv6 net.IP, publicIPv6Port uint16, - statsIP net.IP, statsPort uint16, - secret, adtag string, - statsdIP string, statsdPort uint16, statsdNetwork string, statsdPrefix string, - statsdTagsFormat string, statsdTags map[string]string) (*Config, error) { + bindIP, publicIPv4, publicIPv6, statsIP net.IP, + bindPort, publicIPv4Port, publicIPv6Port, statsPort, statsdPort uint16, + secret, adtag, statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string, + statsdTags map[string]string) (*Config, error) { secureMode := false if strings.HasPrefix(secret, "dd") && len(secret) == 34 { secureMode = true @@ -149,8 +146,8 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String()) } } - if PublicIPv4Port == 0 { - PublicIPv4Port = bindPort + if publicIPv4Port == 0 { + publicIPv4Port = bindPort } if publicIPv6 == nil { @@ -175,7 +172,7 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo BindIP: bindIP, BindPort: bindPort, PublicIPv4: publicIPv4, - PublicIPv4Port: PublicIPv4Port, + PublicIPv4Port: publicIPv4Port, PublicIPv6: publicIPv6, PublicIPv6Port: publicIPv6Port, StatsIP: statsIP, diff --git a/config/urls.go b/config/urls.go index bfa1def..5f4e6d8 100644 --- a/config/urls.go +++ b/config/urls.go @@ -42,7 +42,7 @@ func makeTMeURL(values url.Values) string { } func makeQRCodeURL(data string) string { - QRURL := url.URL{ + qr := url.URL{ Scheme: "https", Host: "api.qrserver.com", Path: "v1/create-qr-code", @@ -52,7 +52,7 @@ func makeQRCodeURL(data string) string { values.Set("qzone", "4") values.Set("format", "svg") values.Set("data", data) - QRURL.RawQuery = values.Encode() + qr.RawQuery = values.Encode() - return QRURL.String() + return qr.String() } diff --git a/main.go b/main.go index a3637ab..712d92c 100644 --- a/main.go +++ b/main.go @@ -129,24 +129,22 @@ func main() { } conf, err := config.NewConfig(*debug, *verbose, - *bindIP, *bindPort, - *publicIPv4, *publicIPv4Port, - *publicIPv6, *publicIPv6Port, - *statsIP, *statsPort, - *secret, *adtag, - *statsdIP, *statsdPort, *statsdNetwork, *statsdPrefix, - *statsdTagsFormat, *statsdTags, + *bindIP, *publicIPv4, *publicIPv6, *statsIP, + *bindPort, *publicIPv4Port, *publicIPv6Port, *statsPort, *statsdPort, + *secret, *adtag, *statsdIP, *statsdNetwork, *statsdPrefix, *statsdTagsFormat, + *statsdTags, ) if err != nil { usage(err.Error()) } atom := zap.NewAtomicLevel() - if conf.Debug { + switch { + case conf.Debug: atom.SetLevel(zapcore.DebugLevel) - } else if conf.Verbose { + case conf.Verbose: atom.SetLevel(zapcore.InfoLevel) - } else { + default: atom.SetLevel(zapcore.ErrorLevel) } encoderCfg := zap.NewProductionEncoderConfig() diff --git a/mtproto/rpc/handshake_response.go b/mtproto/rpc/handshake_response.go index a8c522c..f3d2faa 100644 --- a/mtproto/rpc/handshake_response.go +++ b/mtproto/rpc/handshake_response.go @@ -19,10 +19,10 @@ type HandshakeResponse struct { func (r *HandshakeResponse) Bytes() []byte { buf := &bytes.Buffer{} - buf.Write(r.Type[:]) - buf.Write(r.Flags[:]) - buf.Write(r.SenderPID[:]) - buf.Write(r.PeerPID[:]) + buf.Write(r.Type) + buf.Write(r.Flags) + buf.Write(r.SenderPID) + buf.Write(r.PeerPID) return buf.Bytes() } diff --git a/wrappers/mtproto_cipher.go b/wrappers/mtproto_cipher.go index 363ecd6..9e7900f 100644 --- a/wrappers/mtproto_cipher.go +++ b/wrappers/mtproto_cipher.go @@ -41,9 +41,9 @@ func NewMiddleProxyCipher(conn StreamReadWriteCloser, func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse, client, remote *net.TCPAddr, secret []byte) ([]byte, []byte) { message := bytes.Buffer{} - message.Write(resp.Nonce[:]) - message.Write(req.Nonce[:]) - message.Write(req.CryptoTS[:]) + message.Write(resp.Nonce) + message.Write(req.Nonce) + message.Write(req.CryptoTS) clientIPv4 := emptyIP[:] serverIPv4 := emptyIP[:] @@ -70,13 +70,13 @@ func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceRes binary.LittleEndian.PutUint16(port[:], uint16(remote.Port)) message.Write(port[:]) message.Write(secret) - message.Write(resp.Nonce[:]) + message.Write(resp.Nonce) if client.IP.To4() == nil { message.Write(client.IP.To16()) message.Write(remote.IP.To16()) } - message.Write(req.Nonce[:]) + message.Write(req.Nonce) data := message.Bytes() md5sum := md5.Sum(data[1:]) // nolint: gas