2018-06-10 22:50:03 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (d *Direct) Dial(metadata *C.Metadata) (C.Conn, error) {
|
2019-05-09 21:00:29 +08:00
|
|
|
address := net.JoinHostPort(metadata.Host, metadata.DstPort)
|
|
|
|
if metadata.DstIP != nil {
|
|
|
|
address = net.JoinHostPort(metadata.DstIP.String(), metadata.DstPort)
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:58:59 +08:00
|
|
|
c, err := dialTimeout("tcp", address, tcpTimeout)
|
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
|
|
|
}
|
2018-08-29 15:00:12 +08:00
|
|
|
tcpKeepAlive(c)
|
2019-08-09 01:28:37 +08:00
|
|
|
return newConn(c, d), nil
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:28:37 +08:00
|
|
|
func (d *Direct) DialUDP(metadata *C.Metadata) (C.PacketConn, net.Addr, error) {
|
2019-04-23 23:29:36 +08:00
|
|
|
pc, err := net.ListenPacket("udp", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
addr, err := resolveUDPAddr("udp", metadata.RemoteAddress())
|
2019-04-24 10:29:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-08-09 01:28:37 +08:00
|
|
|
return newPacketConn(pc, d), addr, nil
|
2019-04-23 23:29:36 +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{
|
|
|
|
name: "DIRECT",
|
|
|
|
tp: C.Direct,
|
2019-04-23 23:29:36 +08:00
|
|
|
udp: true,
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|