mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 00:50:06 +08:00
Some checks failed
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Failing after 1s
Test / test (1.21, ubuntu-latest) (push) Failing after 1s
Test / test (1.22, ubuntu-latest) (push) Failing after 1s
Test / test (1.23, ubuntu-latest) (push) Failing after 1s
Test / test (1.24, ubuntu-latest) (push) Failing after 1s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 1s
The DNS resolution of the overall UDP part has been delayed to the connection initiation stage. During the rule matching process, it will only be triggered when the IP rule without no-resolve is matched. For direct and wireguard outbound, the same logic as the TCP part will be followed, that is, when direct-nameserver (or DNS configured by wireguard) exists, the result of the matching process will be discarded and the domain name will be re-resolved. This re-resolution logic is only effective for fakeip. For reject and DNS outbound, no resolution is required. For other outbound, resolution will still be performed when the connection is initiated, and the domain name will not be sent directly to the remote server at present.
136 lines
4.1 KiB
Go
136 lines
4.1 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"strconv"
|
|
"time"
|
|
|
|
CN "github.com/metacubex/mihomo/common/net"
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
"github.com/metacubex/mihomo/component/proxydialer"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
"github.com/metacubex/mihomo/transport/anytls"
|
|
"github.com/metacubex/mihomo/transport/vmess"
|
|
|
|
M "github.com/metacubex/sing/common/metadata"
|
|
"github.com/metacubex/sing/common/uot"
|
|
)
|
|
|
|
type AnyTLS struct {
|
|
*Base
|
|
client *anytls.Client
|
|
dialer proxydialer.SingDialer
|
|
option *AnyTLSOption
|
|
}
|
|
|
|
type AnyTLSOption struct {
|
|
BasicOption
|
|
Name string `proxy:"name"`
|
|
Server string `proxy:"server"`
|
|
Port int `proxy:"port"`
|
|
Password string `proxy:"password"`
|
|
ALPN []string `proxy:"alpn,omitempty"`
|
|
SNI string `proxy:"sni,omitempty"`
|
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
|
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
|
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
|
}
|
|
|
|
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
c, err := t.client.CreateProxy(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return NewConn(c, t), nil
|
|
}
|
|
|
|
func (t *AnyTLS) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// create tcp
|
|
c, err := t.client.CreateProxy(ctx, uot.RequestDestination(2))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// create uot on tcp
|
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
|
return newPacketConn(CN.NewThreadSafePacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination})), t), nil
|
|
}
|
|
|
|
// SupportUOT implements C.ProxyAdapter
|
|
func (t *AnyTLS) SupportUOT() bool {
|
|
return true
|
|
}
|
|
|
|
// ProxyInfo implements C.ProxyAdapter
|
|
func (t *AnyTLS) ProxyInfo() C.ProxyInfo {
|
|
info := t.Base.ProxyInfo()
|
|
info.DialerProxy = t.option.DialerProxy
|
|
return info
|
|
}
|
|
|
|
// Close implements C.ProxyAdapter
|
|
func (t *AnyTLS) Close() error {
|
|
return t.client.Close()
|
|
}
|
|
|
|
func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
outbound := &AnyTLS{
|
|
Base: &Base{
|
|
name: option.Name,
|
|
addr: addr,
|
|
tp: C.AnyTLS,
|
|
udp: option.UDP,
|
|
tfo: option.TFO,
|
|
mpTcp: option.MPTCP,
|
|
iface: option.Interface,
|
|
rmark: option.RoutingMark,
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
},
|
|
option: &option,
|
|
}
|
|
|
|
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...))
|
|
outbound.dialer = singDialer
|
|
|
|
tOption := anytls.ClientConfig{
|
|
Password: option.Password,
|
|
Server: M.ParseSocksaddrHostPort(option.Server, uint16(option.Port)),
|
|
Dialer: singDialer,
|
|
IdleSessionCheckInterval: time.Duration(option.IdleSessionCheckInterval) * time.Second,
|
|
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
|
MinIdleSession: option.MinIdleSession,
|
|
}
|
|
echConfig, err := option.ECHOpts.Parse()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tlsConfig := &vmess.TLSConfig{
|
|
Host: option.SNI,
|
|
SkipCertVerify: option.SkipCertVerify,
|
|
NextProtos: option.ALPN,
|
|
FingerPrint: option.Fingerprint,
|
|
ClientFingerprint: option.ClientFingerprint,
|
|
ECH: echConfig,
|
|
}
|
|
if tlsConfig.Host == "" {
|
|
tlsConfig.Host = option.Server
|
|
}
|
|
tOption.TLSConfig = tlsConfig
|
|
|
|
client := anytls.NewClient(context.TODO(), tOption)
|
|
outbound.client = client
|
|
|
|
return outbound, nil
|
|
}
|