Clash.Meta/adapters/local/http.go

52 lines
772 B
Go
Raw Normal View History

package adapters
import (
2018-08-11 22:51:30 +08:00
"net"
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)
}
type HttpAdapter struct {
addr *C.Addr
2018-08-11 22:51:30 +08:00
conn *PeekedConn
}
func (h *HttpAdapter) Close() {
2018-08-11 22:51:30 +08:00
h.conn.Close()
}
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 {
return &HttpAdapter{
addr: parseHttpAddr(host),
2018-08-11 22:51:30 +08:00
conn: &PeekedConn{
Peeked: peeked,
Conn: conn,
},
}
}