cache dns maybe

This commit is contained in:
bel
2026-07-15 21:02:41 -06:00
parent f1b4b88091
commit e4acfff5a2
4 changed files with 21 additions and 0 deletions
+17
View File
@@ -1,13 +1,18 @@
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/viki-org/dnscache"
)
func main() {
@@ -32,6 +37,8 @@ func main() {
}
}
var resolver = dnscache.New(time.Minute * 500)
func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
endpoint := c.endpoint(r)
if endpoint.To == "" {
@@ -71,8 +78,18 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("[%s] %v", c.key(r), err)
}
var transport http.Transport
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
if ip, _ := resolver.FetchOneString(strings.Split(u.Host, ":")[0]); ip != "" {
transport.DialContext = func(ctx context.Context, network string, address string) (net.Conn, error) {
if idx := strings.LastIndex(u.Host, ":"); idx > 0 {
ip = ip + u.Host[idx:]
}
return (&net.Dialer{}).DialContext(ctx, network, ip)
}
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.Transport = redirPurge{
proxyHost: r.Host,