42 lines
960 B
Go
42 lines
960 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/darkit/goproxy"
|
|
)
|
|
|
|
// HTTPToHTTPSDelegate HTTP 到 HTTPS 代理委托
|
|
type HTTPToHTTPSDelegate struct {
|
|
goproxy.DefaultDelegate
|
|
}
|
|
|
|
// BeforeRequest 请求前事件
|
|
func (d *HTTPToHTTPSDelegate) BeforeRequest(ctx *goproxy.Context) {
|
|
// 将 HTTP 请求转换为 HTTPS
|
|
if ctx.Req.URL.Scheme == "http" {
|
|
ctx.Req.URL.Scheme = "https"
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
// 创建 HTTP 到 HTTPS 代理委托
|
|
delegate := &HTTPToHTTPSDelegate{}
|
|
|
|
// 创建代理实例
|
|
proxy := goproxy.NewProxy(
|
|
goproxy.WithDelegate(delegate),
|
|
goproxy.WithDecryptHTTPS(&goproxy.MemCertCache{}),
|
|
)
|
|
|
|
// 启动代理服务器
|
|
log.Println("HTTP 到 HTTPS 代理服务器启动在 :8080")
|
|
log.Println("配置说明:")
|
|
log.Printf("- 自动将 HTTP 请求转换为 HTTPS\n")
|
|
log.Printf("- 支持 HTTPS 解密\n")
|
|
if err := http.ListenAndServe(":8080", proxy); err != nil {
|
|
log.Fatalf("代理服务器启动失败: %v", err)
|
|
}
|
|
}
|