@@ -209,6 +209,23 @@ func (a *SluiceAddon) consumePendingChecker(dest string) *pendingCheck {
209209 return pc
210210}
211211
212+ // recoverPortFromPending scans the pending checker map for a key whose
213+ // host part matches the given hostname and returns its port as a string.
214+ // Falls back to "443" if no match is found. This handles the case where
215+ // go-mitmproxy's TlsEstablishedServer provides a host-only address
216+ // without the port from the original SOCKS5 CONNECT.
217+ func (a * SluiceAddon ) recoverPortFromPending (host string ) string {
218+ a .pendingMu .Lock ()
219+ defer a .pendingMu .Unlock ()
220+ for key := range a .pendingCheckers {
221+ h , p , err := net .SplitHostPort (key )
222+ if err == nil && h == host {
223+ return p
224+ }
225+ }
226+ return "443"
227+ }
228+
212229// CancelPendingChecker removes the most recent pending checker for the
213230// given host:port key without consuming it for a connection. This must be
214231// called when dialThroughMITM fails after PendingChecker was called, so the
@@ -287,10 +304,12 @@ func (a *SluiceAddon) captureConnectTarget(ctx *mitmproxy.ConnContext) {
287304
288305 host , portStr , err := net .SplitHostPort (addr )
289306 if err != nil {
290- // Address might be host-only without a port (e.g. during
291- // TLS establishment). Default to 443 for TLS connections.
307+ // Address is host-only without a port (common in
308+ // TlsEstablishedServer). Recover the port from the pending
309+ // checker map which was keyed on the exact host:port from
310+ // the SOCKS5 CONNECT.
292311 host = addr
293- portStr = "443"
312+ portStr = a . recoverPortFromPending ( host )
294313 }
295314
296315 port , err := strconv .Atoi (portStr )
0 commit comments