重新整理思路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()
|
||||
}
|
||||
65
srv/internal/resource/parse.go
Normal file
65
srv/internal/resource/parse.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"Crimson-Gatekeeper/internal/utils"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
server struct {
|
||||
port string
|
||||
database *struct {
|
||||
host string
|
||||
user string
|
||||
dbname string
|
||||
sslmode string
|
||||
port string
|
||||
passwd string
|
||||
connectTimeout int
|
||||
sshPoint string
|
||||
}
|
||||
sshPoint map[string]*struct {
|
||||
host string
|
||||
port int
|
||||
user string
|
||||
passwd string
|
||||
keyPath string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getCfg() *Cfg {
|
||||
pwd, err := os.Getwd()
|
||||
utils.PaincEro(err, "获取当前工作目录失败")
|
||||
dirs, err := os.ReadDir(pwd)
|
||||
utils.PaincEro(err, "获取目录文件失败")
|
||||
var data []byte
|
||||
for _, item := range dirs {
|
||||
isyaml := strings.HasSuffix(item.Name(), ".yaml")
|
||||
if isyaml {
|
||||
filePath := path.Join(pwd, item.Name())
|
||||
data, err = os.ReadFile(filePath)
|
||||
utils.PaincEro(err, "读取配置文件失败")
|
||||
break
|
||||
}
|
||||
}
|
||||
if data == nil {
|
||||
fmt.Println("没有找到配置文件,启动失败")
|
||||
panic("没有找到配置文件,进程终止")
|
||||
}
|
||||
cfg := &Cfg{}
|
||||
err = yaml.Unmarshal(data, cfg)
|
||||
utils.PaincEro(err, "解析配置文件失败")
|
||||
for _, item := range cfg.server.sshPoint {
|
||||
if item.port == 0 {
|
||||
item.port = 22
|
||||
}
|
||||
}
|
||||
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