♻️ 重构部分代码,打印调试信息

This commit is contained in:
naiba
2021-04-20 19:30:34 +08:00
parent d3c3e55c88
commit 6286e34af0
7 changed files with 70 additions and 38 deletions

View File

@@ -35,11 +35,10 @@ var (
)
var (
reporting bool
client pb.NezhaServiceClient
ctx = context.Background()
delayWhenError = time.Second * 10 // Agent 重连间隔
updateCh = make(chan struct{}, 0) // Agent 自动更新间隔
delayWhenError = time.Second * 10 // Agent 重连间隔
updateCh = make(chan struct{}) // Agent 自动更新间隔
httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -179,12 +178,14 @@ func doTask(task *pb.Task) {
start := time.Now()
resp, err := httpClient.Get(task.GetData())
if err == nil {
result.Delay = float32(time.Now().Sub(start).Microseconds()) / 1000.0
// 检查 HTTP Response 状态
result.Delay = float32(time.Since(start).Microseconds()) / 1000.0
if resp.StatusCode > 399 || resp.StatusCode < 200 {
err = errors.New("\n应用错误" + resp.Status)
}
}
if err == nil {
// 检查 SSL 证书信息
if strings.HasPrefix(task.GetData(), "https://") {
c := cert.NewCert(task.GetData()[8:])
if c.Error != "" {
@@ -197,6 +198,7 @@ func doTask(task *pb.Task) {
result.Successful = true
}
} else {
// HTTP 请求失败
result.Data = err.Error()
}
case model.TaskTypeICMPPing:
@@ -219,7 +221,7 @@ func doTask(task *pb.Task) {
if err == nil {
conn.Write([]byte("ping\n"))
conn.Close()
result.Delay = float32(time.Now().Sub(start).Microseconds()) / 1000.0
result.Delay = float32(time.Since(start).Microseconds()) / 1000.0
result.Successful = true
} else {
result.Data = err.Error()
@@ -260,7 +262,7 @@ func doTask(task *pb.Task) {
result.Data = string(output)
result.Successful = true
}
result.Delay = float32(time.Now().Sub(startedAt).Seconds())
result.Delay = float32(time.Since(startedAt).Seconds())
default:
log.Printf("Unknown action: %v", task)
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"net/http"
"strings"
@@ -71,7 +70,6 @@ func (oa *oauth2controller) callback(c *gin.Context) {
if err == nil {
gu, _, err = client.Users.Get(ctx, "")
}
log.Printf("%+v", gu)
if err != nil {
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusBadRequest,

View File

@@ -34,7 +34,7 @@ func tcpping() {
}
conn.Write([]byte("ping\n"))
conn.Close()
fmt.Println(time.Now().Sub(start).Microseconds(), float32(time.Now().Sub(start).Microseconds())/1000.0)
fmt.Println(time.Since(start).Microseconds(), float32(time.Since(start).Microseconds())/1000.0)
}
func sysinfo() {