Skip to content

Commit

Permalink
🏪 all: upgrade to go 1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Feb 13, 2025
1 parent 590b5be commit cb8ba33
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 193 deletions.
10 changes: 3 additions & 7 deletions conn/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package conn

import (
"bytes"
"context"
"crypto/rand"
"net/netip"
"strings"
Expand Down Expand Up @@ -144,7 +143,7 @@ func TestAddrIPPort(t *testing.T) {
}

func TestAddrResolveIP(t *testing.T) {
ctx := context.Background()
ctx := t.Context()

ip, err := addrIP.ResolveIP(ctx, "ip")
if err != nil {
Expand All @@ -166,7 +165,7 @@ func TestAddrResolveIP(t *testing.T) {
}

func TestAddrResolveIPPort(t *testing.T) {
ctx := context.Background()
ctx := t.Context()

ipPort, err := addrIP.ResolveIPPort(ctx, "ip")
if err != nil {
Expand Down Expand Up @@ -218,10 +217,7 @@ func TestAddrString(t *testing.T) {

func TestAddrAppendTo(t *testing.T) {
head := make([]byte, 64)
_, err := rand.Read(head)
if err != nil {
t.Fatal(err)
}
rand.Read(head)

b := make([]byte, 0, 128)
b = append(b, head...)
Expand Down
19 changes: 6 additions & 13 deletions direct/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package direct

import (
"bytes"
"context"
"crypto/rand"
"net/netip"
"sync"
Expand All @@ -27,7 +26,7 @@ func TestDirectStreamReadWriter(t *testing.T) {
zerocopy.ReadWriterTestFunc(t, &l, &r)
}

func testShadowsocksNoneStreamReadWriter(t *testing.T, ctx context.Context, clientInitialPayload []byte) {
func testShadowsocksNoneStreamReadWriter(t *testing.T, clientInitialPayload []byte) {
pl, pr := pipe.NewDuplexPipe()
plo := zerocopy.SimpleDirectReadWriteCloserOpener{DirectReadWriteCloser: pl}

Expand All @@ -47,7 +46,7 @@ func testShadowsocksNoneStreamReadWriter(t *testing.T, ctx context.Context, clie

go func() {
defer wg.Done()
c, _, cerr = NewShadowsocksNoneStreamClientReadWriter(ctx, &plo, clientTargetAddr, clientInitialPayload)
c, _, cerr = NewShadowsocksNoneStreamClientReadWriter(t.Context(), &plo, clientTargetAddr, clientInitialPayload)
}()

go func() {
Expand Down Expand Up @@ -83,27 +82,21 @@ func testShadowsocksNoneStreamReadWriter(t *testing.T, ctx context.Context, clie
}

func TestShadowsocksNoneStreamReadWriter(t *testing.T) {
ctx := context.Background()
initialPayload := make([]byte, 1024)
_, err := rand.Read(initialPayload)
if err != nil {
t.Fatal(err)
}
rand.Read(initialPayload)

t.Run("NoInitialPayload", func(t *testing.T) {
testShadowsocksNoneStreamReadWriter(t, ctx, nil)
testShadowsocksNoneStreamReadWriter(t, nil)
})

t.Run("WithInitialPayload", func(t *testing.T) {
testShadowsocksNoneStreamReadWriter(t, ctx, initialPayload)
testShadowsocksNoneStreamReadWriter(t, initialPayload)
})
}

func TestSocks5StreamReadWriter(t *testing.T) {
b := make([]byte, 255+255)
if _, err := rand.Read(b); err != nil {
t.Fatal(err)
}
rand.Read(b)
userInfo255 := socks5.UserInfo{
Username: string(b[:255]),
Password: string(b[255:]),
Expand Down
9 changes: 4 additions & 5 deletions dns/dns_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dns

import (
"context"
"net/netip"
"testing"

Expand All @@ -12,8 +11,9 @@ import (
"go.uber.org/zap/zaptest"
)

func testResolver(t *testing.T, ctx context.Context, name string, serverAddrPort netip.AddrPort, tcpClient zerocopy.TCPClient, udpClient zerocopy.UDPClient, logger *zap.Logger) {
func testResolver(t *testing.T, name string, serverAddrPort netip.AddrPort, tcpClient zerocopy.TCPClient, udpClient zerocopy.UDPClient, logger *zap.Logger) {
r := NewResolver(name, serverAddrPort, tcpClient, udpClient, logger)
ctx := t.Context()

// Uncached lookup.
uncachedResult, err := r.Lookup(ctx, "example.com")
Expand Down Expand Up @@ -42,16 +42,15 @@ func TestResolver(t *testing.T) {
logger := zaptest.NewLogger(t)
defer logger.Sync()

ctx := context.Background()
serverAddrPort := netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53)
tcpClient := direct.NewTCPClient("direct", "tcp", conn.DefaultTCPDialer)
udpClient := direct.NewDirectUDPClient("direct", "ip", 1500, conn.DefaultUDPClientListenConfig)

t.Run("UDP", func(t *testing.T) {
testResolver(t, ctx, "UDP", serverAddrPort, nil, udpClient, logger)
testResolver(t, "UDP", serverAddrPort, nil, udpClient, logger)
})

t.Run("TCP", func(t *testing.T) {
testResolver(t, ctx, "TCP", serverAddrPort, tcpClient, nil, logger)
testResolver(t, "TCP", serverAddrPort, tcpClient, nil, logger)
})
}
6 changes: 4 additions & 2 deletions domainset/matcher_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,16 @@ func TestDomainMapMatcher(t *testing.T) {

func benchmarkDomainMatcher(b *testing.B, count int, name string, m Matcher) {
b.Run(fmt.Sprintf("%d/%s/Hit", count, name), func(b *testing.B) {
for i := range b.N {
var i int
for b.Loop() {
if !m.Match(testDomains[i%count]) {
b.Fatal("unexpected miss")
}
i++
}
})
b.Run(fmt.Sprintf("%d/%s/Miss", count, name), func(b *testing.B) {
for range b.N {
for b.Loop() {
if m.Match(testMissDomain) {
b.Fatal("unexpected hit")
}
Expand Down
10 changes: 6 additions & 4 deletions domainset/matcher_suffix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,30 @@ func TestSuffixTrieMatcher(t *testing.T) {

func benchmarkSuffixMatcher(b *testing.B, count int, name string, m Matcher) {
b.Run(fmt.Sprintf("%d/%s/Hit", count, name), func(b *testing.B) {
for i := range b.N {
var i int
for b.Loop() {
if !m.Match(testSuffixes[i%count]) {
b.Fatal("unexpected miss")
}
i++
}
})
b.Run(fmt.Sprintf("%d/%s/Miss/Short", count, name), func(b *testing.B) {
for range b.N {
for b.Loop() {
if m.Match(shortDomain) {
b.Fatal("unexpected hit")
}
}
})
b.Run(fmt.Sprintf("%d/%s/Miss/Medium", count, name), func(b *testing.B) {
for range b.N {
for b.Loop() {
if m.Match(mediumDomain) {
b.Fatal("unexpected hit")
}
}
})
b.Run(fmt.Sprintf("%d/%s/Miss/Long", count, name), func(b *testing.B) {
for range b.N {
for b.Loop() {
if m.Match(longDomain) {
b.Fatal("unexpected hit")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/database64128/shadowsocks-go

go 1.23.0
go 1.24.0

require (
github.com/database64128/netx-go v0.0.0-20250204233320-d8aeaa11659f
Expand Down
30 changes: 6 additions & 24 deletions socks5/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ var (
func testAddrFromReader(t *testing.T, addr []byte) {
b := make([]byte, 512)
n := copy(b, addr)
_, err := rand.Read(b[n:])
if err != nil {
t.Fatal(err)
}
rand.Read(b[n:])
expectedTail := make([]byte, 512-n)
copy(expectedTail, b[n:])

Expand Down Expand Up @@ -88,10 +85,7 @@ func TestAddrFromReader(t *testing.T) {
func testAddrPortFromSlice(t *testing.T, sa []byte, expectedAddrPort netip.AddrPort, expectedN int, expectedErr error) {
b := make([]byte, 512)
n := copy(b, sa)
_, err := rand.Read(b[n:])
if err != nil {
t.Fatal(err)
}
rand.Read(b[n:])
expectedTail := make([]byte, 512-n)
copy(expectedTail, b[n:])

Expand Down Expand Up @@ -120,10 +114,7 @@ func TestAddrPortFromSlice(t *testing.T) {
func testConnAddrFromSliceAndReader(t *testing.T, sa []byte, expectedAddr conn.Addr) {
b := make([]byte, 512)
n := copy(b, sa)
_, err := rand.Read(b[n:])
if err != nil {
t.Fatal(err)
}
rand.Read(b[n:])
expectedTail := make([]byte, 512-n)
copy(expectedTail, b[n:])

Expand Down Expand Up @@ -168,10 +159,7 @@ func TestConnAddrFromSliceAndReader(t *testing.T) {
func testConnAddrFromSliceWithDomainCache(t *testing.T, sa []byte, cachedDomain string, expectedAddr conn.Addr) string {
b := make([]byte, 512)
n := copy(b, sa)
_, err := rand.Read(b[n:])
if err != nil {
t.Fatal(err)
}
rand.Read(b[n:])
expectedTail := make([]byte, 512-n)
copy(expectedTail, b[n:])

Expand Down Expand Up @@ -218,10 +206,7 @@ func TestConnAddrFromSliceWithDomainCache(t *testing.T) {

func testAppendAddrFromConnAddr(t *testing.T, addr conn.Addr, expectedSA []byte) {
head := make([]byte, 64)
_, err := rand.Read(head)
if err != nil {
t.Fatal(err)
}
rand.Read(head)

b := make([]byte, 0, 512)
b = append(b, head...)
Expand Down Expand Up @@ -250,10 +235,7 @@ func testLengthOfAndWriteAddrFromConnAddr(t *testing.T, addr conn.Addr, expected
}

b := make([]byte, 512)
_, err := rand.Read(b[addrLen:])
if err != nil {
t.Fatal(err)
}
rand.Read(b[addrLen:])
tail := make([]byte, 512-addrLen)
copy(tail, b[addrLen:])

Expand Down
12 changes: 3 additions & 9 deletions ss2022/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ func newRandomCipherConfigTupleNoEIH(method string, enableUDP bool) (clientCiphe
return
}
psk := make([]byte, keySize)
if _, err = rand.Read(psk); err != nil {
return
}
rand.Read(psk)
clientCipherConfig, err = NewClientCipherConfig(psk, nil, enableUDP)
if err != nil {
return
Expand All @@ -29,18 +27,14 @@ func newRandomCipherConfigTupleWithEIH(method string, enableUDP bool) (clientCip
}

iPSK := make([]byte, keySize)
if _, err = rand.Read(iPSK); err != nil {
return
}
rand.Read(iPSK)
iPSKs := [][]byte{iPSK}

var uPSK []byte
userLookupMap = make(UserLookupMap, 7)
for i := range 7 {
uPSK = make([]byte, keySize)
if _, err = rand.Read(uPSK); err != nil {
return
}
rand.Read(uPSK)

uPSKHash := PSKHash(uPSK)
var c *ServerUserCipherConfig
Expand Down
25 changes: 5 additions & 20 deletions ss2022/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func TestWriteAndParseTCPRequestFixedLengthHeader(t *testing.T) {
func TestWriteAndParseTCPRequestVariableLengthHeader(t *testing.T) {
payloadLen := 1 + int(mrand.Uint64()&1023)
payload := make([]byte, payloadLen)
_, err := rand.Read(payload)
if err != nil {
t.Fatal(err)
}
rand.Read(payload)
targetAddr := conn.AddrFromIPPort(netip.AddrPortFrom(netip.IPv6Unspecified(), 443))
targetAddrLen := socks5.LengthOfAddrFromConnAddr(targetAddr)
noPayloadLen := targetAddrLen + 2 + 1 + mrand.IntN(MaxPaddingLength)
Expand Down Expand Up @@ -157,10 +154,7 @@ func TestWriteAndParseTCPResponseHeader(t *testing.T) {
b := make([]byte, bufLen)
length := int(mrand.Uint64() & math.MaxUint16)
requestSalt := make([]byte, saltLen)
_, err := rand.Read(requestSalt)
if err != nil {
t.Fatal(err)
}
rand.Read(requestSalt)

// 1. Good header
WriteTCPResponseHeader(b, requestSalt, uint16(length))
Expand All @@ -174,10 +168,7 @@ func TestWriteAndParseTCPResponseHeader(t *testing.T) {
}

// 2. Bad request salt
_, err = rand.Read(b[1+8 : 1+8+saltLen])
if err != nil {
t.Fatal(err)
}
rand.Read(b[1+8 : 1+8+saltLen])

_, err = ParseTCPResponseHeader(b, requestSalt)
if !errors.Is(err, ErrClientSaltMismatch) {
Expand Down Expand Up @@ -240,10 +231,7 @@ func TestWriteAndParseUDPClientMessageHeader(t *testing.T) {
headerBuf := b[:headerLen]
headerNoPaddingBuf := bNoPadding[:noPaddingLen]
payload := b[headerLen:]
_, err := rand.Read(payload)
if err != nil {
t.Fatal(err)
}
rand.Read(payload)

// 1. Good header (no padding)
WriteUDPClientMessageHeader(headerNoPaddingBuf, 0, targetAddr)
Expand Down Expand Up @@ -347,10 +335,7 @@ func TestWriteAndParseUDPServerMessageHeader(t *testing.T) {
headerBuf := b[:headerLen]
headerNoPaddingBuf := bNoPadding[:noPaddingLen]
payload := b[headerLen:]
_, err := rand.Read(payload)
if err != nil {
t.Fatal(err)
}
rand.Read(payload)

// 1. Good header (no padding)
WriteUDPServerMessageHeader(headerNoPaddingBuf, csid, 0, sourceAddrPort)
Expand Down
5 changes: 1 addition & 4 deletions ss2022/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ func (p *ShadowPacketServerUnpacker) UnpackInPlace(b []byte, sourceAddr netip.Ad
func (p *ShadowPacketServerUnpacker) NewPacker() (zerocopy.ServerPacker, error) {
// Random server session ID.
salt := make([]byte, 8)
_, err := rand.Read(salt)
if err != nil {
return nil, err
}
rand.Read(salt)
ssid := binary.BigEndian.Uint64(salt)

aead, err := p.userCipherConfig.AEAD(salt)
Expand Down
5 changes: 1 addition & 4 deletions ss2022/saltpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import (
func TestSaltPoolAddDuplicateSalts(t *testing.T) {
const retention = 100 * time.Millisecond
var salt [32]byte
_, err := rand.Read(salt[:])
if err != nil {
t.Fatal(err)
}
rand.Read(salt[:])

pool := NewSaltPool[[32]byte](retention)

Expand Down
5 changes: 1 addition & 4 deletions ss2022/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func (rw *ShadowStreamServerReadWriter) WriteZeroCopy(b []byte, payloadStart, pa
copy(ursp, rw.unsafeResponseStreamPrefix)

// Random salt.
_, err := rand.Read(salt)
if err != nil {
return 0, err
}
rand.Read(salt)

// Write response header.
WriteTCPResponseHeader(responseHeader, rw.requestSalt, uint16(payloadLen))
Expand Down
Loading

0 comments on commit cb8ba33

Please sign in to comment.