1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-29 18:45:23 +00:00
v2fly/proxy/http/http.go

243 lines
6.1 KiB
Go
Raw Normal View History

2015-10-28 11:13:27 +00:00
package http
import (
2015-12-14 23:53:40 +00:00
"bufio"
2015-12-15 15:00:47 +00:00
"io"
2015-10-28 11:13:27 +00:00
"net"
2015-12-14 16:26:29 +00:00
"net/http"
2015-12-14 23:53:40 +00:00
"strconv"
2015-12-14 16:26:29 +00:00
"strings"
2015-12-15 15:00:47 +00:00
"sync"
2015-10-28 11:13:27 +00:00
"github.com/v2ray/v2ray-core/app"
2015-12-14 23:53:40 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-01-29 13:39:55 +00:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-12-14 23:53:40 +00:00
"github.com/v2ray/v2ray-core/common/log"
2015-12-02 20:44:01 +00:00
v2net "github.com/v2ray/v2ray-core/common/net"
2016-01-18 11:58:04 +00:00
"github.com/v2ray/v2ray-core/common/serial"
2016-01-19 22:41:40 +00:00
"github.com/v2ray/v2ray-core/proxy"
2016-01-28 16:08:32 +00:00
"github.com/v2ray/v2ray-core/transport/hub"
2015-12-15 15:00:47 +00:00
"github.com/v2ray/v2ray-core/transport/ray"
2015-10-28 11:13:27 +00:00
)
type HttpProxyServer struct {
sync.Mutex
2016-01-19 22:41:40 +00:00
accepting bool
space app.Space
config *Config
2016-01-28 19:47:00 +00:00
tcpListener *hub.TCPHub
2016-01-19 22:41:40 +00:00
listeningPort v2net.Port
2015-10-28 11:13:27 +00:00
}
2016-01-16 12:08:50 +00:00
func NewHttpProxyServer(space app.Space, config *Config) *HttpProxyServer {
2015-10-28 11:13:27 +00:00
return &HttpProxyServer{
2015-12-14 23:53:40 +00:00
space: space,
config: config,
2015-10-28 11:13:27 +00:00
}
}
2016-01-19 22:41:40 +00:00
func (this *HttpProxyServer) Port() v2net.Port {
return this.listeningPort
}
func (this *HttpProxyServer) Close() {
this.accepting = false
if this.tcpListener != nil {
2016-01-03 23:33:25 +00:00
this.Lock()
this.tcpListener.Close()
this.tcpListener = nil
this.Unlock()
}
}
2015-12-14 16:26:29 +00:00
func (this *HttpProxyServer) Listen(port v2net.Port) error {
2016-01-19 22:41:40 +00:00
if this.accepting {
if this.listeningPort == port {
return nil
} else {
return proxy.ErrorAlreadyListening
}
}
this.listeningPort = port
2016-01-28 16:08:32 +00:00
tcpListener, err := hub.ListenTCP(port, this.handleConnection)
2015-12-14 23:53:40 +00:00
if err != nil {
log.Error("Http: Failed listen on port ", port, ": ", err)
2015-12-14 23:53:40 +00:00
return err
2015-10-28 11:13:27 +00:00
}
2016-01-04 07:41:01 +00:00
this.Lock()
this.tcpListener = tcpListener
2016-01-04 07:41:01 +00:00
this.Unlock()
this.accepting = true
2015-12-14 23:53:40 +00:00
return nil
2015-12-14 16:26:29 +00:00
}
2015-12-16 22:53:38 +00:00
func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Destination, error) {
2015-12-15 15:00:47 +00:00
port := defaultPort
2015-12-14 23:53:40 +00: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 15:00:47 +00:00
} else {
intPort, err := strconv.Atoi(rawPort)
if err != nil {
return nil, err
}
port = v2net.Port(intPort)
2015-12-14 16:26:29 +00:00
}
2015-12-15 15:00:47 +00:00
2015-12-14 23:53:40 +00:00
if ip := net.ParseIP(host); ip != nil {
2015-12-16 22:53:38 +00:00
return v2net.TCPDestination(v2net.IPAddress(ip), port), nil
2015-12-14 23:53:40 +00:00
}
2015-12-16 22:53:38 +00:00
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
2015-12-14 16:26:29 +00:00
}
2016-01-28 16:08:32 +00:00
func (this *HttpProxyServer) handleConnection(conn *hub.TCPConn) {
2015-12-15 15:00:47 +00:00
defer conn.Close()
reader := bufio.NewReader(conn)
request, err := http.ReadRequest(reader)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Warning("Failed to read http request: ", err)
return
}
2016-01-18 11:24:33 +00:00
log.Info("Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
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 {
2016-01-18 11:24:33 +00:00
log.Warning("Malformed proxy host (", host, "): ", err)
2016-01-11 11:35:28 +00:00
return
}
if strings.ToUpper(request.Method) == "CONNECT" {
this.handleConnect(request, dest, reader, conn)
} else {
this.handlePlainHTTP(request, dest, reader, conn)
2015-12-16 14:52:40 +00:00
}
}
2015-12-15 21:13:09 +00:00
2015-12-16 22:53:38 +00:00
func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) {
2015-12-16 14:52:40 +00: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 21:13:09 +00:00
2015-12-16 14:52:40 +00:00
buffer := alloc.NewSmallBuffer().Clear()
response.Write(buffer)
writer.Write(buffer.Value)
2015-12-16 15:37:32 +00:00
buffer.Release()
2015-12-15 15:00:47 +00:00
2015-12-16 22:53:38 +00:00
packet := v2net.NewPacket(destination, nil, true)
2015-12-16 14:52:40 +00:00
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
this.transport(reader, writer, ray)
2015-12-15 15:00:47 +00:00
}
func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
var wg sync.WaitGroup
wg.Add(2)
defer wg.Wait()
2015-12-15 15:00:47 +00:00
2015-12-16 14:52:40 +00:00
go func() {
2016-01-29 13:39:55 +00:00
v2io.RawReaderToChan(ray.InboundInput(), input)
2015-12-16 14:52:40 +00:00
close(ray.InboundInput())
wg.Done()
2015-12-16 14:52:40 +00:00
}()
2015-12-15 15:00:47 +00:00
go func() {
2016-01-29 13:39:55 +00:00
v2io.ChanToWriter(output, ray.InboundOutput())
wg.Done()
2015-12-15 15:00:47 +00:00
}()
}
2015-12-15 15:00:47 +00: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 07:41:01 +00:00
requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet.
request.Write(requestBuffer)
2016-01-18 11:58:04 +00:00
log.Debug("Request to remote:\n", serial.BytesLiteral(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 07:41:01 +00:00
responseBuffer := alloc.NewBuffer().Clear()
defer responseBuffer.Release()
response.Write(responseBuffer)
writer.Write(responseBuffer.Value)
response.Body.Close()
}()
wg.Wait()
2015-10-28 11:13:27 +00:00
}