Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: track JetBrains connections#10968

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
code-asher merged 12 commits intomainfromjetbrains/track_connections
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
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
PrevPrevious commit
Fix JetBrains tracking test
The test name only shows up in the process name if you are running thattest directly so we have to spawn a separate process instead.
  • Loading branch information
@code-asher
code-asher committedDec 1, 2023
commita75ed6c26930375588837dbefcc4d27e3eddf423
51 changes: 31 additions & 20 deletionsagent/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package agent_test

import (
"bufio"
"bytes"
"context"
"encoding/json"
Expand DownExpand Up@@ -152,7 +153,7 @@ func TestAgent_Stats_Magic(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, strings.TrimSpace(string(output)))
})
t.Run("Tracks", func(t *testing.T) {
t.Run("TracksVSCode", func(t *testing.T) {
t.Parallel()
if runtime.GOOS == "window" {
t.Skip("Sleeping for infinity doesn't work on Windows")
Expand DownExpand Up@@ -192,36 +193,45 @@ func TestAgent_Stats_Magic(t *testing.T) {
require.NoError(t, err)
})

// This test name must contain the string checked for by the agent, since it
// looks for this string in the process name.
//
// This test sets up a port forward that emulates what Jetbrains IDE's do when
// using gateway. The remote server side of the port forward is spun up using
// the gotest process, which includes the test name.
// So this unit test emulates a PID in the workspace with a similar
// name to the jetbrains IDE. That makes the agent this this SSH port
// forward is a "jetbrains" session.
t.Run("TracksIdea.vendor.name=JetBrains", func(t *testing.T) {
t.Run("TracksJetBrains", func(t *testing.T) {
t.Parallel()
if runtime.GOOS != "linux" {
t.Skip("JetBrains tracking is only supported on Linux")
}

ctx := testutil.Context(t, testutil.WaitLong)

rl, err := net.Listen("tcp", "127.0.0.1:0")
// JetBrains tracking works by looking at the process name listening on the
// forwarded port. If the process's command line includes the magic string
// we are looking for, then we assume it is a JetBrains editor. So when we
// connect to the port we must ensure the process includes that magic string
// to fool the agent into thinking this is JetBrains. To do this we need to
// spawn an external process (in this case a simple echo server) so we can
// control the process name. The -D here is just to mimic how Java options
// are set but is not necessary as the agent looks only for the magic
// string itself anywhere in the command.
_, 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)
defer rl.Close()
tcpAddr, valid := rl.Addr().(*net.TCPAddr)
require.True(t, valid)
remotePort := tcpAddr.Port
go echoOnce(t, rl)
err = echoServerCmd.Start()
require.NoError(t, err)
defer echoServerCmd.Process.Kill()

// 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)
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)

tunneledConn, err := sshClient.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", remotePort))
tunneledConn, err := sshClient.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", remotePort))
require.NoError(t, err)
t.Cleanup(func() {
// always close on failure of test
Expand All@@ -239,9 +249,10 @@ func TestAgent_Stats_Magic(t *testing.T) {
"never saw stats with conn open: %+v", s,
)

//Manually closingthe connection
//Killtheserver andconnection after checking for the echo.
requireEcho(t, tunneledConn)
_ = rl.Close()
_ = echoServerCmd.Process.Kill()
_ = tunneledConn.Close()

require.Eventuallyf(t, func() bool {
var ok bool
Expand Down
50 changes: 50 additions & 0 deletionsscripts/echoserver/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
package main

// A simple echo server. It listens on a random port, prints that port, then
// echos back anything sent to it.

import (
"errors"
"fmt"
"io"
"log"
"net"
)

func main() {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatalf("listen error: err=%s", err)
}

defer l.Close()
tcpAddr, valid := l.Addr().(*net.TCPAddr)
if !valid {
log.Fatal("address is not valid")
}

remotePort := tcpAddr.Port
_, err = fmt.Println(remotePort)
if err != nil {
log.Fatalf("print error: err=%s", err)
}

for {
conn, err := l.Accept()
if err != nil {
log.Fatalf("accept error, err=%s", err)
return
}

go func() {
defer conn.Close()
_, err := io.Copy(conn, conn)

if errors.Is(err, io.EOF) {
return
} else if err != nil {
log.Fatalf("copy error, err=%s", err)
}
}()
}
}

[8]ページ先頭

©2009-2025 Movatter.jp