Skip to content

Commit

Permalink
Just some small improvement...
Browse files Browse the repository at this point in the history
Refactor peerform logic, added missing disposes and added multi-instance checking.
  • Loading branch information
eebssk1 committed Jul 10, 2024
1 parent f84cb54 commit 0710da1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 44 deletions.
6 changes: 3 additions & 3 deletions ChildKiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ internal static class ChildProcessTracker
/// that we are tracking will be automatically killed, too. If the child process terminates
/// first, that's fine, too.</summary>
/// <param name="process"></param>
public static void AddProcess(Process process)
public static bool AddProcess(Process process)
{
if (s_jobHandle != IntPtr.Zero)
{
bool success = AssignProcessToJobObject(s_jobHandle, process.Handle);
if (!success && !process.HasExited)
throw new Win32Exception();
return success || process.HasExited;
}
return false;
}

static ChildProcessTracker()
Expand Down
10 changes: 10 additions & 0 deletions InitTh.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Nucs.JsonSettings;
using PeNet;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
Expand Down Expand Up @@ -50,6 +52,12 @@ internal void Do()
MiscData.isAdmin = false;
handleError(StringRes.StringT.NoAdmin);
}
var insts = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
var instsc = insts.Count((p) => { try { return p.MainModule.FileName.StartsWith(MiscData.PWD); } catch (Win32Exception) { return true; } });
if (instsc >= 2)
{
handleError(StringRes.StringT.MultiIns);
}
Thread.Sleep(60);
NetworkInterface[] taps = NetworkInterface.GetAllNetworkInterfaces();
var TAPFound = false;
Expand Down Expand Up @@ -88,6 +96,7 @@ internal void Do()
if (f == null) goto fc;
f.Write(Properties.Resources.n2n_edge, 0, Properties.Resources.n2n_edge.Length);
f.Close();
f.Dispose();
goto fc;
}
else
Expand All @@ -106,6 +115,7 @@ internal void Do()
{
MiscData.isN2NmatchHash = false;
}
md5.Dispose();
}
Thread.Sleep(60);
PeFile PE = null;
Expand Down
44 changes: 22 additions & 22 deletions Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void BtnStart_Click(object sender, System.EventArgs e)
MessageBox.Show("No TAP Adapter !!!", "!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (TxtComm.Text == "")
if (string.IsNullOrWhiteSpace(TxtComm.Text))
{
MessageBox.Show("Community Name Error !!!", "!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
Expand Down
57 changes: 41 additions & 16 deletions PeerForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
Expand All @@ -19,7 +20,6 @@ public partial class PeerForm : Form
private bool exit = false;
internal bool connected = false;
internal static PeerForm instance;

public PeerForm()
{
InitializeComponent();
Expand All @@ -32,45 +32,40 @@ public PeerForm()

private void PeerForm_Load(object sender, EventArgs e)
{
DataColumn[] cls = new DataColumn[] {new DataColumn
table.PrimaryKey = null;
table.Columns.AddRange(
new DataColumn[] {new DataColumn
{
ColumnName = "Nick",
ReadOnly = true,
DataType = typeof(string),
},
new DataColumn
{
ColumnName = "Mode",
ReadOnly = true,
DataType = typeof(string),
},
new DataColumn
{
ColumnName = "IP",
ReadOnly = true,
DataType = typeof(string),
},
new DataColumn
{
ColumnName = "MAC",
ReadOnly = true,
DataType = typeof(string),
},
new DataColumn
{
ColumnName = "Peer",
ReadOnly = true,
DataType = typeof(string),
},
new DataColumn
{
ColumnName = "Seen",
ReadOnly = true,
DataType = typeof(string),
},
};
table.PrimaryKey = null;
table.Columns.AddRange(cls);
}
);
source.DataSource = table;
Action act = () => { dataGridView1.DataSource = source; };
dataGridView1.Invoke(act);
Expand Down Expand Up @@ -155,20 +150,50 @@ private void Do()
r["MAC"] = d.Value<string>("macaddr");
r["Peer"] = d.Value<string>("sockaddr");
r["Seen"] = DateTimeOffset.FromUnixTimeSeconds(d.Value<int>("last_seen")).ToLocalTime().ToString();
bool create = true;
if (table.Rows.Count != 0)
{
var dr2rm = new List<DataRow>();
foreach (DataRow dr in table.Rows)
{
if (((string)dr["Nick"]).Equals(d.Value<string>("desc")))
if ((((string)dr["Nick"]).Equals(d.Value<string>("desc")) || ((string)dr["MAC"]).Equals(d.Value<string>("macaddr"))))
{
Action act = () => { table.Rows.Remove(dr); };

dr2rm.Add(dr);
}
}
switch (dr2rm.Count)
{
case 0: break;
case 1: create = false; break;
default:
Action act = () => { foreach (var mr in dr2rm) { table.Rows.Remove(mr); } };
dataGridView1.Invoke(act);
break;
}
}
}
Action act1 = () => { table.Rows.Add(r); };
dataGridView1.Invoke(act1);
if (create)
{
Action act1 = () => { table.Rows.Add(r); };
dataGridView1.Invoke(act1);
}
else
{
var cr = table.Rows.Cast<DataRow>().SingleOrDefault((tr) => ((string)tr["MAC"]).Equals(r["MAC"]));
if (cr != null)
{
Action action = () =>
{
cr["IP"] = r["IP"];
cr["Peer"] = r["Peer"];
cr["Seen"] = r["Seen"];
cr["Mode"] = r["Mode"];
cr["Nick"] = r["Nick"];
};
dataGridView1.Invoke(action);
r.Delete();
}
}
}
Thread.Yield();
Thread.Sleep(2550);
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.1")]
[assembly: AssemblyFileVersion("1.0.2.1")]
[assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.3.0")]
3 changes: 3 additions & 0 deletions StringRes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal enum StringT
ITDiscla,
ITDone,
NEnotMatch,
MultiIns,
}

internal static LocaleT locale;
Expand Down Expand Up @@ -121,6 +122,8 @@ private static void InitString()
lower1[StringT.ITDone] = "Install Done. Pls restarted the program.";
lower0[StringT.NEnotMatch] = "n2n可执行文件与内嵌版本哈希不匹配!你可能正在使用旧(新?)版或自定义n2n文件!" + Environment.NewLine + "你可以打开工作文件夹删除n2n文件后重新打开程序以重新释放。";
lower1[StringT.NEnotMatch] = "N2N exe Hash does not match with the inbuilt one! You are probably using a old(new?) or custom one!" + Environment.NewLine + "You can open the working directory to delete the n2n file. Then restart the application to release file again.";
lower0[StringT.MultiIns] = "当前目录下已有实例正在运行。如果要同时运行多个实例建议放入新的文件夹!";
lower1[StringT.MultiIns] = "One or more instances are already running in same directory, If you want to run concurrently pls copy th application to a new directory!";
}
}
}

0 comments on commit 0710da1

Please sign in to comment.