Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
xOS committed Aug 25, 2024
1 parent 2950617 commit ca8d68d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 148 deletions.
90 changes: 0 additions & 90 deletions pkg/utils/http.go
Original file line number Diff line number Diff line change
@@ -1,101 +1,11 @@
package utils

import (
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"strings"
"time"
)

var DNSServersV4 = []string{"8.8.4.4:53", "223.5.5.5:53", "94.140.14.140:53", "119.29.29.29:53"}
var DNSServersV6 = []string{"[2001:4860:4860::8844]:53", "[2400:3200::1]:53", "[2a10:50c0::1:ff]:53", "[2402:4e00::]:53"}
var DNSServersAll = append(DNSServersV4, DNSServersV6...)

func NewSingleStackHTTPClient(httpTimeout, dialTimeout, keepAliveTimeout time.Duration, ipv6 bool) *http.Client {
dialer := &net.Dialer{
Timeout: dialTimeout,
KeepAlive: keepAliveTimeout,
}

transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
ForceAttemptHTTP2: false,
DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
ip, err := resolveIP(addr, ipv6)
if err != nil {
return nil, err
}
return dialer.DialContext(ctx, network, ip)
},
}

return &http.Client{
Transport: transport,
Timeout: httpTimeout,
}
}

func resolveIP(addr string, ipv6 bool) (string, error) {
url := strings.Split(addr, ":")

dnsServers := DNSServersV6
if !ipv6 {
dnsServers = DNSServersV4
}

res, err := net.LookupIP(url[0])
if err != nil {
for i := 0; i < len(dnsServers); i++ {
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Second * 10,
}
return d.DialContext(ctx, "udp", dnsServers[i])
},
}
res, err = r.LookupIP(context.Background(), "ip", url[0])
if err == nil {
break
}
}
}

if err != nil {
return "", err
}

var ipv4Resolved, ipv6Resolved bool

for i := 0; i < len(res); i++ {
ip := res[i].String()
if strings.Contains(ip, ".") && !ipv6 {
ipv4Resolved = true
url[0] = ip
break
}
if strings.Contains(ip, ":") && ipv6 {
ipv6Resolved = true
url[0] = "[" + ip + "]"
break
}
}

if ipv6 && !ipv6Resolved {
return "", errors.New("the AAAA record not resolved")
}

if !ipv6 && !ipv4Resolved {
return "", errors.New("the A record not resolved")
}

return strings.Join(url, ":"), nil
}

var (
HttpClientSkipTlsVerify *http.Client
HttpClient *http.Client
Expand Down
56 changes: 0 additions & 56 deletions pkg/utils/request_wrapper.go

This file was deleted.

4 changes: 2 additions & 2 deletions service/singleton/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func InitNotification() {
NotificationIDToTag = make(map[uint64]string)
}

// LoadNotifications 从 DB 初始化通知方式相关参数
func LoadNotifications() {
// loadNotifications 从 DB 初始化通知方式相关参数
func loadNotifications() {
InitNotification()
notificationsLock.Lock()
defer notificationsLock.Unlock()
Expand Down

0 comments on commit ca8d68d

Please sign in to comment.