Skip to content

Commit 5f6f0fe

Browse files
committed
integrate redis caches
1 parent 735a7c5 commit 5f6f0fe

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

main.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ type proxyArg struct {
177177
value string
178178
}
179179

180+
type cacheKind int
181+
182+
const (
183+
cacheKindDir cacheKind = iota
184+
cacheKindRedis
185+
cacheKindRedisCluster
186+
)
187+
188+
type autocertCache struct {
189+
kind cacheKind
190+
value string
191+
}
192+
180193
type CLIArgs struct {
181194
bindAddress string
182195
bindReusePort bool
@@ -190,7 +203,8 @@ type CLIArgs struct {
190203
showVersion bool
191204
autocert bool
192205
autocertWhitelist CSVArg
193-
autocertDir string
206+
autocertCache autocertCache
207+
autocertCacheRedisPrefix string
194208
autocertACME string
195209
autocertEmail string
196210
autocertHTTP string
@@ -234,6 +248,10 @@ func parse_args() CLIArgs {
234248
netip.MustParsePrefix("::/128"),
235249
netip.MustParsePrefix("fe80::/10"),
236250
},
251+
autocertCache: autocertCache{
252+
kind: cacheKindDir,
253+
value: filepath.Join(home, ".dumbproxy", "autocert"),
254+
},
237255
}
238256
flag.StringVar(&args.bindAddress, "bind-address", ":8080", "HTTP proxy listen address. Set empty value to use systemd socket activation.")
239257
flag.BoolVar(&args.bindReusePort, "bind-reuseport", false, "allow multiple server instances on the same port")
@@ -250,7 +268,28 @@ func parse_args() CLIArgs {
250268
flag.BoolVar(&args.showVersion, "version", false, "show program version and exit")
251269
flag.BoolVar(&args.autocert, "autocert", false, "issue TLS certificates automatically")
252270
flag.Var(&args.autocertWhitelist, "autocert-whitelist", "restrict autocert domains to this comma-separated list")
253-
flag.StringVar(&args.autocertDir, "autocert-dir", filepath.Join(home, ".dumbproxy", "autocert"), "path to autocert cache")
271+
flag.Func("autocert-dir", "use directory path for autocert cache", func(p string) error {
272+
args.autocertCache = autocertCache{
273+
kind: cacheKindDir,
274+
value: p,
275+
}
276+
return nil
277+
})
278+
flag.Func("autocert-cache-redis", "use Redis URL for autocert cache", func(p string) error {
279+
args.autocertCache = autocertCache{
280+
kind: cacheKindRedis,
281+
value: p,
282+
}
283+
return nil
284+
})
285+
flag.Func("autocert-cache-redis-cluster", "use Redis Cluster URL for autocert cache", func(p string) error {
286+
args.autocertCache = autocertCache{
287+
kind: cacheKindRedisCluster,
288+
value: p,
289+
}
290+
return nil
291+
})
292+
flag.StringVar(&args.autocertCacheRedisPrefix, "autocert-cache-redis-prefix", "", "prefix to use for keys in Redis or Redis Cluster cache")
254293
flag.StringVar(&args.autocertACME, "autocert-acme", autocert.DefaultACMEDirectory, "custom ACME endpoint")
255294
flag.StringVar(&args.autocertEmail, "autocert-email", "", "email used for ACME registration")
256295
flag.StringVar(&args.autocertHTTP, "autocert-http", "", "listen address for HTTP-01 challenges handler of ACME")
@@ -502,7 +541,24 @@ func run() int {
502541
}
503542
listener = tls.NewListener(listener, cfg)
504543
} else if args.autocert {
505-
var certCache autocert.Cache = autocert.DirCache(args.autocertDir)
544+
// cert caching chain
545+
var certCache autocert.Cache
546+
switch args.autocertCache.kind {
547+
case cacheKindDir:
548+
certCache = autocert.DirCache(args.autocertCache.value)
549+
case cacheKindRedis:
550+
certCache, err = certcache.RedisCacheFromURL(args.autocertCache.value, args.autocertCacheRedisPrefix)
551+
if err != nil {
552+
mainLogger.Critical("redis cache construction failed: %v", err)
553+
return 3
554+
}
555+
case cacheKindRedisCluster:
556+
certCache, err = certcache.RedisClusterCacheFromURL(args.autocertCache.value, args.autocertCacheRedisPrefix)
557+
if err != nil {
558+
mainLogger.Critical("redis cluster cache construction failed: %v", err)
559+
return 3
560+
}
561+
}
506562
if args.autocertLocalCacheTTL > 0 {
507563
lcc := certcache.NewLocalCertCache(
508564
certCache,
@@ -513,6 +569,7 @@ func run() int {
513569
defer lcc.Stop()
514570
certCache = lcc
515571
}
572+
516573
m := &autocert.Manager{
517574
Cache: certCache,
518575
Prompt: autocert.AcceptTOS,

0 commit comments

Comments
 (0)