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