2023-06-27 15:53:29 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-15 02:27:23 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2023-06-27 15:53:29 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/redis/go-redis/v9"
|
2023-09-28 01:41:18 +08:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"net"
|
|
|
|
"os"
|
2023-06-27 15:53:29 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
. "tinyrdm/backend/storage"
|
|
|
|
"tinyrdm/backend/types"
|
|
|
|
)
|
|
|
|
|
2023-07-16 01:50:01 +08:00
|
|
|
type cmdHistoryItem struct {
|
2023-07-18 23:43:31 +08:00
|
|
|
Timestamp int64 `json:"timestamp"`
|
2023-07-16 01:50:01 +08:00
|
|
|
Server string `json:"server"`
|
|
|
|
Cmd string `json:"cmd"`
|
2023-07-18 23:43:31 +08:00
|
|
|
Cost int64 `json:"cost"`
|
2023-07-16 01:50:01 +08:00
|
|
|
}
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
type connectionService struct {
|
2023-11-05 11:57:52 +08:00
|
|
|
ctx context.Context
|
|
|
|
conns *ConnectionsStorage
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var connection *connectionService
|
|
|
|
var onceConnection sync.Once
|
|
|
|
|
|
|
|
func Connection() *connectionService {
|
|
|
|
if connection == nil {
|
|
|
|
onceConnection.Do(func() {
|
|
|
|
connection = &connectionService{
|
2023-11-05 11:57:52 +08:00
|
|
|
conns: NewConnections(),
|
2023-06-27 15:53:29 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connectionService) Start(ctx context.Context) {
|
|
|
|
c.ctx = ctx
|
|
|
|
}
|
|
|
|
|
2023-10-08 15:24:08 +08:00
|
|
|
func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.Options, error) {
|
2023-09-28 01:41:18 +08:00
|
|
|
var sshClient *ssh.Client
|
|
|
|
if config.SSH.Enable {
|
|
|
|
sshConfig := &ssh.ClientConfig{
|
|
|
|
User: config.SSH.Username,
|
|
|
|
Auth: []ssh.AuthMethod{ssh.Password(config.SSH.Password)},
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
Timeout: time.Duration(config.ConnTimeout) * time.Second,
|
|
|
|
}
|
|
|
|
switch config.SSH.LoginType {
|
|
|
|
case "pwd":
|
|
|
|
sshConfig.Auth = []ssh.AuthMethod{ssh.Password(config.SSH.Password)}
|
|
|
|
case "pkfile":
|
|
|
|
key, err := os.ReadFile(config.SSH.PKFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var signer ssh.Signer
|
|
|
|
if len(config.SSH.Passphrase) > 0 {
|
|
|
|
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(config.SSH.Passphrase))
|
|
|
|
} else {
|
|
|
|
signer, err = ssh.ParsePrivateKey(key)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sshConfig.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid login type")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
sshClient, err = ssh.Dial("tcp", fmt.Sprintf("%s:%d", config.SSH.Addr, config.SSH.Port), sshConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 02:27:23 +08:00
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if config.SSL.Enable {
|
|
|
|
// setup tls config
|
|
|
|
var certs []tls.Certificate
|
|
|
|
if len(config.SSL.CertFile) > 0 && len(config.SSL.KeyFile) > 0 {
|
|
|
|
if cert, err := tls.LoadX509KeyPair(config.SSL.CertFile, config.SSL.KeyFile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
certs = []tls.Certificate{cert}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var caCertPool *x509.CertPool
|
|
|
|
if len(config.SSL.CAFile) > 0 {
|
|
|
|
ca, err := os.ReadFile(config.SSL.CAFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
caCertPool = x509.NewCertPool()
|
|
|
|
caCertPool.AppendCertsFromPEM(ca)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(certs) <= 0 {
|
|
|
|
return nil, errors.New("tls config error")
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
RootCAs: caCertPool,
|
|
|
|
InsecureSkipVerify: false,
|
|
|
|
Certificates: certs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 01:41:18 +08:00
|
|
|
option := &redis.Options{
|
|
|
|
Addr: fmt.Sprintf("%s:%d", config.Addr, config.Port),
|
|
|
|
Username: config.Username,
|
|
|
|
Password: config.Password,
|
|
|
|
DialTimeout: time.Duration(config.ConnTimeout) * time.Second,
|
|
|
|
ReadTimeout: time.Duration(config.ExecTimeout) * time.Second,
|
|
|
|
WriteTimeout: time.Duration(config.ExecTimeout) * time.Second,
|
2023-10-15 02:27:23 +08:00
|
|
|
TLSConfig: tlsConfig,
|
2023-09-28 01:41:18 +08:00
|
|
|
}
|
|
|
|
if sshClient != nil {
|
|
|
|
option.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return sshClient.Dial(network, addr)
|
|
|
|
}
|
|
|
|
option.ReadTimeout = -2
|
|
|
|
option.WriteTimeout = -2
|
|
|
|
}
|
2023-10-08 15:24:08 +08:00
|
|
|
return option, nil
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:26:47 +08:00
|
|
|
func (c *connectionService) createRedisClient(config types.ConnectionConfig) (redis.UniversalClient, error) {
|
2023-10-08 15:24:08 +08:00
|
|
|
option, err := c.buildOption(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-08 01:31:54 +08:00
|
|
|
|
|
|
|
if config.Sentinel.Enable {
|
2023-10-14 21:26:47 +08:00
|
|
|
// get master address via sentinel node
|
2023-10-08 01:31:54 +08:00
|
|
|
sentinel := redis.NewSentinelClient(option)
|
2023-10-08 15:24:08 +08:00
|
|
|
defer sentinel.Close()
|
|
|
|
|
|
|
|
var addr []string
|
|
|
|
addr, err = sentinel.GetMasterAddrByName(c.ctx, config.Sentinel.Master).Result()
|
2023-10-08 01:31:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(addr) < 2 {
|
|
|
|
return nil, errors.New("cannot get master address")
|
|
|
|
}
|
|
|
|
option.Addr = fmt.Sprintf("%s:%s", addr[0], addr[1])
|
|
|
|
option.Username = config.Sentinel.Username
|
|
|
|
option.Password = config.Sentinel.Password
|
|
|
|
}
|
|
|
|
|
2023-09-28 01:41:18 +08:00
|
|
|
rdb := redis.NewClient(option)
|
2023-10-14 21:26:47 +08:00
|
|
|
if config.Cluster.Enable {
|
|
|
|
// connect to cluster
|
|
|
|
var slots []redis.ClusterSlot
|
|
|
|
if slots, err = rdb.ClusterSlots(c.ctx).Result(); err == nil {
|
|
|
|
clusterOptions := &redis.ClusterOptions{
|
|
|
|
//NewClient: nil,
|
|
|
|
//MaxRedirects: 0,
|
|
|
|
//RouteByLatency: false,
|
|
|
|
//RouteRandomly: false,
|
|
|
|
//ClusterSlots: nil,
|
|
|
|
Dialer: option.Dialer,
|
|
|
|
OnConnect: option.OnConnect,
|
|
|
|
Protocol: option.Protocol,
|
|
|
|
Username: option.Username,
|
|
|
|
Password: option.Password,
|
|
|
|
MaxRetries: option.MaxRetries,
|
|
|
|
MinRetryBackoff: option.MinRetryBackoff,
|
|
|
|
MaxRetryBackoff: option.MaxRetryBackoff,
|
|
|
|
DialTimeout: option.DialTimeout,
|
|
|
|
ContextTimeoutEnabled: option.ContextTimeoutEnabled,
|
|
|
|
PoolFIFO: option.PoolFIFO,
|
|
|
|
PoolSize: option.PoolSize,
|
|
|
|
PoolTimeout: option.PoolTimeout,
|
|
|
|
MinIdleConns: option.MinIdleConns,
|
|
|
|
MaxIdleConns: option.MaxIdleConns,
|
|
|
|
ConnMaxIdleTime: option.ConnMaxIdleTime,
|
|
|
|
ConnMaxLifetime: option.ConnMaxLifetime,
|
|
|
|
TLSConfig: option.TLSConfig,
|
|
|
|
DisableIndentity: option.DisableIndentity,
|
|
|
|
}
|
|
|
|
if option.Dialer != nil {
|
|
|
|
clusterOptions.Dialer = option.Dialer
|
|
|
|
clusterOptions.ReadTimeout = -2
|
|
|
|
clusterOptions.WriteTimeout = -2
|
|
|
|
}
|
|
|
|
var addrs []string
|
|
|
|
for _, slot := range slots {
|
|
|
|
for _, node := range slot.Nodes {
|
|
|
|
addrs = append(addrs, node.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clusterOptions.Addrs = addrs
|
|
|
|
clusterClient := redis.NewClusterClient(clusterOptions)
|
|
|
|
return clusterClient, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 01:41:18 +08:00
|
|
|
return rdb, nil
|
|
|
|
}
|
|
|
|
|
2023-10-08 15:24:08 +08:00
|
|
|
// ListSentinelMasters list all master info by sentinel
|
|
|
|
func (c *connectionService) ListSentinelMasters(config types.ConnectionConfig) (resp types.JSResp) {
|
|
|
|
option, err := c.buildOption(config)
|
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if option.DialTimeout > 0 {
|
|
|
|
option.DialTimeout = 10 * time.Second
|
|
|
|
}
|
|
|
|
sentinel := redis.NewSentinelClient(option)
|
|
|
|
defer sentinel.Close()
|
|
|
|
|
|
|
|
var retInfo []map[string]string
|
|
|
|
masterInfos, err := sentinel.Masters(c.ctx).Result()
|
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, info := range masterInfos {
|
|
|
|
if infoMap, ok := info.(map[any]any); ok {
|
|
|
|
retInfo = append(retInfo, map[string]string{
|
|
|
|
"name": infoMap["name"].(string),
|
|
|
|
"addr": fmt.Sprintf("%s:%s", infoMap["ip"].(string), infoMap["port"].(string)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Data = retInfo
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-28 01:41:18 +08:00
|
|
|
func (c *connectionService) TestConnection(config types.ConnectionConfig) (resp types.JSResp) {
|
2023-10-14 21:26:47 +08:00
|
|
|
client, err := c.createRedisClient(config)
|
2023-09-28 01:41:18 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 21:26:47 +08:00
|
|
|
defer client.Close()
|
2023-10-08 15:24:08 +08:00
|
|
|
|
2023-10-14 21:26:47 +08:00
|
|
|
if _, err = client.Ping(c.ctx).Result(); err != nil && err != redis.Nil {
|
2023-06-27 15:53:29 +08:00
|
|
|
resp.Msg = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Success = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListConnection list all saved connection in local profile
|
|
|
|
func (c *connectionService) ListConnection() (resp types.JSResp) {
|
|
|
|
resp.Success = true
|
|
|
|
resp.Data = c.conns.GetConnections()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-26 00:03:43 +08:00
|
|
|
func (c *connectionService) getConnection(name string) *types.Connection {
|
|
|
|
return c.conns.GetConnection(name)
|
|
|
|
}
|
|
|
|
|
2023-07-02 03:25:57 +08:00
|
|
|
// GetConnection get connection profile by name
|
|
|
|
func (c *connectionService) GetConnection(name string) (resp types.JSResp) {
|
2023-10-26 00:03:43 +08:00
|
|
|
conn := c.getConnection(name)
|
2023-07-02 03:25:57 +08:00
|
|
|
resp.Success = conn != nil
|
|
|
|
resp.Data = conn
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 15:53:29 +08:00
|
|
|
// SaveConnection save connection config to local profile
|
2023-07-02 16:52:07 +08:00
|
|
|
func (c *connectionService) SaveConnection(name string, param types.ConnectionConfig) (resp types.JSResp) {
|
2023-07-02 03:25:57 +08:00
|
|
|
var err error
|
2023-08-25 01:11:34 +08:00
|
|
|
if strings.ContainsAny(param.Name, "/") {
|
|
|
|
err = errors.New("connection name contains illegal characters")
|
2023-07-02 03:25:57 +08:00
|
|
|
} else {
|
2023-08-25 01:11:34 +08:00
|
|
|
if len(name) > 0 {
|
|
|
|
// update connection
|
|
|
|
err = c.conns.UpdateConnection(name, param)
|
|
|
|
} else {
|
|
|
|
err = c.conns.CreateConnection(param)
|
|
|
|
}
|
2023-07-02 03:25:57 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-06-27 15:53:29 +08:00
|
|
|
resp.Msg = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Success = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-06 01:22:14 +08:00
|
|
|
// DeleteConnection remove connection by name
|
|
|
|
func (c *connectionService) DeleteConnection(name string) (resp types.JSResp) {
|
|
|
|
err := c.conns.DeleteConnection(name)
|
2023-07-02 03:25:57 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-02 22:25:23 +08:00
|
|
|
// SaveSortedConnection save sorted connection after drag
|
|
|
|
func (c *connectionService) SaveSortedConnection(sortedConns types.Connections) (resp types.JSResp) {
|
|
|
|
err := c.conns.SaveSortedConnection(sortedConns)
|
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:17:59 +08:00
|
|
|
// CreateGroup create a new group
|
2023-07-02 16:52:07 +08:00
|
|
|
func (c *connectionService) CreateGroup(name string) (resp types.JSResp) {
|
|
|
|
err := c.conns.CreateGroup(name)
|
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameGroup rename group
|
|
|
|
func (c *connectionService) RenameGroup(name, newName string) (resp types.JSResp) {
|
|
|
|
err := c.conns.RenameGroup(name, newName)
|
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-14 16:17:59 +08:00
|
|
|
// DeleteGroup remove a group by name
|
2023-07-06 01:22:14 +08:00
|
|
|
func (c *connectionService) DeleteGroup(name string, includeConn bool) (resp types.JSResp) {
|
|
|
|
err := c.conns.DeleteGroup(name, includeConn)
|
2023-07-02 16:52:07 +08:00
|
|
|
if err != nil {
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
return
|
|
|
|
}
|