2018-07-26 00:04:59 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-09-21 15:27:51 +08:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
2018-07-26 00:04:59 +08:00
|
|
|
)
|
|
|
|
|
2018-08-27 00:06:40 +08:00
|
|
|
// SocketAdapter is a adapter for socks and redir connection
|
|
|
|
type SocketAdapter struct {
|
2018-09-30 12:25:52 +08:00
|
|
|
conn net.Conn
|
|
|
|
metadata *C.Metadata
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
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-09-30 12:25:52 +08:00
|
|
|
// Metadata return destination metadata
|
|
|
|
func (s *SocketAdapter) Metadata() *C.Metadata {
|
|
|
|
return s.metadata
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-05 21:13:29 +08:00
|
|
|
func NewSocket(target socks.Addr, conn net.Conn, source C.SourceType) *SocketAdapter {
|
|
|
|
metadata := parseSocksAddr(target)
|
|
|
|
metadata.Source = source
|
2019-02-02 21:03:13 +08:00
|
|
|
metadata.SourceIP = parseSourceIP(conn)
|
2018-12-05 21:13:29 +08:00
|
|
|
|
2018-08-27 00:06:40 +08:00
|
|
|
return &SocketAdapter{
|
2018-09-30 12:25:52 +08:00
|
|
|
conn: conn,
|
2018-12-05 21:13:29 +08:00
|
|
|
metadata: metadata,
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
}
|