-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
56 lines (44 loc) · 1.2 KB
/
client.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
package redis
import (
"crypto/tls"
"fmt"
"net"
)
type Client struct {
conn net.Conn
}
// Open a connection to the Redis server.
func NewClient(addr string, tlsConfig *tls.Config) (*Client, error) {
conn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
fmt.Println("Failed to connect:", err)
return nil, err
}
return &Client{conn: conn}, nil
}
// Open a connection to the Redis server with TLS/SSL already enabled.
func NewPreConfigClient(addr string) (*Client, error) {
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true})
if err != nil {
fmt.Println("Failed to connect:", err)
return nil, err
}
return &Client{conn: conn}, nil
}
// Send an authentication request to the Redis server.
func (c *Client) Auth(username, password string) (string, error) {
_, err := c.conn.Write([]byte(fmt.Sprintf("AUTH %s %s\r\n", username, password)))
if err != nil {
return "Failed to send AUTH command", err
}
response := make([]byte, 1024)
n, err := c.conn.Read(response)
if err != nil {
return "Failed to read response", err
}
return string(response[:n]), nil
}
// Close the connection to the Redis server.
func (c *Client) Close() error {
return c.conn.Close()
}