2023-01-31 12:48:28 +08:00
package task
import (
2023-01-31 20:11:13 +08:00
//"crypto/tls"
//"fmt"
2023-01-31 12:48:28 +08:00
"io"
"log"
"net"
"net/http"
2023-04-25 11:00:46 +08:00
"regexp"
2023-01-31 12:48:28 +08:00
"strings"
"sync"
"time"
)
var (
2023-01-31 20:11:13 +08:00
Httping bool
HttpingStatusCode int
HttpingCFColo string
HttpingCFColomap * sync . Map
2023-04-25 11:00:46 +08:00
OutRegexp = regexp . MustCompile ( ` [A-Z] { 3} ` )
2023-01-31 12:48:28 +08:00
)
// pingReceived pingTotalTime
func ( p * Ping ) httping ( ip * net . IPAddr ) ( int , time . Duration ) {
hc := http . Client {
2023-01-31 20:11:13 +08:00
Timeout : time . Second * 2 ,
2023-01-31 12:48:28 +08:00
Transport : & http . Transport {
2023-01-31 20:11:13 +08:00
DialContext : getDialContext ( ip ) ,
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证
2023-01-31 12:48:28 +08:00
} ,
2023-01-31 20:11:13 +08:00
CheckRedirect : func ( req * http . Request , via [ ] * http . Request ) error {
return http . ErrUseLastResponse // 阻止重定向
} ,
}
2023-01-31 12:48:28 +08:00
2023-01-31 20:11:13 +08:00
// 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
2023-01-31 12:48:28 +08:00
{
2023-01-31 20:11:13 +08:00
requ , err := http . NewRequest ( http . MethodHead , URL , nil )
2023-01-31 12:48:28 +08:00
if err != nil {
return 0 , 0
}
requ . Header . Set ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36" )
resp , err := hc . Do ( requ )
if err != nil {
return 0 , 0
}
defer resp . Body . Close ( )
2023-01-31 20:11:13 +08:00
//fmt.Println("IP:", ip, "StatusCode:", resp.StatusCode, resp.Request.URL)
2023-04-25 11:18:49 +08:00
// 如果未指定的 HTTP 状态码,或指定的状态码不合规,则默认只认为 200、301、302 才算 HTTPing 通过
2023-01-31 20:11:13 +08:00
if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
if resp . StatusCode != 200 && resp . StatusCode != 301 && resp . StatusCode != 302 {
return 0 , 0
}
} else {
if resp . StatusCode != HttpingStatusCode {
return 0 , 0
}
}
2023-01-31 12:48:28 +08:00
2023-01-31 20:11:13 +08:00
io . Copy ( io . Discard , resp . Body )
2023-04-25 11:18:49 +08:00
// 只有指定了地区才匹配机场三字码
2023-01-31 20:11:13 +08:00
if HttpingCFColo != "" {
2023-04-25 11:18:49 +08:00
// 通过头部 Server 值判断是 Cloudflare 还是 AWS CloudFront 并设置 cfRay 为各自的机场三字码完整内容
2023-04-25 11:00:46 +08:00
cfRay := func ( ) string {
if resp . Header . Get ( "Server" ) == "cloudflare" {
2023-04-25 11:18:49 +08:00
return resp . Header . Get ( "CF-RAY" ) // 示例 cf-ray: 7bd32409eda7b020-SJC
2023-04-25 11:00:46 +08:00
}
2023-04-25 11:18:49 +08:00
return resp . Header . Get ( "x-amz-cf-pop" ) // 示例 X-Amz-Cf-Pop: SIN52-P1
2023-04-25 11:00:46 +08:00
} ( )
2023-01-31 20:11:13 +08:00
colo := p . getColo ( cfRay )
2023-04-25 11:18:49 +08:00
if colo == "" { // 没有匹配到三字码或不符合指定地区则直接结束该 IP 测试
2023-01-31 20:11:13 +08:00
return 0 , 0
}
2023-01-31 12:48:28 +08:00
}
}
2023-01-31 20:11:13 +08:00
// 循环测速计算延迟
2023-01-31 12:48:28 +08:00
success := 0
var delay time . Duration
for i := 0 ; i < PingTimes ; i ++ {
2023-01-31 20:11:13 +08:00
requ , err := http . NewRequest ( http . MethodHead , URL , nil )
2023-01-31 12:48:28 +08:00
if err != nil {
log . Fatal ( "意外的错误,情报告:" , err )
return 0 , 0
}
requ . Header . Set ( "User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36" )
if i == PingTimes - 1 {
requ . Header . Set ( "Connection" , "close" )
}
startTime := time . Now ( )
resp , err := hc . Do ( requ )
if err != nil {
continue
}
success ++
io . Copy ( io . Discard , resp . Body )
_ = resp . Body . Close ( )
duration := time . Since ( startTime )
delay += duration
}
return success , delay
}
func MapColoMap ( ) * sync . Map {
2023-01-31 20:11:13 +08:00
if HttpingCFColo == "" {
2023-01-31 12:48:28 +08:00
return nil
}
2023-04-25 11:18:49 +08:00
// 将参数指定的地区三字码转为大写并格式化
colos := strings . Split ( strings . ToUpper ( HttpingCFColo ) , "," )
2023-01-31 12:48:28 +08:00
colomap := & sync . Map { }
for _ , colo := range colos {
colomap . Store ( colo , colo )
}
return colomap
}
func ( p * Ping ) getColo ( b string ) string {
if b == "" {
return ""
}
2023-04-25 11:18:49 +08:00
// 正则匹配并返回 机场三字码
2023-04-25 11:00:46 +08:00
out := OutRegexp . FindString ( b )
2023-01-31 12:48:28 +08:00
2023-01-31 20:11:13 +08:00
if HttpingCFColomap == nil {
2023-01-31 12:48:28 +08:00
return out
}
2023-04-25 11:18:49 +08:00
// 匹配 机场三字码 是否为指定的地区
2023-01-31 20:11:13 +08:00
_ , ok := HttpingCFColomap . Load ( out )
2023-01-31 12:48:28 +08:00
if ok {
return out
}
return ""
}