refactor: move utls roundtripper to pkg/net/utls
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package playback
|
package playback
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
@@ -12,7 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"mal/pkg/net/utls"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -20,9 +19,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
utls "github.com/refraction-networking/utls"
|
|
||||||
"golang.org/x/net/http2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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{
|
var allAnimeUTLSClient = &http.Client{
|
||||||
Transport: &utlsRoundTripper{},
|
Transport: &utls.UtlsRoundTripper{},
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
62
pkg/net/utls/utls.go
Normal file
62
pkg/net/utls/utls.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package utls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
utls "github.com/refraction-networking/utls"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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 == "" {
|
||||||
|
if req.URL.Scheme == "https" {
|
||||||
|
port = "443"
|
||||||
|
} else {
|
||||||
|
port = "80"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addr := net.JoinHostPort(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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user