2015-10-28 07:13:27 -04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2017-02-15 16:51:01 -05:00
|
|
|
"bufio"
|
2017-01-14 18:57:06 -05:00
|
|
|
"context"
|
2017-10-28 08:25:22 -04:00
|
|
|
"encoding/base64"
|
2015-12-15 10:00:47 -05:00
|
|
|
"io"
|
2015-12-14 11:26:29 -05:00
|
|
|
"net/http"
|
2015-12-14 18:53:40 -05:00
|
|
|
"strconv"
|
2015-12-14 11:26:29 -05:00
|
|
|
"strings"
|
2017-01-31 06:42:05 -05:00
|
|
|
"time"
|
2015-10-28 07:13:27 -04:00
|
|
|
|
2018-04-16 18:31:10 -04:00
|
|
|
"v2ray.com/core/transport/pipe"
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
"v2ray.com/core"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 07:17:34 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2018-05-27 05:42:01 -04:00
|
|
|
"v2ray.com/core/common/functions"
|
2017-12-19 15:28:12 -05:00
|
|
|
"v2ray.com/core/common/log"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-12-18 14:59:43 -05:00
|
|
|
http_proto "v2ray.com/core/common/protocol/http"
|
2016-12-29 18:32:20 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2015-10-28 07:13:27 -04:00
|
|
|
)
|
|
|
|
|
2018-04-02 04:12:35 -04:00
|
|
|
// Server is an HTTP proxy server.
|
2016-05-29 10:46:31 -04:00
|
|
|
type Server struct {
|
2017-02-03 16:35:09 -05:00
|
|
|
config *ServerConfig
|
2018-01-10 06:22:37 -05:00
|
|
|
v *core.Instance
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 07:54:11 -05:00
|
|
|
// NewServer creates a new HTTP inbound handler.
|
2017-01-12 18:56:21 -05:00
|
|
|
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
2017-01-06 09:32:36 -05:00
|
|
|
s := &Server{
|
|
|
|
config: config,
|
2018-02-21 11:05:29 -05:00
|
|
|
v: core.MustFromContext(ctx),
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:56:21 -05:00
|
|
|
return s, nil
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) policy() core.Policy {
|
|
|
|
config := s.config
|
|
|
|
p := s.v.PolicyManager().ForLevel(config.UserLevel)
|
|
|
|
if config.Timeout > 0 && config.UserLevel == 0 {
|
|
|
|
p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
func (*Server) Network() net.NetworkList {
|
|
|
|
return net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-14 18:57:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
func parseHost(rawHost string, defaultPort net.Port) (net.Destination, error) {
|
2015-12-15 10:00:47 -05:00
|
|
|
port := defaultPort
|
2015-12-14 18:53:40 -05:00
|
|
|
host, rawPort, err := net.SplitHostPort(rawHost)
|
|
|
|
if err != nil {
|
|
|
|
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
|
|
|
|
host = rawHost
|
|
|
|
} else {
|
2017-08-29 06:56:57 -04:00
|
|
|
return net.Destination{}, err
|
2015-12-14 18:53:40 -05:00
|
|
|
}
|
2017-11-03 08:03:51 -04:00
|
|
|
} else if len(rawPort) > 0 {
|
2015-12-15 10:00:47 -05:00
|
|
|
intPort, err := strconv.Atoi(rawPort)
|
|
|
|
if err != nil {
|
2017-08-29 06:56:57 -04:00
|
|
|
return net.Destination{}, err
|
2015-12-15 10:00:47 -05:00
|
|
|
}
|
2017-08-29 06:56:57 -04:00
|
|
|
port = net.Port(intPort)
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
return net.TCPDestination(net.ParseAddress(host), port), nil
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:21:46 -04:00
|
|
|
func isTimeout(err error) bool {
|
2017-11-16 06:01:42 -05:00
|
|
|
nerr, ok := errors.Cause(err).(net.Error)
|
2017-04-11 18:21:46 -04:00
|
|
|
return ok && nerr.Timeout()
|
|
|
|
}
|
|
|
|
|
2017-10-28 08:25:22 -04:00
|
|
|
func parseBasicAuth(auth string) (username, password string, ok bool) {
|
|
|
|
const prefix = "Basic "
|
|
|
|
if !strings.HasPrefix(auth, prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs := string(c)
|
|
|
|
s := strings.IndexByte(cs, ':')
|
|
|
|
if s < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cs[:s], cs[s+1:], true
|
|
|
|
}
|
|
|
|
|
2017-11-24 17:24:12 -05:00
|
|
|
type readerOnly struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher core.Dispatcher) error {
|
2017-11-24 17:24:12 -05:00
|
|
|
reader := bufio.NewReaderSize(readerOnly{conn}, buf.Size)
|
2017-11-16 21:56:24 -05:00
|
|
|
|
2017-04-11 08:59:58 -04:00
|
|
|
Start:
|
2018-04-20 10:03:42 -04:00
|
|
|
if err := conn.SetReadDeadline(time.Now().Add(s.policy().Timeouts.Handshake)); err != nil {
|
|
|
|
newError("failed to set read deadline").Base(err).WithContext(ctx).WriteToLog()
|
|
|
|
}
|
2015-12-31 18:34:08 -05:00
|
|
|
|
|
|
|
request, err := http.ReadRequest(reader)
|
|
|
|
if err != nil {
|
2017-04-11 08:59:58 -04:00
|
|
|
trace := newError("failed to read http request").Base(err)
|
2017-04-11 18:21:46 -04:00
|
|
|
if errors.Cause(err) != io.EOF && !isTimeout(errors.Cause(err)) {
|
2017-04-11 08:59:58 -04:00
|
|
|
trace.AtWarning()
|
2016-06-03 14:21:46 -04:00
|
|
|
}
|
2017-04-11 08:59:58 -04:00
|
|
|
return trace
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
|
2017-12-05 17:49:16 -05:00
|
|
|
if len(s.config.Accounts) > 0 {
|
2017-10-28 08:25:22 -04:00
|
|
|
user, pass, ok := parseBasicAuth(request.Header.Get("Proxy-Authorization"))
|
2017-12-02 04:07:42 -05:00
|
|
|
if !ok || !s.config.HasAccount(user, pass) {
|
2018-04-11 10:15:29 -04:00
|
|
|
return common.Error2(conn.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"proxy\"\r\n\r\n")))
|
2017-10-26 15:44:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 09:26:00 -05:00
|
|
|
newError("request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]").WithContext(ctx).WriteToLog()
|
2018-04-20 10:03:42 -04:00
|
|
|
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
|
|
|
newError("failed to clear read deadline").Base(err).WithContext(ctx).WriteToLog()
|
|
|
|
}
|
2017-01-31 10:49:59 -05:00
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
defaultPort := net.Port(80)
|
2015-12-31 18:34:08 -05:00
|
|
|
if strings.ToLower(request.URL.Scheme) == "https" {
|
2017-08-29 06:56:57 -04:00
|
|
|
defaultPort = net.Port(443)
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
|
|
|
host := request.Host
|
|
|
|
if len(host) == 0 {
|
|
|
|
host = request.URL.Host
|
|
|
|
}
|
|
|
|
dest, err := parseHost(host, defaultPort)
|
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("malformed proxy host: ", host).AtWarning().Base(err)
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
log.Record(&log.AccessMessage{
|
|
|
|
From: conn.RemoteAddr(),
|
|
|
|
To: request.URL,
|
|
|
|
Status: log.AccessAccepted,
|
|
|
|
})
|
2017-02-03 16:35:09 -05:00
|
|
|
|
2015-12-31 18:34:08 -05:00
|
|
|
if strings.ToUpper(request.Method) == "CONNECT" {
|
2017-11-16 21:56:24 -05:00
|
|
|
return s.handleConnect(ctx, request, reader, conn, dest, dispatcher)
|
2015-12-16 09:52:40 -05:00
|
|
|
}
|
2017-04-11 08:59:58 -04:00
|
|
|
|
|
|
|
keepAlive := (strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive")
|
|
|
|
|
2017-11-17 14:54:24 -05:00
|
|
|
err = s.handlePlainHTTP(ctx, request, conn, dest, dispatcher)
|
2017-04-11 08:59:58 -04:00
|
|
|
if err == errWaitAnother {
|
|
|
|
if keepAlive {
|
|
|
|
goto Start
|
|
|
|
}
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-12-16 09:52:40 -05:00
|
|
|
}
|
2015-12-15 16:13:09 -05:00
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) handleConnect(ctx context.Context, request *http.Request, reader *bufio.Reader, conn internet.Connection, dest net.Destination, dispatcher core.Dispatcher) error {
|
2017-11-17 14:54:24 -05:00
|
|
|
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
|
2017-04-18 07:30:26 -04:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to write back OK response").Base(err)
|
2016-12-22 07:54:11 -05:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2018-05-25 06:08:28 -04:00
|
|
|
plcy := s.policy()
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-05-25 06:08:28 -04:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
|
|
|
|
|
|
|
|
ctx = core.ContextWithBufferPolicy(ctx, plcy.Buffer)
|
2018-04-16 18:31:10 -04:00
|
|
|
link, err := dispatcher.Dispatch(ctx, dest)
|
2017-02-03 16:35:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2017-11-17 14:54:24 -05:00
|
|
|
if reader.Buffered() > 0 {
|
2018-04-01 18:25:42 -04:00
|
|
|
payload, err := buf.ReadSizeToMultiBuffer(reader, int32(reader.Buffered()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 18:31:10 -04:00
|
|
|
if err := link.Writer.WriteMultiBuffer(payload); err != nil {
|
2017-11-17 14:54:24 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-11-17 14:58:46 -05:00
|
|
|
reader = nil
|
2017-11-17 14:54:24 -05:00
|
|
|
}
|
|
|
|
|
2018-04-11 10:45:09 -04:00
|
|
|
requestDone := func() error {
|
2018-01-10 06:22:37 -05:00
|
|
|
defer timer.SetTimeout(s.policy().Timeouts.DownlinkOnly)
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2017-11-17 14:54:24 -05:00
|
|
|
v2reader := buf.NewReader(conn)
|
2018-04-16 18:31:10 -04:00
|
|
|
return buf.Copy(v2reader, link.Writer, buf.UpdateActivity(timer))
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2018-04-11 10:45:09 -04:00
|
|
|
responseDone := func() error {
|
2018-02-23 11:07:45 -05:00
|
|
|
defer timer.SetTimeout(s.policy().Timeouts.UplinkOnly)
|
|
|
|
|
2017-11-17 14:54:24 -05:00
|
|
|
v2writer := buf.NewWriter(conn)
|
2018-04-16 18:31:10 -04:00
|
|
|
if err := buf.Copy(link.Reader, v2writer, buf.UpdateActivity(timer)); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2018-02-23 11:07:45 -05:00
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2018-05-27 05:42:01 -04:00
|
|
|
if err := signal.ExecuteParallel(ctx, functions.OnSuccess(requestDone, functions.Close(link.Writer)), responseDone); err != nil {
|
2018-04-16 18:31:10 -04:00
|
|
|
pipe.CloseError(link.Reader)
|
|
|
|
pipe.CloseError(link.Writer)
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("connection ends").Base(err)
|
2017-01-10 08:22:42 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2017-04-11 08:59:58 -04:00
|
|
|
var errWaitAnother = newError("keep alive")
|
2016-06-01 18:57:08 -04:00
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, writer io.Writer, dest net.Destination, dispatcher core.Dispatcher) error {
|
2017-11-05 16:13:15 -05:00
|
|
|
if !s.config.AllowTransparent && len(request.URL.Host) <= 0 {
|
|
|
|
// RFC 2068 (HTTP/1.1) requires URL to be absolute URL in HTTP proxy.
|
2017-04-11 08:59:58 -04:00
|
|
|
response := &http.Response{
|
|
|
|
Status: "Bad Request",
|
|
|
|
StatusCode: 400,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header(make(map[string][]string)),
|
|
|
|
Body: nil,
|
|
|
|
ContentLength: 0,
|
|
|
|
Close: true,
|
|
|
|
}
|
|
|
|
response.Header.Set("Proxy-Connection", "close")
|
|
|
|
response.Header.Set("Connection", "close")
|
2017-01-26 14:46:44 -05:00
|
|
|
return response.Write(writer)
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
|
|
|
|
2017-11-05 16:13:15 -05:00
|
|
|
if len(request.URL.Host) > 0 {
|
|
|
|
request.Host = request.URL.Host
|
|
|
|
}
|
2017-12-18 14:59:43 -05:00
|
|
|
http_proto.RemoveHopByHopHeaders(request.Header)
|
|
|
|
|
|
|
|
// Prevent UA from being set to golang's default ones
|
|
|
|
if len(request.Header.Get("User-Agent")) == 0 {
|
|
|
|
request.Header.Set("User-Agent", "")
|
|
|
|
}
|
2015-12-31 18:34:08 -05:00
|
|
|
|
2018-04-16 18:31:10 -04:00
|
|
|
link, err := dispatcher.Dispatch(ctx, dest)
|
2017-02-03 16:35:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 18:51:39 -05:00
|
|
|
|
2018-04-20 10:03:42 -04:00
|
|
|
// Plain HTTP request is not a stream. The request always finishes before response. Hense request has to be closed later.
|
|
|
|
defer common.Close(link.Writer)
|
2017-04-18 08:18:09 -04:00
|
|
|
var result error = errWaitAnother
|
|
|
|
|
2018-04-11 10:45:09 -04:00
|
|
|
requestDone := func() error {
|
2017-04-11 08:59:58 -04:00
|
|
|
request.Header.Set("Connection", "close")
|
|
|
|
|
2018-04-16 18:31:10 -04:00
|
|
|
requestWriter := buf.NewBufferedWriter(link.Writer)
|
2017-11-15 05:23:25 -05:00
|
|
|
common.Must(requestWriter.SetBuffered(false))
|
2017-11-26 09:36:05 -05:00
|
|
|
if err := request.Write(requestWriter); err != nil {
|
|
|
|
return newError("failed to write whole request").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
return nil
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2018-04-11 10:45:09 -04:00
|
|
|
responseDone := func() error {
|
2018-04-20 18:54:53 -04:00
|
|
|
responseReader := bufio.NewReaderSize(&buf.BufferedReader{Reader: link.Reader}, buf.Size)
|
2015-12-31 18:34:08 -05:00
|
|
|
response, err := http.ReadResponse(responseReader, request)
|
2017-04-11 08:59:58 -04:00
|
|
|
if err == nil {
|
2017-12-18 14:59:43 -05:00
|
|
|
http_proto.RemoveHopByHopHeaders(response.Header)
|
2017-04-18 07:39:34 -04:00
|
|
|
if response.ContentLength >= 0 {
|
|
|
|
response.Header.Set("Proxy-Connection", "keep-alive")
|
|
|
|
response.Header.Set("Connection", "keep-alive")
|
|
|
|
response.Header.Set("Keep-Alive", "timeout=4")
|
|
|
|
response.Close = false
|
2017-04-18 08:18:09 -04:00
|
|
|
} else {
|
|
|
|
response.Close = true
|
|
|
|
result = nil
|
2017-04-18 07:39:34 -04:00
|
|
|
}
|
2017-04-11 08:59:58 -04:00
|
|
|
} else {
|
2018-02-22 09:26:00 -05:00
|
|
|
newError("failed to read response from ", request.Host).Base(err).AtWarning().WithContext(ctx).WriteToLog()
|
2017-04-11 08:59:58 -04:00
|
|
|
response = &http.Response{
|
|
|
|
Status: "Service Unavailable",
|
|
|
|
StatusCode: 503,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header(make(map[string][]string)),
|
|
|
|
Body: nil,
|
|
|
|
ContentLength: 0,
|
|
|
|
Close: true,
|
|
|
|
}
|
|
|
|
response.Header.Set("Connection", "close")
|
|
|
|
response.Header.Set("Proxy-Connection", "close")
|
2016-05-25 03:32:26 -04:00
|
|
|
}
|
2017-04-17 16:35:20 -04:00
|
|
|
if err := response.Write(writer); err != nil {
|
2017-11-26 09:36:05 -05:00
|
|
|
return newError("failed to write response").Base(err).AtWarning()
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2018-04-11 10:45:09 -04:00
|
|
|
if err := signal.ExecuteParallel(ctx, requestDone, responseDone); err != nil {
|
2018-04-16 18:31:10 -04:00
|
|
|
pipe.CloseError(link.Reader)
|
|
|
|
pipe.CloseError(link.Writer)
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("connection ends").Base(err)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-04-18 08:18:09 -04:00
|
|
|
return result
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
2016-05-07 08:08:27 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 18:56:21 -05:00
|
|
|
common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewServer(ctx, config.(*ServerConfig))
|
|
|
|
}))
|
2016-05-07 08:08:27 -04:00
|
|
|
}
|