-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.vb
More file actions
175 lines (133 loc) · 6.72 KB
/
Main.vb
File metadata and controls
175 lines (133 loc) · 6.72 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
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
''' <summary>SMOKESIGNAL SERVER VERSION 7</summary>
Public Module Main
'CONFIGURATION HAS MOVED! See Config.VB to make any changes to the configuration of your server.
'Hopefully this will mean that if you ever need to update this server, all you have to do is update this file, and not have to worry about the configuration.
''' <summary>Main SmokeSignal Authenticator for this server</summary>
Public Authenticator As ISmokeSignalAuthenticator
''' <summary>All Registered SmokeSignal Non-Authenticated Extensions</summary>
Public Extensions As ISmokeSignalExtension()
''' <summary>All Registered SmokeSignal Authenticated Extensions</summary>
Public AuthenticatedExtensions As ISmokeSignalAuthenticatedExtension()
''' <summary>SmokeSignal Version</summary>
Public Const SMOKESIGNAL_VERSION As String = "7.0"
Public Sub Main()
Console.SetWindowSize(120, 30)
Console.SetBufferSize(120, 30)
'Server Initialization
Console.Title = SERVER_NAME & " [Version " & SERVER_VERSION & "]"
ToConsole("Starting Server...")
'Read settings
If (File.Exists("SmokeSettings.cfg")) Then
'Set Settings
Dim Settings As String() = ReadFromFile("SmokeSettings.cfg").Split(",")
IP = Settings(0)
Port = Integer.Parse(Settings(1))
FileClose(1)
Else
ToFile("SmokeSettings.cfg", IP & "," & Port)
ToConsole("Could Not Find Settings.cfg in current directory, rendered default one", ConsoleColor.Yellow)
End If
'Registering Authenticator
RegisterAuthenticator()
ToConsole("Registered Authenticator " & Authenticator.GetName & " [Version " & Authenticator.GetVersion() & "] With " & Authenticator.GetAllUsers.Count & " User(s)", ConsoleColor.Cyan)
'Extensions Registering
RegisterAllExtensions()
ToConsole("Registered " & Extensions.Count & " Extension(s): ", ConsoleColor.Blue)
For Each SmokeSignal In Extensions
ToConsole(" - " & SmokeSignal.GetName & " [Version " & SmokeSignal.GetVersion & "]", ConsoleColor.Blue)
Next
For Each SmokeSignal In AuthenticatedExtensions
ToConsole(" - " & SmokeSignal.GetName & " [Version " & SmokeSignal.GetVersion & "] (Authenticated)", ConsoleColor.DarkBlue)
Next
'Actually start the server
Dim tcpListener As TcpListener = New TcpListener(IPAddress.Parse(IP), Port)
Dim tcpClient As TcpClient = New TcpClient()
tcpListener.Start()
ToConsole("Server Started!", ConsoleColor.Green)
Dim ClientMSG As String
ToConsole("Waiting for connection...", ConsoleColor.Yellow)
DrawHeader()
'The bulk loop
While True
Dim Wait As Boolean = True
'Check if we have a pending connection
If tcpListener.Pending Then
ClearHeader()
'Accept it...
Dim theSocket As Socket = tcpListener.AcceptSocket
Dim networkStream As NetworkStream = New NetworkStream(theSocket)
Dim binaryWriter As BinaryWriter = New BinaryWriter(networkStream)
Dim binaryReader As BinaryReader = New BinaryReader(networkStream)
ToConsole("Connected from (" & (TryCast(theSocket.RemoteEndPoint, IPEndPoint).Address.ToString) & ")! Waiting for string...", ConsoleColor.Green)
'Try to take the string, and parse it
Try
ClientMSG = binaryReader.ReadString().Trim()
ToConsole("Received (" & ClientMSG & ")")
binaryWriter.Write(ParseCommand(ClientMSG))
Catch ex As Exception
ErrorToConsole("Could not read string for some reason.", ex)
End Try
'Return to the waiting state
ToConsole("Waiting for connection...", ConsoleColor.Yellow)
DrawHeader()
Wait = False
End If
'Tick each time we can.
For Each SmokeSignal In Extensions
SmokeSignal.Tick()
Next
'S P E E N
Spinner(Console.CursorLeft, Console.CursorTop)
'Wait for another go around
If Wait Then Sleep(100)
End While
End Sub
Public Sub DrawHeader()
Box(HEADER_BACK_COLOR, 120, 2, 0, 0)
SetPos(0, 0)
Color(HEADER_BACK_COLOR, HEADER_FONT_COLOR)
CenterText(SERVER_NAME + " [Version " & SERVER_VERSION & "] | Running on SmokeSignal V" & SMOKESIGNAL_VERSION)
SetPos(0, 1)
CenterText(Extensions.Count & " Extension(s) loaded | Listening on " & IP & ":" & Port & " ")
End Sub
Public Sub ClearHeader()
Box(ConsoleColor.Black, 120, 2, 0, 0)
End Sub
Private Function ParseCommand(ClientMSG As String) As String
'Just in case
If String.IsNullOrWhiteSpace(ClientMSG) Then Return InvalidPacketSent()
Dim Result As String
'Attempt parsing with Non-Authenticated extensions
For Each SmokeSignal As ISmokeSignalExtension In Extensions
Result = SmokeSignal.Parse(ClientMSG)
If Not String.IsNullOrEmpty(Result) Then Return Result
Next
'Ask the authenticator to parse this
Result = Authenticator.Parse(ClientMSG)
If Not String.IsNullOrEmpty(Result) Then Return Result
'Split the client message, Should be USER|PASS|COMMAND. If there's less than 3 parts, it must be an invalid packet
Dim ClientSplit As String() = ClientMSG.Split("|")
If ClientSplit.Length < 3 Then Return InvalidPacketSent()
'Attempt to authenticate the executing user. If we cannot, then there was an authentication error
Dim ExecutingUser As ISmokeSignalUser = Authenticator.Authenticate(ClientSplit(0), ClientSplit(1))
If IsNothing(ExecutingUser) Then Return AuthenticationError(ClientSplit(0))
'Attempt to parse with authenticated extensions
For Each AuthSmokeSignal As ISmokeSignalAuthenticatedExtension In AuthenticatedExtensions
Result = AuthSmokeSignal.Parse(ExecutingUser, ClientSplit(2))
If Not String.IsNullOrEmpty(Result) Then Return Result
Next
'Invalid Packet
Return InvalidPacketSent()
End Function
Private Function AuthenticationError(Username As String) As String
ToConsole("Authentication Failed for user " & Username, ConsoleColor.DarkRed)
Return "AUTHERROR"
End Function
Private Function InvalidPacketSent() As String
ToConsole("Invalid Packet Sent")
Return "invalid Packet Sent"
End Function
End Module