Skip to content

Commit 437c1f9

Browse files
authored
Merge pull request #80 from FN-FAL113/Fix/netsh-and-block-checks
Fix: #79 and chores
2 parents 46d076f + 46e4d17 commit 437c1f9

File tree

10 files changed

+60
-36
lines changed

10 files changed

+60
-36
lines changed

CS2ServerPicker.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.33328.57
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35818.85 d17.13
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "CS2ServerPicker", "CS2ServerPicker\CS2ServerPicker.vbproj", "{E7AEBBA1-5F96-40A4-B5ED-0503A6BA4FD5}"
77
EndProject

CS2ServerPicker/App.vb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Public Class App
22

3+
#If DEBUG Then
4+
Public Debug = True
5+
#Else
6+
Public Debug = False
7+
#End If
8+
39
Private serverDictClustered As New Dictionary(Of String, String)
410

511
Private serverDictUnclustered As New Dictionary(Of String, String)
@@ -56,6 +62,11 @@
5662
End Sub
5763

5864
Private Async Sub App_Load(sender As Object, e As EventArgs) Handles MyBase.Load
65+
' Append to window title if using debug build
66+
If Debug Then
67+
Text = Text + " (DEBUG Build)"
68+
End If
69+
5970
Version_Check()
6071

6172
Set_Pending_Operation(True)
@@ -131,7 +142,7 @@
131142
Environment.NewLine +
132143
"Author: FN-FAL113 (github username)" + Environment.NewLine +
133144
"License: GNU General Public License V3" + Environment.NewLine +
134-
"App Version: 2.4.2",
145+
"App Version: " + IIf(Debug, "DEBUG", "2.5.0"),
135146
"App Info"
136147
)
137148
End Sub

CS2ServerPicker/CS2ServerPicker.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<SubType>Form</SubType>
150150
</Compile>
151151
<Compile Include="Services\DataGridViewService.vb" />
152-
<Compile Include="Services\ErrorService.vb" />
152+
<Compile Include="Services\LogService.vb" />
153153
<Compile Include="Services\PingService.vb" />
154154
<Compile Include="My Project\AssemblyInfo.vb" />
155155
<Compile Include="My Project\Application.Designer.vb">

CS2ServerPicker/My Project/AssemblyInfo.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Imports System.Runtime.InteropServices
3232
' by using the '*' as shown below:
3333
' <Assembly: AssemblyVersion("1.0.*")>
3434

35-
<Assembly: AssemblyVersion("2.4.2.0")>
36-
<Assembly: AssemblyFileVersion("2.4.2.0")>
35+
<Assembly: AssemblyVersion("2.5.0.0")>
36+
<Assembly: AssemblyFileVersion("2.5.0.0")>
3737
<Assembly: NeutralResourcesLanguage("en")>

CS2ServerPicker/My Project/Settings.Designer.vb

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CS2ServerPicker/Services/ErrorService.vb

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Imports System.IO
2+
3+
Module LogService
4+
Public Sub Log_To_File(text As String, Optional fileName As String = "")
5+
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory +
6+
IIf(Not fileName = "", fileName, "log_" + DateTimeOffset.Now.ToUnixTimeSeconds.ToString()) + ".txt",
7+
text + Environment.NewLine)
8+
End Sub
9+
10+
Public Sub Log_Exception_To_File(exception As Exception, errorFrom As String, Optional fileName As String = "")
11+
File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory +
12+
IIf(Not fileName = "", fileName, "error_" + DateTimeOffset.Now.ToUnixTimeSeconds.ToString()) + ".txt",
13+
errorFrom + Environment.NewLine + exception.Message)
14+
End Sub
15+
End Module

CS2ServerPicker/Services/ServerService.vb

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Imports System.Net
1+
Imports System.IO
2+
Imports System.Net
23
Imports Newtonsoft.Json.Linq
34
Module ServerService
45

@@ -9,6 +10,8 @@ Module ServerService
910
{"India", "Chennai,Mumbai"}
1011
}
1112

