Skip to content
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

Windows support #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
67 changes: 65 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -61,6 +63,7 @@ var (
Underline(true)

docStyle = lipgloss.NewStyle().Padding(1, 2, 1, 2)
asAdmin = true
)

type item struct {
Expand All @@ -78,6 +81,13 @@ type model struct {
activeButton string
}

type WindowsProcesses []struct {
OwningProcess int `json:"OwningProcess"`
LocalPort int `json:"LocalPort"`
Username string `json:"Username"`
Command string `json:"Command"`
}

var doc = strings.Builder{}

type tickMsg time.Time
Expand Down Expand Up @@ -178,7 +188,7 @@ func tickCmd() tea.Cmd {
})
}

func getProcesses() []list.Item {
func getUnixProcesses() []list.Item {
out, _ := exec.Command("lsof", "-i", "-P", "-n", "-sTCP:LISTEN").Output()
strStdout := string(out)

Expand All @@ -203,12 +213,65 @@ func getProcesses() []list.Item {
return processes
}

func executePowershellCmd() []byte {
var pwshCommand string
// Powershell needs elevated privilege to run "Get-Process" with "-IncludeUserName" parameter
if asAdmin {
pwshCommand = "Get-NetTCPConnection -State Listen | ForEach-Object {$proc=Get-Process -Id $_.OwningProcess -IncludeUserName;[PSCustomObject]@{OwningProcess=$_.OwningProcess;LocalPort=$_.LocalPort;Command=if($proc){$proc.Path};Username=if($proc){$proc.UserName}}} | ConvertTo-Json"
} else {
pwshCommand = "Get-NetTCPConnection -State Listen | ForEach-Object {$proc=Get-Process -Id $_.OwningProcess;[PSCustomObject]@{OwningProcess=$_.OwningProcess;LocalPort=$_.LocalPort;Command=if($proc){$proc.Path};Username=if($proc){$proc.UserName}}} | ConvertTo-Json"
}
out, err := exec.Command("powershell.exe", "-NoLogo", "-Command", pwshCommand).CombinedOutput()
if err != nil {
if strings.Contains(string(out), "requires elevated user rights") {
asAdmin = false
return executePowershellCmd()
} else {
log.Error(err)
}
}
return out
}

func getWindowsProcesses() []list.Item {
out := executePowershellCmd()
var winProcesses WindowsProcesses
if err := json.Unmarshal(out, &winProcesses); err != nil {
log.Error(err)
}
var processes []list.Item
for _, proc := range winProcesses {
pid := proc.OwningProcess
port := proc.LocalPort
user := proc.Username
command := proc.Command

titleStr := fmt.Sprintf("Port :%d (%d)", port, pid)
descStr := fmt.Sprintf("User: %s, Command: %s", user, command)

processes = append(processes, item{title: titleStr, desc: descStr})
}
return processes
}

func getProcesses() []list.Item {
if runtime.GOOS == "windows" {
return getWindowsProcesses()
} else {
return getUnixProcesses()
}
}

func killPort(pid string) {
pidInt, err := strconv.Atoi(pid)
if err != nil {
log.Error("Could not convert to process pid to int")
}
syscall.Kill(pidInt, syscall.SIGKILL)
proc, err := os.FindProcess(pidInt)
if err != nil {
log.Error(err)
}
err = proc.Signal(syscall.SIGKILL)
if err != nil {
log.Error("Could not kill process")
}
Expand Down