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"
|
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 {
|
2015-12-14 18:53:40 -05:00
|
|
|
accepting bool
|
|
|
|
space app.Space
|
|
|
|
config Config
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2015-12-14 18:53:40 -05:00
|
|
|
go this.accept(tcpListener)
|
|
|
|
return nil
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
|
|
|
|
2015-12-14 18:53:40 -05:00
|
|
|
func (this *HttpProxyServer) accept(listener *net.TCPListener) {
|
|
|
|
this.accepting = true
|
|
|
|
for this.accepting {
|
|
|
|
tcpConn, err := listener.AcceptTCP()
|
2015-12-14 11:26:29 -05:00
|
|
|
if err != nil {
|
2015-12-14 18:53:40 -05:00
|
|
|
log.Error("Failed to accept HTTP connection: %v", err)
|
|
|
|
continue
|
2015-12-14 11:26:29 -05:00
|
|
|
}
|
2015-12-14 18:53:40 -05:00
|
|
|
go this.handleConnection(tcpConn)
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 11:26:29 -05:00
|
|
|
|
2015-12-15 10:00:47 -05:00
|
|
|
func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Address, error) {
|
|
|
|
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 {
|
|
|
|
return v2net.IPAddress(ip, port), nil
|
|
|
|
}
|
|
|
|
return 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)
|
|
|
|
for true {
|
|
|
|
request, err := http.ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2015-12-16 09:52:40 -05:00
|
|
|
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)
|
2015-12-14 18:53:40 -05:00
|
|
|
}
|
2015-12-16 09:52:40 -05:00
|
|
|
host := request.Host
|
|
|
|
if len(host) == 0 {
|
|
|
|
host = request.URL.Host
|
2015-12-14 18:53:40 -05:00
|
|
|
}
|
2015-12-16 09:52:40 -05:00
|
|
|
address, err := parseHost(host, defaultPort)
|
2015-12-15 10:00:47 -05:00
|
|
|
if err != nil {
|
2015-12-16 09:52:40 -05:00
|
|
|
log.Warning("Malformed proxy host (%s): %v", host, err)
|
2015-12-15 10:00:47 -05:00
|
|
|
}
|
2015-12-16 09:52:40 -05:00
|
|
|
if strings.ToUpper(request.Method) == "CONNECT" {
|
|
|
|
this.handleConnect(request, address, reader, conn)
|
|
|
|
} else if len(request.URL.Host) > 0 {
|
|
|
|
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, true)
|
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
2015-12-16 10:37:32 -05:00
|
|
|
go func() {
|
|
|
|
defer close(ray.InboundInput())
|
|
|
|
responseReader := bufio.NewReader(NewChanReader(ray.InboundOutput()))
|
|
|
|
response, err := http.ReadResponse(responseReader, request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
responseBuffer := alloc.NewBuffer().Clear()
|
|
|
|
defer responseBuffer.Release()
|
|
|
|
response.Write(responseBuffer)
|
|
|
|
conn.Write(responseBuffer.Value)
|
|
|
|
}()
|
2015-12-16 09:52:40 -05:00
|
|
|
} 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)
|
|
|
|
conn.Write(buffer.Value)
|
|
|
|
buffer.Release()
|
2015-12-15 16:13:09 -05:00
|
|
|
}
|
2015-12-16 09:52:40 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 16:13:09 -05:00
|
|
|
|
2015-12-16 09:52:40 -05:00
|
|
|
func (this *HttpProxyServer) handleConnect(request *http.Request, address v2net.Address, reader io.Reader, writer io.Writer) {
|
|
|
|
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 09:52:40 -05:00
|
|
|
packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true)
|
|
|
|
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-16 09:52:40 -05:00
|
|
|
var outputFinish sync.Mutex
|
2015-12-15 10:00:47 -05:00
|
|
|
outputFinish.Lock()
|
|
|
|
|
2015-12-16 09:52:40 -05:00
|
|
|
go func() {
|
|
|
|
v2net.ReaderToChan(ray.InboundInput(), input)
|
|
|
|
close(ray.InboundInput())
|
|
|
|
}()
|
2015-12-15 10:00:47 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
v2net.ChanToWriter(output, ray.InboundOutput())
|
|
|
|
outputFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
outputFinish.Lock()
|
2015-10-28 07:13:27 -04:00
|
|
|
}
|