[agent-wdd] 完成自定义log部分;完成info network部分; 项目结构基本完成
This commit is contained in:
@@ -1,25 +1,149 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"agent-wdd/log"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CanConnectInternet 判定主机能否上网 请求 www.google.com 如果请求正常 返回2 如果超时三秒 请求baidu.com,如果没有错误返回1 如果错误返回0
|
||||
// 能够联网,就大于这个数字 外网9 国内7 不能联网1 未知0
|
||||
const InternetBaseLine = 1
|
||||
|
||||
// 定义响应数据结构体
|
||||
type IPInfo struct {
|
||||
IP string `json:"ip"`
|
||||
City string `json:"city"`
|
||||
Region string `json:"region"`
|
||||
Country string `json:"country"`
|
||||
Loc string `json:"loc"`
|
||||
Org string `json:"org"`
|
||||
Postal string `json:"postal"`
|
||||
Timezone string `json:"timezone"`
|
||||
}
|
||||
|
||||
// Gather 获取网络相关的信息
|
||||
func (network *Network) Gather() {
|
||||
// 能够联网
|
||||
network.Internet = CanConnectInternet()
|
||||
|
||||
// 获取公网的相关信息
|
||||
pub := PublicInfo{}
|
||||
pub.GetPublicInfo()
|
||||
|
||||
network.Public = pub
|
||||
|
||||
//获取本机网卡相关的内容
|
||||
}
|
||||
|
||||
// CanConnectInternet 判定主机能够上网
|
||||
func CanConnectInternet() int {
|
||||
// 读取 config 文件,判定能否上网
|
||||
internetCode := ConfigCache.Agent.Network.Internet
|
||||
|
||||
if internetCode == 0 {
|
||||
// 没有相关的信息,需要重新判定
|
||||
internetCode = judgeCanConnectInternet()
|
||||
|
||||
// 持久化保存
|
||||
ConfigCache.Agent.Network.Internet = internetCode
|
||||
SaveConfig()
|
||||
}
|
||||
|
||||
return internetCode
|
||||
}
|
||||
|
||||
// judgeCanConnectInternet 请求网址判定主机能否上网 请求 www.google.com 如果请求正常 返回9 如果超时三秒 请求baidu.com,如果没有错误返回7 如果错误返回1
|
||||
func judgeCanConnectInternet() int {
|
||||
client := http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
_, err := client.Get("https://www.google.com")
|
||||
if err == nil {
|
||||
return 2
|
||||
return 9
|
||||
}
|
||||
|
||||
_, err = client.Get("https://www.baidu.com")
|
||||
if err == nil {
|
||||
return 1
|
||||
return 7
|
||||
}
|
||||
|
||||
return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
// GetPublicInfo 获取服务器的公网信息
|
||||
func (p PublicInfo) GetPublicInfo() {
|
||||
|
||||
if CanConnectInternet() == InternetBaseLine {
|
||||
// 无法联网, 假信息
|
||||
|
||||
fakePublicInfo := PublicInfo{
|
||||
IPv4: "1.1.1.1",
|
||||
IPv6: "2400::1",
|
||||
Country: "CN",
|
||||
City: "Shanghai",
|
||||
ASN: "Wdd Inc",
|
||||
}
|
||||
|
||||
ConfigCache.Agent.Network.Public = fakePublicInfo
|
||||
|
||||
SaveConfig()
|
||||
return
|
||||
}
|
||||
|
||||
// 可以联网
|
||||
// 创建带有超时的HTTP客户端
|
||||
client := &http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
// 创建新的请求对象
|
||||
req, err := http.NewRequest("GET", "https://ipinfo.io", nil)
|
||||
if err != nil {
|
||||
log.Error("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Add("Authorization", "Bearer 6ecb0db9bd8f19")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
// 发送请求
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Error("请求PublicInfo失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Error("非200状态码: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Error("读取响应失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析JSON数据
|
||||
var info IPInfo
|
||||
if err := json.Unmarshal(body, &info); err != nil {
|
||||
log.Error("JSON解析失败: %v", err)
|
||||
}
|
||||
|
||||
// 打印解析结果
|
||||
log.Info("IP信息:\n%+v\n", info)
|
||||
|
||||
// 保存信息
|
||||
p.IPv4 = info.IP
|
||||
p.ASN = info.Org
|
||||
p.Country = info.Country
|
||||
p.City = info.City
|
||||
|
||||
ConfigCache.Agent.Network.Public = p
|
||||
|
||||
SaveConfig()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user