-
Notifications
You must be signed in to change notification settings - Fork 2
feat: auto-detect #5
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75951b6
feat: autodetect
fmorency fc982ff
test: remove obsolete tests
fmorency 81cd6a7
chore: refactor
fmorency e70f430
chore: refactor
fmorency 95d35ec
chore: docs
fmorency 23d0d28
chore: warning msg
fmorency 9485fd8
fix: ci
fmorency File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package autodetect | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/shirou/gopsutil/v4/net" | ||
| "github.com/shirou/gopsutil/v4/process" | ||
| ) | ||
|
|
||
| type PortInfo struct { | ||
| Address string | ||
| Port uint32 | ||
| } | ||
|
|
||
| // IsProcessRunning checks if a process with the given name is running. | ||
| // It returns true and the PID if the process is found, otherwise false and 0. | ||
| // An error is returned if there's an issue listing or inspecting processes. | ||
| func IsProcessRunning(processName string) (bool, int32, error) { | ||
| processes, err := process.Processes() | ||
| if err != nil { | ||
| return false, 0, fmt.Errorf("failed to list processes: %w", err) | ||
| } | ||
|
|
||
| for _, p := range processes { | ||
| name, err := p.Name() | ||
| // Ignore errors for individual processes (e.g., permission denied) | ||
| if err != nil { | ||
| continue | ||
| } | ||
|
|
||
| if name == processName { | ||
| return true, p.Pid, nil | ||
| } | ||
| } | ||
|
|
||
| // Process not found | ||
| return false, 0, nil | ||
| } | ||
|
|
||
| // GetListeningPorts returns a list of TCP ports the process with the given PID is listening on. | ||
| func GetListeningPorts(pid int32) ([]PortInfo, error) { | ||
| // Get all network connections (TCP only for listening ports) | ||
| // Use "tcp" to get both tcp4 and tcp6 | ||
| connections, err := net.ConnectionsPid("tcp", pid) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get connections for pid %d: %w", pid, err) | ||
| } | ||
|
|
||
| var listeningPorts []PortInfo | ||
|
|
||
| for _, conn := range connections { | ||
| // Check if the connection status is LISTEN | ||
| if conn.Status == "LISTEN" { | ||
| // Ensure Laddr is not nil and has IP and Port | ||
| if conn.Laddr.IP != "" && conn.Laddr.Port != 0 { | ||
| listeningPorts = append(listeningPorts, PortInfo{ | ||
| Address: conn.Laddr.IP, | ||
| Port: conn.Laddr.Port, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(listeningPorts) == 0 { | ||
| slog.Warn("No listen ports found") | ||
fmorency marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return listeningPorts, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package pkg | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.