init
This commit is contained in:
49
examples/custom_dns_proxy/main.go
Normal file
49
examples/custom_dns_proxy/main.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package custom_dns_proxy
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/darkit/goproxy"
|
||||
"github.com/darkit/goproxy/pkg/dns"
|
||||
)
|
||||
|
||||
// CustomDNSDelegate 自定义 DNS 代理委托
|
||||
type CustomDNSDelegate struct {
|
||||
goproxy.DefaultDelegate
|
||||
dnsResolver dns.Resolver
|
||||
}
|
||||
|
||||
// ResolveBackend 解析后端服务器
|
||||
func (d *CustomDNSDelegate) ResolveBackend(req *http.Request) (string, error) {
|
||||
return d.dnsResolver.Resolve(req.URL.Host)
|
||||
}
|
||||
|
||||
// RunCustomDNSProxy 运行自定义 DNS 代理服务器
|
||||
func RunCustomDNSProxy() error {
|
||||
// 创建自定义 DNS 解析器
|
||||
resolver := dns.NewResolver(dns.WithFallback(true))
|
||||
|
||||
// 添加 DNS 记录
|
||||
resolver.LoadFromMap(map[string]string{
|
||||
"example.com": "http://backend1.example.com",
|
||||
"test.com": "http://backend2.test.com",
|
||||
})
|
||||
|
||||
// 创建自定义 DNS 代理委托
|
||||
delegate := &CustomDNSDelegate{
|
||||
dnsResolver: resolver,
|
||||
}
|
||||
|
||||
// 创建代理实例
|
||||
proxy := goproxy.NewProxy(
|
||||
goproxy.WithDelegate(delegate),
|
||||
)
|
||||
|
||||
// 启动代理服务器
|
||||
log.Println("自定义 DNS 代理服务器启动在 :8080")
|
||||
log.Println("DNS 配置:")
|
||||
log.Printf("- example.com -> backend1.example.com\n")
|
||||
log.Printf("- test.com -> backend2.test.com\n")
|
||||
return http.ListenAndServe(":8080", proxy)
|
||||
}
|
Reference in New Issue
Block a user