2016-10-31 17:26:46 -04:00
|
|
|
package http
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2016-10-31 17:26:46 -04:00
|
|
|
import (
|
2020-06-03 03:16:03 -04:00
|
|
|
"bufio"
|
2016-10-31 17:26:46 -04:00
|
|
|
"bytes"
|
2017-01-12 16:47:10 -05:00
|
|
|
"context"
|
2016-11-03 18:14:27 -04:00
|
|
|
"io"
|
2016-11-02 17:26:21 -04:00
|
|
|
"net"
|
2016-11-06 07:38:32 -05:00
|
|
|
"net/http"
|
2016-12-06 05:03:42 -05:00
|
|
|
"strings"
|
2016-11-06 07:38:32 -05:00
|
|
|
"time"
|
2016-12-09 06:08:25 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
2016-10-31 17:26:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-04-27 05:37:25 -04:00
|
|
|
// CRLF is the line ending in HTTP header
|
|
|
|
CRLF = "\r\n"
|
|
|
|
|
|
|
|
// ENDING is the double line ending between HTTP header and body.
|
2016-10-31 17:26:46 -04:00
|
|
|
ENDING = CRLF + CRLF
|
2016-12-16 11:01:47 -05:00
|
|
|
|
2016-12-19 06:16:57 -05:00
|
|
|
// max length of HTTP header. Safety precaution for DDoS attack.
|
2016-12-16 11:01:47 -05:00
|
|
|
maxHeaderLength = 8192
|
2016-10-31 17:26:46 -04:00
|
|
|
)
|
|
|
|
|
2016-12-06 05:03:42 -05:00
|
|
|
var (
|
2017-04-08 19:43:25 -04:00
|
|
|
ErrHeaderToLong = newError("Header too long.")
|
2020-06-03 03:16:03 -04:00
|
|
|
|
|
|
|
ErrHeaderMisMatch = newError("Header Mismatch.")
|
2016-12-06 05:03:42 -05:00
|
|
|
)
|
|
|
|
|
2016-11-04 20:50:51 -04:00
|
|
|
type Reader interface {
|
2016-12-09 05:35:27 -05:00
|
|
|
Read(io.Reader) (*buf.Buffer, error)
|
2016-11-04 20:50:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Writer interface {
|
|
|
|
Write(io.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoOpReader struct{}
|
|
|
|
|
2017-04-27 05:37:25 -04:00
|
|
|
func (NoOpReader) Read(io.Reader) (*buf.Buffer, error) {
|
2016-11-04 20:50:51 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoOpWriter struct{}
|
|
|
|
|
2017-04-27 05:37:25 -04:00
|
|
|
func (NoOpWriter) Write(io.Writer) error {
|
2016-11-04 20:50:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:14:27 -04:00
|
|
|
type HeaderReader struct {
|
2020-06-03 03:16:03 -04:00
|
|
|
req *http.Request
|
|
|
|
expectedHeader *RequestConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HeaderReader) ExpectThisRequest(expectedHeader *RequestConfig) *HeaderReader {
|
|
|
|
h.expectedHeader = expectedHeader
|
|
|
|
return h
|
2016-11-03 18:14:27 -04:00
|
|
|
}
|
|
|
|
|
2020-06-03 03:16:03 -04:00
|
|
|
func (h *HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) {
|
2017-04-15 15:19:21 -04:00
|
|
|
buffer := buf.New()
|
2018-04-02 14:00:50 -04:00
|
|
|
totalBytes := int32(0)
|
2016-12-19 06:16:57 -05:00
|
|
|
endingDetected := false
|
2020-06-03 03:16:03 -04:00
|
|
|
|
|
|
|
var headerBuf bytes.Buffer
|
|
|
|
|
2016-12-16 11:01:47 -05:00
|
|
|
for totalBytes < maxHeaderLength {
|
2018-11-02 10:01:33 -04:00
|
|
|
_, err := buffer.ReadFrom(reader)
|
2016-11-03 18:14:27 -04:00
|
|
|
if err != nil {
|
2017-04-27 05:29:44 -04:00
|
|
|
buffer.Release()
|
2016-11-03 18:14:27 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-06 05:03:42 -05:00
|
|
|
if n := bytes.Index(buffer.Bytes(), []byte(ENDING)); n != -1 {
|
2020-06-03 03:16:03 -04:00
|
|
|
headerBuf.Write(buffer.BytesRange(0, int32(n+len(ENDING))))
|
2018-04-19 17:48:38 -04:00
|
|
|
buffer.Advance(int32(n + len(ENDING)))
|
2016-12-19 06:16:57 -05:00
|
|
|
endingDetected = true
|
2016-11-03 18:14:27 -04:00
|
|
|
break
|
|
|
|
}
|
2018-11-02 16:34:04 -04:00
|
|
|
lenEnding := int32(len(ENDING))
|
|
|
|
if buffer.Len() >= lenEnding {
|
|
|
|
totalBytes += buffer.Len() - lenEnding
|
2020-06-03 03:16:03 -04:00
|
|
|
headerBuf.Write(buffer.BytesRange(0, buffer.Len()-lenEnding))
|
2018-11-02 16:34:04 -04:00
|
|
|
leftover := buffer.BytesFrom(-lenEnding)
|
|
|
|
buffer.Clear()
|
|
|
|
copy(buffer.Extend(lenEnding), leftover)
|
2020-06-03 03:44:25 -04:00
|
|
|
|
|
|
|
if _, err := readRequest(bufio.NewReader(bytes.NewReader(headerBuf.Bytes())), false); err != io.ErrUnexpectedEOF {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-03 18:14:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-06-03 03:16:03 -04:00
|
|
|
|
2016-12-19 06:16:57 -05:00
|
|
|
if !endingDetected {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, ErrHeaderToLong
|
|
|
|
}
|
2020-06-03 03:16:03 -04:00
|
|
|
|
|
|
|
if h.expectedHeader == nil {
|
|
|
|
if buffer.IsEmpty() {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
// Parse the request
|
2020-06-03 03:16:03 -04:00
|
|
|
if req, err := readRequest(bufio.NewReader(bytes.NewReader(headerBuf.Bytes())), false); err != nil {
|
|
|
|
return nil, err
|
2020-11-21 16:05:01 -05:00
|
|
|
} else { // nolint: golint
|
2020-06-03 03:16:03 -04:00
|
|
|
h.req = req
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
// Check req
|
2020-06-03 03:16:03 -04:00
|
|
|
path := h.req.URL.Path
|
2020-10-11 07:22:46 -04:00
|
|
|
hasThisURI := false
|
2020-06-03 03:16:03 -04:00
|
|
|
for _, u := range h.expectedHeader.Uri {
|
|
|
|
if u == path {
|
2020-10-11 07:22:46 -04:00
|
|
|
hasThisURI = true
|
2020-06-03 03:16:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
if !hasThisURI {
|
2020-06-03 03:16:03 -04:00
|
|
|
return nil, ErrHeaderMisMatch
|
|
|
|
}
|
|
|
|
|
|
|
|
if buffer.IsEmpty() {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:14:27 -04:00
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HeaderWriter struct {
|
2016-12-09 05:35:27 -05:00
|
|
|
header *buf.Buffer
|
2016-11-03 18:14:27 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 05:35:27 -05:00
|
|
|
func NewHeaderWriter(header *buf.Buffer) *HeaderWriter {
|
2016-11-04 16:59:19 -04:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 16:17:58 -04:00
|
|
|
func (w *HeaderWriter) Write(writer io.Writer) error {
|
|
|
|
if w.header == nil {
|
2016-11-03 18:14:27 -04:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-28 09:03:40 -04:00
|
|
|
err := buf.WriteAllBytes(writer, w.header.Bytes())
|
2017-04-13 16:17:58 -04:00
|
|
|
w.header.Release()
|
|
|
|
w.header = nil
|
2016-11-03 18:14:27 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
type Conn struct {
|
2016-11-02 17:26:21 -04:00
|
|
|
net.Conn
|
|
|
|
|
2020-06-03 03:16:03 -04:00
|
|
|
readBuffer *buf.Buffer
|
|
|
|
oneTimeReader Reader
|
|
|
|
oneTimeWriter Writer
|
|
|
|
errorWriter Writer
|
|
|
|
errorMismatchWriter Writer
|
|
|
|
errorTooLongWriter Writer
|
2020-10-11 07:22:46 -04:00
|
|
|
errReason error
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func NewConn(conn net.Conn, reader Reader, writer Writer, errorWriter Writer, errorMismatchWriter Writer, errorTooLongWriter Writer) *Conn {
|
|
|
|
return &Conn{
|
2020-06-03 03:16:03 -04:00
|
|
|
Conn: conn,
|
|
|
|
oneTimeReader: reader,
|
|
|
|
oneTimeWriter: writer,
|
|
|
|
errorWriter: errorWriter,
|
|
|
|
errorMismatchWriter: errorMismatchWriter,
|
|
|
|
errorTooLongWriter: errorTooLongWriter,
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 17:26:46 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (c *Conn) Read(b []byte) (int, error) {
|
2017-04-13 16:17:58 -04:00
|
|
|
if c.oneTimeReader != nil {
|
|
|
|
buffer, err := c.oneTimeReader.Read(c.Conn)
|
2016-11-03 18:14:27 -04:00
|
|
|
if err != nil {
|
2020-06-03 03:16:03 -04:00
|
|
|
c.errReason = err
|
2016-11-03 18:14:27 -04:00
|
|
|
return 0, err
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2017-04-13 16:17:58 -04:00
|
|
|
c.readBuffer = buffer
|
|
|
|
c.oneTimeReader = nil
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2017-04-19 15:27:21 -04:00
|
|
|
if !c.readBuffer.IsEmpty() {
|
|
|
|
nBytes, _ := c.readBuffer.Read(b)
|
|
|
|
if c.readBuffer.IsEmpty() {
|
2017-04-13 16:17:58 -04:00
|
|
|
c.readBuffer.Release()
|
|
|
|
c.readBuffer = nil
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2017-04-19 15:27:21 -04:00
|
|
|
return nBytes, nil
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2016-10-31 17:26:46 -04:00
|
|
|
|
2017-04-13 16:17:58 -04:00
|
|
|
return c.Conn.Read(b)
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2017-04-21 09:36:05 -04:00
|
|
|
// Write implements io.Writer.
|
2020-10-11 07:22:46 -04:00
|
|
|
func (c *Conn) Write(b []byte) (int, error) {
|
2017-04-13 16:17:58 -04:00
|
|
|
if c.oneTimeWriter != nil {
|
|
|
|
err := c.oneTimeWriter.Write(c.Conn)
|
|
|
|
c.oneTimeWriter = nil
|
2016-10-31 17:26:46 -04:00
|
|
|
if err != nil {
|
2016-11-02 17:26:21 -04:00
|
|
|
return 0, err
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 16:17:58 -04:00
|
|
|
return c.Conn.Write(b)
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2016-12-14 06:25:22 -05:00
|
|
|
// Close implements net.Conn.Close().
|
2020-10-11 07:22:46 -04:00
|
|
|
func (c *Conn) Close() error {
|
2017-04-13 16:17:58 -04:00
|
|
|
if c.oneTimeWriter != nil && c.errorWriter != nil {
|
2016-12-14 06:25:22 -05:00
|
|
|
// Connection is being closed but header wasn't sent. This means the client request
|
|
|
|
// is probably not valid. Sending back a server error header in this case.
|
2020-06-03 03:16:03 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
// Write response based on error reason
|
|
|
|
switch c.errReason {
|
|
|
|
case ErrHeaderMisMatch:
|
2020-06-03 03:16:03 -04:00
|
|
|
c.errorMismatchWriter.Write(c.Conn)
|
2020-10-11 07:22:46 -04:00
|
|
|
case ErrHeaderToLong:
|
2020-06-03 03:16:03 -04:00
|
|
|
c.errorTooLongWriter.Write(c.Conn)
|
2020-10-11 07:22:46 -04:00
|
|
|
default:
|
2020-06-03 03:16:03 -04:00
|
|
|
c.errorWriter.Write(c.Conn)
|
|
|
|
}
|
2016-12-14 06:25:22 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 16:17:58 -04:00
|
|
|
return c.Conn.Close()
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2016-12-14 06:25:22 -05:00
|
|
|
func formResponseHeader(config *ResponseConfig) *HeaderWriter {
|
2017-04-15 15:19:21 -04:00
|
|
|
header := buf.New()
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(strings.Join([]string{config.GetFullVersion(), config.GetStatusValue().Code, config.GetStatusValue().Reason}, " ")))
|
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-10-31 17:26:46 -04:00
|
|
|
|
2016-11-02 17:26:21 -04:00
|
|
|
headers := config.PickHeaders()
|
|
|
|
for _, h := range headers {
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(h))
|
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
2016-12-14 06:25:22 -05:00
|
|
|
if !config.HasHeader("Date") {
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString("Date: "))
|
|
|
|
common.Must2(header.WriteString(time.Now().Format(http.TimeFormat)))
|
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-12-14 06:25:22 -05:00
|
|
|
}
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-11-03 18:14:27 -04:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
type Authenticator struct {
|
2016-12-14 06:25:22 -05:00
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (a Authenticator) GetClientWriter() *HeaderWriter {
|
2017-04-15 15:19:21 -04:00
|
|
|
header := buf.New()
|
2017-04-13 16:17:58 -04:00
|
|
|
config := a.config.Request
|
2020-10-11 07:22:46 -04:00
|
|
|
common.Must2(header.WriteString(strings.Join([]string{config.GetMethodValue(), config.PickURI(), config.GetFullVersion()}, " ")))
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-10-31 17:26:46 -04:00
|
|
|
|
2016-11-02 17:26:21 -04:00
|
|
|
headers := config.PickHeaders()
|
2016-10-31 17:26:46 -04:00
|
|
|
for _, h := range headers {
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(h))
|
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-11-06 07:38:32 -05:00
|
|
|
}
|
2018-11-02 16:34:04 -04:00
|
|
|
common.Must2(header.WriteString(CRLF))
|
2016-11-03 18:14:27 -04:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
2016-11-02 17:26:21 -04:00
|
|
|
}
|
2016-10-31 17:26:46 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (a Authenticator) GetServerWriter() *HeaderWriter {
|
2017-04-13 16:17:58 -04:00
|
|
|
return formResponseHeader(a.config.Response)
|
2016-12-14 06:25:22 -05:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (a Authenticator) Client(conn net.Conn) net.Conn {
|
2017-04-13 16:17:58 -04:00
|
|
|
if a.config.Request == nil && a.config.Response == nil {
|
2016-11-03 18:14:27 -04:00
|
|
|
return conn
|
|
|
|
}
|
2017-04-27 05:37:25 -04:00
|
|
|
var reader Reader = NoOpReader{}
|
2017-04-13 16:17:58 -04:00
|
|
|
if a.config.Request != nil {
|
2016-11-04 20:50:51 -04:00
|
|
|
reader = new(HeaderReader)
|
|
|
|
}
|
|
|
|
|
2017-04-27 05:37:25 -04:00
|
|
|
var writer Writer = NoOpWriter{}
|
2017-04-13 16:17:58 -04:00
|
|
|
if a.config.Response != nil {
|
|
|
|
writer = a.GetClientWriter()
|
2016-11-04 20:50:51 -04:00
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
return NewConn(conn, reader, writer, NoOpWriter{}, NoOpWriter{}, NoOpWriter{})
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func (a Authenticator) Server(conn net.Conn) net.Conn {
|
2017-04-13 16:17:58 -04:00
|
|
|
if a.config.Request == nil && a.config.Response == nil {
|
2016-11-03 18:14:27 -04:00
|
|
|
return conn
|
|
|
|
}
|
2020-10-11 07:22:46 -04:00
|
|
|
return NewConn(conn, new(HeaderReader).ExpectThisRequest(a.config.Request), a.GetServerWriter(),
|
2020-06-03 03:16:03 -04:00
|
|
|
formResponseHeader(resp400),
|
|
|
|
formResponseHeader(resp404),
|
|
|
|
formResponseHeader(resp400))
|
2016-10-31 17:26:46 -04:00
|
|
|
}
|
2016-10-31 19:42:55 -04:00
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
func NewAuthenticator(ctx context.Context, config *Config) (Authenticator, error) {
|
|
|
|
return Authenticator{
|
2017-01-13 07:47:44 -05:00
|
|
|
config: config,
|
2017-01-12 16:47:10 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-31 19:42:55 -04:00
|
|
|
func init() {
|
2017-01-13 07:47:44 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2020-10-11 07:22:46 -04:00
|
|
|
return NewAuthenticator(ctx, config.(*Config))
|
2017-01-13 07:47:44 -05:00
|
|
|
}))
|
2016-10-31 19:42:55 -04:00
|
|
|
}
|