Fix lint gosec and ifshort issues

This commit is contained in:
Lawrence, Rendall
2022-04-12 18:51:03 +03:00
parent 554ae6fcd6
commit 64eaf9d733
10 changed files with 25 additions and 22 deletions

View File

@@ -5,10 +5,11 @@ output:
sort-results: true sort-results: true
linters-settings: linters-settings:
goimports: goimports:
local-prefixes: "github.com/sot-te.ch/mochi" local-prefixes: "sot-te.ch/mochi"
gosec: gosec:
excludes: excludes:
- "G404" # Allow the usage of math/rand - "G404" # Allow the usage of math/rand
- "G505" # Allow SHA1 usage
linters: linters:
enable: enable:
- "bidichk" - "bidichk"

View File

@@ -2,23 +2,26 @@
[![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://opensource.org/licenses/BSD-2-Clause) [![License](https://img.shields.io/badge/license-BSD-blue.svg)](https://opensource.org/licenses/BSD-2-Clause)
Modified version of [Chihaya](https://github.com/chihaya/chihaya), Modified version of [Chihaya](https://github.com/chihaya/chihaya), an open source BitTorrent tracker written in Go.
an open source BitTorrent tracker written in Go.
## Differences from the original project ## Differences from the original project
* Support BittorrentV2 hashes (SHA-256 and _hybrid_ SHA-256-to-160 [BEP52](https://www.bittorrent.org/beps/bep_0052.html), tested with qBittorrent); * Support BittorrentV2 hashes (SHA-256 and _hybrid_
SHA-256-to-160 [BEP52](https://www.bittorrent.org/beps/bep_0052.html), tested with qBittorrent);
* Support storage in middleware modules to persist useful data; * Support storage in middleware modules to persist useful data;
* Metrics can be turned off (not enabled till it really needed). * Metrics can be turned off (not enabled till it really needed).
## Main goal ## Main goal
The main goal of made modifications is to create semi-private tracker like [Hefur](https://github.com/sot-tech/hefur) The main goal of made modifications is to create semi-private tracker like [Hefur](https://github.com/sot-tech/hefur)
but with cluster support (allowed torrents limited by pre-existent `list` middleware and another `directory` middleware to limit registered torrents). but with cluster support (allowed torrents limited by pre-existent `list` middleware and another `directory` middleware
to limit registered torrents).
## Notice ## Notice
Modifications made with particular purpose, so if you need _pure_ tracker,
with guaranteed stability, please, use the [original project](https://github.com/chihaya/chihaya).
Developer(s) of this project (MoChi) do not declare themselves as authors of original Modifications made with particular purpose, so if you need _pure_ tracker, with guaranteed stability, please, use
project (Chihaya) and do not claim any other rights of original project. the [original project](https://github.com/chihaya/chihaya).
Developer(s) of this project (MoChi) do not declare themselves as authors of original project (Chihaya) and do not claim
any other rights of original project.

View File

@@ -14,7 +14,7 @@ import (
"github.com/sot-tech/mochi/middleware" "github.com/sot-tech/mochi/middleware"
"github.com/sot-tech/mochi/pkg/log" "github.com/sot-tech/mochi/pkg/log"
"github.com/sot-tech/mochi/pkg/metrics" "github.com/sot-tech/mochi/pkg/metrics"
_ "github.com/sot-tech/mochi/pkg/rand_seed" _ "github.com/sot-tech/mochi/pkg/randseed"
"github.com/sot-tech/mochi/pkg/stop" "github.com/sot-tech/mochi/pkg/stop"
"github.com/sot-tech/mochi/storage" "github.com/sot-tech/mochi/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"

View File

@@ -167,6 +167,7 @@ func NewFrontend(logic frontend.TrackerLogic, provided Config) (*Frontend, error
var err error var err error
f.tlsCfg = &tls.Config{ f.tlsCfg = &tls.Config{
Certificates: make([]tls.Certificate, 1), Certificates: make([]tls.Certificate, 1),
MinVersion: tls.VersionTLS12,
} }
f.tlsCfg.Certificates[0], err = tls.LoadX509KeyPair(cfg.TLSCertPath, cfg.TLSKeyPath) f.tlsCfg.Certificates[0], err = tls.LoadX509KeyPair(cfg.TLSCertPath, cfg.TLSKeyPath)
if err != nil { if err != nil {

View File

@@ -5,7 +5,7 @@ import (
"github.com/sot-tech/mochi/frontend/udp" "github.com/sot-tech/mochi/frontend/udp"
"github.com/sot-tech/mochi/middleware" "github.com/sot-tech/mochi/middleware"
_ "github.com/sot-tech/mochi/pkg/rand_seed" _ "github.com/sot-tech/mochi/pkg/randseed"
"github.com/sot-tech/mochi/storage" "github.com/sot-tech/mochi/storage"
_ "github.com/sot-tech/mochi/storage/memory" _ "github.com/sot-tech/mochi/storage/memory"
) )
@@ -22,8 +22,7 @@ func TestStartStopRaceIssue437(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
errC := fe.Stop() errC := fe.Stop()
errs := <-errC if errs := <-errC; len(errs) != 0 {
if len(errs) != 0 { t.Fatal(errs)
t.Fatal(errs[0])
} }
} }

View File

@@ -94,8 +94,7 @@ func ParseAnnounce(r Request, v6Action bool, opts ParseOptions) (*bittorrent.Ann
ip := r.IP ip := r.IP
ipProvided := false ipProvided := false
ipBytes := r.Packet[84:ipEnd] if ipBytes := r.Packet[84:ipEnd]; opts.AllowIPSpoofing {
if opts.AllowIPSpoofing {
// Make sure the bytes are copied to a new slice. // Make sure the bytes are copied to a new slice.
copy(ip, ipBytes) copy(ip, ipBytes)
ipProvided = true ipProvided = true

View File

@@ -4,7 +4,7 @@ import (
"math/rand" "math/rand"
"testing" "testing"
_ "github.com/sot-tech/mochi/pkg/rand_seed" _ "github.com/sot-tech/mochi/pkg/randseed"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )

View File

@@ -54,7 +54,7 @@ func build(confBytes []byte, st storage.Storage) (container.Container, error) {
for _, hashString := range c.HashList { for _, hashString := range c.HashList {
ih, err := bittorrent.NewInfoHash(hashString) ih, err := bittorrent.NewInfoHash(hashString)
if err != nil { if err != nil {
return nil, fmt.Errorf("whitelist : %s : %v", hashString, err) return nil, fmt.Errorf("whitelist : %s : %w", hashString, err)
} }
init = append(init, storage.Pair{Left: ih.RawString(), Right: DUMMY}) init = append(init, storage.Pair{Left: ih.RawString(), Right: DUMMY})
if len(ih) == bittorrent.InfoHashV2Len { if len(ih) == bittorrent.InfoHashV2Len {

View File

@@ -1,5 +1,5 @@
// Package rand_seed just seeds (math) rand.Rand // Package randseed just seeds (math) rand.Rand
package rand_seed package randseed
import ( import (
cr "crypto/rand" cr "crypto/rand"

View File

@@ -8,7 +8,7 @@ import (
"testing" "testing"
"github.com/sot-tech/mochi/bittorrent" "github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/rand_seed" "github.com/sot-tech/mochi/pkg/randseed"
"github.com/sot-tech/mochi/storage" "github.com/sot-tech/mochi/storage"
) )
@@ -28,7 +28,7 @@ func generateInfohashes() (a [1000]bittorrent.InfoHash) {
} }
func generatePeers() (a [1000]bittorrent.Peer) { func generatePeers() (a [1000]bittorrent.Peer) {
r := rand.New(rand.NewSource(rand_seed.GenSeed())) r := rand.New(rand.NewSource(randseed.GenSeed()))
for i := range a { for i := range a {
ip := make([]byte, 4) ip := make([]byte, 4)
n, err := r.Read(ip) n, err := r.Read(ip)