barely working http proxy

pull/62/head
v2ray 2015-12-15 16:00:47 +01:00
parent d0a75ce9f3
commit e12e5a0ecb
10 changed files with 139 additions and 31 deletions

View File

@ -78,9 +78,9 @@ func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.Outbou
} }
writeMutex.Lock() writeMutex.Lock()
if tcpConn, ok := conn.(*net.TCPConn); ok { //if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite() // tcpConn.CloseWrite()
} //}
readMutex.Lock() readMutex.Lock()
conn.Close() conn.Close()

View File

@ -3,7 +3,6 @@ package freedom
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"testing" "testing"
@ -127,9 +126,9 @@ func TestSocksTcpConnect(t *testing.T) {
tcpConn.CloseWrite() tcpConn.CloseWrite()
} }
dataReturned, err := ioutil.ReadAll(conn) dataReturned, err := v2net.ReadFrom(conn, nil)
assert.Error(err).IsNil() assert.Error(err).IsNil()
conn.Close() conn.Close()
assert.Bytes(dataReturned).Equals([]byte("Processed: Data to be sent to remote")) assert.Bytes(dataReturned.Value).Equals([]byte("Processed: Data to be sent to remote"))
} }

View File

@ -2,15 +2,18 @@ package http
import ( import (
"bufio" "bufio"
"io"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/ray"
) )
type HttpProxyServer struct { type HttpProxyServer struct {
@ -50,22 +53,23 @@ func (this *HttpProxyServer) accept(listener *net.TCPListener) {
} }
} }
func parseHost(rawHost string) (v2net.Address, error) { func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Address, error) {
port := v2net.Port(80) port := defaultPort
host, rawPort, err := net.SplitHostPort(rawHost) host, rawPort, err := net.SplitHostPort(rawHost)
if err != nil { if err != nil {
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") { if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
host = rawHost host = rawHost
port = v2net.Port(80)
} else { } else {
return nil, err 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 { if ip := net.ParseIP(host); ip != nil {
return v2net.IPAddress(ip, port), 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) { func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
httpReader := bufio.NewReader(conn) defer conn.Close()
request, err := http.ReadRequest(httpReader) reader := bufio.NewReader(conn)
if err != nil { for true {
log.Warning("Malformed HTTP request: %v", err) request, err := http.ReadRequest(reader)
return 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" { if strings.ToUpper(request.Method) == "CONNECT" {
address, err := parseHost(request.Host) address, err := parseHost(request.Host, defaultPort)
if err != nil { if err != nil {
log.Warning("Malformed proxy host: %v", err) log.Warning("Malformed proxy host: %v", err)
return return
@ -99,9 +114,64 @@ func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
buffer := alloc.NewSmallBuffer().Clear() buffer := alloc.NewSmallBuffer().Clear()
response.Write(buffer) response.Write(buffer)
conn.Write(buffer.Value) writer.Write(buffer.Value)
packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true) 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()
}

View File

@ -0,0 +1,17 @@
package http
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
)
type HttpProxyServerFactory struct {
}
func (this HttpProxyServerFactory) Create(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
return NewHttpProxyServer(space, rawConfig.(Config)), nil
}
func init() {
connhandler.RegisterInboundConnectionHandlerFactory("http", HttpProxyServerFactory{})
}

View File

@ -21,6 +21,8 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/dokodemo/json" _ "github.com/v2ray/v2ray-core/proxy/dokodemo/json"
_ "github.com/v2ray/v2ray-core/proxy/freedom" _ "github.com/v2ray/v2ray-core/proxy/freedom"
_ "github.com/v2ray/v2ray-core/proxy/freedom/json" _ "github.com/v2ray/v2ray-core/proxy/freedom/json"
_ "github.com/v2ray/v2ray-core/proxy/http"
_ "github.com/v2ray/v2ray-core/proxy/http/json"
_ "github.com/v2ray/v2ray-core/proxy/socks" _ "github.com/v2ray/v2ray-core/proxy/socks"
_ "github.com/v2ray/v2ray-core/proxy/socks/json" _ "github.com/v2ray/v2ray-core/proxy/socks/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"

View File

@ -24,6 +24,7 @@ func TestDokodemoTCP(t *testing.T) {
} }
_, err := tcpServer.Start() _, err := tcpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer tcpServer.Close()
assert.Error(InitializeServerSetOnce("test_2")).IsNil() assert.Error(InitializeServerSetOnce("test_2")).IsNil()

View File

@ -24,6 +24,7 @@ func TestRouter(t *testing.T) {
} }
_, err := tcpServer.Start() _, err := tcpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer tcpServer.Close()
tcpServer2Accessed := false tcpServer2Accessed := false
tcpServer2 := &tcp.Server{ tcpServer2 := &tcp.Server{
@ -35,6 +36,7 @@ func TestRouter(t *testing.T) {
} }
_, err = tcpServer2.Start() _, err = tcpServer2.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer tcpServer2.Close()
assert.Error(InitializeServerSetOnce("test_3")).IsNil() assert.Error(InitializeServerSetOnce("test_3")).IsNil()

View File

@ -31,12 +31,13 @@ func TestTCPConnection(t *testing.T) {
} }
_, err := tcpServer.Start() _, err := tcpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer tcpServer.Close()
assert.Error(InitializeServerSetOnce("test_1")).IsNil() assert.Error(InitializeServerSetOnce("test_1")).IsNil()
socksPort := v2net.Port(50000) socksPort := v2net.Port(50000)
for i := 0; i < 100; i++ { for i := 0; i < 10; i++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: int(socksPort), Port: int(socksPort),
@ -98,6 +99,7 @@ func TestTCPBind(t *testing.T) {
} }
_, err := tcpServer.Start() _, err := tcpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer tcpServer.Close()
assert.Error(InitializeServerSetOnce("test_1")).IsNil() assert.Error(InitializeServerSetOnce("test_1")).IsNil()
@ -146,6 +148,7 @@ func TestUDPAssociate(t *testing.T) {
} }
_, err := udpServer.Start() _, err := udpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
defer udpServer.Close()
assert.Error(InitializeServerSetOnce("test_1")).IsNil() assert.Error(InitializeServerSetOnce("test_1")).IsNil()

View File

@ -2,7 +2,6 @@ package tcp
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
@ -11,6 +10,7 @@ import (
type Server struct { type Server struct {
Port v2net.Port Port v2net.Port
MsgProcessor func(msg []byte) []byte MsgProcessor func(msg []byte) []byte
accepting bool
} }
func (server *Server) Start() (v2net.Address, error) { func (server *Server) Start() (v2net.Address, error) {
@ -28,7 +28,9 @@ func (server *Server) Start() (v2net.Address, error) {
} }
func (server *Server) acceptConnections(listener *net.TCPListener) { func (server *Server) acceptConnections(listener *net.TCPListener) {
for { server.accepting = true
defer listener.Close()
for server.accepting {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
fmt.Printf("Failed accept TCP connection: %v", err) fmt.Printf("Failed accept TCP connection: %v", err)
@ -40,12 +42,17 @@ func (server *Server) acceptConnections(listener *net.TCPListener) {
} }
func (server *Server) handleConnection(conn net.Conn) { func (server *Server) handleConnection(conn net.Conn) {
request, err := ioutil.ReadAll(conn) for true {
if err != nil { request, err := v2net.ReadFrom(conn, nil)
fmt.Printf("Failed to read request: %v", err) if err != nil {
return break
}
response := server.MsgProcessor(request.Value)
conn.Write(response)
} }
response := server.MsgProcessor(request)
conn.Write(response)
conn.Close() conn.Close()
} }
func (this *Server) Close() {
this.accepting = true
}

View File

@ -10,6 +10,7 @@ import (
type Server struct { type Server struct {
Port v2net.Port Port v2net.Port
MsgProcessor func(msg []byte) []byte MsgProcessor func(msg []byte) []byte
accepting bool
} }
func (server *Server) Start() (v2net.Address, error) { func (server *Server) Start() (v2net.Address, error) {
@ -27,6 +28,8 @@ func (server *Server) Start() (v2net.Address, error) {
} }
func (server *Server) handleConnection(conn *net.UDPConn) { func (server *Server) handleConnection(conn *net.UDPConn) {
server.accepting = true
defer conn.Close()
for { for {
buffer := make([]byte, 2*1024) buffer := make([]byte, 2*1024)
nBytes, addr, err := conn.ReadFromUDP(buffer) nBytes, addr, err := conn.ReadFromUDP(buffer)
@ -39,3 +42,7 @@ func (server *Server) handleConnection(conn *net.UDPConn) {
conn.WriteToUDP(response, addr) conn.WriteToUDP(response, addr)
} }
} }
func (server *Server) Close() {
server.accepting = false
}