版本更新
This commit is contained in:
parent
9bbd67f41a
commit
6e1419a06c
@ -32,6 +32,7 @@ type SystemConfig struct {
|
||||
OperationUrl string `yaml:"operation_url"` // 运营url
|
||||
OperationChatId string `yaml:"operation_chat_id"` // 运营群id
|
||||
ClientChatId string `yaml:"client_chat_id"` // 客户端群id
|
||||
Ssh bool `yaml:"ssh"` // 是否开启SSH连接
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@ -107,3 +108,7 @@ func GetOperationChatId() string {
|
||||
func GetClientChatId() string {
|
||||
return config.System.ClientChatId
|
||||
}
|
||||
|
||||
func GetSsh() bool {
|
||||
return config.System.Ssh
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ system:
|
||||
operation_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/64bad1f3-3a41-4dca-9037-399067ffb252'
|
||||
operation_chat_id: 'oc_f6e10a55f28f31e2a5677bdcb6aed599'
|
||||
client_chat_id: 'oc_f6e10a55f28f31e2a5677bdcb6aed599'
|
||||
ssh: false
|
||||
mysqls:
|
||||
- host: '127.0.0.1'
|
||||
name: 'merge_pet_test'
|
||||
@ -27,7 +28,7 @@ mysqls:
|
||||
password: 'GWFj1cHaqjpzvcsHBWTFtLWtm8MUZKROx_wvbV6jPg=='
|
||||
database: 'merge_pet_london_%d'
|
||||
idleTimeout: Infinity
|
||||
- host: '127.0.0.1'
|
||||
- host: '47.254.83.25'
|
||||
name: 'merge_pet_online'
|
||||
port: 3306
|
||||
user: 'root'
|
||||
|
||||
@ -2,31 +2,32 @@ system:
|
||||
nmap: true
|
||||
feishu_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/70e24a79-b019-434a-b4d1-4592bbf7c311'
|
||||
notice_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/48944500-477a-4647-a7e0-c56c43bee263'
|
||||
operation_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/e3122bc9-99ca-46b4-9634-862d3c8cdc7e'
|
||||
operation_chat_id: 'oc_967a93dcade6d55c3db434f23767a414'
|
||||
operation_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/81a6b9a3-0e67-481a-9446-d90eb8369fa4'
|
||||
operation_chat_id: 'oc_0badf343e1dbbe155c1ff8bfa340a60b'
|
||||
client_chat_id: 'oc_ea859b078fa5bf2fd0d482a98826c0df'
|
||||
mysqls:
|
||||
- host: '127.0.0.1'
|
||||
- host: '1.15.182.107'
|
||||
name: 'merge_pet_test'
|
||||
port: 3306
|
||||
user: 'root'
|
||||
password: 'GWFj1cHaqjpzvcsHBWTFtLWtm8MUZKROx_wvbV6jPg=='
|
||||
database: 'pet_home_%d'
|
||||
idleTimeout: Infinity
|
||||
- host: '127.0.0.1'
|
||||
- host: '1.15.182.107'
|
||||
name: 'merge_pet_sdk'
|
||||
port: 3306
|
||||
user: 'root'
|
||||
password: 'GWFj1cHaqjpzvcsHBWTFtLWtm8MUZKROx_wvbV6jPg=='
|
||||
database: 'Merge_Pet_sdk'
|
||||
idleTimeout: Infinity
|
||||
- host: '127.0.0.1'
|
||||
- host: '8.208.47.208'
|
||||
name: 'merge_pet_london'
|
||||
port: 3306
|
||||
user: 'root'
|
||||
password: 'GWFj1cHaqjpzvcsHBWTFtLWtm8MUZKROx_wvbV6jPg=='
|
||||
database: 'merge_pet_london_%d'
|
||||
idleTimeout: Infinity
|
||||
- host: '127.0.0.1'
|
||||
- host: '47.254.83.25'
|
||||
name: 'merge_pet_online'
|
||||
port: 3306
|
||||
user: 'root'
|
||||
|
||||
1208
log/backend.log
1208
log/backend.log
File diff suppressed because it is too large
Load Diff
@ -12,6 +12,7 @@ func GetUserList(AppId, ServerId, PageSize, CurrentPage int) ([]*User, int, erro
|
||||
return nil, 0, err
|
||||
}
|
||||
db := util.MPool.GetMysqlDB(App.ServerName, App.MysqlName, ServerId)
|
||||
defer db.Close()
|
||||
if db.DB == nil {
|
||||
return nil, 0, fmt.Errorf("failed to get MySQL database")
|
||||
}
|
||||
|
||||
BIN
release/backend
BIN
release/backend
Binary file not shown.
107
util/Mysql.go
107
util/Mysql.go
@ -29,6 +29,7 @@ type poolInfo struct {
|
||||
type Db struct {
|
||||
*sqlx.DB
|
||||
key string
|
||||
ssh *ssh.Client
|
||||
}
|
||||
|
||||
func (db *Db) Close() error {
|
||||
@ -47,6 +48,10 @@ func (m *MysqlPool) getDb(key string) *Db {
|
||||
db := pool.DbList[0]
|
||||
pool.DbList = pool.DbList[1:]
|
||||
if err := db.Ping(); err != nil {
|
||||
db.Close()
|
||||
if db.ssh != nil {
|
||||
db.ssh.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return db
|
||||
@ -79,13 +84,13 @@ func (m *MysqlPool) GetMysqlDB(Server, Mysql string, ServerId int) *Db {
|
||||
var SQLDb *sqlx.DB
|
||||
var err error
|
||||
|
||||
SQLDb, err = connectToMySQLViaSSH(Server, Mysql, ServerId)
|
||||
SQLDb, sshConn, err := connectToMySQLViaSSH(Server, Mysql, ServerId)
|
||||
if err != nil {
|
||||
log.Printf("failed to connect to mysql: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
ADb = &Db{SQLDb, key}
|
||||
ADb = &Db{SQLDb, key, sshConn}
|
||||
// m.putDb(key, Db)
|
||||
return ADb
|
||||
}
|
||||
@ -101,7 +106,7 @@ func (m *MysqlPool) GetGameDB() *Db {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
ADb = &Db{SQLDb, key}
|
||||
ADb = &Db{SQLDb, key, nil}
|
||||
// m.putDb(key, Db)
|
||||
return ADb
|
||||
}
|
||||
@ -117,7 +122,7 @@ func (m *MysqlPool) GetTopicDB(Topic string) *Db {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
ADb = &Db{SQLDb, key}
|
||||
ADb = &Db{SQLDb, key, nil}
|
||||
// m.putDb(key, Db)
|
||||
return ADb
|
||||
}
|
||||
@ -128,56 +133,72 @@ func init() {
|
||||
|
||||
var MPool *MysqlPool
|
||||
|
||||
func connectToMySQLViaSSH(ServerName, MysqlName string, ServerId int) (*sqlx.DB, error) {
|
||||
func connectToMySQLViaSSH(ServerName, MysqlName string, ServerId int) (*sqlx.DB, *ssh.Client, error) {
|
||||
SshConfig, err := common.GetServerConfig(ServerName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get SSH config: %v", err)
|
||||
return nil, nil, fmt.Errorf("failed to get SSH config: %v", err)
|
||||
}
|
||||
MysqlConfig, err := common.GetMysqlConfig(MysqlName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get MySQL config: %v", err)
|
||||
return nil, nil, fmt.Errorf("failed to get MySQL config: %v", err)
|
||||
}
|
||||
SP, _ := Decrypt(SshConfig.Password)
|
||||
// 创建 SSH 客户端配置
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: SshConfig.Username,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(SP),
|
||||
},
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
}
|
||||
if common.GetSsh() {
|
||||
// 创建 SSH 客户端配置
|
||||
sshConfig := &ssh.ClientConfig{
|
||||
User: SshConfig.Username,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(SP),
|
||||
},
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||
}
|
||||
|
||||
// 连接到 SSH 服务器
|
||||
sshConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", SshConfig.Host, SshConfig.Port), sshConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to dial SSH: %v", err)
|
||||
}
|
||||
defer sshConn.Close()
|
||||
// 创建到 MySQL 服务器的隧道
|
||||
mysqlConn, err := sshConn.Dial("tcp", fmt.Sprintf("%s:%d", MysqlConfig.Host, MysqlConfig.Port))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to dial MySQL: %v", err)
|
||||
}
|
||||
// 连接到 SSH 服务器
|
||||
sshConn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", SshConfig.Host, SshConfig.Port), sshConfig)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to dial SSH: %v", err)
|
||||
}
|
||||
// defer sshConn.Close()
|
||||
// 创建到 MySQL 服务器的隧道
|
||||
mysqlConn, err := sshConn.Dial("tcp", fmt.Sprintf("%s:%d", MysqlConfig.Host, MysqlConfig.Port))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to dial MySQL: %v", err)
|
||||
}
|
||||
|
||||
// 注册 MySQL 驱动
|
||||
mysql.RegisterDialContext("mysql+tcp", func(ctx context.Context, addr string) (net.Conn, error) {
|
||||
return mysqlConn, nil
|
||||
})
|
||||
var Database string
|
||||
if strings.Contains(MysqlConfig.Database, "%") {
|
||||
Database = fmt.Sprintf(MysqlConfig.Database, ServerId)
|
||||
// 注册 MySQL 驱动
|
||||
mysql.RegisterDialContext("mysql+tcp", func(ctx context.Context, addr string) (net.Conn, error) {
|
||||
return mysqlConn, nil
|
||||
})
|
||||
var Database string
|
||||
if strings.Contains(MysqlConfig.Database, "%") {
|
||||
Database = fmt.Sprintf(MysqlConfig.Database, ServerId)
|
||||
} else {
|
||||
Database = MysqlConfig.Database
|
||||
}
|
||||
MP, _ := Decrypt(MysqlConfig.Password)
|
||||
// 连接到 MySQL 数据库
|
||||
dsn := fmt.Sprintf("%s:%s@mysql+tcp(%s:%d)/%s", MysqlConfig.Username, MP, MysqlConfig.Host, MysqlConfig.Port, Database)
|
||||
db, err := sqlx.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open MySQL: %v", err)
|
||||
}
|
||||
|
||||
return db, sshConn, nil
|
||||
} else {
|
||||
Database = MysqlConfig.Database
|
||||
var Database string
|
||||
if strings.Contains(MysqlConfig.Database, "%") {
|
||||
Database = fmt.Sprintf(MysqlConfig.Database, ServerId)
|
||||
} else {
|
||||
Database = MysqlConfig.Database
|
||||
}
|
||||
MP, _ := Decrypt(MysqlConfig.Password)
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", MysqlConfig.Username, MP, MysqlConfig.Host, MysqlConfig.Port, Database)
|
||||
db, err := sqlx.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to open MySQL: %v", err)
|
||||
}
|
||||
return db, nil, nil
|
||||
}
|
||||
MP, _ := Decrypt(MysqlConfig.Password)
|
||||
// 连接到 MySQL 数据库
|
||||
dsn := fmt.Sprintf("%s:%s@mysql+tcp(%s:%d)/%s", MysqlConfig.Username, MP, MysqlConfig.Host, MysqlConfig.Port, Database)
|
||||
db, err := sqlx.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open MySQL: %v", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func ConnectMysql(MysqlName, DataBase string) (*sqlx.DB, error) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user