Skip to content

Commit fd46042

Browse files
committed
Bugfixes; Added Exitcodes
1 parent 676bdb7 commit fd46042

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

LinuxProxyChanger.csproj

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
<Company>First-Coder</Company>
88
<Copyright>L. Gmann</Copyright>
99
<PackageProjectUrl>https://first-coder.de/</PackageProjectUrl>
10-
<AssemblyVersion>1.0.0.2</AssemblyVersion>
11-
<FileVersion>1.0.0.2</FileVersion>
10+
<AssemblyVersion>1.0.1.0</AssemblyVersion>
11+
<FileVersion>1.0.1.0</FileVersion>
12+
<Version>1.0.1</Version>
1213
</PropertyGroup>
1314

1415
<ItemGroup>
@@ -22,6 +23,7 @@
2223
</ItemGroup>
2324

2425
<ItemGroup>
26+
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
2527
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2628
</ItemGroup>
2729

Program.cs

+50-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using LinuxProxyChanger.Models;
2+
using Mono.Unix.Native;
23
using Newtonsoft.Json;
34
using System;
45
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78
using System.Net.NetworkInformation;
89
using System.Reflection;
10+
using System.Runtime.InteropServices;
911
using System.Text;
1012
using System.Text.RegularExpressions;
1113

1214
namespace LinuxProxyChanger
1315
{
1416
class Program
1517
{
18+
private static bool IsRoot => RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? Syscall.getuid().Equals(0) : false;
19+
1620
/// <summary>
1721
/// Settings json file
1822
/// </summary>
@@ -58,6 +62,11 @@ static void Main(string[] args)
5862
}
5963
}
6064

65+
if (settings.CallOnNetworkchange)
66+
{
67+
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
68+
}
69+
6170
ConsoleKeyInfo cki;
6271
do
6372
{
@@ -79,6 +88,8 @@ static void Main(string[] args)
7988
break;
8089
}
8190
} while (cki.Key != ConsoleKey.Escape);
91+
92+
Environment.Exit(0);
8293
}
8394

8495
/// <summary>
@@ -110,6 +121,11 @@ static void Clear()
110121
WriteColor($"[// Title:] {Assembly.GetEntryAssembly().GetName().Name}", ConsoleColor.DarkGreen);
111122
WriteColor($"[// Version:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}", ConsoleColor.DarkGreen);
112123
WriteColor($"[// Autor:] {Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright}", ConsoleColor.DarkGreen);
124+
WriteColor(@"[//--Exit Codes---------------------------------------------------]", ConsoleColor.DarkGreen);
125+
WriteColor($"[// 0:] Application successful exited", ConsoleColor.DarkGreen);
126+
WriteColor($"[// 1:] Supported OS is not given", ConsoleColor.DarkGreen);
127+
WriteColor($"[// 2:] User has no root permissions", ConsoleColor.DarkGreen);
128+
WriteColor($"[// 3:] Networksadapters are not set", ConsoleColor.DarkGreen);
113129
WriteColor(@"[//--Settings-----------------------------------------------------]", ConsoleColor.DarkGreen);
114130
WriteColor($"[// Call on Networkchange:] {settings.CallOnNetworkchange}", ConsoleColor.DarkGreen);
115131
WriteColor($"[// Set proxy on Autostart:] {settings.SetProxyOnStartUp}", ConsoleColor.DarkGreen);
@@ -129,7 +145,22 @@ static void Clear()
129145
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
130146
if (!Debugger.IsAttached)
131147
{
132-
return;
148+
Environment.Exit(1);
149+
}
150+
else
151+
{
152+
Console.WriteLine(Environment.NewLine);
153+
}
154+
}
155+
156+
if (!IsRoot)
157+
{
158+
WriteColor(@"[//--No root permissions------------------------------------------]", ConsoleColor.DarkRed);
159+
WriteColor($"[//:] Please start this tool as root", ConsoleColor.DarkRed);
160+
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
161+
if (!Debugger.IsAttached)
162+
{
163+
Environment.Exit(2);
133164
}
134165
else
135166
{
@@ -144,7 +175,7 @@ static void Clear()
144175
WriteColor(@"[//---------------------------------------------------------------]", ConsoleColor.DarkRed);
145176
if (!Debugger.IsAttached)
146177
{
147-
return;
178+
Environment.Exit(3);
148179
}
149180
else
150181
{
@@ -160,15 +191,16 @@ static void Clear()
160191
/// <param name="e"></param>
161192
static void AddressChangedCallback(object sender, EventArgs e)
162193
{
194+
status = IPStatus.Unknown;
163195
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
164196

165-
if(adapters == null) // No networkadapters found
197+
if (adapters != null) // No networkadapters found
166198
{
167199
var networkChangeAdapterList = settings.NetworkChangeAdapters.Split(",");
168200

169201
foreach (NetworkInterface n in adapters)
170202
{
171-
if (n.OperationalStatus == OperationalStatus.Up && networkChangeAdapterList.Contains(n.Id))
203+
if (n.OperationalStatus == OperationalStatus.Up && networkChangeAdapterList.Contains(n.Id) && status != IPStatus.Success)
172204
{
173205
status = PingTest();
174206

@@ -196,15 +228,22 @@ static void AddressChangedCallback(object sender, EventArgs e)
196228
/// <returns>Return status of the request</returns>
197229
static IPStatus PingTest()
198230
{
199-
Ping sender = new Ping();
200-
PingOptions options = new PingOptions();
231+
try
232+
{
233+
Ping sender = new Ping();
234+
PingOptions options = new PingOptions();
201235

202-
options.DontFragment = true;
203-
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
204-
byte[] buffer = Encoding.ASCII.GetBytes(data);
236+
options.DontFragment = true;
237+
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
238+
byte[] buffer = Encoding.ASCII.GetBytes(data);
205239

206-
PingReply reply = sender.Send(settings.ProxyIp, settings.Timeout, buffer, options);
207-
return reply.Status;
240+
PingReply reply = sender.Send(settings.ProxyIp, settings.Timeout, buffer, options);
241+
return reply.Status;
242+
}
243+
catch
244+
{
245+
return IPStatus.DestinationHostUnreachable;
246+
}
208247
}
209248

210249
/// <summary>

0 commit comments

Comments
 (0)