2018-06-10 22:50:03 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-11-21 13:47:46 +08:00
|
|
|
"encoding/json"
|
2018-06-10 22:50:03 +08:00
|
|
|
"net"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirectAdapter is a directly connected adapter
|
|
|
|
type DirectAdapter struct {
|
|
|
|
conn net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close connection
|
|
|
|
func (d *DirectAdapter) Close() {
|
|
|
|
d.conn.Close()
|
|
|
|
}
|
|
|
|
|
2018-06-17 22:41:32 +08:00
|
|
|
// Conn is used to http request
|
2018-06-14 01:00:58 +08:00
|
|
|
func (d *DirectAdapter) Conn() net.Conn {
|
|
|
|
return d.conn
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
type Direct struct{}
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2018-06-16 21:34:13 +08:00
|
|
|
func (d *Direct) Name() string {
|
2018-10-19 20:28:19 +08:00
|
|
|
return "DIRECT"
|
2018-06-16 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
func (d *Direct) Type() C.AdapterType {
|
|
|
|
return C.Direct
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (d *Direct) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) {
|
2018-10-22 21:14:22 +08:00
|
|
|
c, err := net.DialTimeout("tcp", net.JoinHostPort(metadata.String(), metadata.Port), tcpTimeout)
|
2018-06-10 22:50:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-08-29 15:00:12 +08:00
|
|
|
tcpKeepAlive(c)
|
2018-07-26 00:04:59 +08:00
|
|
|
return &DirectAdapter{conn: c}, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func (d *Direct) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]string{
|
|
|
|
"type": d.Type().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
func NewDirect() *Direct {
|
|
|
|
return &Direct{}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|