2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2018-06-10 22:50:03 +08:00
|
|
|
"net"
|
|
|
|
|
2020-02-09 17:02:48 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-06-10 22:50:03 +08:00
|
|
|
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-10-12 23:55:39 +08:00
|
|
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
2020-05-05 12:39:25 +08:00
|
|
|
address := net.JoinHostPort(metadata.String(), metadata.DstPort)
|
2018-12-05 21:13:29 +08:00
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", address)
|
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)
|
2020-03-21 23:46:49 +08:00
|
|
|
return NewConn(c, d), nil
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (d *Direct) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
2020-02-09 17:02:48 +08:00
|
|
|
pc, err := dialer.ListenPacket("udp", "")
|
2019-04-23 23:29:36 +08:00
|
|
|
if err != nil {
|
2020-01-31 14:43:54 +08:00
|
|
|
return nil, err
|
2019-04-24 10:29:29 +08:00
|
|
|
}
|
2020-02-17 17:34:19 +08:00
|
|
|
return newPacketConn(&directPacketConn{pc}, d), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type directPacketConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|