clash/adapters/direct.go

54 lines
1003 B
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package adapters
import (
"io"
"net"
C "github.com/Dreamacro/clash/constant"
)
// DirectAdapter is a directly connected adapter
type DirectAdapter struct {
conn net.Conn
}
2018-06-14 01:00:58 +08:00
// ReadWriter is used to handle network traffic
func (d *DirectAdapter) ReadWriter() io.ReadWriter {
2018-06-10 22:50:03 +08:00
return d.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-06-10 22:50:03 +08:00
type Direct struct {
2018-06-17 22:41:32 +08:00
traffic *C.Traffic
2018-06-10 22:50:03 +08:00
}
2018-06-16 21:34:13 +08:00
func (d *Direct) Name() string {
return "Direct"
}
func (d *Direct) Type() C.AdapterType {
return C.Direct
}
2018-06-10 22:50:03 +08:00
func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
2018-06-11 18:36:39 +08:00
c, err := net.Dial("tcp", net.JoinHostPort(addr.String(), addr.Port))
2018-06-10 22:50:03 +08:00
if err != nil {
return
}
c.(*net.TCPConn).SetKeepAlive(true)
2018-06-17 22:41:32 +08:00
return &DirectAdapter{conn: NewTrafficTrack(c, d.traffic)}, nil
2018-06-10 22:50:03 +08:00
}
2018-06-17 22:41:32 +08:00
func NewDirect(traffic *C.Traffic) *Direct {
return &Direct{traffic: traffic}
2018-06-10 22:50:03 +08:00
}