http.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package http
  2. import (
  3. "bufio"
  4. "io"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "github.com/v2ray/v2ray-core/app"
  11. "github.com/v2ray/v2ray-core/common/alloc"
  12. "github.com/v2ray/v2ray-core/common/log"
  13. v2net "github.com/v2ray/v2ray-core/common/net"
  14. "github.com/v2ray/v2ray-core/transport/ray"
  15. )
  16. type HttpProxyServer struct {
  17. accepting bool
  18. space app.Space
  19. config Config
  20. }
  21. func NewHttpProxyServer(space app.Space, config Config) *HttpProxyServer {
  22. return &HttpProxyServer{
  23. space: space,
  24. config: config,
  25. }
  26. }
  27. func (this *HttpProxyServer) Listen(port v2net.Port) error {
  28. tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
  29. Port: int(port.Value()),
  30. IP: []byte{0, 0, 0, 0},
  31. })
  32. if err != nil {
  33. return err
  34. }
  35. go this.accept(tcpListener)
  36. return nil
  37. }
  38. func (this *HttpProxyServer) accept(listener *net.TCPListener) {
  39. this.accepting = true
  40. for this.accepting {
  41. tcpConn, err := listener.AcceptTCP()
  42. if err != nil {
  43. log.Error("Failed to accept HTTP connection: %v", err)
  44. continue
  45. }
  46. go this.handleConnection(tcpConn)
  47. }
  48. }
  49. func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Address, error) {
  50. port := defaultPort
  51. host, rawPort, err := net.SplitHostPort(rawHost)
  52. if err != nil {
  53. if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
  54. host = rawHost
  55. } else {
  56. return nil, err
  57. }
  58. } else {
  59. intPort, err := strconv.Atoi(rawPort)
  60. if err != nil {
  61. return nil, err
  62. }
  63. port = v2net.Port(intPort)
  64. }
  65. if ip := net.ParseIP(host); ip != nil {
  66. return v2net.IPAddress(ip, port), nil
  67. }
  68. return v2net.DomainAddress(host, port), nil
  69. }
  70. func (this *HttpProxyServer) handleConnection(conn *net.TCPConn) {
  71. defer conn.Close()
  72. reader := bufio.NewReader(conn)
  73. for true {
  74. request, err := http.ReadRequest(reader)
  75. if err != nil {
  76. break
  77. }
  78. this.handleRequest(request, reader, conn)
  79. }
  80. }
  81. func (this *HttpProxyServer) handleRequest(request *http.Request, reader io.Reader, writer io.Writer) {
  82. log.Info("Request to Method [%s] Host [%s] with URL [%s]", request.Method, request.Host, request.URL.String())
  83. defaultPort := v2net.Port(80)
  84. if strings.ToLower(request.URL.Scheme) == "https" {
  85. defaultPort = v2net.Port(443)
  86. }
  87. if strings.ToUpper(request.Method) == "CONNECT" {
  88. address, err := parseHost(request.Host, defaultPort)
  89. if err != nil {
  90. log.Warning("Malformed proxy host: %v", err)
  91. return
  92. }
  93. response := &http.Response{
  94. Status: "200 OK",
  95. StatusCode: 200,
  96. Proto: "HTTP/1.1",
  97. ProtoMajor: 1,
  98. ProtoMinor: 1,
  99. Header: http.Header(make(map[string][]string)),
  100. Body: nil,
  101. ContentLength: 0,
  102. Close: false,
  103. }
  104. buffer := alloc.NewSmallBuffer().Clear()
  105. response.Write(buffer)
  106. writer.Write(buffer.Value)
  107. packet := v2net.NewPacket(v2net.NewTCPDestination(address), nil, true)
  108. ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
  109. this.transport(reader, writer, ray)
  110. } else if len(request.URL.Host) > 0 {
  111. address, err := parseHost(request.URL.Host, defaultPort)
  112. if err != nil {
  113. log.Warning("Malformed proxy host: %v", err)
  114. return
  115. }
  116. request.Host = request.URL.Host
  117. request.Header.Set("Connection", "keep-alive")
  118. request.Header.Del("Proxy-Connection")
  119. buffer := alloc.NewBuffer().Clear()
  120. request.Write(buffer)
  121. log.Info("Request to remote: %s", string(buffer.Value))
  122. packet := v2net.NewPacket(v2net.NewTCPDestination(address), buffer, false)
  123. ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
  124. this.transport(nil, writer, ray)
  125. } else {
  126. response := &http.Response{
  127. Status: "400 Bad Request",
  128. StatusCode: 400,
  129. Proto: "HTTP/1.1",
  130. ProtoMajor: 1,
  131. ProtoMinor: 1,
  132. Header: http.Header(make(map[string][]string)),
  133. Body: nil,
  134. ContentLength: 0,
  135. Close: false,
  136. }
  137. buffer := alloc.NewSmallBuffer().Clear()
  138. response.Write(buffer)
  139. writer.Write(buffer.Value)
  140. }
  141. }
  142. func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
  143. var inputFinish, outputFinish sync.Mutex
  144. inputFinish.Lock()
  145. outputFinish.Lock()
  146. if input != nil {
  147. go func() {
  148. v2net.ReaderToChan(ray.InboundInput(), input)
  149. inputFinish.Unlock()
  150. close(ray.InboundInput())
  151. }()
  152. } else {
  153. close(ray.InboundInput())
  154. }
  155. go func() {
  156. v2net.ChanToWriter(output, ray.InboundOutput())
  157. outputFinish.Unlock()
  158. }()
  159. outputFinish.Lock()
  160. }