mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 07:46:47 +00:00
feat: support network: h3-l4proxy for masque outbound
This commit is contained in:
1 parent
2c6ff7251d
commit
8e2aba40fc
3 files changed
+225
-21
No files matched your search
+73
-21
@@ -41,6 +41,7 @@ type Masque struct {
|
||||
resolver resolver.Resolver
|
||||
uri string
|
||||
h2Transport *http.Transport
|
||||
l4Client *masque.L4Client
|
||||
|
||||
runCtx context.Context
|
||||
runCancel context.CancelFunc
|
||||
@@ -144,6 +145,8 @@ func NewMasque(option MasqueOption) (*Masque, error) {
|
||||
return nil, fmt.Errorf("failed to assert public key as ECDSA")
|
||||
}
|
||||
|
||||
l4proxy := option.Network == "h3-l4proxy"
|
||||
|
||||
uri := option.URI
|
||||
if uri == "" {
|
||||
uri = masque.ConnectURI
|
||||
@@ -153,6 +156,9 @@ func NewMasque(option MasqueOption) (*Masque, error) {
|
||||
sni := option.SNI
|
||||
if sni == "" {
|
||||
sni = masque.ConnectSNI
|
||||
if l4proxy {
|
||||
sni = masque.L4ConnectSNI
|
||||
}
|
||||
}
|
||||
|
||||
tlsConfig, err := masque.PrepareTlsConfig(privKey, ecPubKey, sni, option.SkipCertVerify)
|
||||
@@ -197,30 +203,39 @@ func NewMasque(option MasqueOption) (*Masque, error) {
|
||||
KeepAlivePeriod: 30 * time.Second,
|
||||
}
|
||||
|
||||
prefixes, err := option.Prefixes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outbound.option = option
|
||||
|
||||
mtu := option.MTU
|
||||
if mtu == 0 {
|
||||
mtu = 1280
|
||||
}
|
||||
if len(prefixes) == 0 {
|
||||
return nil, errors.New("missing local address")
|
||||
}
|
||||
outbound.tunDevice, err = wireguard.NewStackDevice(prefixes, uint32(mtu))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create device: %w", err)
|
||||
}
|
||||
|
||||
var has6 bool
|
||||
for _, address := range prefixes {
|
||||
if !address.Addr().Unmap().Is4() {
|
||||
has6 = true
|
||||
break
|
||||
if l4proxy {
|
||||
outbound.l4Client = masque.NewL4Client(outbound.runCtx, outbound.dialQuic)
|
||||
if outbound.udp {
|
||||
log.Warnln("[Masque](%s) L4 proxy mode is not supported for UDP", outbound.name)
|
||||
outbound.udp = false
|
||||
}
|
||||
has6 = true // l4 proxy mode always has ipv6
|
||||
} else {
|
||||
prefixes, err := option.Prefixes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(prefixes) == 0 {
|
||||
return nil, errors.New("missing local address")
|
||||
}
|
||||
outbound.tunDevice, err = wireguard.NewStackDevice(prefixes, uint32(mtu))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create device: %w", err)
|
||||
}
|
||||
|
||||
for _, address := range prefixes {
|
||||
if !address.Addr().Unmap().Is4() {
|
||||
has6 = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,6 +256,15 @@ func NewMasque(option MasqueOption) (*Masque, error) {
|
||||
return outbound, nil
|
||||
}
|
||||
|
||||
func (w *Masque) dialQuic(ctx context.Context) (net.PacketConn, *quic.Conn, error) {
|
||||
pc, quicConn, err := common.DialQuic(ctx, w.addr, w.DialOptions(), w.dialer, w.tlsConfig, w.quicConfig, false)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
common.SetCongestionController(quicConn, w.option.CongestionController, w.option.CWND, w.option.BBRProfile)
|
||||
return pc, quicConn, nil
|
||||
}
|
||||
|
||||
func (w *Masque) run(ctx context.Context) error {
|
||||
if w.running.Load() {
|
||||
return nil
|
||||
@@ -275,11 +299,7 @@ func (w *Masque) run(ctx context.Context) error {
|
||||
}
|
||||
} else {
|
||||
var quicConn *quic.Conn
|
||||
pc, quicConn, err = common.DialQuic(ctx, w.addr, w.DialOptions(), w.dialer, w.tlsConfig, w.quicConfig, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
common.SetCongestionController(quicConn, w.option.CongestionController, w.option.CWND, w.option.BBRProfile)
|
||||
pc, quicConn, err = w.dialQuic(ctx)
|
||||
|
||||
closer, ipConn, err = masque.ConnectTunnel(ctx, quicConn, w.uri)
|
||||
if err != nil {
|
||||
@@ -358,10 +378,39 @@ func (w *Masque) Close() error {
|
||||
if w.tunDevice != nil {
|
||||
w.tunDevice.Close()
|
||||
}
|
||||
if w.l4Client != nil {
|
||||
w.l4Client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Masque) dialContextL4(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||
var conn net.Conn
|
||||
if !metadata.Resolved() || w.resolver != nil {
|
||||
r := resolver.DefaultResolver
|
||||
if w.resolver != nil {
|
||||
r = w.resolver
|
||||
}
|
||||
options := w.DialOptions()
|
||||
options = append(options, dialer.WithResolver(r))
|
||||
options = append(options, dialer.WithNetDialer(w.l4Client))
|
||||
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
||||
} else {
|
||||
conn, err = w.l4Client.DialContext(ctx, "tcp", metadata.AddrPort().String())
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if conn == nil {
|
||||
return nil, errors.New("conn is nil")
|
||||
}
|
||||
return NewConn(conn, w), nil
|
||||
}
|
||||
|
||||
func (w *Masque) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||
if w.l4Client != nil {
|
||||
return w.dialContextL4(ctx, metadata)
|
||||
}
|
||||
var conn net.Conn
|
||||
if err = w.run(ctx); err != nil {
|
||||
return nil, err
|
||||
@@ -388,6 +437,9 @@ func (w *Masque) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Con
|
||||
}
|
||||
|
||||
func (w *Masque) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||
if w.l4Client != nil {
|
||||
return nil, errors.New("masque L4 proxy mode is not supported for UDP")
|
||||
}
|
||||
var pc net.PacketConn
|
||||
if err = w.run(ctx); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -1228,6 +1228,21 @@ proxies: # socks5
|
||||
# dns: [ 1.1.1.1, 8.8.8.8 ] # 仅在 remote-dns-resolve 为 true 时生效
|
||||
# congestion-controller: bbr # 默认不开启
|
||||
|
||||
# masque-h3-l4proxy
|
||||
- name: "masque-h3-l4proxy"
|
||||
type: masque
|
||||
server: 162.159.198.1
|
||||
port: 443
|
||||
private-key: MHcCAQEEILI1eOtnbEIh89Fj4yNDuFR6UjayCKI3NdLl3DhetimWoAoGCCqGSM49AwEHoUQDQgAEgyXrE8v+hHsHy3ewSb3WcRjYgCrM9T9hiE0Uv6k2DZ1+4kefrDT9v1Q/8wdRigTf6t6gGNUV8W+IUMdrfUt+9g==
|
||||
public-key: MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEIaU7MToJm9NKp8YfGxR6r+/h4mcG7SxI8tsW8OR1A5tv/zCzVbCRRh2t87/kxnP6lAy0lkr7qYwu+ox+k3dr6w==
|
||||
udp: false # 目前 l4proxy 模式不支持 udp
|
||||
network: h3-l4proxy
|
||||
# 一个出站代理的标识。当值不为空时,将使用指定的 proxy 发出连接
|
||||
# dialer-proxy: "ss1"
|
||||
# remote-dns-resolve: true # 强制 dns 远程解析,默认值为 false
|
||||
# dns: [ tls://1.1.1.1, tls://1.0.0.1 ] # 仅在 remote-dns-resolve 为 true 时生效
|
||||
# congestion-controller: bbr # 默认不开启
|
||||
|
||||
# masque-h2
|
||||
- name: "masque-h2"
|
||||
type: masque
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package masque
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"unsafe"
|
||||
|
||||
"github.com/metacubex/mihomo/common/contextutils"
|
||||
|
||||
"github.com/metacubex/http"
|
||||
"github.com/metacubex/quic-go"
|
||||
"github.com/metacubex/quic-go/http3"
|
||||
"golang.org/x/sync/semaphore"
|
||||
)
|
||||
|
||||
const (
|
||||
L4ConnectSNI = "consumer-masque-proxy.cloudflareclient.com"
|
||||
)
|
||||
|
||||
type DialQuicFn func(ctx context.Context) (net.PacketConn, *quic.Conn, error)
|
||||
|
||||
type L4Client struct {
|
||||
dialFn DialQuicFn
|
||||
runLock *semaphore.Weighted
|
||||
runCtx context.Context
|
||||
clientConn *http3.ClientConn
|
||||
}
|
||||
|
||||
func NewL4Client(runCtx context.Context, dialFn DialQuicFn) *L4Client {
|
||||
return &L4Client{
|
||||
dialFn: dialFn,
|
||||
runLock: semaphore.NewWeighted(1),
|
||||
runCtx: runCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes the client.
|
||||
// The caller should cancel runCtx before calling Close
|
||||
func (c *L4Client) Close() error {
|
||||
return c.closeConn(nil)
|
||||
}
|
||||
|
||||
func (c *L4Client) closeConn(clientConn *http3.ClientConn) error {
|
||||
_ = c.runLock.Acquire(context.Background(), 1) // background context never returns error
|
||||
if clientConn == nil {
|
||||
clientConn = c.clientConn
|
||||
}
|
||||
if c.clientConn == clientConn {
|
||||
c.clientConn = nil
|
||||
}
|
||||
c.runLock.Release(1)
|
||||
|
||||
if clientConn != nil {
|
||||
return clientConn.CloseWithError(0, "client closed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *L4Client) dialConn(ctx context.Context) (*http3.ClientConn, error) {
|
||||
err := c.runLock.Acquire(ctx, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer c.runLock.Release(1)
|
||||
if c.clientConn != nil {
|
||||
return c.clientConn, nil
|
||||
}
|
||||
|
||||
if err = c.runCtx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dialCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
stop := contextutils.AfterFunc(c.runCtx, cancel)
|
||||
defer stop()
|
||||
|
||||
_, quicConn, err := c.dialFn(dialCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tr := &http3.Transport{}
|
||||
clientConn := tr.NewClientConn(quicConn)
|
||||
c.clientConn = clientConn
|
||||
return clientConn, nil
|
||||
}
|
||||
|
||||
func (c *L4Client) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
clientConn, err := c.dialConn(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stream, err := clientConn.OpenRequestStream(ctx)
|
||||
if err != nil {
|
||||
_ = c.closeConn(clientConn) // close underlay connection if we failed to open a stream
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodConnect, "https://"+address, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Host = address
|
||||
if err = stream.SendRequestHeader(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := stream.ReadResponse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode > 299 {
|
||||
return nil, fmt.Errorf("CONNECT rejected with status %d", response.StatusCode)
|
||||
}
|
||||
return &l4StreamConn{stream, clientConn}, nil
|
||||
}
|
||||
|
||||
type l4StreamConn struct {
|
||||
*http3.RequestStream
|
||||
clientConn *http3.ClientConn
|
||||
}
|
||||
|
||||
func (c *l4StreamConn) quicConn() *quic.Conn {
|
||||
return *(**quic.Conn)(unsafe.Pointer(c.clientConn)) // the first field of the http3.ClientConn is a *quic.Conn
|
||||
}
|
||||
|
||||
func (c *l4StreamConn) LocalAddr() net.Addr {
|
||||
return c.quicConn().LocalAddr()
|
||||
}
|
||||
|
||||
func (c *l4StreamConn) RemoteAddr() net.Addr {
|
||||
return c.quicConn().RemoteAddr()
|
||||
}
|
||||
|
||||
func (c *l4StreamConn) Close() error {
|
||||
c.RequestStream.CancelRead(0)
|
||||
return c.RequestStream.Close()
|
||||
}
|
||||
Reference in new issue
Block a user