重新整理思路3
This commit is contained in:
1
srv/internal/resource/database.go
Normal file
1
srv/internal/resource/database.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package resource
|
||||||
7
srv/internal/resource/init.go
Normal file
7
srv/internal/resource/init.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package resource
|
||||||
|
|
||||||
|
var cfg *Cfg
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cfg = getCfg()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package parse
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -7,13 +7,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"Crimson-Gatekeeper/internal/utils"
|
"Crimson-Gatekeeper/internal/utils"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
server struct {
|
server struct {
|
||||||
port string
|
port string
|
||||||
database struct {
|
database *struct {
|
||||||
host string
|
host string
|
||||||
user string
|
user string
|
||||||
dbname string
|
dbname string
|
||||||
@@ -23,9 +24,9 @@ type Cfg struct {
|
|||||||
connectTimeout int
|
connectTimeout int
|
||||||
sshPoint string
|
sshPoint string
|
||||||
}
|
}
|
||||||
sshPoint map[string]struct {
|
sshPoint map[string]*struct {
|
||||||
host string
|
host string
|
||||||
port string
|
port int
|
||||||
user string
|
user string
|
||||||
passwd string
|
passwd string
|
||||||
keyPath string
|
keyPath string
|
||||||
@@ -33,7 +34,7 @@ type Cfg struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCfg() *Cfg {
|
func getCfg() *Cfg {
|
||||||
pwd, err := os.Getwd()
|
pwd, err := os.Getwd()
|
||||||
utils.PaincEro(err, "获取当前工作目录失败")
|
utils.PaincEro(err, "获取当前工作目录失败")
|
||||||
dirs, err := os.ReadDir(pwd)
|
dirs, err := os.ReadDir(pwd)
|
||||||
@@ -55,5 +56,10 @@ func GetCfg() *Cfg {
|
|||||||
cfg := &Cfg{}
|
cfg := &Cfg{}
|
||||||
err = yaml.Unmarshal(data, cfg)
|
err = yaml.Unmarshal(data, cfg)
|
||||||
utils.PaincEro(err, "解析配置文件失败")
|
utils.PaincEro(err, "解析配置文件失败")
|
||||||
|
for _, item := range cfg.server.sshPoint {
|
||||||
|
if item.port == 0 {
|
||||||
|
item.port = 22
|
||||||
|
}
|
||||||
|
}
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
25
srv/internal/resource/ssh.go
Normal file
25
srv/internal/resource/ssh.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Crimson-Gatekeeper/internal/utils"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getSshEnv(name string) *ssh.Client {
|
||||||
|
sshCfg := cfg.server.sshPoint[name]
|
||||||
|
if sshCfg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
clientCfg := &ssh.ClientConfig{
|
||||||
|
User: sshCfg.user,
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.Password(sshCfg.passwd),
|
||||||
|
},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
}
|
||||||
|
client, err := ssh.Dial("tcp", sshCfg.host+strconv.Itoa(sshCfg.port), clientCfg)
|
||||||
|
utils.PaincEro(err, "建立 SSH 隧道失败,程序中止")
|
||||||
|
return client
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user