代码拉取完成,页面将自动刷新
package helper
import (
"context"
"encoding/json"
"errors"
"gitee.com/up-zero/redis-desktop-client/internal/define"
"github.com/go-redis/redis/v8"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"os"
"os/user"
"time"
)
func GetConnection(identity string) (*define.Connection, error) {
conf := new(define.Config)
nowPath := GetConfPath()
data, err := ioutil.ReadFile(nowPath + string(os.PathSeparator) + define.ConfigName)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, conf)
if err != nil {
return nil, err
}
for _, v := range conf.Connections {
if v.Identity == identity {
return v, nil
}
}
return nil, errors.New("连接数据不存在")
}
// GetConfPath 获取配置文件路径
func GetConfPath() string {
current, _ := user.Current()
return current.HomeDir
}
func getSSHClient(username, password, addr string) (*ssh.Client, error) {
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 15 * time.Second,
}
return ssh.Dial("tcp", addr, config)
}
func getRedisConn(username, password, addr, redisAddr string) (net.Conn, error) {
client, err := getSSHClient(username, password, addr)
if err != nil {
return nil, err
}
return client.Dial("tcp", redisAddr)
}
// GetRedisClient 获取 Redis 客户端对象
//
// connectionIdentity 连接唯一标识
// db 选中的数据库
func GetRedisClient(connectionIdentity string, db int) (*redis.Client, error) {
conn, err := GetConnection(connectionIdentity)
if err != nil {
return nil, err
}
redisOption := &redis.Options{
Addr: net.JoinHostPort(conn.Addr, conn.Port),
Username: conn.Username,
Password: conn.Password,
DB: db,
ReadTimeout: -1,
WriteTimeout: -1,
}
if conn.Type == "ssh" {
redisOption.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
return getRedisConn(conn.SSHUsername, conn.SSHPassword, conn.SSHAddr+":"+conn.SSHPort, addr)
}
}
rdb := redis.NewClient(redisOption)
return rdb, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。