Skip to content

Commit 455dbb9

Browse files
committed
2 parents 9ccf2eb + a5878ca commit 455dbb9

File tree

20 files changed

+248
-65
lines changed

20 files changed

+248
-65
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,4 @@ $RECYCLE.BIN/
157157

158158
#Roslyn files
159159
*.sln.ide
160+
/.vs/

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
11
# Network AutoSwitch [![Build Status](https://ci.appveyor.com/api/projects/status/github/Tulpep/network-autoswitch)](https://ci.appveyor.com/project/tulpep/network-autoswitch)
22

3-
Network AutoSwitch will automatically disable wired network adapters when a wireless connection is detected. When you disconnect your computer from the wireless network it will enable wired adapters automatically.
3+
Network AutoSwitch will automatically disable or enable wired and wireless network adapters according to network connection. Depending on the priority selected (Wired or Wireless), when you disconnect your computer from the wireless network it will enable wired adapters automatically or it will enable wireless network adapters when wired network is disabled.
4+
5+
Proxy AutoSwitch will automatically disable or enable IE proxy server according to network connection. When you disconnect your computer from the wireless network or the wired network adapters are disabled, it will enable or disable IE proxy server depending on the chosen configuration.
46

57
## [Download now](https://github.com/Tulpep/Network-AutoSwitch/releases/latest)
68

7-
Network AutoSwitch can run in interactive mode for your testing purposes and also can run as Windows Service. In Windows Service mode it will work in background without user interaction. You can distribute it using Active Directory or your current deployment software solution to use it as part of your networking hardening strategy.
9+
Network AutoSwitch and Proxy AutoSwitch can run in interactive mode for your testing purposes and also can run as Windows Service. In Windows Service mode it will work in background without user interaction. You can distribute it using Active Directory or your current deployment software solution to use it as part of your networking hardening strategy.
810

911
# How to use it
10-
Run using a command prompt with administative rights as:
12+
-p or --priority flag are mandatory to choose the right configuration, passing "Wired" or "Wireless" parameters. Administrative rights are neccesary to enable or disable network interfaces. Run using a command prompt with administative rights as:
1113
```bash
12-
NetworkAutoSwitch.exe
14+
NetworkAutoSwitch.exe -p Wired
1315
```
14-
Administrative rights are neccesary to enable or disable network interfaces.
16+
or
1517

16-
When using as a Windows Service it will work for all users in the machine, even it they dont have Windows administrative rights. It will start automatically with your System.
18+
```bash
19+
NetworkAutoSwitch.exe -p Wireless
20+
```
21+
When using as a Windows Service it will work for all users in the machine, even it they dont have Windows administrative rights. It will start automatically with your System. You must choose also the priority when
1722
To install it run it as:
1823
```bash
19-
NetworkAutoSwitch.exe --install
24+
NetworkAutoSwitch.exe --install -p Wired
2025
```
2126

2227
To uninstall it run as:
2328
```bash
2429
NetworkAutoSwitch.exe --uninstall
2530
```
2631

32+
Proxy AutoSwitch can be used in the same way as NetworkAutoSwitch. The priority flag is also mandatory.
33+
34+
```bash
35+
ProxyAutoSwitch.exe -p Wired
36+
```
37+
or
38+
39+
```bash
40+
ProxyAutoSwitch.exe -p Wireless
41+
```
42+
43+
and
44+
45+
To install it run it as:
46+
```bash
47+
ProxyAutoSwitch.exe --install -p Wireless
48+
```
49+
50+
To uninstall it run as:
51+
```bash
52+
ProxyAutoSwitch.exe --uninstall
53+
```
2754

2855
**This project is supported. Your issues will be handle by our team and we are open to your PRs!**
2956

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tulpep.NetworkAutoSwitch.NetworkService
7+
{
8+
public static class Constants
9+
{
10+
public const string SERVICE_NAME = "NetworkAutoSwitch";
11+
public const string SERVICE_DISPLAY_NAME = "Tulpep Network AutoSwitch Service";
12+
public const string SERVICE_DESCRIPTION = "For detailed information visit https://github.com/Tulpep/Network-AutoSwitch";
13+
}
14+
}

