mihomo/adapters/outbound/direct.go

46 lines
860 B
Go
Raw Normal View History

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-03-03 11:59:07 +08:00
func (d *Direct) Dial(metadata *C.Metadata) (net.Conn, error) {
2018-12-05 21:13:29 +08:00
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)
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
}
tcpKeepAlive(c)
2018-12-22 23:56:42 +08:00
return c, nil
2018-11-21 13:47:46 +08:00
}
2019-04-23 23:29:36 +08:00
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, _ := net.ResolveUDPAddr("udp", net.JoinHostPort(metadata.String(), metadata.Port))
return pc, addr, nil
}
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
}