2016-10-31 21:26:46 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-19 11:16:57 +00:00
|
|
|
"errors"
|
2016-11-03 22:14:27 +00:00
|
|
|
"io"
|
2016-11-02 21:26:21 +00:00
|
|
|
"net"
|
2016-11-06 12:38:32 +00:00
|
|
|
"net/http"
|
2016-12-06 10:03:42 +00:00
|
|
|
"strings"
|
2016-11-06 12:38:32 +00:00
|
|
|
"time"
|
2016-12-09 11:08:25 +00:00
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-06 10:03:42 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-10-31 21:26:46 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CRLF = "\r\n"
|
|
|
|
ENDING = CRLF + CRLF
|
2016-12-16 16:01:47 +00:00
|
|
|
|
2016-12-19 11:16:57 +00:00
|
|
|
// max length of HTTP header. Safety precaution for DDoS attack.
|
2016-12-16 16:01:47 +00:00
|
|
|
maxHeaderLength = 8192
|
2016-10-31 21:26:46 +00:00
|
|
|
)
|
|
|
|
|
2016-12-06 10:03:42 +00:00
|
|
|
var (
|
2016-12-19 11:16:57 +00:00
|
|
|
ErrHeaderToLong = errors.New("Header too long.")
|
|
|
|
writeCRLF = serial.WriteString(CRLF)
|
2016-12-06 10:03:42 +00:00
|
|
|
)
|
|
|
|
|
2016-11-05 00:50:51 +00:00
|
|
|
type Reader interface {
|
2016-12-09 10:35:27 +00:00
|
|
|
Read(io.Reader) (*buf.Buffer, error)
|
2016-11-05 00:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Writer interface {
|
|
|
|
Write(io.Writer) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoOpReader struct{}
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
func (v *NoOpReader) Read(io.Reader) (*buf.Buffer, error) {
|
2016-11-05 00:50:51 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoOpWriter struct{}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *NoOpWriter) Write(io.Writer) error {
|
2016-11-05 00:50:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-03 22:14:27 +00:00
|
|
|
type HeaderReader struct {
|
|
|
|
}
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
func (*HeaderReader) Read(reader io.Reader) (*buf.Buffer, error) {
|
2016-12-09 11:08:25 +00:00
|
|
|
buffer := buf.NewSmall()
|
2016-12-16 16:01:47 +00:00
|
|
|
totalBytes := 0
|
2016-12-19 11:16:57 +00:00
|
|
|
endingDetected := false
|
2016-12-16 16:01:47 +00:00
|
|
|
for totalBytes < maxHeaderLength {
|
2016-12-09 11:08:25 +00:00
|
|
|
err := buffer.AppendSupplier(buf.ReadFrom(reader))
|
2016-11-03 22:14:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-06 10:03:42 +00:00
|
|
|
if n := bytes.Index(buffer.Bytes(), []byte(ENDING)); n != -1 {
|
2016-11-03 22:14:27 +00:00
|
|
|
buffer.SliceFrom(n + len(ENDING))
|
2016-12-19 11:16:57 +00:00
|
|
|
endingDetected = true
|
2016-11-03 22:14:27 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if buffer.Len() >= len(ENDING) {
|
2016-12-16 16:01:47 +00:00
|
|
|
totalBytes += buffer.Len() - len(ENDING)
|
|
|
|
leftover := buffer.BytesFrom(-len(ENDING))
|
2016-12-11 20:43:16 +00:00
|
|
|
buffer.Reset(func(b []byte) (int, error) {
|
|
|
|
return copy(b, leftover), nil
|
|
|
|
})
|
2016-11-03 22:14:27 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-05 00:50:51 +00:00
|
|
|
if buffer.IsEmpty() {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-12-19 11:16:57 +00:00
|
|
|
if !endingDetected {
|
|
|
|
buffer.Release()
|
|
|
|
return nil, ErrHeaderToLong
|
|
|
|
}
|
2016-11-03 22:14:27 +00:00
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HeaderWriter struct {
|
2016-12-09 10:35:27 +00:00
|
|
|
header *buf.Buffer
|
2016-11-03 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
func NewHeaderWriter(header *buf.Buffer) *HeaderWriter {
|
2016-11-04 20:59:19 +00:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *HeaderWriter) Write(writer io.Writer) error {
|
|
|
|
if v.header == nil {
|
2016-11-03 22:14:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-06 10:03:42 +00:00
|
|
|
_, err := writer.Write(v.header.Bytes())
|
2016-11-27 20:39:09 +00:00
|
|
|
v.header.Release()
|
|
|
|
v.header = nil
|
2016-11-03 22:14:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-02 21:26:21 +00:00
|
|
|
type HttpConn struct {
|
|
|
|
net.Conn
|
|
|
|
|
2016-12-09 10:35:27 +00:00
|
|
|
readBuffer *buf.Buffer
|
2016-11-05 00:50:51 +00:00
|
|
|
oneTimeReader Reader
|
|
|
|
oneTimeWriter Writer
|
2016-12-14 11:25:22 +00:00
|
|
|
isServer bool
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:25:22 +00:00
|
|
|
func NewHttpConn(conn net.Conn, reader Reader, writer Writer, isServer bool) *HttpConn {
|
2016-11-02 21:26:21 +00:00
|
|
|
return &HttpConn{
|
2016-11-03 22:14:27 +00:00
|
|
|
Conn: conn,
|
|
|
|
oneTimeReader: reader,
|
|
|
|
oneTimeWriter: writer,
|
2016-12-14 11:25:22 +00:00
|
|
|
isServer: isServer,
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 21:26:46 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *HttpConn) Read(b []byte) (int, error) {
|
|
|
|
if v.oneTimeReader != nil {
|
|
|
|
buffer, err := v.oneTimeReader.Read(v.Conn)
|
2016-11-03 22:14:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
v.readBuffer = buffer
|
|
|
|
v.oneTimeReader = nil
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.readBuffer.Len() > 0 {
|
|
|
|
nBytes, err := v.readBuffer.Read(b)
|
|
|
|
if nBytes == v.readBuffer.Len() {
|
|
|
|
v.readBuffer.Release()
|
|
|
|
v.readBuffer = nil
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
|
|
|
return nBytes, err
|
|
|
|
}
|
2016-10-31 21:26:46 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
return v.Conn.Read(b)
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *HttpConn) Write(b []byte) (int, error) {
|
|
|
|
if v.oneTimeWriter != nil {
|
|
|
|
err := v.oneTimeWriter.Write(v.Conn)
|
|
|
|
v.oneTimeWriter = nil
|
2016-10-31 21:26:46 +00:00
|
|
|
if err != nil {
|
2016-11-02 21:26:21 +00:00
|
|
|
return 0, err
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
return v.Conn.Write(b)
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:25:22 +00:00
|
|
|
// Close implements net.Conn.Close().
|
|
|
|
func (v *HttpConn) Close() error {
|
|
|
|
if v.isServer && v.oneTimeWriter != nil {
|
|
|
|
// 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.
|
|
|
|
writer := formResponseHeader(&ResponseConfig{
|
|
|
|
Version: &Version{
|
|
|
|
Value: "1.1",
|
|
|
|
},
|
|
|
|
Status: &Status{
|
|
|
|
Code: "500",
|
|
|
|
Reason: "Internal Server Error",
|
|
|
|
},
|
|
|
|
Header: []*Header{
|
|
|
|
{
|
|
|
|
Name: "Connection",
|
|
|
|
Value: []string{"close"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Cache-Control",
|
|
|
|
Value: []string{"private"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Content-Length",
|
|
|
|
Value: []string{"0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
writer.Write(v.Conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.Conn.Close()
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:25:22 +00:00
|
|
|
func formResponseHeader(config *ResponseConfig) *HeaderWriter {
|
2016-12-09 11:08:25 +00:00
|
|
|
header := buf.NewSmall()
|
2016-12-14 11:25:22 +00:00
|
|
|
header.AppendSupplier(serial.WriteString(strings.Join([]string{config.GetFullVersion(), config.Status.GetCode(), config.Status.GetReason()}, " ")))
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-10-31 21:26:46 +00:00
|
|
|
|
2016-11-02 21:26:21 +00:00
|
|
|
headers := config.PickHeaders()
|
|
|
|
for _, h := range headers {
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(serial.WriteString(h))
|
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
2016-12-14 11:25:22 +00:00
|
|
|
if !config.HasHeader("Date") {
|
|
|
|
header.AppendSupplier(serial.WriteString("Date: "))
|
|
|
|
header.AppendSupplier(serial.WriteString(time.Now().Format(http.TimeFormat)))
|
|
|
|
header.AppendSupplier(writeCRLF)
|
|
|
|
}
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-11-03 22:14:27 +00:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 11:25:22 +00:00
|
|
|
type HttpAuthenticator struct {
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v HttpAuthenticator) GetClientWriter() *HeaderWriter {
|
2016-12-09 11:08:25 +00:00
|
|
|
header := buf.NewSmall()
|
2016-12-14 11:25:22 +00:00
|
|
|
config := v.config.Request
|
|
|
|
header.AppendSupplier(serial.WriteString(strings.Join([]string{config.Method.GetValue(), config.PickUri(), config.GetFullVersion()}, " ")))
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-10-31 21:26:46 +00:00
|
|
|
|
2016-11-02 21:26:21 +00:00
|
|
|
headers := config.PickHeaders()
|
2016-10-31 21:26:46 +00:00
|
|
|
for _, h := range headers {
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(serial.WriteString(h))
|
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-11-06 12:38:32 +00:00
|
|
|
}
|
2016-12-09 11:08:25 +00:00
|
|
|
header.AppendSupplier(writeCRLF)
|
2016-11-03 22:14:27 +00:00
|
|
|
return &HeaderWriter{
|
|
|
|
header: header,
|
|
|
|
}
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
2016-10-31 21:26:46 +00:00
|
|
|
|
2016-12-14 11:25:22 +00:00
|
|
|
func (v HttpAuthenticator) GetServerWriter() *HeaderWriter {
|
|
|
|
return formResponseHeader(v.config.Response)
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v HttpAuthenticator) Client(conn net.Conn) net.Conn {
|
|
|
|
if v.config.Request == nil && v.config.Response == nil {
|
2016-11-03 22:14:27 +00:00
|
|
|
return conn
|
|
|
|
}
|
2016-11-05 00:50:51 +00:00
|
|
|
var reader Reader = new(NoOpReader)
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.config.Request != nil {
|
2016-11-05 00:50:51 +00:00
|
|
|
reader = new(HeaderReader)
|
|
|
|
}
|
|
|
|
|
|
|
|
var writer Writer = new(NoOpWriter)
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.config.Response != nil {
|
|
|
|
writer = v.GetClientWriter()
|
2016-11-05 00:50:51 +00:00
|
|
|
}
|
2016-12-14 11:25:22 +00:00
|
|
|
return NewHttpConn(conn, reader, writer, false)
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v HttpAuthenticator) Server(conn net.Conn) net.Conn {
|
|
|
|
if v.config.Request == nil && v.config.Response == nil {
|
2016-11-03 22:14:27 +00:00
|
|
|
return conn
|
|
|
|
}
|
2016-12-14 11:25:22 +00:00
|
|
|
return NewHttpConn(conn, new(HeaderReader), v.GetServerWriter(), true)
|
2016-10-31 21:26:46 +00:00
|
|
|
}
|
2016-10-31 23:42:55 +00:00
|
|
|
|
2016-11-02 21:26:21 +00:00
|
|
|
type HttpAuthenticatorFactory struct{}
|
2016-10-31 23:42:55 +00:00
|
|
|
|
2016-11-02 21:26:21 +00:00
|
|
|
func (HttpAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
|
|
|
|
return HttpAuthenticator{
|
|
|
|
config: config.(*Config),
|
2016-10-31 23:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-15 10:51:09 +00:00
|
|
|
internet.RegisterConnectionAuthenticator(serial.GetMessageType(new(Config)), HttpAuthenticatorFactory{})
|
2016-10-31 23:42:55 +00:00
|
|
|
}
|