Tulpep.NetworkAutoSwitch.NetworkService/ManageNetworkState.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Net.NetworkInformation;
1+
using System;
2+
using System.IO;
3+
using System.Net.NetworkInformation;
4+
using System.Reflection;
25
using Tulpep.Network.NetworkStateService;
36
using Tulpep.NetworkAutoSwitch.NetworkStateLibrary;
47

@@ -25,6 +28,33 @@ public static void AnalyzeNow(Priority priority)
2528
NetworkStateService.LogChangeStateAdapters(networkState.WirelessAdapters, true);
2629
}
2730
}
31+
32+
public static Priority GetPriorityConfig()
33+
{
34+
string currentPath = Assembly.GetExecutingAssembly().Location;
35+
string system32Path = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
36+
string configInSystem32Path = Path.Combine(system32Path, Constants.SERVICE_NAME + "Config.txt");
37+
38+
string firstLine;
39+
40+
using (StreamReader reader = new StreamReader(configInSystem32Path))
41+
{
42+
firstLine = reader.ReadLine() ?? "";
43+
}
44+
45+
Priority priority = Priority.None;
46+
47+
if (firstLine == "1")
48+
{
49+
priority = Priority.Wired;
50+
}
51+
else if (firstLine == "0")
52+
{
53+
priority = Priority.Wireless;
54+
}
55+
56+
return priority;
57+
}
2858
}
2959

3060
}

Tulpep.NetworkAutoSwitch.NetworkService/NetworkAutoSwitch.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CommandLine;
22
using System.ServiceProcess;
3+
using Tulpep.NetworkAutoSwitch.NetworkStateLibrary;
34

45
namespace Tulpep.NetworkAutoSwitch.NetworkService
56
{
@@ -17,7 +18,10 @@ protected override void OnStart(string[] args)
1718
{
1819
Options = new Options();
1920
if (Parser.Default.ParseArguments(args, Options))
20-
_detectNetworkChanges = new DetectNetworkChanges(Options.Priority);
21+
{
22+
Priority priority = Options.Priority == Priority.None ? ManageNetworkState.GetPriorityConfig() : Options.Priority;
23+
_detectNetworkChanges = new DetectNetworkChanges(priority);
24+
}
2125
}
2226

2327
protected override void OnStop()

Tulpep.NetworkAutoSwitch.NetworkService/Program.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.ServiceProcess;
7+
using System.Text;
78
using System.Threading;
89
using Tulpep.Network.NetworkStateService;
910
using Tulpep.NetworkAutoSwitch.NetworkStateLibrary;
@@ -20,19 +21,16 @@ static class Program
2021

2122
static int Main(string[] args)
2223
{
23-
2424
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
2525

2626
Options = new Options();
2727
if (!Parser.Default.ParseArguments(args, Options))
2828
return 1;
29-
30-
29+
3130
if (Environment.UserInteractive)
3231
{
33-
const string serviceName = "NetworkAutoSwitch";
34-
const string exeFileName = "NetworkAutoSwitch.exe";
35-
const string installStateFileName = "NetworkAutoSwitch.InstallState";
32+
const string exeFileName = Constants.SERVICE_NAME + ".exe";
33+
const string installStateFileName = Constants.SERVICE_NAME + ".InstallState";
3634

3735
string currentPath = Assembly.GetExecutingAssembly().Location;
3836
string system32Path = Environment.GetFolderPath(Environment.SpecialFolder.System);
@@ -55,7 +53,7 @@ static int Main(string[] args)
5553
}
5654
ManagedInstallerClass.InstallHelper(new string[] { "/LogFile=", "/LogToConsole=true", serviceInSystem32Path });
5755
Logging.WriteMessage("Service Installed");
58-
StartService(serviceName, 500);
56+
StartService(Constants.SERVICE_NAME, 500, Options.Priority);
5957
return 0;
6058
}
6159
if (Options.Uninstall)
@@ -112,19 +110,31 @@ static int Main(string[] args)
112110

113111
private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
114112
{
115-
Logging.WriteMessage(e.ExceptionObject.ToString());
113+
Logging.WriteMessageEventViewerError(Constants.SERVICE_NAME, e.ExceptionObject.ToString());
116114
Environment.Exit(1);
117115
}
118116

119117

120-
private static void StartService(string serviceName, int timeoutMilliseconds)
118+
private static void StartService(string serviceName, int timeoutMilliseconds, Priority priority)
121119
{
122120
Logging.WriteMessage("Starting Windows Service {0} with timeout of {1} ms", serviceName, timeoutMilliseconds);
123121
ServiceController service = new ServiceController(serviceName);
124122
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
125-
service.Start();
123+
service.Start(new string[] { "-p", priority.ToString() });
126124
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
127125
Logging.WriteMessage("Service running");
126+
127+
string currentPath = Assembly.GetExecutingAssembly().Location;
128+
string system32Path = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
129+
string configInSystem32Path = Path.Combine(system32Path, Constants.SERVICE_NAME + "Config.txt");
130+
Byte[] info = null;
131+
132+
using (FileStream fs = File.Create(configInSystem32Path))
133+
{
134+
info = new UTF8Encoding(true).GetBytes(priority == Priority.Wired ? "1" : "0");
135+
fs.Write(info, 0, info.Length);
136+
137+
}
128138
}
129139

130140
}

