Skip to content

Commit

Permalink
fix code review
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangnew committed Oct 16, 2023
1 parent a97d2df commit 11e4697
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,12 @@ func handleIcmpPingTask(task *pb.Task, result *pb.TaskResult) {

func handleHttpGetTask(task *pb.Task, result *pb.TaskResult) {
start := time.Now()
resp, err := httpClient.Get(task.GetData())
taskUrl := task.GetData()
resp, err := httpClient.Get(taskUrl)
checkHttpResp(taskUrl, start, resp, err, result)
}

func checkHttpResp(taskUrl string, start time.Time, resp *http.Response, err error, result *pb.TaskResult) {
if err == nil {
// 检查 HTTP Response 状态
result.Delay = float32(time.Since(start).Microseconds()) / 1000.0
Expand All @@ -409,9 +414,9 @@ func handleHttpGetTask(task *pb.Task, result *pb.TaskResult) {
result.Data = c.Issuer.CommonName + "|" + c.NotAfter.String()
}
altSvc := resp.Header.Get("Alt-Svc")
parsedUrl, _ := url.Parse(task.Data)
originalHost := parsedUrl.Hostname()
if checkAltSvc(altSvc, originalHost, result) {
if altSvc != "" {
checkAltSvc(altSvc, taskUrl, result)
} else {
result.Successful = true
}
} else {
Expand All @@ -420,51 +425,51 @@ func handleHttpGetTask(task *pb.Task, result *pb.TaskResult) {
}
}

func checkAltSvc(altSvcStr string, originalHost string, result *pb.TaskResult) bool {
if altSvcStr == "" {
return true
func checkAltSvc(altSvcStr string, taskUrl string, result *pb.TaskResult) {
altSvcList, err := altsvc.Parse(altSvcStr)
if err != nil {
result.Data = err.Error()
result.Successful = false
return
}

parsedUrl, _ := url.Parse(taskUrl)
originalHost := parsedUrl.Hostname()
originalPort := parsedUrl.Port()
if originalPort == "" {
switch parsedUrl.Scheme {
case "http":
originalPort = "80"
case "https":
originalPort = "443"
}
}
altSvcList, _ := altsvc.Parse(altSvcStr)

altAuthorityHost := ""
altAuthorityPort := ""
for _, altSvc := range altSvcList {
if altSvc.AltAuthority.Host != "" && altSvc.AltAuthority.Host != originalHost {
if altSvc.AltAuthority.Host != "" {
altAuthorityHost = altSvc.AltAuthority.Host
altAuthorityPort = altSvc.AltAuthority.Port
}
altAuthorityPort = altSvc.AltAuthority.Port
}
if altAuthorityHost == "" {
return true
altAuthorityHost = originalHost
}
if altAuthorityHost == originalHost && altAuthorityPort == originalPort {
result.Successful = true
return
}

altAuthorityUrl := "https://" + altAuthorityHost + ":" + altAuthorityPort + "/"
println("checkAltSvc originalHost: ", originalHost, " AltSvc: ", altAuthorityUrl)
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 {
// 检查 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
checkHttpResp(taskUrl, start, resp, err, result)
}

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

0 comments on commit 11e4697

Please sign in to comment.