Skip to content

Commit 6f1939d

Browse files
refactor: minor improvement
1 parent b5084b4 commit 6f1939d

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

proxy/proxy.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,27 @@ func (p *Server) isStopped() bool {
118118

119119
func (p *Server) handleConnectionWithTLSDetection(conn net.Conn) {
120120
// Detect protocol using TLS handshake detection
121-
conn, isTLS := p.isTLSConnection(conn)
121+
wrappedConn, isTLS, err := p.isTLSConnection(conn)
122+
if err != nil {
123+
p.logger.Error("Failed to check connection type", "error", err)
124+
conn.Close()
125+
return
126+
}
122127
if isTLS {
123-
p.logger.Info("🔒 Detected TLS connection - handling as HTTPS")
124-
p.handleTLSConnection(conn)
128+
p.logger.Debug("🔒 Detected TLS connection - handling as HTTPS")
129+
p.handleTLSConnection(wrappedConn)
125130
} else {
126-
p.logger.Info("🌐 Detected HTTP connection")
127-
p.handleHTTPConnection(conn)
131+
p.logger.Debug("🌐 Detected HTTP connection")
132+
p.handleHTTPConnection(wrappedConn)
128133
}
129134
}
130135

131-
func (p *Server) isTLSConnection(conn net.Conn) (net.Conn, bool) {
136+
func (p *Server) isTLSConnection(conn net.Conn) (net.Conn, bool, error) {
132137
// Read first byte to detect TLS
133138
buf := make([]byte, 1)
134139
n, err := conn.Read(buf)
135140
if err != nil || n == 0 {
136-
// TODO: return error?
137-
return nil, false
141+
return nil, false, fmt.Errorf("failed to read first byte from connection: %v, read %v bytes", err, n)
138142
}
139143

140144
connWrapper := &connectionWrapper{conn, buf, false}
@@ -147,10 +151,10 @@ func (p *Server) isTLSConnection(conn net.Conn) (net.Conn, bool) {
147151
isTLS := buf[0] == 0x16 || buf[0] == 0x17 || buf[0] == 0x14 || buf[0] == 0x15
148152

149153
if isTLS {
150-
p.logger.Info("TLS detected", "first byte", buf[0])
154+
p.logger.Debug("TLS detected", "first byte", buf[0])
151155
}
152156

153-
return connWrapper, isTLS
157+
return connWrapper, isTLS, nil
154158
}
155159

156160
func (p *Server) handleHTTPConnection(conn net.Conn) {
@@ -168,9 +172,9 @@ func (p *Server) handleHTTPConnection(conn net.Conn) {
168172
return
169173
}
170174

171-
p.logger.Info("🌐 HTTP Request: %s %s", req.Method, req.URL.String())
172-
p.logger.Info(" Host", "host", req.Host)
173-
p.logger.Info(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
175+
p.logger.Debug("🌐 HTTP Request: %s %s", req.Method, req.URL.String())
176+
p.logger.Debug(" Host", "host", req.Host)
177+
p.logger.Debug(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
174178

175179
// Check if request should be allowed
176180
result := p.ruleEngine.Evaluate(req.Method, req.Host)
@@ -209,7 +213,7 @@ func (p *Server) handleTLSConnection(conn net.Conn) {
209213
return
210214
}
211215

212-
p.logger.Info("✅ TLS handshake successful")
216+
p.logger.Debug("✅ TLS handshake successful")
213217

214218
// Read HTTP request over TLS
215219
req, err := http.ReadRequest(bufio.NewReader(tlsConn))
@@ -218,9 +222,9 @@ func (p *Server) handleTLSConnection(conn net.Conn) {
218222
return
219223
}
220224

221-
p.logger.Info("🔒 HTTPS Request", "method", req.Method, "url", req.URL.String())
222-
p.logger.Info(" Host", "host", req.Host)
223-
p.logger.Info(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
225+
p.logger.Debug("🔒 HTTPS Request", "method", req.Method, "url", req.URL.String())
226+
p.logger.Debug(" Host", "host", req.Host)
227+
p.logger.Debug(" User-Agent", "user-agent", req.Header.Get("User-Agent"))
224228

225229
// Check if request should be allowed
226230
result := p.ruleEngine.Evaluate(req.Method, req.Host)
@@ -286,7 +290,7 @@ func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {
286290
return
287291
}
288292

289-
p.logger.Info("🔒 HTTPS Response", "status code", resp.StatusCode, "status", resp.Status)
293+
p.logger.Debug("🔒 HTTPS Response", "status code", resp.StatusCode, "status", resp.Status)
290294

291295
bodyBytes, err := io.ReadAll(resp.Body)
292296
if err != nil {
@@ -309,7 +313,7 @@ func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {
309313
return
310314
}
311315

312-
p.logger.Info("Successfully wrote to connection")
316+
p.logger.Debug("Successfully wrote to connection")
313317
}
314318

315319
func (p *Server) writeBlockedResponse(conn net.Conn, req *http.Request) {

0 commit comments

Comments
 (0)