-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTServerHelper.cs
273 lines (223 loc) · 8.15 KB
/
STServerHelper.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using Mono.Nat;
using System.Configuration;
using Telerik.WinControls.UI;
using System.Threading;
namespace ST
{
public partial class STServerHelper : Telerik.WinControls.UI.RadForm
{
//TODO
//Implement UPNP and assign it to the game server executable so then you can port forward temporarily for the user if their router supports it.
//update thing - https://updates.skyrim-together.com/hashes_489830.txt line.split(' ')[0] if(line.contains("server.exe")).
private bool isStarted = false;
string ServerDirectory = ConfigurationManager.AppSettings["Server Directory"];
BinaryHelper.BinaryStorageHelper Storage = new BinaryHelper.BinaryStorageHelper();
public STServerHelper()
{
InitializeComponent();
this.FormElement.TitleBar.ForeColor = System.Drawing.Color.White;
this.FormClosed += StopBtn_Click;
Test();
}
private void Test()
{
LocalStorage localStorage = new LocalStorage()
{
Signature = "f3efaf848f2d9f78b567cac334d4a4a5cf3df2c090b22c839468ee757f88bca1"
};
//Storage.WriteToBinaryFile(localStorage, true);
}
public void STServerHelper_Load(object sender, EventArgs e)
{
//NatCheck();
if (isStarted == false) { stopBtn.Enabled = false; }
}
public void NatCheck()
{
//NatUtility.DeviceFound += DeviceLost;
NatUtility.DeviceFound += DeviceFound;
NatUtility.StartDiscovery();
}
private void DeviceFound(object sender, DeviceEventArgs args)
{
INatDevice device = args.Device;
PublicIP.Invoke(new Action(() => PublicIP.Text = device.GetExternalIP().ToString()));
if (isStarted == true)
{
if (portEnabled.Value == true)
{
device.GetSpecificMapping(Protocol.Tcp, Convert.ToInt32(portTxt.Text));
device.CreatePortMap(new Mapping(Protocol.Tcp, Convert.ToInt32(portTxt.Text), Convert.ToInt32(portTxt.Text)));
}
else
{
device.GetSpecificMapping(Protocol.Tcp, 10578);
device.CreatePortMap(new Mapping(Protocol.Tcp, 10578, 10578));
}
}
}
private void DeviceLost(object sender, DeviceEventArgs args)
{
INatDevice device = args.Device;
MessageBox.Show("Nat device lost!");
if (portEnabled.Value == true)
{
device.DeletePortMap(new Mapping(Protocol.Tcp, Convert.ToInt32(portTxt.Text), Convert.ToInt32(portTxt.Text)));
}
else
{
device.DeletePortMap(new Mapping(Protocol.Tcp, 10578, 10578));
}
}
private void ShowMappedPorts(object sender, DeviceEventArgs args)
{
INatDevice device = args.Device;
foreach (Mapping portMap in device.GetAllMappings())
{
MessageBox.Show(portMap.ToString());
}
}
private Dictionary<RadToggleSwitch, string> GetRelationships(STServerHelper st)
{
return new Dictionary<RadToggleSwitch, string>
{
{ st.tokenEnabled, "-token " + st.txtToken.Text },
{ st.portEnabled, "-port " + st.portTxt.Text },
{ st.premiumEnabled, st.premiumEnabled.Value ? "-premium" : string.Empty}
};
}
public void StartBtn_Click(object sender, EventArgs e)
{
string args = string.Empty;
foreach(var data in GetRelationships(this))
{
if (data.Key.Value)
{
args += data.Value + " ";
}
}
Process process = new Process();
process.StartInfo.FileName = ServerDirectory + "\\Server.exe";
process.StartInfo.Arguments = args;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.OutputDataReceived += OutputReceived;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.BeginOutputReadLine();
var pid = process.Id;
ID = pid;
isStarted = true;
stopBtn.Enabled = true;
startBtn.Enabled = false;
}
private void OutputReceived(object sender, DataReceivedEventArgs e)
{
consoleBox.Invoke(new Action(() => consoleBox.Text += e.Data + "\r\n"));
}
public int ID { get; set; }
public void StopBtn_Click(object sender, EventArgs e)
{
if(isStarted == false) { return; }
try
{
Process processByID = Process.GetProcessById(ID);
Process process = processByID;
process.Kill();
isStarted = false;
startBtn.Enabled = true;
stopBtn.Enabled = false;
consoleBox.AppendText("\r\n-------- Server Stopped ---------\r\n");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if(restartEnabled.Value == true)
{
StartBtn_Click(sender, e);
}
}
private void CopyBtn_Click(object sender, EventArgs e)
{
Clipboard.SetText(LocalIP.Text);
}
private void Copy2Btn_Click(object sender, EventArgs e)
{
Clipboard.SetText(PublicIP.Text);
}
private void LogsBtn_Click(object sender, EventArgs e)
{
var directory = new DirectoryInfo( ServerDirectory + "\\logs");
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
Process.Start(myFile.FullName);
}
private void PortBtn_Click(object sender, EventArgs e)
{
NatCheck();
}
private void LogsFolderBtn_Click(object sender, EventArgs e)
{
var directory = new DirectoryInfo(ServerDirectory + "\\logs");
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
Process.Start(myFile.DirectoryName);
}
private void TxtToken_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtToken.Text))
{
tokenEnabled.Value = true;
}
else
{
tokenEnabled.Value = false;
}
}
private void PortTxt_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(portTxt.Text))
{
portEnabled.Value = true;
}
else
{
portEnabled.Value = false;
}
}
private void UpdatesBtn_Click(object sender, EventArgs e)
{
var client = new WebClient();
using (var stream = client.OpenRead("https://updates.skyrim-together.com/hashes_489830.txt"))
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("Server.exe"))
{
string serverSignature = line.Split(' ')[0];
MessageBox.Show(serverSignature);
}
}
}
}
}
}