2019-12-08 04:17:24 +00:00
|
|
|
package inbound
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
2018-07-25 16:04:59 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-08-26 16:06:40 +00:00
|
|
|
// SocketAdapter is a adapter for socks and redir connection
|
|
|
|
type SocketAdapter struct {
|
2019-04-23 15:29:36 +00:00
|
|
|
net.Conn
|
2018-09-30 04:25:52 +00:00
|
|
|
metadata *C.Metadata
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
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-26 16:06:40 +00:00
|
|
|
// NewSocket is SocketAdapter generator
|
2020-04-24 16:39:30 +00:00
|
|
|
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type) *SocketAdapter {
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata := parseSocksAddr(target)
|
2020-04-24 16:39:30 +00:00
|
|
|
metadata.NetWork = C.TCP
|
2019-05-09 13:00:29 +00:00
|
|
|
metadata.Type = source
|
|
|
|
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
|
|
|
metadata.SrcIP = ip
|
|
|
|
metadata.SrcPort = port
|
|
|
|
}
|
2018-12-05 13:13:29 +00:00
|
|
|
|
2018-08-26 16:06:40 +00:00
|
|
|
return &SocketAdapter{
|
2019-04-23 15:29:36 +00:00
|
|
|
Conn: conn,
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata: metadata,
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
}
|