68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/darkit/goproxy"
|
|
"github.com/darkit/goproxy/config"
|
|
"github.com/darkit/goproxy/pkg/metrics"
|
|
)
|
|
|
|
// CustomDelegate 自定义代理委托
|
|
type CustomDelegate struct {
|
|
goproxy.DefaultDelegate
|
|
}
|
|
|
|
// Connect 处理连接事件
|
|
func (d *CustomDelegate) Connect(ctx *goproxy.Context, rw http.ResponseWriter) {
|
|
log.Printf("新的连接: %s", ctx.Req.RemoteAddr)
|
|
}
|
|
|
|
// 运行监控代理服务器
|
|
func main() {
|
|
// 创建监控指标收集器
|
|
metricsCollector := metrics.NewSimpleMetrics()
|
|
|
|
// 创建基础配置
|
|
cfg := config.DefaultConfig()
|
|
cfg.EnableRetry = true
|
|
cfg.MaxRetries = 3
|
|
cfg.BaseBackoff = time.Second
|
|
cfg.MaxBackoff = 10 * time.Second
|
|
cfg.EnableCache = true
|
|
cfg.CacheTTL = 5 * time.Minute
|
|
cfg.ConnectionPoolSize = 100
|
|
cfg.RateLimit = 1000
|
|
cfg.EnableMetrics = true
|
|
cfg.DecryptHTTPS = false // 禁用HTTPS解密
|
|
cfg.InsecureSkipVerify = true // 跳过证书验证
|
|
cfg.RequestTimeout = 30 * time.Second
|
|
cfg.IdleTimeout = 60 * time.Second
|
|
|
|
// 创建代理实例
|
|
proxy := goproxy.NewProxy(
|
|
goproxy.WithMetrics(metricsCollector),
|
|
goproxy.WithRequestTimeout(30*time.Second),
|
|
goproxy.WithIdleTimeout(60*time.Second),
|
|
goproxy.WithConfig(cfg),
|
|
goproxy.WithDelegate(&CustomDelegate{}),
|
|
)
|
|
|
|
// 启动监控指标服务器
|
|
go func() {
|
|
http.Handle("/metrics", metricsCollector.GetHandler())
|
|
log.Println("监控指标服务器启动在 :9090")
|
|
if err := http.ListenAndServe(":9090", nil); err != nil {
|
|
log.Printf("监控指标服务器启动失败: %v", err)
|
|
}
|
|
}()
|
|
|
|
// 启动代理服务器
|
|
log.Println("监控代理服务器启动在 :8080")
|
|
if err := http.ListenAndServe(":8080", proxy); err != nil {
|
|
log.Printf("代理服务器启动失败: %v", err)
|
|
}
|
|
}
|