1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-21 12:44:17 -04:00
v2fly/proxy/http/server.go

288 lines
7.4 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
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
v2io "v2ray.com/core/common/io"
2016-10-16 08:22:21 -04:00
"v2ray.com/core/common/loader"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2015-10-28 07:13:27 -04:00
)
2016-05-29 10:46:31 -04:00
// Server is a HTTP proxy server.
type Server struct {
sync.Mutex
2016-01-31 11:01:28 -05:00
accepting bool
packetDispatcher dispatcher.PacketDispatcher
2016-08-25 15:55:49 -04:00
config *ServerConfig
2016-06-14 16:54:08 -04:00
tcpListener *internet.TCPHub
2016-06-04 08:25:13 -04:00
meta *proxy.InboundHandlerMeta
2015-10-28 07:13:27 -04:00
}
2016-08-25 15:55:49 -04:00
func NewServer(config *ServerConfig, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
2016-05-29 10:46:31 -04:00
return &Server{
2016-01-31 11:01:28 -05:00
packetDispatcher: packetDispatcher,
config: config,
2016-06-04 08:25:13 -04:00
meta: meta,
2015-10-28 07:13:27 -04:00
}
}
2016-05-29 10:46:31 -04:00
func (this *Server) Port() v2net.Port {
2016-06-04 08:25:13 -04:00
return this.meta.Port
2016-01-19 17:41:40 -05:00
}
2016-05-29 10:46:31 -04:00
func (this *Server) Close() {
this.accepting = false
if this.tcpListener != nil {
2016-01-03 18:33:25 -05:00
this.Lock()
this.tcpListener.Close()
this.tcpListener = nil
this.Unlock()
}
}
2016-06-03 18:38:22 -04:00
func (this *Server) Start() error {
2016-01-19 17:41:40 -05:00
if this.accepting {
2016-06-03 18:38:22 -04:00
return nil
2016-01-19 17:41:40 -05:00
}
2016-06-14 16:54:08 -04:00
tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, this.meta.StreamSettings)
2015-12-14 18:53:40 -05:00
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("HTTP: Failed listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
2015-12-14 18:53:40 -05:00
return err
2015-10-28 07:13:27 -04:00
}
2016-01-04 02:41:01 -05:00
this.Lock()
this.tcpListener = tcpListener
2016-01-04 02:41:01 -05:00
this.Unlock()
this.accepting = true
2015-12-14 18:53:40 -05:00
return nil
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 {
2016-09-20 05:53:05 -04:00
return v2net.Destination{}, err
2015-12-14 18:53:40 -05:00
}
2015-12-15 10:00:47 -05:00
} else {
intPort, err := strconv.Atoi(rawPort)
if err != nil {
2016-09-20 05:53:05 -04:00
return v2net.Destination{}, err
2015-12-15 10:00:47 -05:00
}
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
}
2016-06-14 16:54:08 -04:00
func (this *Server) handleConnection(conn internet.Connection) {
2015-12-15 10:00:47 -05:00
defer conn.Close()
2016-07-10 09:34:14 -04:00
timedReader := v2net.NewTimeOutReader(this.config.Timeout, conn)
2016-07-16 07:22:34 -04:00
reader := bufio.NewReaderSize(timedReader, 2048)
request, err := http.ReadRequest(reader)
if err != nil {
2016-06-03 14:21:46 -04:00
if err != io.EOF {
log.Warning("HTTP: Failed to read http request: ", err)
}
return
}
2016-06-01 18:57:08 -04:00
log.Info("HTTP: 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-06-01 18:57:08 -04:00
log.Warning("HTTP: Malformed proxy host (", host, "): ", err)
2016-01-11 06:35:28 -05:00
return
}
2016-06-05 09:02:15 -04:00
log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "")
2016-08-14 11:08:01 -04:00
session := &proxy.SessionInfo{
2016-08-14 17:20:23 -04:00
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
2016-08-14 11:08:01 -04:00
Destination: dest,
2016-11-13 08:33:00 -05:00
Inbound: this.meta,
2016-08-14 11:08:01 -04:00
}
if strings.ToUpper(request.Method) == "CONNECT" {
2016-08-14 11:08:01 -04:00
this.handleConnect(request, session, reader, conn)
} else {
2016-08-14 11:08:01 -04:00
this.handlePlainHTTP(request, session, reader, conn)
2015-12-16 09:52:40 -05:00
}
}
2015-12-15 16:13:09 -05:00
2016-08-14 11:08:01 -04:00
func (this *Server) handleConnect(request *http.Request, session *proxy.SessionInfo, 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,
}
2016-07-16 07:22:34 -04:00
response.Write(writer)
2015-12-15 10:00:47 -05:00
2016-11-13 08:33:00 -05:00
ray := this.packetDispatcher.DispatchToOutbound(session)
2015-12-16 09:52:40 -05:00
this.transport(reader, writer, ray)
2015-12-15 10:00:47 -05:00
}
2016-05-29 10:46:31 -04:00
func (this *Server) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
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() {
v2reader := v2io.NewAdaptiveReader(input)
defer v2reader.Release()
v2io.Pipe(v2reader, ray.InboundInput())
2016-04-18 12:44:10 -04:00
ray.InboundInput().Close()
wg.Done()
2015-12-16 09:52:40 -05:00
}()
2015-12-15 10:00:47 -05:00
go func() {
v2writer := v2io.NewAdaptiveWriter(output)
defer v2writer.Release()
v2io.Pipe(ray.InboundOutput(), v2writer)
2016-04-18 12:44:10 -04:00
ray.InboundOutput().Release()
wg.Done()
2015-12-15 10:00:47 -05:00
}()
}
2015-12-15 10:00:47 -05:00
2016-02-20 17:27:06 -05:00
// @VisibleForTesting
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))
}
}
2016-06-01 18:57:08 -04:00
func (this *Server) GenerateResponse(statusCode int, status string) *http.Response {
hdr := http.Header(make(map[string][]string))
hdr.Set("Connection", "close")
return &http.Response{
Status: status,
StatusCode: statusCode,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: hdr,
Body: nil,
ContentLength: 0,
Close: false,
}
}
2016-08-14 11:08:01 -04:00
func (this *Server) handlePlainHTTP(request *http.Request, session *proxy.SessionInfo, reader *bufio.Reader, writer io.Writer) {
if len(request.URL.Host) <= 0 {
2016-06-01 18:57:08 -04:00
response := this.GenerateResponse(400, "Bad Request")
2016-07-16 07:22:34 -04:00
response.Write(writer)
return
}
request.Host = request.URL.Host
2016-02-20 17:27:06 -05:00
StripHopByHopHeaders(request)
2016-11-13 08:33:00 -05:00
ray := this.packetDispatcher.DispatchToOutbound(session)
2016-04-18 12:44:10 -04:00
defer ray.InboundInput().Close()
defer ray.InboundOutput().Release()
var finish sync.WaitGroup
finish.Add(1)
go func() {
defer finish.Done()
requestWriter := v2io.NewBufferedWriter(v2io.NewChainWriter(ray.InboundInput()))
err := request.Write(requestWriter)
if err != nil {
log.Warning("HTTP: Failed to write request: ", err)
return
}
requestWriter.Flush()
}()
finish.Add(1)
go func() {
defer finish.Done()
2016-05-25 16:36:52 -04:00
responseReader := bufio.NewReader(v2io.NewChanReader(ray.InboundOutput()))
response, err := http.ReadResponse(responseReader, request)
if err != nil {
log.Warning("HTTP: Failed to read response: ", err)
2016-06-01 18:57:08 -04:00
response = this.GenerateResponse(503, "Service Unavailable")
}
responseWriter := v2io.NewBufferedWriter(writer)
err = response.Write(responseWriter)
if err != nil {
log.Warning("HTTP: Failed to write response: ", err)
return
}
responseWriter.Flush()
}()
finish.Wait()
2015-10-28 07:13:27 -04:00
}
2016-05-07 08:08:27 -04:00
2016-06-14 16:54:08 -04:00
type ServerFactory struct{}
2016-10-02 17:43:58 -04:00
func (this *ServerFactory) StreamCapability() v2net.NetworkList {
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_RawTCP},
}
2016-06-14 16:54:08 -04:00
}
func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
2016-08-18 02:21:20 -04:00
return nil, common.ErrBadConfiguration
2016-06-14 16:54:08 -04:00
}
return NewServer(
2016-08-25 15:55:49 -04:00
rawConfig.(*ServerConfig),
2016-06-14 16:54:08 -04:00
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
meta), nil
}
2016-05-07 08:08:27 -04:00
func init() {
2016-10-16 08:22:21 -04:00
registry.MustRegisterInboundHandlerCreator(loader.GetType(new(ServerConfig)), new(ServerFactory))
2016-05-07 08:08:27 -04:00
}