mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: detect JetBrains running on local ipv6 (#11653)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package agentssh
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -11,24 +12,33 @@ import (
|
||||
)
|
||||
|
||||
func getListeningPortProcessCmdline(port uint32) (string, error) {
|
||||
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
|
||||
acceptFn := func(s *netstat.SockTabEntry) bool {
|
||||
return s.LocalAddr != nil && uint32(s.LocalAddr.Port) == port
|
||||
})
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("inspect port %d: %w", port, err)
|
||||
}
|
||||
if len(tabs) == 0 {
|
||||
return "", nil
|
||||
tabs, err := netstat.TCPSocks(acceptFn)
|
||||
tabs6, err6 := netstat.TCP6Socks(acceptFn)
|
||||
|
||||
// Only return the error if the other method found nothing.
|
||||
if (err != nil && len(tabs6) == 0) || (err6 != nil && len(tabs) == 0) {
|
||||
return "", xerrors.Errorf("inspect port %d: %w", port, errors.Join(err, err6))
|
||||
}
|
||||
|
||||
// Defensive check.
|
||||
if tabs[0].Process == nil {
|
||||
var proc *netstat.Process
|
||||
if len(tabs) > 0 {
|
||||
proc = tabs[0].Process
|
||||
} else if len(tabs6) > 0 {
|
||||
proc = tabs6[0].Process
|
||||
}
|
||||
if proc == nil {
|
||||
// Either nothing is listening on this port or we were unable to read the
|
||||
// process details (permission issues reading /proc/$pid/* potentially).
|
||||
// Or, perhaps /proc/net/tcp{,6} is not listing the port for some reason.
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// The process name provided by go-netstat does not include the full command
|
||||
// line so grab that instead.
|
||||
pid := tabs[0].Process.Pid
|
||||
pid := proc.Pid
|
||||
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("read /proc/%d/cmdline: %w", pid, err)
|
||||
|
||||
Reference in New Issue
Block a user