Skip to content

Commit 7fefccc

Browse files
authored
Merge pull request #123 from sot-tech/redis_tls
Add Redis TLS option
2 parents ebe4dc3 + f1a32a0 commit 7fefccc

File tree

6 files changed

+125
-61
lines changed

6 files changed

+125
-61
lines changed

dist/example_config_redis.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ storage:
7070
# only first address used
7171
addresses: ["127.0.0.1:6379"]
7272

73+
# Use TLS for connecting to specified addresses.
74+
tls: false
75+
76+
# Trusted CA certificates to check while TLS connection.
77+
# If not provided, system certificates used.
78+
ca_certs: []
79+
7380
# Database to be selected after connecting to the server.
7481
db: 0
7582

docs/storage/redis.md

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ mochi:
3737

3838
# The addresses of redis storage.
3939
addresses: ["127.0.0.1:6379"]
40+
41+
# Use TLS for connecting to specified addresses.
42+
# Note: server names must strictly match with names,
43+
# provided in certificates (CN or SAN field) and
44+
# issuer must be trusted by system or provided in ca_certs.
45+
tls: false
46+
47+
# Trusted CA certificates to check while TLS connection.
48+
# If not provided, system certificates used.
49+
ca_certs: []
4050

4151
# Database number
4252
db: 0

go.mod

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go 1.23
55
toolchain go1.23.1
66

