mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-27 12:35:39 +08:00
de4341c8cd
This reverts commit 8a85c63b08
.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type Direct struct {
|
|
*Base
|
|
}
|
|
|
|
// DialContext implements C.ProxyAdapter
|
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
|
opts = append(opts, dialer.WithDirect())
|
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tcpKeepAlive(c)
|
|
return NewConn(c, d), nil
|
|
}
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
|
opts = append(opts, dialer.WithDirect())
|
|
pc, err := dialer.ListenPacket(ctx, "udp", "", d.Base.DialOptions(opts...)...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newPacketConn(&directPacketConn{pc}, d), nil
|
|
}
|
|
|
|
type directPacketConn struct {
|
|
net.PacketConn
|
|
}
|
|
|
|
func NewDirect() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "DIRECT",
|
|
tp: C.Direct,
|
|
udp: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewCompatible() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "COMPATIBLE",
|
|
tp: C.Compatible,
|
|
udp: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewPass() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "PASS",
|
|
tp: C.Pass,
|
|
udp: true,
|
|
},
|
|
}
|
|
}
|