(conn net.Conn, req *http.Request, isConnectMethod bool)
| 181 | } |
| 182 | |
| 183 | func (s *Server) embedHandleHTTP(conn net.Conn, req *http.Request, isConnectMethod bool) error { |
| 184 | defer func() { |
| 185 | _ = conn.Close() |
| 186 | }() |
| 187 | |
| 188 | targetAddr := req.URL.Host |
| 189 | host, portStr, err := net.SplitHostPort(targetAddr) |
| 190 | if err != nil { |
| 191 | host = targetAddr |
| 192 | if req.URL.Scheme == "https" || isConnectMethod { |
| 193 | portStr = "443" |
| 194 | } else { |
| 195 | portStr = "80" |
| 196 | } |
| 197 | targetAddr = net.JoinHostPort(host, portStr) |
| 198 | } |
| 199 | |
| 200 | target, err := s.ProxyDial(s.Context, "tcp", targetAddr) |
| 201 | if err != nil { |
| 202 | http.Error( |
| 203 | NewHTTPResponseWriter(conn), |
| 204 | err.Error(), |
| 205 | http.StatusServiceUnavailable, |
| 206 | ) |
| 207 | return err |
| 208 | } |
| 209 | defer func() { |
| 210 | _ = target.Close() |
| 211 | }() |
| 212 | |
| 213 | if isConnectMethod { |
| 214 | _, err = conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")) |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | } else { |
| 219 | err = req.Write(target) |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | var buf1, buf2 []byte |
| 226 | if s.BytesPool != nil { |
| 227 | buf1 = s.BytesPool.Get() |
| 228 | buf2 = s.BytesPool.Get() |
| 229 | defer func() { |
| 230 | s.BytesPool.Put(buf1) |
| 231 | s.BytesPool.Put(buf2) |
| 232 | }() |
| 233 | } else { |
| 234 | buf1 = make([]byte, 32*1024) |
| 235 | buf2 = make([]byte, 32*1024) |
| 236 | } |
| 237 | return statute.Tunnel(s.Context, target, conn, buf1, buf2) |
| 238 | } |
no test coverage detected