2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2023-04-12 12:57:59 +08:00
|
|
|
"errors"
|
2024-06-12 04:46:13 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2023-09-02 16:25:55 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
2024-04-03 08:42:15 +08:00
|
|
|
"github.com/metacubex/mihomo/component/loopback"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-06-12 04:46:13 +08:00
|
|
|
"github.com/metacubex/mihomo/constant/features"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2024-06-12 04:46:13 +08:00
|
|
|
var DisableLoopBackDetector, _ = strconv.ParseBool(os.Getenv("DISABLE_LOOPBACK_DETECTOR"))
|
|
|
|
|
2018-12-22 23:56:42 +08:00
|
|
|
type Direct struct {
|
|
|
|
*Base
|
2024-04-03 08:42:15 +08:00
|
|
|
loopBack *loopback.Detector
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:33:03 +08:00
|
|
|
type DirectOption struct {
|
|
|
|
BasicOption
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2024-06-12 04:46:13 +08:00
|
|
|
if !features.CMFA && !DisableLoopBackDetector {
|
|
|
|
if err := d.loopBack.CheckConn(metadata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-20 13:11:00 +08:00
|
|
|
}
|
2024-10-04 13:19:41 +08:00
|
|
|
opts = append(opts, dialer.WithResolver(resolver.DirectHostResolver))
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
2018-12-22 23:56:42 +08:00
|
|
|
return nil, err
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2023-12-20 13:11:00 +08:00
|
|
|
return d.loopBack.NewConn(NewConn(c, d)), nil
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2024-06-12 04:46:13 +08:00
|
|
|
if !features.CMFA && !DisableLoopBackDetector {
|
|
|
|
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-20 13:11:00 +08:00
|
|
|
}
|
2023-04-12 12:57:59 +08:00
|
|
|
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
|
|
|
if !metadata.Resolved() {
|
2024-10-04 13:19:41 +08:00
|
|
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DirectHostResolver)
|
2023-04-12 12:57:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("can't resolve ip")
|
|
|
|
}
|
|
|
|
metadata.DstIP = ip
|
|
|
|
}
|
2024-06-15 00:33:03 +08:00
|
|
|
pc, err := dialer.NewDialer(d.Base.DialOptions(opts...)...).ListenPacket(ctx, "udp", "", metadata.AddrPort())
|
2019-04-23 23:29:36 +08:00
|
|
|
if err != nil {
|
2020-01-31 14:43:54 +08:00
|
|
|
return nil, err
|
2019-04-24 10:29:29 +08:00
|
|
|
}
|
2023-12-20 13:11:00 +08:00
|
|
|
return d.loopBack.NewPacketConn(newPacketConn(pc, d)), nil
|
2020-02-17 17:34:19 +08:00
|
|
|
}
|
|
|
|
|
2024-06-18 13:12:44 +08:00
|
|
|
func (d *Direct) IsL3Protocol(metadata *C.Metadata) bool {
|
|
|
|
return true // tell DNSDialer don't send domain to DialContext, avoid lookback to DefaultResolver
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:33:03 +08:00
|
|
|
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),
|
|
|
|
},
|
2024-04-03 08:42:15 +08:00
|
|
|
loopBack: loopback.NewDetector(),
|
2023-08-24 23:33:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func NewDirect() *Direct {
|
2018-12-22 23:56:42 +08:00
|
|
|
return &Direct{
|
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: "DIRECT",
|
|
|
|
tp: C.Direct,
|
|
|
|
udp: true,
|
|
|
|
prefer: C.DualStack,
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
2024-04-03 08:42:15 +08:00
|
|
|
loopBack: loopback.NewDetector(),
|
2018-12-22 23:56:42 +08:00
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2022-01-18 21:09:36 +08:00
|
|
|
|
|
|
|
func NewCompatible() *Direct {
|
|
|
|
return &Direct{
|
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: "COMPATIBLE",
|
|
|
|
tp: C.Compatible,
|
|
|
|
udp: true,
|
|
|
|
prefer: C.DualStack,
|
2022-01-18 21:09:36 +08:00
|
|
|
},
|
2024-04-03 08:42:15 +08:00
|
|
|
loopBack: loopback.NewDetector(),
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
|
|
|
}
|