170 lines
3.6 KiB
Go
170 lines
3.6 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type SshConfig struct {
|
|
Name string `yaml:"name"`
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
Username string `yaml:"username"`
|
|
Password string `yaml:"password"`
|
|
PrivateKeyPath string
|
|
}
|
|
|
|
type MysqlConfig struct {
|
|
Name string `yaml:"name"`
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
Username string `yaml:"user"`
|
|
Password string `yaml:"password"`
|
|
// Database string `yaml:"database"`
|
|
}
|
|
|
|
type SystemConfig struct {
|
|
NMap bool `yaml:"nmap"` // 是否开启端口扫描
|
|
FeishUrl string `yaml:"feishu_url"` // 飞书机器人url
|
|
NoticeUrl string `yaml:"notice_url"` // 通知url
|
|
OperationUrl string `yaml:"operation_url"` // 运营url
|
|
OperationChatId string `yaml:"operation_chat_id"` // 运营群id
|
|
USOperationChatId string `yaml:"us_operation_chat_id"` // US运营群id
|
|
ClientChatId string `yaml:"client_chat_id"` // 客户端群id
|
|
Ssh bool `yaml:"ssh"` // 是否开启SSH连接
|
|
Env string `yaml:"env"` // 环境变量
|
|
RpcSwitch bool `yaml:"rpc_switch"` // 是否开启RPC调用
|
|
}
|
|
|
|
type Config struct {
|
|
Servers []SshConfig `yaml:"servers"`
|
|
Mysqls []MysqlConfig `yaml:"mysqls"`
|
|
System SystemConfig `yaml:"system"`
|
|
}
|
|
|
|
type TranlaterUser struct {
|
|
Account string `yaml:"account"`
|
|
Keys string `yaml:"keys"`
|
|
KeysType string `yaml:"keysType"`
|
|
Colunm string `yaml:"colunm"`
|
|
}
|
|
|
|
type TranlaterConfig struct {
|
|
User []TranlaterUser `yaml:"user"`
|
|
}
|
|
|
|
type LanguageLimit struct {
|
|
Key string `json:"key"`
|
|
Zh_CN int `json:"zh_CN"`
|
|
Latin int `json:"latin"`
|
|
}
|
|
|
|
var config *Config
|
|
var tranlaterConfig *TranlaterConfig
|
|
|
|
func Init() {
|
|
err := loadConfig()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
}
|
|
|
|
func InitTranlater() error {
|
|
data, err := os.ReadFile("conf/translater.yml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = yaml.Unmarshal(data, &tranlaterConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func loadConfig() error {
|
|
data, err := os.ReadFile("conf/server.yml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = yaml.Unmarshal(data, &config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetServerConfig(name string) (*SshConfig, error) {
|
|
for _, server := range config.Servers {
|
|
if server.Name == name {
|
|
return &server, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("server configuration not found: %s", name)
|
|
}
|
|
|
|
func GetMysqlConfig(name string) (*MysqlConfig, error) {
|
|
for _, mysql := range config.Mysqls {
|
|
if mysql.Name == name {
|
|
return &mysql, nil
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("mysql configuration not found: %s", name)
|
|
}
|
|
|
|
func GetWsConfig(name string) (*SshConfig, error) {
|
|
return GetServerConfig("ws")
|
|
}
|
|
|
|
func GetFeishuUrl() string {
|
|
return config.System.FeishUrl
|
|
}
|
|
|
|
func GetNoticeUrl() string {
|
|
return config.System.NoticeUrl
|
|
}
|
|
|
|
func GetOperationUrl() string {
|
|
return config.System.OperationUrl
|
|
}
|
|
|
|
func GetNMap() bool {
|
|
return config.System.NMap
|
|
}
|
|
|
|
func GetOperationChatId() string {
|
|
return config.System.OperationChatId
|
|
}
|
|
|
|
func GetUSOperationChatId() string {
|
|
return config.System.USOperationChatId
|
|
}
|
|
|
|
func GetClientChatId() string {
|
|
return config.System.ClientChatId
|
|
}
|
|
|
|
func GetSsh() bool {
|
|
return config.System.Ssh
|
|
}
|
|
|
|
func GetEnv() string {
|
|
return config.System.Env
|
|
}
|
|
|
|
func GetRpcSwitch() bool {
|
|
return config.System.RpcSwitch
|
|
}
|
|
|
|
func GetTranlaterConfig(account string) *TranlaterUser {
|
|
for _, user := range tranlaterConfig.User {
|
|
if user.Account == account {
|
|
return &user
|
|
}
|
|
}
|
|
return nil
|
|
}
|