2018-07-26 00:04:59 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/riobard/go-shadowsocks2/socks"
|
|
|
|
)
|
|
|
|
|
2018-08-27 00:06:40 +08:00
|
|
|
// SocketAdapter is a adapter for socks and redir connection
|
|
|
|
type SocketAdapter struct {
|
2018-07-26 00:04:59 +08:00
|
|
|
conn net.Conn
|
|
|
|
addr *C.Addr
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:18:58 +08:00
|
|
|
// Close socks and redir connection
|
2018-08-27 00:06:40 +08:00
|
|
|
func (s *SocketAdapter) Close() {
|
2018-07-26 00:04:59 +08:00
|
|
|
s.conn.Close()
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:18:58 +08:00
|
|
|
// Addr return destination address
|
2018-08-27 00:06:40 +08:00
|
|
|
func (s *SocketAdapter) Addr() *C.Addr {
|
2018-07-26 00:04:59 +08:00
|
|
|
return s.addr
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:18:58 +08:00
|
|
|
// Conn return raw net.Conn
|
2018-08-27 00:06:40 +08:00
|
|
|
func (s *SocketAdapter) Conn() net.Conn {
|
2018-07-26 00:04:59 +08:00
|
|
|
return s.conn
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:06:40 +08:00
|
|
|
// NewSocket is SocketAdapter generator
|
|
|
|
func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
|
|
|
|
return &SocketAdapter{
|
2018-07-26 00:04:59 +08:00
|
|
|
conn: conn,
|
|
|
|
addr: parseSocksAddr(target),
|
|
|
|
}
|
|
|
|
}
|