From 36d7a5bdc5f06efc6a707a85d09de50a15c37713 Mon Sep 17 00:00:00 2001 From: Sergei Khoroshilov Date: Fri, 12 Jul 2024 23:08:14 +0200 Subject: [PATCH] feat: use range in for loops (#21) --- .golangci.yml | 1 - README.md | 2 +- cmd/swat4master/modules/prober/worker.go | 2 +- internal/persistence/memory/probes/probes_test.go | 4 ++-- internal/testutils/browsing.go | 2 +- internal/testutils/fixtures.go | 3 +-- pkg/gamespy/browsing/browsing.go | 2 +- pkg/gamespy/browsing/query/filter/filter.go | 2 +- pkg/gamespy/crypt/crypt.go | 5 ++--- pkg/gamespy/crypt/state.go | 2 +- pkg/gamespy/serverquery/params/decode.go | 2 +- pkg/gamespy/serverquery/params/encode.go | 2 +- tests/modules/browser_test.go | 2 +- tests/modules/exporter_test.go | 2 +- 14 files changed, 15 insertions(+), 18 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a24054b..8d4f384 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,7 +29,6 @@ linters: - testifylint - perfsprint - contextcheck - - intrange - revive linters-settings: cyclop: diff --git a/README.md b/README.md index 6a61d97..0314583 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ For other tags see [container registry][packages]. Alternatively you can download and run a server binary suitable for your platform from one of the [releases][releases]. ## Building from source -To build the project from source you need Go 1.21+ +To build the project from source you need Go 1.22+ ``` go build -o swat4master cmd/swat4master/main.go ``` diff --git a/cmd/swat4master/modules/prober/worker.go b/cmd/swat4master/modules/prober/worker.go index df83611..78c86df 100644 --- a/cmd/swat4master/modules/prober/worker.go +++ b/cmd/swat4master/modules/prober/worker.go @@ -38,7 +38,7 @@ func NewWorkerGroup( func (wg *WorkerGroup) Run(ctx context.Context) chan probe.Probe { wg.metrics.DiscoveryWorkersAvailable.Add(float64(wg.concurrency)) queue := make(chan probe.Probe, wg.concurrency) - for i := 0; i < wg.concurrency; i++ { + for range wg.concurrency { go wg.work(ctx, queue) } return queue diff --git a/internal/persistence/memory/probes/probes_test.go b/internal/persistence/memory/probes/probes_test.go index e03ab93..2e8c103 100644 --- a/internal/persistence/memory/probes/probes_test.go +++ b/internal/persistence/memory/probes/probes_test.go @@ -301,7 +301,7 @@ func TestProbesMemoryRepo_Pop(t *testing.T) { <-started // advance 100ms in steps - for i := 0; i < 100; i++ { + for range 100 { c.Advance(time.Millisecond * 1) advanced <- true } @@ -343,7 +343,7 @@ func TestProbesMemoryRepo_PopAny(t *testing.T) { ) popped := make([]string, 0) - for i := 0; i < 5; i++ { + for range 5 { prb, err := repo.PopAny(ctx) require.NoError(t, err) popped = append(popped, prb.Addr.GetDottedIP()) diff --git a/internal/testutils/browsing.go b/internal/testutils/browsing.go index 5eb5a38..2585b3e 100644 --- a/internal/testutils/browsing.go +++ b/internal/testutils/browsing.go @@ -83,7 +83,7 @@ func UnpackServerList(resp []byte) []map[string]string { fieldCount := int(resp[6]) fields := make([]string, 0, fieldCount) unparsed := resp[8:] - for i := 0; i < fieldCount; i++ { + for range fieldCount { field, rem := binutils.ConsumeCString(unparsed) // consume extra null byte at the end of the field unparsed = rem[1:] diff --git a/internal/testutils/fixtures.go b/internal/testutils/fixtures.go index 330edef..a1fd861 100644 --- a/internal/testutils/fixtures.go +++ b/internal/testutils/fixtures.go @@ -8,8 +8,7 @@ import ( ) func GenRandomIP() net.IP { - var i int - for i = 0; i < 10; i++ { + for i := range 10 { rand32int := make([]byte, 4) binary.BigEndian.PutUint32(rand32int, rand.Uint32()) // nolint: gosec randIP := net.IPv4(rand32int[0], rand32int[1], rand32int[2], rand32int[3]) diff --git a/pkg/gamespy/browsing/browsing.go b/pkg/gamespy/browsing/browsing.go index aafa5f8..afeb8ad 100644 --- a/pkg/gamespy/browsing/browsing.go +++ b/pkg/gamespy/browsing/browsing.go @@ -60,7 +60,7 @@ func NewRequest(data []byte) (Request, error) { func (req *Request) parse() error { // consume 2 equal strings with game identifier (such as swat4 or swat4xp1) // because they don't serve any purpose for us, just bin them - for i := 0; i < 2; i++ { + for range 2 { _, rem := binutils.ConsumeCString(req.unparsed) if rem == nil { return ErrInvalidRequestFormat diff --git a/pkg/gamespy/browsing/query/filter/filter.go b/pkg/gamespy/browsing/query/filter/filter.go index 2bcb4f2..c43401e 100644 --- a/pkg/gamespy/browsing/query/filter/filter.go +++ b/pkg/gamespy/browsing/query/filter/filter.go @@ -265,7 +265,7 @@ func getStructField(v any, fieldName string) (any, error) { sv := rv.Elem() st := reflect.TypeOf(sv.Interface()) - for i := 0; i < st.NumField(); i++ { + for i := range st.NumField() { field := st.Field(i) // ignore private fields if !field.IsExported() { diff --git a/pkg/gamespy/crypt/crypt.go b/pkg/gamespy/crypt/crypt.go index 9c4bcf4..a6fa701 100644 --- a/pkg/gamespy/crypt/crypt.go +++ b/pkg/gamespy/crypt/crypt.go @@ -56,7 +56,7 @@ func Encrypt(gameSecret [GMSL]byte, challenge [CCHL]byte, data []byte) []byte { payload := make([]byte, HDRL+len(data)) // init crypt header and fill it with random // bytes 9-23 will be the crypt key xor'ed with the game secret and the client's challenge - for i := 0; i < HDRL; i++ { + for i := range HDRL { payload[i] = uint8(random.RandInt(1, 255)) ^ gameSecret[i%GMSL] ^ challenge[i%CCHL] } svrChallenge := payload[9:HDRL] @@ -75,14 +75,13 @@ func Encrypt(gameSecret [GMSL]byte, challenge [CCHL]byte, data []byte) []byte { } func Decrypt(gameSecret [GMSL]byte, clientChallenge [CCHL]byte, data []byte) []byte { - var i uint8 var cryptKey [CRTL]byte // combine secret key, client and server challenges into a crypt key svrChOffset := (data[0] ^ 0xec) + 2 // 9 svrChLen := data[svrChOffset-1] ^ 0xea // 14 svrChallenge := data[svrChOffset : svrChOffset+svrChLen] // [9..23) copy(cryptKey[:], clientChallenge[:]) - for i = 0; i < svrChLen; i++ { + for i := range svrChLen { k := (i * gameSecret[i%GMSL]) % CCHL cryptKey[k] ^= (cryptKey[i%CCHL] ^ svrChallenge[i]) & 0xFF } diff --git a/pkg/gamespy/crypt/state.go b/pkg/gamespy/crypt/state.go index 1f0dbfb..39d5fb3 100644 --- a/pkg/gamespy/crypt/state.go +++ b/pkg/gamespy/crypt/state.go @@ -13,7 +13,7 @@ func newCipherState(cryptKey [CRTL]byte) cipherState { var toswap, rsum, keypos uint8 cs := cipherState{} // Start with state->cards all in order, one of each. - for i := 0; i < 256; i++ { + for i := range 256 { cs.cards[i] = uint8(i) } keypos = 0 // Start with first byte of the crypt key diff --git a/pkg/gamespy/serverquery/params/decode.go b/pkg/gamespy/serverquery/params/decode.go index c1940e6..5677984 100644 --- a/pkg/gamespy/serverquery/params/decode.go +++ b/pkg/gamespy/serverquery/params/decode.go @@ -17,7 +17,7 @@ func Marshal(v any) (map[string]string, error) { func marshal(sv reflect.Value) (map[string]string, error) { params := make(map[string]string) st := reflect.TypeOf(sv.Interface()) - for i := 0; i < st.NumField(); i++ { + for i := range st.NumField() { field := st.Field(i) // ignore private fields if !field.IsExported() { diff --git a/pkg/gamespy/serverquery/params/encode.go b/pkg/gamespy/serverquery/params/encode.go index b9418ab..7cf25b9 100644 --- a/pkg/gamespy/serverquery/params/encode.go +++ b/pkg/gamespy/serverquery/params/encode.go @@ -16,7 +16,7 @@ func Unmarshal(params map[string]string, v any) error { func unmarshal(params map[string]string, sv reflect.Value) error { st := reflect.TypeOf(sv.Interface()) - for i := 0; i < st.NumField(); i++ { + for i := range st.NumField() { field := st.Field(i) // ignore private fields if !field.IsExported() { diff --git a/tests/modules/browser_test.go b/tests/modules/browser_test.go index 3862a3c..be2d93a 100644 --- a/tests/modules/browser_test.go +++ b/tests/modules/browser_test.go @@ -264,7 +264,7 @@ func TestBrowser_ParseResponse(t *testing.T) { assert.Equal(t, 9, fieldCount) fields := make([]string, 0, fieldCount) unparsed := resp[8:] - for i := 0; i < fieldCount; i++ { + for range fieldCount { field, rem := binutils.ConsumeCString(unparsed) assert.True(t, len(field) > 0) assert.Equal(t, uint8(0), rem[0]) diff --git a/tests/modules/exporter_test.go b/tests/modules/exporter_test.go index 3910f4c..1240958 100644 --- a/tests/modules/exporter_test.go +++ b/tests/modules/exporter_test.go @@ -82,7 +82,7 @@ func TestExporter_MasterMetrics(t *testing.T) { sendUDP("127.0.0.1:33811", []byte{0x09}) // invalid keepalive request (no prior heartbeat) - for i := 0; i < 2; i++ { + for range 2 { sendUDP("127.0.0.1:33811", []byte{0x08, 0xde, 0xad, 0xbe, 0xef}) }