13+
Private netshPath As String = Path.Combine(Environment.SystemDirectory, "netsh.exe")
14+
1215
Public Async Function Fetch_Server_Data() As Task(Of String)
1316
Try
1417
Dim webReq As String = Await New WebClient().DownloadStringTaskAsync("https://api.steampowered.com/ISteamApps/GetSDRConfig/v1/?appid=730")
@@ -106,7 +109,7 @@ Module ServerService
106109
Await Block_Unblock_All_Servers(False, False)
107110

108111
' is passed as a reference, should be cloned in case of unwanted modification
109-
Dim serverDIctionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
112+
Dim serverDictionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
110113

111114
Dim presetServersDataGridView As DataGridView = Presets.PresetServerListDataGridView
112115

@@ -115,7 +118,7 @@ Module ServerService
115118
Cancel_Pending_Ping()
116119

117120
' offload this blocking task into a seperate thread from the thread pool to lessen load in the UI thread
118-
Await Task.Run(Sub() Handle_Block_Except_Preset_Servers(presetServersDataGridView, serverDIctionary, exceptPresetServers))
121+
Await Task.Run(Sub() Handle_Block_Except_Preset_Servers(presetServersDataGridView, serverDictionary, exceptPresetServers))
119122

120123
App.Set_Pending_Operation(False)
121124

@@ -145,7 +148,7 @@ Module ServerService
145148
End If
146149

147150
Try
148-
proc.StartInfo.Arguments = "/c netsh advfirewall firewall add rule " +
151+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall firewall add rule " +
149152
"name=CS2ServerPicker_" + serverKvp.Key.Replace(" ", "") + " dir=out action=block protocol=ANY " +
150153
"remoteip=" + serverKvp.Value
151154
proc.Start()
@@ -168,7 +171,7 @@ Module ServerService
168171

169172
Public Async Sub Block_Unblock_Selected_Servers(block As Boolean)
170173
Dim mainDataGridView As DataGridView = App.Get_DataGridView_Control()
171-
Dim serverDIctionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
174+
Dim serverDictionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
172175

173176
Dim selectedRows As DataGridViewSelectedRowCollection = mainDataGridView.SelectedRows
174177

@@ -189,14 +192,14 @@ Module ServerService
189192
Cancel_Pending_Ping()
190193

191194
' offload this blocking task into a seperate thread from the thread pool to lessen load in the UI thread
192-
Await Task.Run(Sub() Handle_Block_Unblock_Selected_Servers(mainDataGridView, serverDIctionary, block))
195+
Await Task.Run(Sub() Handle_Block_Unblock_Selected_Servers(mainDataGridView, serverDictionary, block))
193196

194197
App.Set_Pending_Operation(False)
195198

196199
Ping_Servers(selectedRows)
197200
End Sub
198201

199-
Private Sub Handle_Block_Unblock_Selected_Servers(mainDataGridView As DataGridView, serverDIctionary As Dictionary(Of String, String), block As Boolean)
202+
Private Sub Handle_Block_Unblock_Selected_Servers(mainDataGridView As DataGridView, serverDictionary As Dictionary(Of String, String), block As Boolean)
200203
Dim proc As Process = Create_Custom_CMD_Process()
201204

202205
' traverse every datagrid row and block/unblock selected servers
@@ -208,9 +211,9 @@ Module ServerService
208211
Try
209212
Dim region As String = row.Cells(1).Value
210213

211-
proc.StartInfo.Arguments = "/c netsh advfirewall firewall " + If(block, "add", "delete") + " rule " +
214+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall firewall " + If(block, "add", "delete") + " rule " +
212215
"name=CS2ServerPicker_" + region.Replace(" ", "") + If(block, " dir=out action=block protocol=ANY " +
213-
"remoteip=" + serverDIctionary.Item(region), "")
216+
"remoteip=" + serverDictionary.Item(region), "")
214217
proc.Start()
215218
proc.WaitForExit()
216219

@@ -232,7 +235,7 @@ Module ServerService
232235
Public Async Function Block_Unblock_All_Servers(block As Boolean, Optional pingServers As Boolean = True) As Task
233236
' this method was converted async since its invoked by other methods/tasks and
234237
' it does not evaluate synchronously due to its async operation that must be awaited
235-
Dim serverDIctionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
238+
Dim serverDictionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
236239
Dim mainDataGridView As DataGridView = App.Get_DataGridView_Control()
237240