Tulpep.NetworkAutoSwitch.NetworkService/ProjectInstaller.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tulpep.NetworkAutoSwitch.NetworkService/Tulpep.NetworkAutoSwitch.NetworkService.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Reference Include="System.Xml" />
7979
</ItemGroup>
8080
<ItemGroup>
81+
<Compile Include="Constants.cs" />
8182
<Compile Include="ManageNetworkState.cs" />
8283
<Compile Include="DetectNetworkChanges.cs" />
8384
<Compile Include="NetworkAutoSwitch.cs">
@@ -125,9 +126,6 @@
125126
<Install>false</Install>
126127
</BootstrapperPackage>
127128
</ItemGroup>
128-
<ItemGroup>
129-
<Content Include="Tulpep.NetworkAutoSwitch.UtilityLibrary.csproj" />
130-
</ItemGroup>
131129
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
132130
<Import Project="..\packages\Fody.2.0.0\build\portable-net+sl+win+wpa+wp\Fody.targets" Condition="Exists('..\packages\Fody.2.0.0\build\portable-net+sl+win+wpa+wp\Fody.targets')" />
133131
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

Tulpep.NetworkAutoSwitch.NetworkStateLibrary/Logging.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23

34
namespace Tulpep.Network.NetworkStateService
45
{
@@ -8,5 +9,27 @@ public static void WriteMessage(string text, params object[] args)
89
{
910
Console.WriteLine(DateTime.Now + "\t" + string.Format(text, args));
1011
}
12+
13+
public static void WriteMessageEventViewerInfo(string source, string text, params object[] args)
14+
{
15+
WriteMessageEventViewer(source, text, args);
16+
EventLog.WriteEntry(source, string.Format(text, args), EventLogEntryType.Information);
17+
}
18+
19+
public static void WriteMessageEventViewerError(string source, string text, params object[] args)
20+
{
21+
WriteMessageEventViewer(source, text, args);
22+
EventLog.WriteEntry(source, string.Format(text, args), EventLogEntryType.Error);
23+
}
24+
25+
private static void WriteMessageEventViewer(string source, string text, params object[] args)
26+
{
27+
WriteMessage(string.Format(text, args));
28+
29+
string sEvent = source + " Log";
30+
31+
if (!EventLog.SourceExists(source))
32+
EventLog.CreateEventSource(source, sEvent);
33+
}
1134
}
1235
}

Tulpep.NetworkAutoSwitch.NetworkStateLibrary/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
88
[assembly: AssemblyTitle("Tulpep.Network.NetworkStateService")]
9-
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyDescription("Detailed information at https://github.com/Tulpep/Network-AutoSwitch")]
1010
[assembly: AssemblyConfiguration("")]
11-
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyCompany("Tulpep")]
1212
[assembly: AssemblyProduct("Tulpep.Network.NetworkStateService")]
1313
[assembly: AssemblyCopyright("Copyright © 2017")]
1414
[assembly: AssemblyTrademark("")]

0 commit comments

Comments
 (0)