2022-08-22 14:19:25 +00:00
|
|
|
package v2rayhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-01-04 07:19:39 +00:00
|
|
|
"sync"
|
2022-08-22 14:19:25 +00:00
|
|
|
"time"
|
|
|
|
|
2022-08-29 02:10:41 +00:00
|
|
|
"github.com/sagernet/sing-box/common/baderror"
|
2022-08-22 14:19:25 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2023-01-04 07:19:39 +00:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-08-22 14:19:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPConn struct {
|
|
|
|
reader io.Reader
|
|
|
|
writer io.Writer
|
2022-09-11 02:22:52 +00:00
|
|
|
create chan struct{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHTTPConn(reader io.Reader, writer io.Writer) HTTPConn {
|
|
|
|
return HTTPConn{
|
|
|
|
reader: reader,
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLateHTTPConn(writer io.Writer) *HTTPConn {
|
|
|
|
return &HTTPConn{
|
|
|
|
create: make(chan struct{}),
|
|
|
|
writer: writer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) setup(reader io.Reader, err error) {
|
|
|
|
c.reader = reader
|
|
|
|
c.err = err
|
|
|
|
close(c.create)
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) Read(b []byte) (n int, err error) {
|
2022-09-11 02:22:52 +00:00
|
|
|
if c.reader == nil {
|
|
|
|
<-c.create
|
|
|
|
if c.err != nil {
|
|
|
|
return 0, c.err
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 02:56:57 +00:00
|
|
|
n, err = c.reader.Read(b)
|
2022-08-29 02:10:41 +00:00
|
|
|
return n, baderror.WrapH2(err)
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) Write(b []byte) (n int, err error) {
|
2022-08-25 02:56:57 +00:00
|
|
|
n, err = c.writer.Write(b)
|
2022-08-29 02:10:41 +00:00
|
|
|
return n, baderror.WrapH2(err)
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) Close() error {
|
|
|
|
return common.Close(c.reader, c.writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) LocalAddr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) RemoteAddr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) SetDeadline(t time.Time) error {
|
2022-11-23 06:56:31 +00:00
|
|
|
if responseWriter, loaded := c.writer.(interface {
|
2022-11-11 14:08:20 +00:00
|
|
|
SetWriteDeadline(time.Time) error
|
2022-11-23 06:56:31 +00:00
|
|
|
}); loaded {
|
|
|
|
return responseWriter.SetWriteDeadline(t)
|
2022-11-11 14:08:20 +00:00
|
|
|
}
|
2022-11-23 06:56:31 +00:00
|
|
|
return os.ErrInvalid
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) SetReadDeadline(t time.Time) error {
|
2022-11-23 06:56:31 +00:00
|
|
|
if responseWriter, loaded := c.writer.(interface {
|
2022-11-11 14:08:20 +00:00
|
|
|
SetReadDeadline(time.Time) error
|
2022-11-23 06:56:31 +00:00
|
|
|
}); loaded {
|
|
|
|
return responseWriter.SetReadDeadline(t)
|
2022-11-11 14:08:20 +00:00
|
|
|
}
|
2022-11-23 06:56:31 +00:00
|
|
|
return os.ErrInvalid
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPConn) SetWriteDeadline(t time.Time) error {
|
2022-11-23 06:56:31 +00:00
|
|
|
if responseWriter, loaded := c.writer.(interface {
|
2022-11-11 14:08:20 +00:00
|
|
|
SetWriteDeadline(time.Time) error
|
2022-11-23 06:56:31 +00:00
|
|
|
}); loaded {
|
|
|
|
return responseWriter.SetWriteDeadline(t)
|
2022-11-11 14:08:20 +00:00
|
|
|
}
|
2022-11-23 06:56:31 +00:00
|
|
|
return os.ErrInvalid
|
2022-08-22 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHTTPConn struct {
|
|
|
|
HTTPConn
|
|
|
|
flusher http.Flusher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerHTTPConn) Write(b []byte) (n int, err error) {
|
|
|
|
n, err = c.writer.Write(b)
|
|
|
|
if err == nil {
|
|
|
|
c.flusher.Flush()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-01-04 07:19:39 +00:00
|
|
|
|
|
|
|
type HTTP2ConnWrapper struct {
|
|
|
|
N.ExtendedConn
|
|
|
|
access sync.Mutex
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTP2Wrapper(conn net.Conn) *HTTP2ConnWrapper {
|
|
|
|
return &HTTP2ConnWrapper{
|
|
|
|
ExtendedConn: bufio.NewExtendedConn(conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *HTTP2ConnWrapper) Write(p []byte) (n int, err error) {
|
|
|
|
w.access.Lock()
|
|
|
|
defer w.access.Unlock()
|
|
|
|
if w.closed {
|
|
|
|
return 0, net.ErrClosed
|
|
|
|
}
|
|
|
|
return w.ExtendedConn.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *HTTP2ConnWrapper) WriteBuffer(buffer *buf.Buffer) error {
|
|
|
|
w.access.Lock()
|
|
|
|
defer w.access.Unlock()
|
|
|
|
if w.closed {
|
|
|
|
return net.ErrClosed
|
|
|
|
}
|
|
|
|
return w.ExtendedConn.WriteBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *HTTP2ConnWrapper) CloseWrapper() {
|
|
|
|
w.access.Lock()
|
|
|
|
defer w.access.Unlock()
|
|
|
|
w.closed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *HTTP2ConnWrapper) Upstream() any {
|
|
|
|
return w.ExtendedConn
|
|
|
|
}
|