Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpGet support Alt-Svc #3

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ Agent of Nezha Monitoring

<!--GAMFC_DELIMITER--><a href="https://github.com/naiba" title="naiba">
<img src="https://avatars.githubusercontent.com/u/29243953?v=4" width="50;" alt="naiba"/>
</a>
<a href="https://github.com/zhangnew" title="zhangnew">
<img src="https://avatars.githubusercontent.com/u/9146834?v=4" width="50;" alt="zhangnew"/>
</a><!--GAMFC_DELIMITER_END-->
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/creack/pty v1.1.18
github.com/dean2021/goss v0.0.0-20230129073947-df90431348f1
github.com/ebi-yade/altsvc-go v0.1.1
github.com/go-ping/ping v1.1.0
github.com/gorilla/websocket v1.5.0
github.com/iamacarpet/go-winpty v1.0.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dean2021/goss v0.0.0-20230129073947-df90431348f1 h1:5UiJ324LiCdOF/3w/5IeXrKVjdnwHoalvLG2smb3wi4=
github.com/dean2021/goss v0.0.0-20230129073947-df90431348f1/go.mod h1:NiLueuVb3hYcdF4ta+2ezcKJh6BEjhrBz9Hts6XJ5Sc=
github.com/ebi-yade/altsvc-go v0.1.1 h1:HmZDNb5ZOPlkyXhi34LnRckawFCux7yPYw+dtInIixo=
github.com/ebi-yade/altsvc-go v0.1.1/go.mod h1:K/U20bLcsOVrbTeDhqRjp+e3tgNT5iAqSiQzPoU0/Q0=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
63 changes: 62 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/AlecAivazis/survey/v2"
bpc "github.com/DaRealFreak/cloudflare-bp-go"
"github.com/blang/semver"
"github.com/ebi-yade/altsvc-go"
"github.com/go-ping/ping"
"github.com/gorilla/websocket"
"github.com/nezhahq/go-github-selfupdate/selfupdate"
Expand Down Expand Up @@ -48,6 +50,7 @@ type AgentCliParam struct {
ClientSecret string // 客户端密钥
ReportDelay int // 报告间隔
TLS bool // 是否使用TLS加密传输至服务端
Version bool // 当前版本号
}

var (
Expand Down Expand Up @@ -155,8 +158,14 @@ func main() {
flag.BoolVar(&agentCliParam.DisableAutoUpdate, "disable-auto-update", false, "禁用自动升级")
flag.BoolVar(&agentCliParam.DisableForceUpdate, "disable-force-update", false, "禁用强制升级")
flag.BoolVar(&agentCliParam.TLS, "tls", false, "启用SSL/TLS加密")
flag.BoolVarP(&agentCliParam.Version, "version", "v", false, "查看当前版本号")
flag.Parse()

if agentCliParam.Version {
fmt.Println(version)
return
}

if isEditAgentConfig {
editAgentConfig()
return
Expand Down Expand Up @@ -399,11 +408,63 @@ func handleHttpGetTask(task *pb.Task, result *pb.TaskResult) {
c := resp.TLS.PeerCertificates[0]
result.Data = c.Issuer.CommonName + "|" + c.NotAfter.String()
}
result.Successful = true
altSvc := resp.Header.Get("Alt-Svc")
parsedUrl, _ := url.Parse(task.Data)
zhangnew marked this conversation as resolved.
Show resolved Hide resolved
originalHost := parsedUrl.Hostname()
if checkAltSvc(altSvc, originalHost, result) {
result.Successful = true
}
} else {
// HTTP 请求失败
result.Data = err.Error()
}
}

func checkAltSvc(altSvcStr string, originalHost string, result *pb.TaskResult) bool {
if altSvcStr == "" {
return true
}
altSvcList, _ := altsvc.Parse(altSvcStr)
altAuthorityHost := ""
altAuthorityPort := ""
for _, altSvc := range altSvcList {
if altSvc.AltAuthority.Host != "" && altSvc.AltAuthority.Host != originalHost {
altAuthorityHost = altSvc.AltAuthority.Host
altAuthorityPort = altSvc.AltAuthority.Port
}
}
if altAuthorityHost == "" {
return true
zhangnew marked this conversation as resolved.
Show resolved Hide resolved
}
altAuthorityUrl := "https://" + altAuthorityHost + ":" + altAuthorityPort + "/"
println("checkAltSvc originalHost: ", originalHost, " AltSvc: ", altAuthorityUrl)
zhangnew marked this conversation as resolved.
Show resolved Hide resolved
successful := false
req, _ := http.NewRequest("GET", altAuthorityUrl, nil)
req.Host = originalHost
req.Header.Add("Upgrade", originalHost)
req.Header.Add("Connection", "Upgrade")

start := time.Now()
resp, err := httpClient.Do(req)
if err == nil {
zhangnew marked this conversation as resolved.
Show resolved Hide resolved
// 检查 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 resp.TLS != nil && len(resp.TLS.PeerCertificates) > 0 {
c := resp.TLS.PeerCertificates[0]
result.Data = c.Issuer.CommonName + "|" + c.NotAfter.String()
}
successful = true
} else {
// HTTP 请求失败
result.Data = err.Error()
}
return successful
}

func handleCommandTask(task *pb.Task, result *pb.TaskResult) {
Expand Down