2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-12 15:55:39 +00:00
|
|
|
"context"
|
2018-06-10 14:50:03 +00:00
|
|
|
"net"
|
|
|
|
|
2020-02-09 09:02:48 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2018-06-10 14:50:03 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-12-22 15:56:42 +00:00
|
|
|
type Direct struct {
|
|
|
|
*Base
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2022-03-27 16:44:13 +00:00
|
|
|
opts = append(opts, dialer.WithDirect())
|
2021-11-07 08:48:51 +00:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
2018-06-10 14:50:03 +00:00
|
|
|
if err != nil {
|
2018-12-22 15:56:42 +00:00
|
|
|
return nil, err
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-08-29 07:00:12 +00:00
|
|
|
tcpKeepAlive(c)
|
2020-03-21 15:46:49 +00:00
|
|
|
return NewConn(c, d), nil
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:44:53 +00:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 08:48:51 +00:00
|
|
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2022-03-27 16:44:13 +00:00
|
|
|
opts = append(opts, dialer.WithDirect())
|
2021-11-07 08:48:51 +00:00
|
|
|
pc, err := dialer.ListenPacket(ctx, "udp", "", d.Base.DialOptions(opts...)...)
|
2019-04-23 15:29:36 +00:00
|
|
|
if err != nil {
|
2020-01-31 06:43:54 +00:00
|
|
|
return nil, err
|
2019-04-24 02:29:29 +00:00
|
|
|
}
|
2020-02-17 09:34:19 +00:00
|
|
|
return newPacketConn(&directPacketConn{pc}, d), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type directPacketConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
func NewDirect() *Direct {
|
2018-12-22 15:56:42 +00:00
|
|
|
return &Direct{
|
|
|
|
Base: &Base{
|
|
|
|
name: "DIRECT",
|
|
|
|
tp: C.Direct,
|
2019-04-23 15:29:36 +00:00
|
|
|
udp: true,
|
2018-12-22 15:56:42 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2022-01-18 13:09:36 +00:00
|
|
|
|
|
|
|
func NewCompatible() *Direct {
|
|
|
|
return &Direct{
|
|
|
|
Base: &Base{
|
|
|
|
name: "COMPATIBLE",
|
|
|
|
tp: C.Compatible,
|
|
|
|
udp: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 08:36:34 +00:00
|
|
|
|
|
|
|
func NewPass() *Direct {
|
|
|
|
return &Direct{
|
|
|
|
Base: &Base{
|
2022-04-17 06:11:58 +00:00
|
|
|
name: "PASS",
|
2022-03-28 08:36:34 +00:00
|
|
|
tp: C.Pass,
|
|
|
|
udp: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|