-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cs
More file actions
281 lines (253 loc) · 10.8 KB
/
main.cs
File metadata and controls
281 lines (253 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace TAMUVPNApplication {
/* Constants used by this application. Please make edits here */
static class Constants {
public const int MAX_ATTEMPTS = 5;
public const string RASDIAL = "rasdial.exe";
public const string VPN_NAME = "TAMU VPN";
public const string LOG_DIR = @".\Logs\";
}
/* Log is used for logging the program */
class Log {
private StreamWriter log;
private int status;
/* Default constructor. Will create a file with date format if filename is null. */
public Log(string filename = null) {
EnsureDirectoryExists(Constants.LOG_DIR);
if (filename == null) {
filename = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_log.txt";
}
log = new StreamWriter(Constants.LOG_DIR + filename);
log.WriteLine(">>> [TAMU VPN - https://github.com/jacobdonais/DoIT.VPNHelper]");
log.WriteLine(">>> " +
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") +
": Section start");
status = 0;
log.AutoFlush = true;
}
/* Finalizer for log. Will close the log file. */
~Log() {
log.WriteLine("<<< [" +
DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") +
": Section end]");
log.WriteLine("<<< [Exit Status(" + status + ")]");
log.Close();
}
/* Name: SetStatus
/* Description: This function is used to set the status of the log. */
public void SetStatus(int _status) {
status = _status;
}
/* Name: WriteLine
/* Description: This function is used to write a line with a newline in the log. */
public void WriteLine(string msg) {
log.WriteLine(msg);
}
/* Name: Write
/* Description: This function is used to write a line with no newline in the log. */
public void Write(string msg) {
log.Write(msg);
}
/* Name: EnsureDirectoryExists
/* Description: This function is used to ensure a directory exists. */
private void EnsureDirectoryExists(string filePath) {
FileInfo fi = new FileInfo(filePath);
if (!fi.Directory.Exists) {
System.IO.Directory.CreateDirectory(fi.DirectoryName);
}
}
}
class TAMUVPN {
/* Name: GetPassword()
/* Description: Function will read the keys pressed to create the
password string. Will accept the password once the enter key is pressed and
will accept backspace to delete a character. */
static string GetPassword() {
string password = "";
System.ConsoleKeyInfo info = Console.ReadKey(true);
while (info.Key != ConsoleKey.Enter) {
if (info.Key != ConsoleKey.Backspace) {
password += info.KeyChar;
}
else {
if (!string.IsNullOrEmpty(password)) {
password = password.Substring(0, password.Length - 1);
}
}
info = Console.ReadKey(true);
}
return password;
}
/* Name: CreateVPN()
/* Description: This command will use PowerShell to create the VPN.
Configuration for the VPN comes from the L2TP guide from TAMU. */
static void CreateVPN() {
try {
PowerShell ps = PowerShell.Create();
ps.AddCommand("Add-VpnConnection");
ps.AddParameter("Name", Constants.VPN_NAME);
ps.AddParameter("ServerAddress", "connect.tamu.edu");
ps.AddParameter("TunnelType", "L2tp");
ps.AddParameter("L2tpPsk", "tamuvpn");
ps.AddParameter("EncryptionLevel", "Maximum");
ps.AddParameter("Force");
ps.Invoke();
}
catch {}
}
/* Name: DeleteVPN()
/* Description: This will delete the VPN off the computer. */
static void DeleteVPN() {
try {
PowerShell ps = PowerShell.Create();
ps.AddCommand("Remove-VpnConnection");
ps.AddParameter("Name", Constants.VPN_NAME);
ps.AddParameter("Force");
ps.Invoke();
}
catch {}
}
/* Name: ConnectVPN()
/* Description: This function will connect to the VPN.*/
static void ConnectVPN(string username, string password) {
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = Constants.RASDIAL,
Arguments = "\"" + Constants.VPN_NAME + "\" " + username + " " + password,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
}
};
process.Start();
process.WaitForExit();
}
/* Name: TerminateVPN()
/* Description: This function will end the VPN session */
static void TerminateVPN() {
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = Constants.RASDIAL,
Arguments = "\"" + Constants.VPN_NAME + "\" /d",
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
}
};
process.Start();
process.WaitForExit();
}
/* Name: GetIPAddress()
/* Description: This function will search for the IP address for the TAMU
VPN and will return it. If it isn't found, then it will return null. */
static string GetIPAddress() {
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface TAMU_VPN = Array.Find(networkInterfaces, element => element.Name == Constants.VPN_NAME);
try {
foreach (UnicastIPAddressInformation ip in TAMU_VPN.GetIPProperties().UnicastAddresses) {
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
return ip.Address.ToString();
}
}
}
catch {
return null;
}
return null;
}
static void Main(string[] args) {
string username = null;
string password = null;
string ip = null;
int count = Constants.MAX_ATTEMPTS;
string hostName = Dns.GetHostName();
string logName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_log.txt";
// 0. Create the Log file
Log log = new Log(logName);
// 1. Create the VPN
log.WriteLine(" Creating the VPN...");
try {
CreateVPN();
log.WriteLine(" Successfully created the VPN.");
}
catch {
log.WriteLine(" Failed to create the VPN.");
log.SetStatus(-1);
throw new System.ArgumentException("Failed to create the VPN");
}
// 2. Get the user's username and password
Console.WriteLine("Please enter your NetID credentials.");
Console.Write("Username: ");
username = Console.ReadLine();
Console.Write("Passowrd: ");
password = GetPassword();
log.WriteLine(" Using username = " + username);
// 3/4. Connect to the VPN and get the IP address
log.WriteLine(" Attempting to connect to TAMU VPN...");
while(ip == null && count > 0) {
log.WriteLine(" Attempt = " + (Constants.MAX_ATTEMPTS - count + 1) + " out of " + Constants.MAX_ATTEMPTS);
try {
ConnectVPN(username, password);
}
catch {
log.SetStatus(-2);
throw new System.ArgumentException("Failed to execute ConnectVPN()");
}
try {
ip = GetIPAddress();
}
catch {
log.SetStatus(-3);
throw new System.ArgumentException("Failed to get IP address");
}
count--;
}
if (count <= 0) {
Console.WriteLine("Failed to connect to TAMU VPN; please try again later.");
log.WriteLine(" Failed to conect to TAMU VPN.");
log.SetStatus(-4);
return;
}
else {
// 5. Output the info to the user.
log.WriteLine(" Attempts needed = " + (Constants.MAX_ATTEMPTS - count));
log.WriteLine(" Successfully connected at " + DateTime.Now.ToString("HH:mm:ss yyyy/MM/dd"));
Console.WriteLine();
Console.WriteLine("Hostname: " + hostName);
log.WriteLine(" Hostname: " + hostName);
Console.WriteLine("IP Address: " + ip);
log.WriteLine(" IP Address: " + ip);
Console.WriteLine();
Console.Write("Press f to disconnect");
while (Console.ReadKey(true).Key != ConsoleKey.F) {
}
// 6. Disconnect from the VPN
log.WriteLine(" Attempting to disconnect from VPN...");
try {
TerminateVPN();
log.WriteLine(" Successfully disconnected at " + DateTime.Now.ToString("HH:mm:ss yyyy/MM/dd"));
}
catch {
log.WriteLine(" Failed to disconnect from the VPN.");
log.SetStatus(-5);
throw new System.ArgumentException("Failed to execute TerminateVPN()");
}
}
// 7. Delete the VPN
log.WriteLine(" Attempting to delete the VPN...");
try {
DeleteVPN();
log.WriteLine(" Successfully deleted the VPN.");
}
catch {
log.WriteLine(" Failed to delete the VPN.");
log.SetStatus(-6);
throw new System.ArgumentException("Failed to execute TerminateVPN()");
}
log.SetStatus(1);
}
}
}