sing-box/transport/v2rayhttp/conn.go

152 lines
2.8 KiB
Go
Raw Permalink Normal View History

2022-08-22 22:19:25 +08:00
package v2rayhttp
import (
"io"
"net"
"net/http"
"os"
2023-01-04 15:19:39 +08:00
"sync"
2022-08-22 22:19:25 +08:00
"time"
2022-08-29 10:10:41 +08:00
"github.com/sagernet/sing-box/common/baderror"
2022-08-22 22:19:25 +08:00
"github.com/sagernet/sing/common"
2023-01-04 15:19:39 +08:00
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
N "github.com/sagernet/sing/common/network"
2022-08-22 22:19:25 +08:00
)
type HTTPConn struct {
reader io.Reader
writer io.Writer
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 22:19:25 +08:00
}
func (c *HTTPConn) Read(b []byte) (n int, err error) {
if c.reader == nil {
<-c.create
if c.err != nil {
return 0, c.err
}
}
2022-08-25 10:56:57 +08:00
n, err = c.reader.Read(b)
2022-08-29 10:10:41 +08:00
return n, baderror.WrapH2(err)
2022-08-22 22:19:25 +08:00
}
func (c *HTTPConn) Write(b []byte) (n int, err error) {
2022-08-25 10:56:57 +08:00
n, err = c.writer.Write(b)
2022-08-29 10:10:41 +08:00
return n, baderror.WrapH2(err)
2022-08-22 22:19:25 +08: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 14:56:31 +08:00
if responseWriter, loaded := c.writer.(interface {
2022-11-11 22:08:20 +08:00
SetWriteDeadline(time.Time) error
2022-11-23 14:56:31 +08:00
}); loaded {
return responseWriter.SetWriteDeadline(t)
2022-11-11 22:08:20 +08:00
}
2022-11-23 14:56:31 +08:00
return os.ErrInvalid
2022-08-22 22:19:25 +08:00
}
func (c *HTTPConn) SetReadDeadline(t time.Time) error {
2022-11-23 14:56:31 +08:00
if responseWriter, loaded := c.writer.(interface {
2022-11-11 22:08:20 +08:00
SetReadDeadline(time.Time) error
2022-11-23 14:56:31 +08:00
}); loaded {
return responseWriter.SetReadDeadline(t)
2022-11-11 22:08:20 +08:00
}
2022-11-23 14:56:31 +08:00
return os.ErrInvalid
2022-08-22 22:19:25 +08:00
}
func (c *HTTPConn) SetWriteDeadline(t time.Time) error {
2022-11-23 14:56:31 +08:00
if responseWriter, loaded := c.writer.(interface {
2022-11-11 22:08:20 +08:00
SetWriteDeadline(time.Time) error
2022-11-23 14:56:31 +08:00
}); loaded {
return responseWriter.SetWriteDeadline(t)
2022-11-11 22:08:20 +08:00
}
2022-11-23 14:56:31 +08:00
return os.ErrInvalid
2022-08-22 22:19:25 +08: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 15:19:39 +08: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
}