v2ray-core/proxy/http/http.go

108 lines
2.4 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-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-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"
"github.com/v2ray/v2ray-core/common/log"
2015-12-02 20:44:01 +00:00
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-28 11:13:27 +00:00
)
type HttpProxyServer struct {
2015-12-14 23:53:40 +00:00
accepting bool
space app.Space
config Config
2015-10-28 11:13:27 +00:00
}
2015-12-14 23:53:40 +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
}
}
2015-12-14 16:26:29 +00:00
func (this *HttpProxyServer) Listen(port v2net.Port) error {
2015-12-14 23:53:40 +00: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 11:13:27 +00:00
}
2015-12-14 23:53:40 +00:00
go this.accept(tcpListener)
return nil
2015-12-14 16:26:29 +00:00
}
2015-12-14 23:53:40 +00:00
func (this *HttpProxyServer) accept(listener *net.TCPListener) {
this.accepting = true
for this.accepting {
tcpConn, err := listener.AcceptTCP()
2015-12-14 16:26:29 +00:00
if err != nil {
2015-12-14 23:53:40 +00:00
log.Error("Failed to accept HTTP connection: %v", err)
continue
2015-12-14 16:26:29 +00:00
}
2015-12-14 23:53:40 +00:00
go this.handleConnection(tcpConn)
}
}
2015-12-14 16:26:29 +00:00
2015-12-14 23:53:40 +00:00
func parseHost(rawHost string) (v2net.Address, error) {
port := v2net.Port(80)
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
}
2015-12-14 16:26:29 +00:00
}
2015-12-14 23:53:40 +00:00
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
}
return v2net.DomainAddress(host, port), nil
2015-12-14 16:26:29 +00:00
}
2015-12-14 23:53:40 +00:00
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
}
if strings.ToUpper(request.Method) == "CONNECT" {
address, err := parseHost(request.Host)
if err != nil {
log.Warning("Malformed proxy host: %v", err)
return
}
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,
}
buffer := alloc.NewSmallBuffer().Clear()
response.Write(buffer)
conn.Write(buffer.Value)
2015-12-14 16:26:29 +00:00
2015-12-14 23:53:40 +00:00
packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true)
this.space.PacketDispatcher().DispatchToOutbound(packet)
}
2015-10-28 11:13:27 +00:00
}