|
|
@@ -2,15 +2,18 @@ package http
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
+ "io"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
+ "github.com/v2ray/v2ray-core/transport/ray"
|
|
|
)
|
|
|
|
|
|
type HttpProxyServer struct {
|
|
|
@@ -50,22 +53,23 @@ func (this *HttpProxyServer) accept(listener *net.TCPListener) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func parseHost(rawHost string) (v2net.Address, error) {
|
|
|
- port := v2net.Port(80)
|
|
|
+func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Address, error) {
|
|
|
+ port := defaultPort
|
|
|
host, rawPort, err := net.SplitHostPort(rawHost)
|
|
|
if err != nil {
|
|
|
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
|
|
|
host = rawHost
|
|
|
- port = v2net.Port(80)
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ } else {
|
|
|
+ intPort, err := strconv.Atoi(rawPort)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ port = v2net.Port(intPort)
|
|
|
}
|
|
|
- intPort, err := strconv.Atoi(rawPort)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- port = v2net.Port(intPort)
|
|
|
+
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
return v2net.IPAddress(ip, port), nil
|
|
|
}
|
|
|
@@ -73,14 +77,25 @@ func parseHost(rawHost string) (v2net.Address, error) {
|
|
|
}
|
|
|
|
|
|
func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
|
|
|
- httpReader := bufio.NewReader(conn)
|
|
|
- request, err := http.ReadRequest(httpReader)
|
|
|
- if err != nil {
|
|
|
- log.Warning("Malformed HTTP request: %v", err)
|
|
|
- return
|
|
|
+ defer conn.Close()
|
|
|
+ reader := bufio.NewReader(conn)
|
|
|
+ for true {
|
|
|
+ request, err := http.ReadRequest(reader)
|
|
|
+ if err != nil {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ this.handleRequest(request, reader, conn)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (this *HttpProxyServer) handleRequest(request *http.Request, reader io.Reader, writer io.Writer) {
|
|
|
+ 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)
|
|
|
}
|
|
|
if strings.ToUpper(request.Method) == "CONNECT" {
|
|
|
- address, err := parseHost(request.Host)
|
|
|
+ address, err := parseHost(request.Host, defaultPort)
|
|
|
if err != nil {
|
|
|
log.Warning("Malformed proxy host: %v", err)
|
|
|
return
|
|
|
@@ -99,9 +114,64 @@ func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
|
|
|
|
|
|
buffer := alloc.NewSmallBuffer().Clear()
|
|
|
response.Write(buffer)
|
|
|
- conn.Write(buffer.Value)
|
|
|
+ writer.Write(buffer.Value)
|
|
|
|
|
|
packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true)
|
|
|
- this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
+ ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
+ this.transport(reader, writer, ray)
|
|
|
+ } else if len(request.URL.Host) > 0 {
|
|
|
+ address, err := parseHost(request.URL.Host, defaultPort)
|
|
|
+ if err != nil {
|
|
|
+ log.Warning("Malformed proxy host: %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ request.Host = request.URL.Host
|
|
|
+ request.Header.Set("Connection", "keep-alive")
|
|
|
+ request.Header.Del("Proxy-Connection")
|
|
|
+ buffer := alloc.NewBuffer().Clear()
|
|
|
+ request.Write(buffer)
|
|
|
+ log.Info("Request to remote: %s", string(buffer.Value))
|
|
|
+ packet := v2net.NewPacket(v2net.NewTCPDestination(address), buffer, false)
|
|
|
+ ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
+ this.transport(nil, writer, ray)
|
|
|
+ } else {
|
|
|
+ response := &http.Response{
|
|
|
+ Status: "400 Bad Request",
|
|
|
+ StatusCode: 400,
|
|
|
+ Proto: "HTTP/1.1",
|
|
|
+ ProtoMajor: 1,
|
|
|
+ ProtoMinor: 1,
|
|
|
+ Header: http.Header(make(map[string][]string)),
|
|
|
+ Body: nil,
|
|
|
+ ContentLength: 0,
|
|
|
+ Close: false,
|
|
|
+ }
|
|
|
+
|
|
|
+ buffer := alloc.NewSmallBuffer().Clear()
|
|
|
+ response.Write(buffer)
|
|
|
+ writer.Write(buffer.Value)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
|
|
|
+ var inputFinish, outputFinish sync.Mutex
|
|
|
+ inputFinish.Lock()
|
|
|
+ outputFinish.Lock()
|
|
|
+
|
|
|
+ if input != nil {
|
|
|
+ go func() {
|
|
|
+ v2net.ReaderToChan(ray.InboundInput(), input)
|
|
|
+ inputFinish.Unlock()
|
|
|
+ close(ray.InboundInput())
|
|
|
+ }()
|
|
|
+ } else {
|
|
|
+ close(ray.InboundInput())
|
|
|
}
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ v2net.ChanToWriter(output, ray.InboundOutput())
|
|
|
+ outputFinish.Unlock()
|
|
|
+ }()
|
|
|
+
|
|
|
+ outputFinish.Lock()
|
|
|
}
|