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.
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
"github.com/metacubex/mihomo/component/loopback"
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
)
|
|
|
|
type Direct struct {
|
|
*Base
|
|
loopBack *loopback.Detector
|
|
}
|
|
|
|
type DirectOption struct {
|
|
BasicOption
|
|
Name string `proxy:"name"`
|
|
}
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
if err := d.loopBack.CheckConn(metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
opts := d.DialOptions()
|
|
opts = append(opts, dialer.WithResolver(resolver.DirectHostResolver))
|
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return d.loopBack.NewConn(NewConn(c, d)), nil
|
|
}
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
|
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := d.ResolveUDP(ctx, metadata); err != nil {
|
|
return nil, err
|
|
}
|
|
pc, err := dialer.NewDialer(d.DialOptions()...).ListenPacket(ctx, "udp", "", metadata.AddrPort())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return d.loopBack.NewPacketConn(newPacketConn(pc, d)), nil
|
|
}
|
|
|
|
func (d *Direct) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
|
if (!metadata.Resolved() || resolver.DirectHostResolver != resolver.DefaultResolver) && metadata.Host != "" {
|
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DirectHostResolver)
|
|
if err != nil {
|
|
return fmt.Errorf("can't resolve ip: %w", err)
|
|
}
|
|
metadata.DstIP = ip
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *Direct) IsL3Protocol(metadata *C.Metadata) bool {
|
|
return true // tell DNSDialer don't send domain to DialContext, avoid lookback to DefaultResolver
|
|
}
|
|
|
|
func NewDirectWithOption(option DirectOption) *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: option.Name,
|
|
tp: C.Direct,
|
|
udp: true,
|
|
tfo: option.TFO,
|
|
mpTcp: option.MPTCP,
|
|
iface: option.Interface,
|
|
rmark: option.RoutingMark,
|
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
},
|
|
loopBack: loopback.NewDetector(),
|
|
}
|
|
}
|
|
|
|
func NewDirect() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "DIRECT",
|
|
tp: C.Direct,
|
|
udp: true,
|
|
prefer: C.DualStack,
|
|
},
|
|
loopBack: loopback.NewDetector(),
|
|
}
|
|
}
|
|
|
|
func NewCompatible() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "COMPATIBLE",
|
|
tp: C.Compatible,
|
|
udp: true,
|
|
prefer: C.DualStack,
|
|
},
|
|
loopBack: loopback.NewDetector(),
|
|
}
|
|
}
|