mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
49 lines
905 B
Go
49 lines
905 B
Go
package adapters
|
|
|
|
import (
|
|
"net"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type Direct struct {
|
|
*Base
|
|
}
|
|
|
|
func (d *Direct) Dial(metadata *C.Metadata) (net.Conn, error) {
|
|
address := net.JoinHostPort(metadata.Host, metadata.Port)
|
|
if metadata.IP != nil {
|
|
address = net.JoinHostPort(metadata.IP.String(), metadata.Port)
|
|
}
|
|
|
|
c, err := net.DialTimeout("tcp", address, tcpTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tcpKeepAlive(c)
|
|
return c, nil
|
|
}
|
|
|
|
func (d *Direct) DialUDP(metadata *C.Metadata) (net.PacketConn, net.Addr, error) {
|
|
pc, err := net.ListenPacket("udp", "")
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(metadata.String(), metadata.Port))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return pc, addr, nil
|
|
}
|
|
|
|
func NewDirect() *Direct {
|
|
return &Direct{
|
|
Base: &Base{
|
|
name: "DIRECT",
|
|
tp: C.Direct,
|
|
udp: true,
|
|
},
|
|
}
|
|
}
|