238241
If App.Get_Pending_Operation() Then
@@ -246,7 +249,7 @@ Module ServerService
246249
Cancel_Pending_Ping()
247250

248251
' offload this blocking task into a seperate thread from the thread pool to lessen load in the UI thread
249-
Await Task.Run(Sub() Handle_Block_Unblock_All_Servers(mainDataGridView, serverDIctionary, block))
252+
Await Task.Run(Sub() Handle_Block_Unblock_All_Servers(mainDataGridView, serverDictionary, block))
250253

251254
App.Set_Pending_Operation(False)
252255

@@ -255,7 +258,7 @@ Module ServerService
255258
End If
256259
End Function
257260

258-
Private Sub Handle_Block_Unblock_All_Servers(mainDataGridView As DataGridView, serverDIctionary As Dictionary(Of String, String), block As Boolean)
261+
Private Sub Handle_Block_Unblock_All_Servers(mainDataGridView As DataGridView, serverDictionary As Dictionary(Of String, String), block As Boolean)
259262
Dim proc As Process = Create_Custom_CMD_Process()
260263

261264
' traverse every datagrid row and block/unblock all servers
@@ -267,9 +270,9 @@ Module ServerService
267270
End If
268271

269272
Try
270-
proc.StartInfo.Arguments = "/c netsh advfirewall firewall " + If(block, "add", "delete") + " rule " +
273+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall firewall " + If(block, "add", "delete") + " rule " +
271274
"name=CS2ServerPicker_" + region.Replace(" ", "") + If(block, " dir=out action=block protocol=ANY " +
272-
"remoteip=" + serverDIctionary.Item(region), "")
275+
"remoteip=" + serverDictionary.Item(region), "")
273276
proc.Start()
274277
proc.WaitForExit()
275278

@@ -294,8 +297,7 @@ Module ServerService
294297
Dim is_rule_exist As Boolean
295298
Dim region_trimmed As String = region.Replace(" ", "")
296299

297-
proc.StartInfo.Arguments = "/c netsh advfirewall firewall show rule name=CS2ServerPicker_" +
298-
region_trimmed + " | findstr CS2ServerPicker_" + region_trimmed
300+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall firewall show rule name=CS2ServerPicker_" + region_trimmed
299301
proc.Start()
300302
proc.WaitForExit()
301303

CS2ServerPicker/Services/VersionService.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Imports Newtonsoft.Json.Linq
44
Module VersionService
55
Public Async Sub Version_Check()
66
Try
7-
If Not My.Settings.VersionCheckOnStartup Then
7+
If Not My.Settings.VersionCheckOnStartup Or App.Debug Then
88
Return
99
End If
1010

CS2ServerPicker/Settings.vb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
Public Class Settings
1+
Imports System.IO
2+
3+
Public Class Settings
4+
5+
Private netshPath As String = Path.Combine(Environment.SystemDirectory, "netsh.exe")
26

37
Private Sub VersionCheckerCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles VersionCheckerCheckBox.CheckedChanged
48
My.Settings.VersionCheckOnStartup = VersionCheckerCheckBox.Checked
@@ -20,7 +24,7 @@
2024
Try
2125
Dim proc As Process = Create_Custom_CMD_Process()
2226

23-
proc.StartInfo.Arguments = "/c netsh advfirewall reset"
27+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall reset"
2428
proc.Start()
2529
proc.WaitForExit()
2630

@@ -48,7 +52,7 @@
4852
Try
4953
Dim proc As Process = Create_Custom_CMD_Process()
5054

51-
proc.StartInfo.Arguments = "/c netsh advfirewall show allprofiles state"
55+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall show allprofiles state"
5256
proc.Start()
5357
proc.WaitForExit()
5458

@@ -63,7 +67,7 @@
6367
Return
6468
End If
6569

66-
proc.StartInfo.Arguments = "/c netsh advfirewall set allprofiles state on"
70+
proc.StartInfo.Arguments = $"/c {netshPath} advfirewall set allprofiles state on"
6771
proc.Start()
6872
proc.WaitForExit()
6973

0 commit comments

Comments
 (0)