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

Enhance: Flow.Launcher.Plugin.ProcessKiller #1320

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

watchingfun
Copy link

Plugin allows searching by listening tcp port
After the kill fails, try to execute it again with administrator privileges
#1317

Copy link
Member

@taooceros taooceros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this feature. There're some style issues that worth taking a look.

I am not pretty familiar with WMI so I may take a look on that later.

@@ -83,14 +83,14 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
results.Add(new Result()
{
IcoPath = path,
Title = p.ProcessName + " - " + p.Id,
Title = p.ProcessName + " - " + p.Id + (pr.Port!=0? $" - [{pr.Port}]":""),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use string interpolation.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

By the way, please add space around != and :.

SubTitle = path,
TitleHighlightData = StringMatcher.FuzzySearch(termToSearch, p.ProcessName).MatchData,
Score = pr.Score,
ContextData = p.ProcessName,
AutoCompleteText = $"{_context.CurrentPluginMetadata.ActionKeyword}{Plugin.Query.TermSeparator}{p.ProcessName}",
Action = (c) =>
{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the white space.

public string Path { get; set; }
public override string ToString()
{
return string.Format(@" Process Name: {0} ,Process ID: {1} ,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string interpolation as well

Comment on lines +58 to +60
string commandArgument = string.Format("/c netstat -an -o -p tcp|findstr \":{0}.*LISTENING\"", port);

string commandOut = ExecuteCommandAndCaptureOutput(COMMAND_EXE, commandArgument);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that netstat is a exe that can be called directly without cmd. Could you take a try?
Sorry I didn't see the findstr before. Though, I still think we shall be able to process the string directly with either regex or a manually written parser.

}

// split host:port
var hostPortTokens = stringTokens[1].Split(new char[] { ':' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to have multiple port in one line? Or that won't happen for Listening port?

}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use Console.WriteLine. Try to use Log in the IPublicAPI inside PluginInitContext.

// interested in first result.
foreach (ManagementObject item in results)
{
result = Tuple.Create<string, string>(Convert.ToString(item["Name"]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for above.

process.StartInfo.RedirectStandardError = true;
process.Start();

commandOut = process.StandardOutput.ReadToEnd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be stuck if the process never ends. I think we should put it lower after waiting process ends. Also, probably make this part async.

Copy link
Author

@watchingfun watchingfun Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thank you very much for reviewing and pointing out the problems 😊, but in addition to some simple style problems, there are many problems that I can't fix due to my limited ability. You should close this pr, if you have time, can you add these two features🥺?sorry for wasting your precious time. (Actually, I know this code is shit, but I don't have the energy to learn how to write better implementations recently. 😢

Copy link
Member

@taooceros taooceros Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the code is that bad. Those things I mention can be adjusted fairly easily.
Though, if you don't have time, I can work on this based on your work for sure (but I probably will not be free to do that for a while).
Thanks for the enhancement idea and an implementation draft!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, then I close this pr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leave it here. I will finish it once I got time.

@@ -38,10 +38,19 @@ public List<ProcessResult> GetMatchingProcesses(string searchTerm)
{
var processlist = new List<ProcessResult>();

int portNum;
bool canConvert = int.TryParse(searchTerm, out portNum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put int.TryParse inside the if, and initialize portNum with the out..

foreach (var p in Process.GetProcesses())
{
if (IsSystemProcess(p)) continue;

if (tcpPortListeningProcess != null && tcpPortListeningProcess.Item1 && tcpPortListeningProcess.Item2.Process.Id == p.Id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tuple is not supposed to be null (ValueTuple is not able to be null without explicit written). So maybe you may need to adjust part of this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +107 to +108
startInfo.FileName = "powershell.exe";
startInfo.Arguments = $"Start cmd.exe -ArgumentList \"/k\",\"taskkill\",\"/f\",\"/pid\", \"{p.Id}\" -Verb Runas";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget this one. It's pretty weird that you start taskkill inside a cmd inside the powershell.

Copy link
Member

@taooceros taooceros Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The startInfo do provide an ArgumentList and Verb API that worth taking a look.

@taooceros taooceros marked this pull request as draft August 10, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants