2019-08-10 18:26:43 +08:00
|
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"HFish/utils/log"
|
|
|
|
|
"HFish/utils/conf"
|
2019-08-25 01:00:42 +08:00
|
|
|
|
"HFish/core/rpc/core"
|
|
|
|
|
"strings"
|
2019-08-10 18:26:43 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 上报状态结构
|
|
|
|
|
type Status struct {
|
2019-08-25 15:22:37 +08:00
|
|
|
|
AgentIp string
|
|
|
|
|
AgentName string
|
|
|
|
|
Web, Deep, Ssh, Redis, Mysql, Http, Telnet, Ftp, MemCahe, Plug string
|
2019-08-10 18:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上报结果结构
|
|
|
|
|
type Result struct {
|
|
|
|
|
AgentIp string
|
|
|
|
|
AgentName string
|
|
|
|
|
Type string
|
|
|
|
|
ProjectName string
|
|
|
|
|
SourceIp string
|
|
|
|
|
Info string
|
|
|
|
|
Id string // 数据库ID,更新用 0 为新插入数据
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 01:00:42 +08:00
|
|
|
|
func createClient() (*rpc.Client, string, bool) {
|
2019-08-10 18:26:43 +08:00
|
|
|
|
rpcAddr := conf.Get("rpc", "addr")
|
2019-08-25 01:00:42 +08:00
|
|
|
|
client, conn, err := rpc.Dial("tcp", rpcAddr)
|
|
|
|
|
|
2019-08-10 18:26:43 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Pr("RPC", "127.0.0.1", "连接 RPC Server 失败")
|
2019-08-25 01:00:42 +08:00
|
|
|
|
return client, "", false
|
2019-08-10 18:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 12:56:08 +08:00
|
|
|
|
ipArr := strings.Split(conn.LocalAddr().String(), ":")
|
|
|
|
|
|
2019-08-25 01:00:42 +08:00
|
|
|
|
return client, ipArr[0], true
|
2019-08-10 18:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 15:22:37 +08:00
|
|
|
|
func reportStatus(rpcName string, ftpStatus string, telnetStatus string, httpStatus string, mysqlStatus string, redisStatus string, sshStatus string, webStatus string, darkStatus string, memCacheStatus string, plugStatus string) {
|
2019-08-25 01:00:42 +08:00
|
|
|
|
client, addr, boolStatus := createClient()
|
2019-08-10 18:26:43 +08:00
|
|
|
|
|
|
|
|
|
if boolStatus {
|
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
|
|
status := Status{
|
2019-08-25 01:00:42 +08:00
|
|
|
|
addr,
|
2019-08-10 18:26:43 +08:00
|
|
|
|
rpcName,
|
|
|
|
|
webStatus,
|
|
|
|
|
darkStatus,
|
|
|
|
|
sshStatus,
|
|
|
|
|
redisStatus,
|
|
|
|
|
mysqlStatus,
|
|
|
|
|
httpStatus,
|
|
|
|
|
telnetStatus,
|
2019-08-11 20:14:28 +08:00
|
|
|
|
ftpStatus,
|
2019-08-25 15:22:37 +08:00
|
|
|
|
memCacheStatus,
|
|
|
|
|
plugStatus,
|
2019-08-10 18:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reply string
|
|
|
|
|
err := client.Call("HFishRPCService.ReportStatus", status, &reply)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Pr("RPC", "127.0.0.1", "上报服务状态失败", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ReportResult(typex string, projectName string, sourceIp string, info string, id string) string {
|
|
|
|
|
// projectName 只有 WEB 才需要传项目名 其他协议空即可
|
|
|
|
|
// id 0 为 新插入数据,非 0 都是更新数据
|
|
|
|
|
// id 非 0 的时候 sourceIp 为空
|
2019-08-25 01:00:42 +08:00
|
|
|
|
client, addr, boolStatus := createClient()
|
2019-08-10 18:26:43 +08:00
|
|
|
|
|
|
|
|
|
if boolStatus {
|
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
|
|
rpcName := conf.Get("rpc", "name")
|
|
|
|
|
|
|
|
|
|
result := Result{
|
2019-08-25 01:00:42 +08:00
|
|
|
|
addr,
|
2019-08-10 18:26:43 +08:00
|
|
|
|
rpcName,
|
|
|
|
|
typex,
|
|
|
|
|
projectName,
|
|
|
|
|
sourceIp,
|
|
|
|
|
info,
|
|
|
|
|
id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reply string
|
|
|
|
|
err := client.Call("HFishRPCService.ReportResult", result, &reply)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Pr("RPC", "127.0.0.1", "上报上钩结果失败")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 15:22:37 +08:00
|
|
|
|
func Start(rpcName string, ftpStatus string, telnetStatus string, httpStatus string, mysqlStatus string, redisStatus string, sshStatus string, webStatus string, darkStatus string, memCacheStatus string, plugStatus string) {
|
|
|
|
|
reportStatus(rpcName, ftpStatus, telnetStatus, httpStatus, mysqlStatus, redisStatus, sshStatus, webStatus, darkStatus, memCacheStatus, plugStatus)
|
2019-08-10 18:26:43 +08:00
|
|
|
|
}
|