77
require (
8-
code.cloudfoundry.org/go-diodes v0.0.0-20241202111822-779c0b5a3368
9-
github.com/MicahParks/jwkset v0.5.20
10-
github.com/MicahParks/keyfunc/v3 v3.3.5
11-
github.com/PowerDNS/lmdb-go v1.9.2
8+
code.cloudfoundry.org/go-diodes v0.0.0-20250107092748-442ea8f9957b
9+
github.com/MicahParks/jwkset v0.8.0
10+
github.com/MicahParks/keyfunc/v3 v3.3.10
11+
github.com/PowerDNS/lmdb-go v1.9.3
1212
github.com/cespare/xxhash/v2 v2.3.0
13-
github.com/fasthttp/router v1.5.3
13+
github.com/fasthttp/router v1.5.4
1414
github.com/golang-jwt/jwt/v5 v5.2.1
1515
github.com/jackc/pgx/v5 v5.7.2
1616
github.com/libp2p/go-reuseport v0.4.0
@@ -34,7 +34,7 @@ require (
3434
github.com/jackc/puddle/v2 v2.2.2 // indirect
3535
github.com/klauspost/compress v1.17.11 // indirect
3636
github.com/kr/text v0.2.0 // indirect
37-
github.com/mattn/go-colorable v0.1.13 // indirect
37+
github.com/mattn/go-colorable v0.1.14 // indirect
3838
github.com/mattn/go-isatty v0.0.20 // indirect
3939
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4040
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -43,11 +43,11 @@ require (
4343
github.com/prometheus/procfs v0.15.1 // indirect
4444
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
4545
github.com/valyala/bytebufferpool v1.0.0 // indirect
46-
golang.org/x/crypto v0.31.0 // indirect
47-
golang.org/x/net v0.33.0 // indirect
46+
golang.org/x/crypto v0.32.0 // indirect
47+
golang.org/x/net v0.34.0 // indirect
4848
golang.org/x/sync v0.10.0 // indirect
49-
golang.org/x/sys v0.28.0 // indirect
49+
golang.org/x/sys v0.29.0 // indirect
5050
golang.org/x/text v0.21.0 // indirect
51-
golang.org/x/time v0.8.0 // indirect
52-
google.golang.org/protobuf v1.36.0 // indirect
51+
golang.org/x/time v0.9.0 // indirect
52+
google.golang.org/protobuf v1.36.2 // indirect
5353
)

go.sum

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
code.cloudfoundry.org/go-diodes v0.0.0-20241202111822-779c0b5a3368 h1:MkeJ/1SwyulJ1kArYPbl3EhuSwBd/bK36kgv4rj/H6c=
2-
code.cloudfoundry.org/go-diodes v0.0.0-20241202111822-779c0b5a3368/go.mod h1:vuWO6pLIcI5+vK8qfMytmpoy58jhUgNY6uvz7yb8jVo=
3-
github.com/MicahParks/jwkset v0.5.20 h1:gTIKx9AofTqQJ0srd8AL7ty9NeadP5WUXSPOZadTpOI=
4-
github.com/MicahParks/jwkset v0.5.20/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY=
5-
github.com/MicahParks/keyfunc/v3 v3.3.5 h1:7ceAJLUAldnoueHDNzF8Bx06oVcQ5CfJnYwNt1U3YYo=
6-
github.com/MicahParks/keyfunc/v3 v3.3.5/go.mod h1:SdCCyMJn/bYqWDvARspC6nCT8Sk74MjuAY22C7dCST8=
7-
github.com/PowerDNS/lmdb-go v1.9.2 h1:Cmgerh9y3ZKBZGz1irxSShhfmFyRUh+Zdk4cZk7ZJvU=
8-
github.com/PowerDNS/lmdb-go v1.9.2/go.mod h1:TE0l+EZK8Z1B4dx070ZxkWTlp8RG1mjN0/+FkFRQMtU=
1+
code.cloudfoundry.org/go-diodes v0.0.0-20250107092748-442ea8f9957b h1:7/gk6mXgNTD4AYC/e0SJwGLOnH0A0R0q8r2BznL101c=
2+
code.cloudfoundry.org/go-diodes v0.0.0-20250107092748-442ea8f9957b/go.mod h1:0VGmmzbiipVB6xQB/9dFusj8EzMkRG42PgYybL7Xj5Q=
3+
github.com/MicahParks/jwkset v0.8.0 h1:jHtclI38Gibmu17XMI6+6/UB59srp58pQVxePHRK5o8=
4+
github.com/MicahParks/jwkset v0.8.0/go.mod h1:fVrj6TmG1aKlJEeceAz7JsXGTXEn72zP1px3us53JrA=
5+
github.com/MicahParks/keyfunc/v3 v3.3.10 h1:JtEGE8OcNeI297AMrR4gVXivV8fyAawFUMkbwNreJRk=
6+
github.com/MicahParks/keyfunc/v3 v3.3.10/go.mod h1:1TEt+Q3FO7Yz2zWeYO//fMxZMOiar808NqjWQQpBPtU=
7+
github.com/PowerDNS/lmdb-go v1.9.3 h1:AUMY2pZT8WRpkEv39I9Id3MuoHd+NZbTVpNhruVkPTg=
8+
github.com/PowerDNS/lmdb-go v1.9.3/go.mod h1:TE0l+EZK8Z1B4dx070ZxkWTlp8RG1mjN0/+FkFRQMtU=
99
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
1010
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
1111
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -23,8 +23,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
2323
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2424
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
2525
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
26-
github.com/fasthttp/router v1.5.3 h1:BFWXqa3e4thRI3MgPKTNtz0Oiq6UYN2OsEtb+YQ5TMI=
27-
github.com/fasthttp/router v1.5.3/go.mod h1:b864KkDIapOYh77AVG/SNkwfRZ6k6ecWvD+ZRXmP5pw=
26+
github.com/fasthttp/router v1.5.4 h1:oxdThbBwQgsDIYZ3wR1IavsNl6ZS9WdjKukeMikOnC8=
27+
github.com/fasthttp/router v1.5.4/go.mod h1:3/hysWq6cky7dTfzaaEPZGdptwjwx0qzTgFCKEWRjgc=
2828
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
2929
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
3030
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
@@ -34,8 +34,8 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w
3434
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3535
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3636
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
37-
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgYQBbFN4U4JNXUNYpxael3UzMyo=
38-
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
37+
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad h1:a6HEuzUHeKH6hwfN/ZoQgRgVIWFJljSWa/zetS2WTvg=
38+
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
3939
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
4040
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
4141
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
@@ -54,8 +54,9 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
5454
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
5555
github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
5656
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
57-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
5857
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
58+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
59+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
5960
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6061
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
6162
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@@ -64,10 +65,10 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
6465
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
6566
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
6667
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
67-
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
68-
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
69-
github.com/onsi/gomega v1.36.0 h1:Pb12RlruUtj4XUuPUqeEWc6j5DkVVVA49Uf6YLfC95Y=
70-
github.com/onsi/gomega v1.36.0/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
68+
github.com/onsi/ginkgo/v2 v2.22.2 h1:/3X8Panh8/WwhU/3Ssa6rCKqPLuAkVY2I0RoyDLySlU=
69+
github.com/onsi/ginkgo/v2 v2.22.2/go.mod h1:oeMosUL+8LtarXBHu/c0bx2D/K9zyQ6uX3cTyztHwsk=
70+
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
71+
github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY=
7172
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
7273
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7374
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -101,25 +102,25 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
101102
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
102103
github.com/zeebo/bencode v1.0.0 h1:zgop0Wu1nu4IexAZeCZ5qbsjU4O1vMrfCrVgUjbHVuA=
103104
github.com/zeebo/bencode v1.0.0/go.mod h1:Ct7CkrWIQuLWAy9M3atFHYq4kG9Ao/SsY5cdtCXmp9Y=
104-
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
105-
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
106-
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
107-
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
105+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
106+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
107+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
108+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
108109
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
109110
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
110111
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
111112
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
112113
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
113-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
114-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
114+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
115+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
115116
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
116117
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
117-
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
118-
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
119-
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
120-
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
121-
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
122-
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
118+
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
119+
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
120+
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
121+
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
122+
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
123+
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
123124
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
124125
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
125126
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

middleware/jwt/jwt.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"errors"
1111
"fmt"
12-
"net/url"
1312
"strings"
1413
"time"
1514

@@ -89,21 +88,16 @@ func build(config conf.MapConfig, _ storage.PeerStorage) (h middleware.Hook, err
8988

9089
var jwks keyfunc.Keyfunc
9190
if cfg.HandleAnnounce || cfg.HandleScrape {
92-
var jwkURL *url.URL
93-
jwkURL, err = url.Parse(cfg.JWKSetURL)
91+
var httpStorage jwkset.Storage
92+
httpStorage, err = jwkset.NewStorageFromHTTP(cfg.JWKSetURL, jwkset.HTTPClientStorageOptions{
93+
NoErrorReturnFirstHTTPReq: true,
94+
RefreshErrorHandler: func(_ context.Context, err error) {
95+
logger.Error().Err(err).Msg("error occurred while updating JWKs")
96+
},
97+
RefreshInterval: cfg.JWKUpdateInterval,
98+
})
9499
if err == nil {
95-
var httpStorage jwkset.Storage
96-
httpStorage, err = jwkset.NewStorageFromHTTP(jwkURL, jwkset.HTTPClientStorageOptions{
97-
NoErrorReturnFirstHTTPReq: true,
98-
RefreshErrorHandler: func(_ context.Context, err error) {
99-
logger.Error().Err(err).Msg("error occurred while updating JWKs")
100-
},
101-
RefreshInterval: cfg.JWKUpdateInterval,
102-
Storage: nil,
103-
})
104-
if err == nil {
105-
jwks, err = keyfunc.New(keyfunc.Options{Storage: httpStorage})
106-
}
100+
jwks, err = keyfunc.New(keyfunc.Options{Storage: httpStorage})
107101
}
108102
} else {
109103
logger.Warn().Msg("both announce and scrape handle disabled")
@@ -127,7 +121,9 @@ type announceClaims struct {
127121
InfoHash string `json:"infohash,omitempty"`
128122
}
129123

130-
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, _ *bittorrent.AnnounceResponse) (context.Context, error) {
124+
func (h *hook) HandleAnnounce(
125+
ctx context.Context, req *bittorrent.AnnounceRequest, _ *bittorrent.AnnounceResponse,
126+
) (context.Context, error) {
131127
if !h.cfg.HandleAnnounce {
132128
return ctx, nil
133129
}
@@ -171,7 +167,9 @@ type scrapeClaims struct {
171167
InfoHashes []string `json:"infohashes,omitempty"`
172168
}
173169

174-
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, _ *bittorrent.ScrapeResponse) (context.Context, error) {
170+
func (h *hook) HandleScrape(
171+
ctx context.Context, req *bittorrent.ScrapeRequest, _ *bittorrent.ScrapeResponse,
172+
) (context.Context, error) {
175173
if !h.cfg.HandleScrape {
176174
return ctx, nil
177175
}

storage/redis/storage.go

+52-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ package redis
2424

2525
import (
2626
"context"
27+
"crypto/tls"
28+
"crypto/x509"
2729
"encoding/binary"
2830
"errors"
2931
"fmt"
3032
"net"
3133
"net/netip"
34+
"os"
3235
"strconv"
3336
"strings"
3437
"sync"
@@ -119,6 +122,8 @@ func NewStore(cfg Config) (storage.PeerStorage, error) {
119122
type Config struct {
120123
PeerLifetime time.Duration `cfg:"peer_lifetime"`
121124
Addresses []string
125+
TLS bool
126+
CACerts []string `cfg:"ca_certs"`
122127
DB int
123128
PoolSize int `cfg:"pool_size"`
124129
Login string
@@ -187,12 +192,46 @@ func (cfg Config) Validate() (Config, error) {
187192
Msg("falling back to default configuration")
188193
}
189194

195+
if cfg.TLS {
196+
for _, cert := range cfg.CACerts {
197+
if _, err := os.Stat(cert); err != nil {
198+
return cfg, err
199+
}
200+
}
201+
}
202+
190203
return validCfg, nil
191204
}
192205

193206
// Connect creates redis client from configuration
194207
func (cfg Config) Connect() (con Connection, err error) {
195208
var rs redis.UniversalClient
209+
var tlsConf *tls.Config
210+
if cfg.TLS {
211+
tlsConf = &tls.Config{MinVersion: tls.VersionTLS12}
212+
if len(cfg.CACerts) > 0 {
213+
certPool := x509.NewCertPool()
214+
var ok bool
215+
for _, cert := range cfg.CACerts {
216+
var certData []byte
217+
if certData, err = os.ReadFile(cert); err != nil {
218+
logger.Warn().
219+
Err(err).
220+
Str("path", cert).
221+
Msg("unable to read certificate(s) file")
222+
} else if added := certPool.AppendCertsFromPEM(certData); added {
223+
ok = true
224+
} else {
225+
logger.Warn().
226+
Str("path", cert).
227+
Msg("unable to append certificate(s) to trusted pool")
228+
}
229+
}
230+
if ok {
231+
tlsConf.RootCAs = certPool
232+
}
233+
}
234+
}
196235
switch {
197236
case cfg.Cluster:
198237
rs = redis.NewClusterClient(&redis.ClusterOptions{
@@ -203,6 +242,7 @@ func (cfg Config) Connect() (con Connection, err error) {
203242
ReadTimeout: cfg.ReadTimeout,
204243
WriteTimeout: cfg.WriteTimeout,
205244
PoolSize: cfg.PoolSize,
245+
TLSConfig: tlsConf,
206246
})
207247
case cfg.Sentinel:
208248
rs = redis.NewFailoverClient(&redis.FailoverOptions{
@@ -215,6 +255,7 @@ func (cfg Config) Connect() (con Connection, err error) {
215255
WriteTimeout: cfg.WriteTimeout,
216256
PoolSize: cfg.PoolSize,
217257
DB: cfg.DB,
258+
TLSConfig: tlsConf,
218259
})
219260
default:
220261
rs = redis.NewClient(&redis.Options{
@@ -226,6 +267,7 @@ func (cfg Config) Connect() (con Connection, err error) {
226267
WriteTimeout: cfg.WriteTimeout,
227268
PoolSize: cfg.PoolSize,
228269
DB: cfg.DB,
270+
TLSConfig: tlsConf,
229271
})
230272
}
231273
if err = rs.Ping(context.Background()).Err(); err == nil && !errors.Is(err, redis.Nil) {
@@ -464,7 +506,8 @@ func (ps *store) GraduateLeecher(ctx context.Context, ih bittorrent.InfoHash, pe
464506
// peerMinimumLen is the least allowed length of string serialized Peer
465507
const peerMinimumLen = bittorrent.PeerIDLen + 2 + net.IPv4len
466508

467-
var errInvalidPeerDataSize = fmt.Errorf("invalid peer data (must be at least %d bytes (PeerID + Port + IPv4))", peerMinimumLen)
509+
var errInvalidPeerDataSize = fmt.Errorf("invalid peer data (must be at least %d bytes (PeerID + Port + IPv4))",
510+
peerMinimumLen)
468511

469512
// UnpackPeer constructs Peer from serialized by Peer.PackPeer data: PeerID[20by]Port[2by]net.IP[4/16by]
470513
func UnpackPeer(data string) (peer bittorrent.Peer, err error) {
@@ -510,7 +553,9 @@ type getPeersFn func(context.Context, string, int) *redis.StringSliceCmd
510553
// converts result to bittorrent.Peer array.
511554
// If forSeeder set to true - returns only leechers, if false -
512555
// seeders and if maxCount not reached - leechers.
513-
func (ps *Connection) GetPeers(ctx context.Context, ih bittorrent.InfoHash, forSeeder bool, maxCount int, isV6 bool, membersFn getPeersFn) (out []bittorrent.Peer, err error) {
556+
func (ps *Connection) GetPeers(
557+
ctx context.Context, ih bittorrent.InfoHash, forSeeder bool, maxCount int, isV6 bool, membersFn getPeersFn,
558+
) (out []bittorrent.Peer, err error) {
514559
infoHash := ih.RawString()
515560

516561
infoHashKeys := make([]string, 1, 2)
@@ -544,7 +589,9 @@ func (ps *Connection) GetPeers(ctx context.Context, ih bittorrent.InfoHash, forS
544589
return
545590
}
546591

547-
func (ps *store) AnnouncePeers(ctx context.Context, ih bittorrent.InfoHash, forSeeder bool, numWant int, v6 bool) ([]bittorrent.Peer, error) {
592+
func (ps *store) AnnouncePeers(
593+
ctx context.Context, ih bittorrent.InfoHash, forSeeder bool, numWant int, v6 bool,
594+
) ([]bittorrent.Peer, error) {
548595
logger.Trace().
549596
Stringer("infoHash", ih).
550597
Bool("forSeeder", forSeeder).
@@ -720,7 +767,8 @@ func (ps *store) gc(cutoff time.Time) {
720767
for _, infoHashKey := range infoHashKeys {
721768
var cntKey string
722769
var seeder bool
723-
if seeder = strings.HasPrefix(infoHashKey, IH4SeederKey) || strings.HasPrefix(infoHashKey, IH6SeederKey); seeder {
770+
if seeder = strings.HasPrefix(infoHashKey, IH4SeederKey) || strings.HasPrefix(infoHashKey,
771+
IH6SeederKey); seeder {
724772
cntKey = CountSeederKey
725773
} else if strings.HasPrefix(infoHashKey, IH4LeecherKey) || strings.HasPrefix(infoHashKey, IH6LeecherKey) {
726774
cntKey = CountLeecherKey

0 commit comments

Comments
 (0)