mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-09-12 11:52:33 +00:00
48 lines
919 B
Go
48 lines
919 B
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) (C.Conn, error) {
|
|
address := net.JoinHostPort(metadata.String(), metadata.DstPort)
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tcpKeepAlive(c)
|
|
return NewConn(c, d), nil
|
|
}
|
|
|
|
// DialUDP implements C.ProxyAdapter
|
|
func (d *Direct) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
|
pc, err := dialer.ListenPacket("udp", "")
|
|
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,
|
|
},
|
|
}
|
|
}
|