1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/proxy/http/http.go

184 lines
4.6 KiB
Go
Raw Normal View History

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-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)
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 17:53:38 -05:00
dest, 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" {
2015-12-16 17:53:38 -05:00
this.handleConnect(request, dest, reader, conn)
2015-12-16 09:52:40 -05:00
} 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))
2015-12-16 17:53:38 -05:00
packet := v2net.NewPacket(dest, buffer, true)
2015-12-16 09:52:40 -05:00
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 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-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
}