2018-07-26 00:04:59 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-08-11 22:51:30 +08:00
|
|
|
"net"
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-08-11 22:51:30 +08:00
|
|
|
type PeekedConn struct {
|
|
|
|
net.Conn
|
|
|
|
Peeked []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PeekedConn) Read(p []byte) (n int, err error) {
|
|
|
|
if len(c.Peeked) > 0 {
|
|
|
|
n = copy(p, c.Peeked)
|
|
|
|
c.Peeked = c.Peeked[n:]
|
|
|
|
if len(c.Peeked) == 0 {
|
|
|
|
c.Peeked = nil
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
return c.Conn.Read(p)
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
type HttpAdapter struct {
|
|
|
|
addr *C.Addr
|
2018-08-11 22:51:30 +08:00
|
|
|
conn *PeekedConn
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpAdapter) Close() {
|
2018-08-11 22:51:30 +08:00
|
|
|
h.conn.Close()
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HttpAdapter) Addr() *C.Addr {
|
|
|
|
return h.addr
|
|
|
|
}
|
|
|
|
|
2018-08-11 22:51:30 +08:00
|
|
|
func (h *HttpAdapter) Conn() net.Conn {
|
|
|
|
return h.conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttp(host string, peeked []byte, conn net.Conn) *HttpAdapter {
|
2018-07-26 00:04:59 +08:00
|
|
|
return &HttpAdapter{
|
|
|
|
addr: parseHttpAddr(host),
|
2018-08-11 22:51:30 +08:00
|
|
|
conn: &PeekedConn{
|
|
|
|
Peeked: peeked,
|
|
|
|
Conn: conn,
|
|
|
|
},
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|