Skip to content

Commit dad4a41

Browse files
authored
refactor: commonize HTTP request processing (#95)
1 parent 2bb3595 commit dad4a41

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

proxy/proxy.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -219,28 +219,7 @@ func (p *Server) handleHTTPConnection(conn net.Conn) {
219219
}
220220

221221
p.logger.Debug("🌐 HTTP Request: %s %s", req.Method, req.URL.String())
222-
p.logger.Debug(" Host", "host", req.Host)
223-
p.logger.Debug(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
224-
225-
// Check if request should be allowed
226-
result := p.ruleEngine.Evaluate(req.Method, req.Host+req.URL.String())
227-
228-
// Audit the request
229-
p.auditor.AuditRequest(audit.Request{
230-
Method: req.Method,
231-
URL: req.URL.String(),
232-
Host: req.Host,
233-
Allowed: result.Allowed,
234-
Rule: result.Rule,
235-
})
236-
237-
if !result.Allowed {
238-
p.writeBlockedResponse(conn, req)
239-
return
240-
}
241-
242-
// Forward HTTP request to destination
243-
p.forwardRequest(conn, req, false)
222+
p.processHTTPRequest(conn, req, false)
244223
}
245224

246225
func (p *Server) handleTLSConnection(conn net.Conn) {
@@ -270,6 +249,10 @@ func (p *Server) handleTLSConnection(conn net.Conn) {
270249
}
271250

272251
p.logger.Debug("🔒 HTTPS Request", "method", req.Method, "url", req.URL.String())
252+
p.processHTTPRequest(tlsConn, req, true)
253+
}
254+
255+
func (p *Server) processHTTPRequest(conn net.Conn, req *http.Request, https bool) {
273256
p.logger.Debug(" Host", "host", req.Host)
274257
p.logger.Debug(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
275258

@@ -286,12 +269,12 @@ func (p *Server) handleTLSConnection(conn net.Conn) {
286269
})
287270

288271
if !result.Allowed {
289-
p.writeBlockedResponse(tlsConn, req)
272+
p.writeBlockedResponse(conn, req)
290273
return
291274
}
292275

293-
// Forward HTTPS request to destination
294-
p.forwardRequest(tlsConn, req, true)
276+
// Forward request to destination
277+
p.forwardRequest(conn, req, https)
295278
}
296279

297280
func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {

0 commit comments

Comments
 (0)