️ agent: SSL证书校验优化

This commit is contained in:
naiba 2021-08-29 14:41:00 +08:00
parent d7ea114068
commit 5abcdf5362
3 changed files with 4 additions and 25 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -10,13 +9,11 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"os" "os"
"os/exec" "os/exec"
"time" "time"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/genkiroid/cert"
"github.com/go-ping/ping" "github.com/go-ping/ping"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/p14yground/go-github-selfupdate/selfupdate" "github.com/p14yground/go-github-selfupdate/selfupdate"
@ -33,7 +30,6 @@ import (
) )
func init() { func init() {
cert.TimeoutSeconds = 30
http.DefaultClient.Timeout = time.Second * 5 http.DefaultClient.Timeout = time.Second * 5
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
} }
@ -49,9 +45,6 @@ var (
inited bool inited bool
updateCh = make(chan struct{}) // Agent 自动更新间隔 updateCh = make(chan struct{}) // Agent 自动更新间隔
httpClient = &http.Client{ httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse
}, },
@ -274,22 +267,11 @@ func handleHttpGetTask(task *pb.Task, result *pb.TaskResult) {
} }
if err == nil { if err == nil {
// 检查 SSL 证书信息 // 检查 SSL 证书信息
serviceUrl, err := url.Parse(task.GetData()) if len(resp.TLS.PeerCertificates) > 0 {
if err == nil { c := resp.TLS.PeerCertificates[0]
if serviceUrl.Scheme == "https" { result.Data = c.Issuer.CommonName + "|" + c.NotAfter.In(time.Local).String()
c := cert.NewCert(serviceUrl.Host) }
if c.Error != "" {
result.Data = "SSL证书错误" + c.Error
} else {
result.Data = c.Issuer + "|" + c.NotAfter
result.Successful = true result.Successful = true
}
} else {
result.Successful = true
}
} else {
result.Data = "URL解析错误" + err.Error()
}
} else { } else {
// HTTP 请求失败 // HTTP 请求失败
result.Data = err.Error() result.Data = err.Error()

1
go.mod
View File

@ -8,7 +8,6 @@ require (
github.com/blang/semver v3.5.1+incompatible github.com/blang/semver v3.5.1+incompatible
github.com/creack/pty v1.1.14 github.com/creack/pty v1.1.14
github.com/fsnotify/fsnotify v1.4.9 github.com/fsnotify/fsnotify v1.4.9
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50
github.com/gin-contrib/pprof v1.3.0 github.com/gin-contrib/pprof v1.3.0
github.com/gin-gonic/gin v1.7.0 github.com/gin-gonic/gin v1.7.0
github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741 github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741

2
go.sum
View File

@ -80,8 +80,6 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50 h1:vLwmYBduhnWWqShoUGbVgDulhcLdanoYtCQxYMzwaqQ=
github.com/genkiroid/cert v0.0.0-20191007122723-897560fbbe50/go.mod h1:Pb7nyGYAfDyE/IkU6AJeRshIFko0wJC9cOqeYzYQffk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0= github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0=
github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0= github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0=