refactor: move utls roundtripper to pkg/net/utls
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package playback
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/aes"
|
||||
@@ -12,7 +11,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"mal/pkg/net/utls"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -20,9 +19,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
utls "github.com/refraction-networking/utls"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -50,54 +46,8 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// utlsRoundTripper uses uTLS + HTTP/2 to mimic Firefox and bypass Cloudflare JA3 detection
|
||||
type utlsRoundTripper struct{}
|
||||
|
||||
func (rt *utlsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
dialer := &net.Dialer{Timeout: 10 * time.Second}
|
||||
host := req.URL.Hostname()
|
||||
port := req.URL.Port()
|
||||
if port == "" {
|
||||
port = "443"
|
||||
}
|
||||
addr := host + ":" + port
|
||||
|
||||
rawConn, err := dialer.DialContext(req.Context(), "tcp", addr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("tcp dial: %w", err)
|
||||
}
|
||||
|
||||
uconn := utls.UClient(rawConn, &utls.Config{
|
||||
ServerName: host,
|
||||
NextProtos: []string{"h2", "http/1.1"},
|
||||
}, utls.HelloFirefox_120)
|
||||
|
||||
if err := uconn.HandshakeContext(req.Context()); err != nil {
|
||||
uconn.Close()
|
||||
return nil, fmt.Errorf("utls handshake: %w", err)
|
||||
}
|
||||
|
||||
alpn := uconn.ConnectionState().NegotiatedProtocol
|
||||
if alpn == "h2" {
|
||||
t := &http2.Transport{}
|
||||
cc, err := t.NewClientConn(uconn)
|
||||
if err != nil {
|
||||
uconn.Close()
|
||||
return nil, fmt.Errorf("http2 client conn: %w", err)
|
||||
}
|
||||
return cc.RoundTrip(req)
|
||||
}
|
||||
|
||||
// Fallback to HTTP/1.1
|
||||
if err := req.Write(uconn); err != nil {
|
||||
uconn.Close()
|
||||
return nil, fmt.Errorf("http1 write: %w", err)
|
||||
}
|
||||
return http.ReadResponse(bufio.NewReader(uconn), req)
|
||||
}
|
||||
|
||||
var allAnimeUTLSClient = &http.Client{
|
||||
Transport: &utlsRoundTripper{},
|
||||
Transport: &utls.UtlsRoundTripper{},
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user