1
0
mirror of https://github.com/fumiama/terasu-cloudflared.git synced 2026-06-26 23:20:27 +08:00

feat: embed terasu inside

This commit is contained in:
源文雨
2025-12-21 16:51:38 +08:00
parent 0d2a7a0385
commit f99b9330df
23 changed files with 1195 additions and 18 deletions

View File

@@ -31,6 +31,7 @@ import (
"sync/atomic"
"time"
"github.com/fumiama/terasu"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/http2/hpack"
"golang.org/x/net/idna"
@@ -3275,13 +3276,22 @@ func traceGot1xxResponseFunc(trace *httptrace.ClientTrace) func(int, textproto.M
// dialTLSWithContext uses tls.Dialer, added in Go 1.15, to open a TLS
// connection.
func (t *Transport) dialTLSWithContext(ctx context.Context, network, addr string, cfg *tls.Config) (*tls.Conn, error) {
dialer := &tls.Dialer{
Config: cfg,
}
dialer := &net.Dialer{}
cn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
tlsCn := cn.(*tls.Conn) // DialContext comment promises this will always succeed
tlsCn := tls.Client(terasu.NewConn(cn), cfg)
if deadline, ok := ctx.Deadline(); ok {
cn.SetDeadline(deadline)
}
err = tlsCn.Handshake()
if _, ok := ctx.Deadline(); ok {
cn.SetDeadline(time.Time{})
}
if err != nil {
cn.Close()
return nil, err
}
return tlsCn, nil
}

View File

@@ -8,6 +8,9 @@ import (
"context"
"crypto/tls"
"net"
"time"
"github.com/fumiama/terasu"
)
func dialWithDialer(ctx context.Context, dialer *net.Dialer, config *Config) (conn net.Conn, err error) {
@@ -16,12 +19,23 @@ func dialWithDialer(ctx context.Context, dialer *net.Dialer, config *Config) (co
conn, err = dialer.DialContext(ctx, "tcp", parseAuthority(config.Location))
case "wss":
tlsDialer := &tls.Dialer{
NetDialer: dialer,
Config: config.TlsConfig,
var rawConn net.Conn
rawConn, err = dialer.DialContext(ctx, "tcp", parseAuthority(config.Location))
if err == nil {
tlsCn := tls.Client(terasu.NewConn(rawConn), config.TlsConfig)
if deadline, ok := ctx.Deadline(); ok {
rawConn.SetDeadline(deadline)
}
err = tlsCn.Handshake()
if _, ok := ctx.Deadline(); ok {
rawConn.SetDeadline(time.Time{})
}
if err != nil {
rawConn.Close()
} else {
conn = tlsCn
}
}
conn, err = tlsDialer.DialContext(ctx, "tcp", parseAuthority(config.Location))
default:
err = ErrBadScheme
}