测试代理是否可用
This commit is contained in:
60
main.go
60
main.go
@@ -2,27 +2,65 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"httppp/common"
|
||||
"httppp/common/viper"
|
||||
"httppp/core"
|
||||
"httppp/global"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
viper.InitViper()
|
||||
|
||||
// 读取节点文件
|
||||
nodes, err := common.ReadNodesFile(global.Conf.Files.Nodes)
|
||||
if err != nil {
|
||||
log.Fatalf("读取节点文件失败: %v", err)
|
||||
}
|
||||
global.Nodes = nodes
|
||||
}
|
||||
|
||||
// 主程序,遍历多个代理地址
|
||||
func main() {
|
||||
//dump.P(global.Conf)
|
||||
for _, node := range global.Conf.Nodes {
|
||||
for _, port := range node.Ports {
|
||||
node.Port = port
|
||||
success, message := core.CheckHttpsProxy(&node, global.Conf.TargetURL)
|
||||
if success {
|
||||
fmt.Printf("代理 %s 端口 %d 可用.\n", node.Addr, node.Port)
|
||||
} else {
|
||||
fmt.Printf("代理 %s 端口 %d 不可用: %s\n", node.Addr, node.Port, message)
|
||||
}
|
||||
// 创建代理检测器
|
||||
checker := core.NewProxyChecker(global.Conf.ProxyCheck, global.Nodes, global.Conf.TargetURL)
|
||||
|
||||
// 记录开始时间
|
||||
startTime := time.Now()
|
||||
|
||||
// 执行检测
|
||||
availableNodes, unavailableNodes := checker.Check()
|
||||
|
||||
// 计算测试用时
|
||||
duration := time.Since(startTime)
|
||||
|
||||
// 打印测试报告到命令行
|
||||
fmt.Println("\n========== 代理测试报告 ==========")
|
||||
fmt.Printf("测试时间: %s\n", time.Now().Format("2006-01-02 15:04:05"))
|
||||
fmt.Printf("测试用时: %v\n", duration)
|
||||
fmt.Printf("总节点数: %d\n", len(availableNodes)+len(unavailableNodes))
|
||||
fmt.Printf("可用节点: %d\n", len(availableNodes))
|
||||
fmt.Printf("不可用节点: %d\n", len(unavailableNodes))
|
||||
fmt.Println("================================")
|
||||
|
||||
// 保存结果
|
||||
if len(availableNodes) > 0 {
|
||||
filename := fmt.Sprintf("%s_%s.txt",
|
||||
global.Conf.Files.AvailableNodes,
|
||||
time.Now().Format("20060102_150405"))
|
||||
if err := common.SaveAvailableNodes(filename, availableNodes); err != nil {
|
||||
log.Printf("保存可用节点失败: %v", err)
|
||||
} else {
|
||||
fmt.Printf("可用节点已保存到: %s\n", filename)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果配置了保存报告,则保存详细报告
|
||||
if global.Conf.ProxyCheck.SaveReport {
|
||||
filename := fmt.Sprintf("%s_%s.txt",
|
||||
global.Conf.Files.Report,
|
||||
time.Now().Format("20060102_150405"))
|
||||
core.SaveDetailedReport(filename, availableNodes, unavailableNodes, duration)
|
||||
fmt.Printf("详细报告已保存到: %s\n", filename)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user