using clashHttp in config/fetch.go

This commit is contained in:
wwqgtxx 2023-10-22 20:09:33 +08:00
parent e5563b392e
commit d4c078bab0
3 changed files with 7 additions and 63 deletions

@ -1 +1 @@
Subproject commit 150bf7fc65c7736d7c56e2ce7866f03a620d9014 Subproject commit e4abecd4e25514ec0266e6d855de328a449dbfd1

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -12,7 +13,8 @@ import (
"time" "time"
"cfa/native/app" "cfa/native/app"
"github.com/Dreamacro/clash/component/dialer"
clashHttp "github.com/Dreamacro/clash/component/http"
) )
type Status struct { type Status struct {
@ -22,30 +24,15 @@ type Status struct {
MaxProgress int `json:"max"` MaxProgress int `json:"max"`
} }
var client = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DialContext: dialer.DialTunnelContext,
},
Timeout: 60 * time.Second,
}
func openUrl(url string) (io.ReadCloser, error) { func openUrl(url string) (io.ReadCloser, error) {
request, err := http.NewRequest(http.MethodGet, url, nil) ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
response, err := clashHttp.HttpRequest(ctx, url, http.MethodGet, http.Header{"User-Agent": {"ClashMetaForAndroid/" + app.VersionName()}}, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
request.Header.Set("User-Agent", "ClashMetaForAndroid/"+app.VersionName())
response, err := client.Do(request)
if err != nil {
return nil, err
}
return response.Body, nil return response.Body, nil
} }

View File

@ -1,43 +0,0 @@
package tunnel
import (
"context"
"net"
"strconv"
"strings"
"github.com/Dreamacro/clash/component/dialer"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/tunnel"
)
func init() {
dialer.DefaultTunnelDialer = func(context context.Context, network, address string) (net.Conn, error) {
if !strings.HasPrefix(network, "tcp") {
return nil, net.UnknownNetworkError("unsupported network")
}
host, port, err := net.SplitHostPort(address)
dstPort, _ := strconv.Atoi(port)
if err != nil {
return nil, err
}
left, right := net.Pipe()
metadata := &C.Metadata{
NetWork: C.TCP,
Type: C.HTTPS,
SrcIP: loopback,
SrcPort: 65535,
DstPort: uint16(dstPort),
Host: host,
RawSrcAddr: left.RemoteAddr(),
RawDstAddr: left.LocalAddr(),
}
go tunnel.Tunnel.HandleTCPConn(right, metadata)
return left, nil
}
}