mihomo/adapter/outbound/direct.go

86 lines
2.0 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
2018-06-10 22:50:03 +08:00
import (
"context"
"errors"
N "github.com/Dreamacro/clash/common/net"
2020-02-09 17:02:48 +08:00
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
2018-06-10 22:50:03 +08:00
C "github.com/Dreamacro/clash/constant"
)
2018-12-22 23:56:42 +08:00
type Direct struct {
*Base
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
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
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
}
N.TCPKeepAlive(c)
return NewConn(c, d), nil
2018-11-21 13:47:46 +08:00
}
// ListenPacketContext implements C.ProxyAdapter
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
if !metadata.Resolved() {
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DefaultResolver)
if err != nil {
return nil, errors.New("can't resolve ip")
}
metadata.DstIP = ip
}
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", metadata.DstIP), "", d.Base.DialOptions(opts...)...)
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
}
return newPacketConn(pc, d), nil
2020-02-17 17:34:19 +08:00
}
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),
},
}
}
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
},
}
2018-06-10 22:50:03 +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,
},
}
}