From 2d69b3de590c287baec0445c8612e4635519dd73 Mon Sep 17 00:00:00 2001 From: xiaoyutab Date: Sun, 25 Jun 2023 10:27:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BB=A5=E4=B8=8B?= =?UTF-8?q?=E9=9C=80=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.完成需求:[I7FKIO](https://gitee.com/xiaoyutab/xgotool/issues/I7FKIO) 2. --- xgodb/config.go | 59 ++++++++++++++ xgodb/xgodbconfig/set.go | 113 ++++++++++++++++++++++++++ xgodb/xgodbconfig/xgodbconfig_test.go | 9 ++ 3 files changed, 181 insertions(+) create mode 100644 xgodb/xgodbconfig/set.go diff --git a/xgodb/config.go b/xgodb/config.go index 6c804d7..83362d3 100644 --- a/xgodb/config.go +++ b/xgodb/config.go @@ -26,3 +26,62 @@ func GetFloat(name string) float64 { func GetSelect(name string) string { return xgodbconfig.GetSelect(name) } + +// 设置int类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetInt(name string, val int64) error { + return xgodbconfig.SetInt(name, val) +} + +// 设置string类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetString(name, val string) error { + return xgodbconfig.SetString(name, val) +} + +// 设置float类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetFloat(name string, val float64) error { + return xgodbconfig.SetFloat(name, val) +} + +// 设置bool类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetBool(name string, val bool) error { + return xgodbconfig.SetBool(name, val) +} + +// 设置select类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetSelect(name, val string) error { + return xgodbconfig.SetSelect(name, val) +} + +// 设置text类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetText(name, val string) error { + return xgodbconfig.SetText(name, val) +} + +// 设置longtext类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetLongText(name, val string) error { + return xgodbconfig.SetLongText(name, val) +} diff --git a/xgodb/xgodbconfig/set.go b/xgodb/xgodbconfig/set.go new file mode 100644 index 0000000..04702f1 --- /dev/null +++ b/xgodb/xgodbconfig/set.go @@ -0,0 +1,113 @@ +package xgodbconfig + +import ( + "errors" + "time" + + "gitee.com/xiaoyutab/xgotool" +) + +// 设置int类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetInt(name string, val int64) error { + conf := Getconfigure(name) + conf.ValueInt = val + conf.Types = "int" + return setconfigure(conf) +} + +// 设置string类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetString(name, val string) error { + conf := Getconfigure(name) + conf.ValueString = val + conf.Types = "string" + return setconfigure(conf) +} + +// 设置float类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetFloat(name string, val float64) error { + conf := Getconfigure(name) + conf.ValueFloat = val + conf.Types = "float" + return setconfigure(conf) +} + +// 设置bool类型配置 +// +// name 配置项的名称 +// val 配置项的值 +func SetBool(name string, val bool) error { + conf := Getconfigure(name) + if val { + conf.ValueBool = 1 + } else { + conf.ValueBool = 0 + } + conf.Types = "bool" + return setconfigure(conf) +} + +// 设置select类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetSelect(name, val string) error { + conf := Getconfigure(name) + conf.ValueString = val + conf.Types = "select" + return setconfigure(conf) +} + +// 设置text类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetText(name, val string) error { + conf := Getconfigure(name) + conf.ValueString = val + conf.Types = "text" + return setconfigure(conf) +} + +// 设置longtext类型配置 +// 下拉列表,多项使用,分割key和key_cn使用:分割 +// +// name 配置项的名称 +// val 配置项的值 +func SetLongText(name, val string) error { + conf := Getconfigure(name) + conf.ValueString = val + conf.Types = "longtext" + return setconfigure(conf) +} + +// 设置配置项 +func setconfigure(conf Configure) error { + // 如果配置项为空,则直接返回成功 + if conf.NameKey == "" { + return nil + } + if conf.Id <= 0 { + return errors.New("配置项【" + conf.NameKey + "】不存在") + } + conf.UpdatedAt = time.Now().Format(xgotool.DATETIMES) + err := DefaultConfig.DB.Table("configure").Where("name_key", conf.NameKey).Save(&conf).Error + if err != nil { + return err + } + // 修改缓存 + _cache_config_look.Lock() + _cache_config[conf.NameKey] = conf + _cache_config_look.Unlock() + return nil +} diff --git a/xgodb/xgodbconfig/xgodbconfig_test.go b/xgodb/xgodbconfig/xgodbconfig_test.go index 9260069..b265042 100644 --- a/xgodb/xgodbconfig/xgodbconfig_test.go +++ b/xgodb/xgodbconfig/xgodbconfig_test.go @@ -8,9 +8,18 @@ import ( "gorm.io/gorm" ) +// 读取配置项 func TestConfig(t *testing.T) { db, _ := gorm.Open(mysql.Open("admin:admin@tcp(localhost:3306)/self_xiaoyutab?charset=utf8"), &gorm.Config{}) xgodbconfig.DefaultConfig.DB = db // 注册数据库 // 获取配置项 t.Log(xgodbconfig.GetBool("FILE_UPLOAD_IMAGE_STATUS")) } + +// 设置配置项 +func TestConfigSet(t *testing.T) { + db, _ := gorm.Open(mysql.Open("admin:admin@tcp(localhost:3306)/self_xiaoyutab?charset=utf8"), &gorm.Config{}) + xgodbconfig.DefaultConfig.DB = db // 注册数据库 + // 设置配置项 + t.Log(xgodbconfig.SetBool("FILE_UPLOAD_IMAGE_STATUS", false)) +} -- Gitee From be600c65ca8a063aa25e0335cde3d5c125324421 Mon Sep 17 00:00:00 2001 From: xiaoyutab Date: Sun, 25 Jun 2023 11:25:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0SSH=E8=AF=81=E4=B9=A6?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=92=8C=E5=91=BD=E4=BB=A4=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 ++ ssh.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ xgo_test.go | 14 ++++++++ 3 files changed, 113 insertions(+) create mode 100644 ssh.go diff --git a/go.mod b/go.mod index 745e2fe..7a45080 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/mssola/user_agent v0.6.0 + golang.org/x/crypto v0.10.0 gorm.io/driver/mysql v1.5.1 gorm.io/gorm v1.25.1 ) @@ -12,4 +13,5 @@ require ( github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + golang.org/x/sys v0.9.0 // indirect ) diff --git a/ssh.go b/ssh.go new file mode 100644 index 0000000..82f3163 --- /dev/null +++ b/ssh.go @@ -0,0 +1,97 @@ +package xgotool + +import ( + "errors" + "fmt" + "io/ioutil" + "log" + "net" + "strings" + "time" + + "golang.org/x/crypto/ssh" +) + +// 连接的配置 +type ClientConfig struct { + Host string //ip + Port int // 端口 + Username string //用户名 + Password string //密码 + IsPK bool // 是否是证书【如果是证书的话,password项填写证书位置】 + Client *ssh.Client //ssh client +} + +// 创建连接 +func sshClient(cliConf *ClientConfig) error { + if cliConf.Username == "" { + return errors.New("SSH: 登陆账号不允许为空") + } + if cliConf.Password == "" { + return errors.New("SSH: 登陆密码不允许为空") + } + if strings.LastIndex(cliConf.Username, "*") > 0 { + return errors.New("SSH: 登陆账号不允许存在 *") + } + if cliConf.Port == 0 { + cliConf.Port = 22 + } + config := ssh.ClientConfig{ + User: cliConf.Username, + Auth: []ssh.AuthMethod{ssh.Password(cliConf.Password)}, + HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { + return nil + }, + Timeout: 60 * time.Second, // 超时时间设置为60秒 + } + if cliConf.IsPK { + config.Auth = []ssh.AuthMethod{publicKeyAuthFunc(cliConf.Password)} + } + addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port) + + //获取client + client, err := ssh.Dial("tcp", addr, &config) + if err != nil { + return err + } + cliConf.Client = client + return nil +} + +// 运行Shell并获取输出内容 +// +// shell 待运行的命令 +func SshExec(cliConf *ClientConfig, shell string) (string, error) { + if cliConf.Client == nil { + err := sshClient(cliConf) + if err != nil { + return "", err + } + } + //获取session,这个session是用来远程执行操作的 + session, err := cliConf.Client.NewSession() + if err != nil { + return "", err + } + // 执行命令 + output, err := session.CombinedOutput(shell) + if err != nil { + return "", err + } + return string(output), nil +} + +// 根据KPATH进行证书签名 +// +// kPath 私钥证书路径 +func publicKeyAuthFunc(kPath string) ssh.AuthMethod { + key, err := ioutil.ReadFile(kPath) + if err != nil { + log.Fatal("ssh 密钥文件读取失败", err) + } + signer, err := ssh.ParsePrivateKey(key) + if err != nil { + log.Fatal("ssh 关键签名失败", err) + } + return ssh.PublicKeys(signer) +} diff --git a/xgo_test.go b/xgo_test.go index c917257..61659f0 100644 --- a/xgo_test.go +++ b/xgo_test.go @@ -42,3 +42,17 @@ func TestUa(t *testing.T) { Mobile t.Log(pc) } + +// 测试SSH命令执行 +func TestSshExec(t *testing.T) { + cmd, err := xgotool.SshExec(&xgotool.ClientConfig{ + Host: "api1.xinfushenghuo.cn", + Username: "public_user", + Password: "/home/xiaoyutab/.ssh/id_rsa", + IsPK: true, + }, "uname -a") + if err != nil { + t.Error(err) + } + t.Log(cmd) +} -- Gitee