forked from seatgeek/resec
/
new.go
79 lines (71 loc) · 1.94 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package redis
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"time"
"github.com/go-redis/redis"
"github.com/jpillora/backoff"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/nirahapp/resec/resec/state"
)
func NewConnection(m *cli.Context) (*Manager, error) {
redisConfig := &Config{
Address: m.String("redis-addr"),
}
var redisClient *redis.Client
if m.Bool("tls") {
cert, err := tls.LoadX509KeyPair(m.Path("tls-cert-file"), m.Path("tls-key-file"))
if err != nil {
return nil, fmt.Errorf("failed to load TLS key pair: %s", err)
}
caCert, err := os.ReadFile(m.Path("tls-ca-cert-file"))
if err != nil {
return nil, fmt.Errorf("failed to read CA certificate: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: m.Bool("tls-insecure-skip-verify"),
}
redisClient = redis.NewClient(&redis.Options{
Addr: redisConfig.Address,
DialTimeout: 1 * time.Second,
Password: m.String("redis-password"),
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
TLSConfig: tlsConfig,
})
} else {
redisClient = redis.NewClient(&redis.Options{
Addr: redisConfig.Address,
DialTimeout: 1 * time.Second,
Password: m.String("redis-password"),
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
})
}
instance := &Manager{
client: redisClient,
config: redisConfig,
logger: log.WithFields(log.Fields{
"system": "redis",
"redis_addr": m.String("redis-addr"),
}),
state: &state.Redis{},
stateCh: make(chan state.Redis, 10),
commandCh: make(chan Command, 10),
stopCh: make(chan interface{}, 1),
backoff: &backoff.Backoff{
Min: 50 * time.Millisecond,
Max: 10 * time.Second,
Factor: 1.5,
Jitter: false,
},
}
return instance, nil
}