diff --git a/srv/internal/resource/database.go b/srv/internal/resource/database.go new file mode 100644 index 0000000..958e354 --- /dev/null +++ b/srv/internal/resource/database.go @@ -0,0 +1 @@ +package resource diff --git a/srv/internal/resource/init.go b/srv/internal/resource/init.go new file mode 100644 index 0000000..fd0b112 --- /dev/null +++ b/srv/internal/resource/init.go @@ -0,0 +1,7 @@ +package resource + +var cfg *Cfg + +func init() { + cfg = getCfg() +} diff --git a/srv/internal/parse/index.go b/srv/internal/resource/parse.go similarity index 84% rename from srv/internal/parse/index.go rename to srv/internal/resource/parse.go index 324ccbb..0b07ea1 100644 --- a/srv/internal/parse/index.go +++ b/srv/internal/resource/parse.go @@ -1,4 +1,4 @@ -package parse +package resource import ( "fmt" @@ -7,13 +7,14 @@ import ( "strings" "Crimson-Gatekeeper/internal/utils" + "gopkg.in/yaml.v3" ) type Cfg struct { server struct { port string - database struct { + database *struct { host string user string dbname string @@ -23,9 +24,9 @@ type Cfg struct { connectTimeout int sshPoint string } - sshPoint map[string]struct { + sshPoint map[string]*struct { host string - port string + port int user string passwd string keyPath string @@ -33,7 +34,7 @@ type Cfg struct { } } -func GetCfg() *Cfg { +func getCfg() *Cfg { pwd, err := os.Getwd() utils.PaincEro(err, "获取当前工作目录失败") dirs, err := os.ReadDir(pwd) @@ -55,5 +56,10 @@ func GetCfg() *Cfg { 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 } diff --git a/srv/internal/resource/ssh.go b/srv/internal/resource/ssh.go new file mode 100644 index 0000000..7407430 --- /dev/null +++ b/srv/internal/resource/ssh.go @@ -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 +}