Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e5f54c

Browse files
committed
fix: detect JetBrains running on local ipv6
1 parent 552e9fe commit 4e5f54c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

agent/agentssh/portinspection_supported.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package agentssh
44

55
import (
6+
"errors"
67
"fmt"
78
"os"
89

@@ -11,24 +12,33 @@ import (
1112
)
1213

1314
func getListeningPortProcessCmdline(port uint32) (string, error) {
14-
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
15+
acceptFn := func(s *netstat.SockTabEntry) bool {
1516
return s.LocalAddr != nil && uint32(s.LocalAddr.Port) == port
16-
})
17-
if err != nil {
18-
return "", xerrors.Errorf("inspect port %d: %w", port, err)
1917
}
20-
if len(tabs) == 0 {
21-
return "", nil
18+
tabs, err := netstat.TCPSocks(acceptFn)
19+
tabs6, err6 := netstat.TCP6Socks(acceptFn)
20+
21+
// Only return the error if the other method found nothing.
22+
if (err != nil && len(tabs6) == 0) || (err6 != nil && len(tabs) == 0) {
23+
return "", xerrors.Errorf("inspect port %d: %w", port, errors.Join(err, err6))
2224
}
2325

24-
// Defensive check.
25-
if tabs[0].Process == nil {
26+
var proc *netstat.Process
27+
if len(tabs) > 0 {
28+
proc = tabs[0].Process
29+
} else if len(tabs6) > 0 {
30+
proc = tabs6[0].Process
31+
}
32+
if proc == nil {
33+
// Either nothing is listening on this port or we were unable to read the
34+
// process details (permission issues reading /proc/$pid/* potentially).
35+
// Or, perhaps /proc/net/tcp{,6} is not listing the port for some reason.
2636
return "", nil
2737
}
2838

2939
// The process name provided by go-netstat does not include the full command
3040
// line so grab that instead.
31-
pid := tabs[0].Process.Pid
41+
pid := proc.Pid
3242
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
3343
if err != nil {
3444
return "", xerrors.Errorf("read /proc/%d/cmdline: %w", pid, err)

0 commit comments

Comments
 (0)