2015-10-28 07:13:27 -04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2015-12-14 18:53:40 -05:00
|
|
|
"bufio"
|
2015-12-15 10:00:47 -05:00
|
|
|
"io"
|
2015-10-28 07:13:27 -04:00
|
|
|
"net"
|
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"
|
2015-12-15 10:00:47 -05:00
|
|
|
"sync"
|
2015-10-28 07:13:27 -04:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2015-12-14 18:53:40 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-12-02 15:44:01 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-03 17:30:37 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
2015-12-15 10:00:47 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
2015-10-28 07:13:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type HttpProxyServer struct {
|
2016-01-03 17:30:37 -05:00
|
|
|
sync.Mutex
|
|
|
|
accepting bool
|
|
|
|
space app.Space
|
|
|
|
config Config
|
|
|
|
tcpListener *net.TCPListener
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
|
|
|
|
2015-12-14 18:53:40 -05:00
|
|
|
func NewHttpProxyServer(space app.Space, config Config) *HttpProxyServer {
|
2015-10-28 07:13:27 -04:00
|
|
|
return &HttpProxyServer{
|
2015-12-14 18:53:40 -05:00
|
|
|
space: space,
|
|
|
|
config: config,
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *HttpProxyServer) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
if this.tcpListener != nil {
|
|
|
|
this.tcpListener.Close()
|
2016-01-03 18:33:25 -05:00
|
|
|
this.Lock()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.tcpListener = nil
|
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 11:26:29 -05:00
|
|
|
func (this *HttpProxyServer) Listen(port v2net.Port) error {
|
2015-12-14 18:53:40 -05:00
|
|
|
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
|
|
|
Port: int(port.Value()),
|
|
|
|
IP: []byte{0, 0, 0, 0},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|
2016-01-04 02:41:01 -05:00
|
|
|
this.Lock()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.tcpListener = tcpListener
|
2016-01-04 02:41:01 -05:00
|
|
|
this.Unlock()
|
2016-01-03 17:30:37 -05:00
|
|
|
this.accepting = true
|
|
|
|
go this.accept()
|
2015-12-14 18:53:40 -05:00
|
|
|
return nil
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
|
|
|
|
2016-01-03 17:30:37 -05:00
|
|
|
func (this *HttpProxyServer) accept() {
|
2015-12-14 18:53:40 -05:00
|
|
|
for this.accepting {
|
2016-01-03 17:30:37 -05:00
|
|
|
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
2016-01-03 19:19:27 -05:00
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
2016-01-03 19:13:43 -05:00
|
|
|
if !this.accepting {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-03 19:19:27 -05:00
|
|
|
tcpConn, err := this.tcpListener.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to accept HTTP connection: %v", err)
|
|
|
|
return err
|
2016-01-03 17:30:37 -05:00
|
|
|
}
|
2016-01-03 19:19:27 -05:00
|
|
|
go this.handleConnection(tcpConn)
|
2016-01-03 17:30:37 -05:00
|
|
|
return nil
|
|
|
|
})
|
2015-12-14 18:53:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-14 11:26:29 -05:00
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
func parseHost(rawHost string, defaultPort v2net.Port) (v2net.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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
} else {
|
|
|
|
intPort, err := strconv.Atoi(rawPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
port = v2net.Port(intPort)
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2015-12-14 18:53:40 -05:00
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
2015-12-16 17:53:38 -05:00
|
|
|
return v2net.TCPDestination(v2net.IPAddress(ip), port), nil
|
2015-12-14 18:53:40 -05:00
|
|
|
}
|
2015-12-16 17:53:38 -05:00
|
|
|
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
|
|
|
|
2015-12-14 18:53:40 -05:00
|
|
|
func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
|
2015-12-15 10:00:47 -05:00
|
|
|
defer conn.Close()
|
|
|
|
reader := bufio.NewReader(conn)
|
2015-12-31 18:34:08 -05:00
|
|
|
|
|
|
|
request, err := http.ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Request to Method [%s] Host [%s] with URL [%s]", request.Method, request.Host, request.URL.String())
|
|
|
|
defaultPort := v2net.Port(80)
|
|
|
|
if strings.ToLower(request.URL.Scheme) == "https" {
|
|
|
|
defaultPort = v2net.Port(443)
|
|
|
|
}
|
|
|
|
host := request.Host
|
|
|
|
if len(host) == 0 {
|
|
|
|
host = request.URL.Host
|
|
|
|
}
|
|
|
|
dest, err := parseHost(host, defaultPort)
|
|
|
|
if err != nil {
|
|
|
|
log.Warning("Malformed proxy host (%s): %v", host, err)
|
|
|
|
}
|
|
|
|
if strings.ToUpper(request.Method) == "CONNECT" {
|
|
|
|
this.handleConnect(request, dest, reader, conn)
|
|
|
|
} else {
|
|
|
|
this.handlePlainHTTP(request, dest, reader, conn)
|
2015-12-16 09:52:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 16:13:09 -05:00
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) {
|
2015-12-16 09:52:40 -05:00
|
|
|
response := &http.Response{
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: http.Header(make(map[string][]string)),
|
|
|
|
Body: nil,
|
|
|
|
ContentLength: 0,
|
|
|
|
Close: false,
|
|
|
|
}
|
2015-12-15 16:13:09 -05:00
|
|
|
|
2015-12-16 09:52:40 -05:00
|
|
|
buffer := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(buffer)
|
|
|
|
writer.Write(buffer.Value)
|
2015-12-16 10:37:32 -05:00
|
|
|
buffer.Release()
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
packet := v2net.NewPacket(destination, nil, true)
|
2015-12-16 09:52:40 -05:00
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
|
this.transport(reader, writer, ray)
|
2015-12-15 10:00:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
|
2015-12-31 18:34:08 -05:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
defer wg.Wait()
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2015-12-16 09:52:40 -05:00
|
|
|
go func() {
|
|
|
|
v2net.ReaderToChan(ray.InboundInput(), input)
|
|
|
|
close(ray.InboundInput())
|
2015-12-31 18:34:08 -05:00
|
|
|
wg.Done()
|
2015-12-16 09:52:40 -05:00
|
|
|
}()
|
2015-12-15 10:00:47 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
v2net.ChanToWriter(output, ray.InboundOutput())
|
2015-12-31 18:34:08 -05:00
|
|
|
wg.Done()
|
2015-12-15 10:00:47 -05:00
|
|
|
}()
|
2015-12-31 18:34:08 -05:00
|
|
|
}
|
2015-12-15 10:00:47 -05:00
|
|
|
|
2015-12-31 18:34:08 -05:00
|
|
|
func stripHopByHopHeaders(request *http.Request) {
|
|
|
|
// Strip hop-by-hop header basaed on RFC:
|
|
|
|
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
|
|
|
|
// https://www.mnot.net/blog/2011/07/11/what_proxies_must_do
|
|
|
|
|
|
|
|
request.Header.Del("Proxy-Connection")
|
|
|
|
request.Header.Del("Proxy-Authenticate")
|
|
|
|
request.Header.Del("Proxy-Authorization")
|
|
|
|
request.Header.Del("TE")
|
|
|
|
request.Header.Del("Trailers")
|
|
|
|
request.Header.Del("Transfer-Encoding")
|
|
|
|
request.Header.Del("Upgrade")
|
|
|
|
|
|
|
|
// TODO: support keep-alive
|
|
|
|
connections := request.Header.Get("Connection")
|
|
|
|
request.Header.Set("Connection", "close")
|
|
|
|
if len(connections) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, h := range strings.Split(connections, ",") {
|
|
|
|
request.Header.Del(strings.TrimSpace(h))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) {
|
|
|
|
if len(request.URL.Host) <= 0 {
|
|
|
|
hdr := http.Header(make(map[string][]string))
|
|
|
|
hdr.Set("Connection", "close")
|
|
|
|
response := &http.Response{
|
|
|
|
Status: "400 Bad Request",
|
|
|
|
StatusCode: 400,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
Header: hdr,
|
|
|
|
Body: nil,
|
|
|
|
ContentLength: 0,
|
|
|
|
Close: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(buffer)
|
|
|
|
writer.Write(buffer.Value)
|
|
|
|
buffer.Release()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Host = request.URL.Host
|
|
|
|
stripHopByHopHeaders(request)
|
|
|
|
|
2016-01-04 02:41:01 -05:00
|
|
|
requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet.
|
2015-12-31 18:34:08 -05:00
|
|
|
request.Write(requestBuffer)
|
|
|
|
log.Info("Request to remote:\n%s", string(requestBuffer.Value))
|
|
|
|
|
|
|
|
packet := v2net.NewPacket(dest, requestBuffer, true)
|
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
|
defer close(ray.InboundInput())
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
responseReader := bufio.NewReader(NewChanReader(ray.InboundOutput()))
|
|
|
|
response, err := http.ReadResponse(responseReader, request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-01-04 02:41:01 -05:00
|
|
|
responseBuffer := alloc.NewBuffer().Clear()
|
|
|
|
defer responseBuffer.Release()
|
2015-12-31 18:34:08 -05:00
|
|
|
response.Write(responseBuffer)
|
|
|
|
writer.Write(responseBuffer.Value)
|
|
|
|
response.Body.Close()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|