Skip to content

fix: revert "detect JetBrains running on local ipv6 (#11653)" #11664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,59 +214,46 @@ func TestAgent_Stats_Magic(t *testing.T) {
_, b, _, ok := runtime.Caller(0)
require.True(t, ok)
dir := filepath.Join(filepath.Dir(b), "../scripts/echoserver/main.go")
echoServerCmd := exec.Command("go", "run", dir,
"-D", agentssh.MagicProcessCmdlineJetBrains)
stdout, err := echoServerCmd.StdoutPipe()
require.NoError(t, err)
err = echoServerCmd.Start()
require.NoError(t, err)
defer echoServerCmd.Process.Kill()

spawnServer := func(network string) (string, *exec.Cmd) {
echoServerCmd := exec.Command("go", "run", dir,
network, "-D", agentssh.MagicProcessCmdlineJetBrains)
stdout, err := echoServerCmd.StdoutPipe()
require.NoError(t, err)
err = echoServerCmd.Start()
require.NoError(t, err)
t.Cleanup(func() {
echoServerCmd.Process.Kill()
})

// The echo server prints its port as the first line.
sc := bufio.NewScanner(stdout)
sc.Scan()
return sc.Text(), echoServerCmd
}

port4, cmd4 := spawnServer("tcp4")
port6, cmd6 := spawnServer("tcp6")
// The echo server prints its port as the first line.
sc := bufio.NewScanner(stdout)
sc.Scan()
remotePort := sc.Text()

//nolint:dogsled
conn, _, stats, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
defer conn.Close()

sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)

tunnel4, err := sshClient.Dial("tcp4", fmt.Sprintf("127.0.0.1:%s", port4))
require.NoError(t, err)
defer tunnel4.Close()

tunnel6, err := sshClient.Dial("tcp6", fmt.Sprintf("[::]:%s", port6))
tunneledConn, err := sshClient.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", remotePort))
require.NoError(t, err)
defer tunnel6.Close()
t.Cleanup(func() {
// always close on failure of test
_ = conn.Close()
_ = tunneledConn.Close()
})

require.Eventuallyf(t, func() bool {
s, ok := <-stats
t.Logf("got stats with conn open: ok=%t, ConnectionCount=%d, SessionCountJetBrains=%d",
ok, s.ConnectionCount, s.SessionCountJetBrains)
return ok && s.ConnectionCount > 0 &&
s.SessionCountJetBrains == 2
s.SessionCountJetBrains == 1
}, testutil.WaitLong, testutil.IntervalFast,
"never saw stats with conn open",
)

// Kill the server and connection after checking for the echo.
requireEcho(t, tunnel4)
requireEcho(t, tunnel6)
_ = cmd4.Process.Kill()
_ = cmd6.Process.Kill()
_ = tunnel4.Close()
_ = tunnel6.Close()
requireEcho(t, tunneledConn)
_ = echoServerCmd.Process.Kill()
_ = tunneledConn.Close()

require.Eventuallyf(t, func() bool {
s, ok := <-stats
Expand Down
28 changes: 9 additions & 19 deletions agent/agentssh/portinspection_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package agentssh

import (
"errors"
"fmt"
"os"

Expand All @@ -12,33 +11,24 @@ import (
)

func getListeningPortProcessCmdline(port uint32) (string, error) {
acceptFn := func(s *netstat.SockTabEntry) bool {
tabs, err := netstat.TCPSocks(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)
}
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))
if len(tabs) == 0 {
return "", 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.
// Defensive check.
if tabs[0].Process == nil {
return "", nil
}

// The process name provided by go-netstat does not include the full command
// line so grab that instead.
pid := proc.Pid
pid := tabs[0].Process.Pid
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err != nil {
return "", xerrors.Errorf("read /proc/%d/cmdline: %w", pid, err)
Expand Down
13 changes: 1 addition & 12 deletions scripts/echoserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@ import (
"io"
"log"
"net"
"os"
)

func main() {
network := os.Args[1]
var address string
switch network {
case "tcp4":
address = "127.0.0.1"
case "tcp6":
address = "[::]"
default:
log.Fatalf("invalid network: %s", network)
}
l, err := net.Listen(network, address+":0")
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatalf("listen error: err=%s", err)
}
Expand Down