added server pinging

This commit is contained in:
Bitl 2021-09-27 10:42:40 -07:00
parent 237602849d
commit 476c8c456a
5 changed files with 77 additions and 11 deletions

View File

@ -34,6 +34,8 @@ namespace NovetusCMD
GlobalFuncs.ConsolePrint("-port <port number> | Sets the server port.", 4, true);
GlobalFuncs.ConsolePrint("-maxplayers <number of players> | Sets the number of players.", 4, true);
GlobalFuncs.ConsolePrint("-notifications <true/false> | Toggles server join/leave notifications.", 4, true);
GlobalFuncs.ConsolePrint("-serverbrowsername <server name> | Changes the name the server uses upon connection to the master server.", 4, true);
GlobalFuncs.ConsolePrint("-serverbrowseraddress <master server address> | Changes the master server address.", 4, true);
GlobalFuncs.ConsolePrint("---------", 1, true);
GlobalFuncs.ConsolePrint("How to launch:", 3, true);
GlobalFuncs.ConsolePrint("---------", 1, true);

View File

@ -38,7 +38,7 @@ namespace NovetusCMD
try
{
NetFuncs.StartUPnP(device,protocol,port);
string IP = (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString());
string IP = !string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString();
GlobalFuncs.ConsolePrint("UPnP: Port " + port + " opened on '" + IP + "' (" + protocol.ToString() + ")", 3);
}
catch (Exception ex)
@ -56,7 +56,7 @@ namespace NovetusCMD
try
{
NetFuncs.StopUPnP(device,protocol,port);
string IP = (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString());
string IP = !string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString();
GlobalFuncs.ConsolePrint("UPnP: Port " + port + " closed on '" + IP + "' (" + protocol.ToString() + ")", 3);
}
catch (Exception ex)
@ -72,7 +72,7 @@ namespace NovetusCMD
try
{
INatDevice device = args.Device;
string IP = (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString());
string IP = !string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString();
GlobalFuncs.ConsolePrint("UPnP: Device '" + IP + "' registered.", 3);
StartUPnP(device, Protocol.Udp, GlobalVars.UserConfiguration.RobloxPort);
StartUPnP(device, Protocol.Tcp, GlobalVars.UserConfiguration.RobloxPort);
@ -89,7 +89,7 @@ namespace NovetusCMD
try
{
INatDevice device = args.Device;
string IP = (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString());
string IP = !string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : device.GetExternalIP().ToString();
GlobalFuncs.ConsolePrint("UPnP: Device '" + IP + "' disconnected.", 3);
StopUPnP(device, Protocol.Udp, GlobalVars.UserConfiguration.RobloxPort);
StopUPnP(device, Protocol.Tcp, GlobalVars.UserConfiguration.RobloxPort);
@ -293,6 +293,18 @@ namespace NovetusCMD
LocalVars.OverrideINI = true;
GlobalVars.UserConfiguration.PlayerLimit = Convert.ToInt32(CommandLine["maxplayers"]);
}
if (CommandLine["serverbrowsername"] != null)
{
LocalVars.OverrideINI = true;
GlobalVars.UserConfiguration.ServerBrowserServerName = CommandLine["serverbrowsername"];
}
if (CommandLine["serverbrowseraddress"] != null)
{
LocalVars.OverrideINI = true;
GlobalVars.UserConfiguration.ServerBrowserServerAddress = CommandLine["serverbrowseraddress"];
}
}
}
#endregion
@ -305,6 +317,16 @@ namespace NovetusCMD
static void ServerExited(object sender, EventArgs e)
{
string IP = SecurityFuncs.GetExternalIPAddress();
string pingURL = "http://" + GlobalVars.UserConfiguration.ServerBrowserServerAddress +
"/query.php?name=" + GlobalVars.UserConfiguration.ServerBrowserServerName +
"&ip=" + (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : IP) +
"&port=" + GlobalVars.UserConfiguration.RobloxPort +
"&client=" + GlobalVars.UserConfiguration.SelectedClient + "&online=0";
GlobalFuncs.ConsolePrint("Server closed. Pinging master server.", 4);
string response = GlobalFuncs.HttpGet(pingURL);
GlobalFuncs.ConsolePrint(!response.Contains("ERROR:") ? "Pinging done. Response from the server was: " + response : response, response.Contains("ERROR:") ? 2 : 4);
Environment.Exit(0);
}
#endregion

View File

@ -8,6 +8,7 @@ using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
@ -1978,5 +1979,29 @@ public class GlobalFuncs
}
}
#endif
//https://stackoverflow.com/questions/27108264/how-to-properly-make-a-http-web-get-request
public static string HttpGet(string uri)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
catch (Exception ex)
{
#if LAUNCHER || CMD || URI
LogExceptions(ex);
#endif
return "ERROR: " + ex.Message;
}
}
}
#endregion

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
#endregion
@ -276,7 +277,7 @@ namespace NovetusLauncher
string IP = await SecurityFuncs.GetExternalIPAddressAsync();
box.Text = "";
string[] lines1 = {
SecurityFuncs.Base64Encode((!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : IP)),
SecurityFuncs.Base64Encode(!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : IP),
SecurityFuncs.Base64Encode(GlobalVars.UserConfiguration.RobloxPort.ToString()),
SecurityFuncs.Base64Encode(GlobalVars.UserConfiguration.SelectedClient)
};
@ -425,7 +426,7 @@ namespace NovetusLauncher
GlobalFuncs.LaunchRBXClient(ScriptType.Client, false, true, new EventHandler(ClientExited), ConsoleBox);
break;
case ScriptType.Server:
GlobalFuncs.LaunchRBXClient(ScriptType.Server, no3d, false, new EventHandler(ClientExitedBase), ConsoleBox);
GlobalFuncs.LaunchRBXClient(ScriptType.Server, no3d, false, new EventHandler(ServerExited), ConsoleBox);
break;
case ScriptType.Solo:
GlobalFuncs.LaunchRBXClient(ScriptType.Solo, false, false, new EventHandler(ClientExited), ConsoleBox);
@ -483,6 +484,22 @@ namespace NovetusLauncher
ClientExitedBase(sender, e);
}
void ServerExited(object sender, EventArgs e)
{
string IP = SecurityFuncs.GetExternalIPAddress();
string pingURL = "http://" + GlobalVars.UserConfiguration.ServerBrowserServerAddress +
"/query.php?name=" + GlobalVars.UserConfiguration.ServerBrowserServerName +
"&ip=" + (!string.IsNullOrWhiteSpace(GlobalVars.UserConfiguration.AlternateServerIP) ? GlobalVars.UserConfiguration.AlternateServerIP : IP) +
"&port=" + GlobalVars.UserConfiguration.RobloxPort +
"&client=" + GlobalVars.UserConfiguration.SelectedClient + "&online=0";
GlobalFuncs.ConsolePrint("Server closed. Pinging master server.", 4, ConsoleBox);
string response = GlobalFuncs.HttpGet(pingURL);
GlobalFuncs.ConsolePrint(!response.Contains("ERROR:") ? "Pinging done. Response from the server was: " + response : response, response.Contains("ERROR:") ? 2 : 4, ConsoleBox);
ClientExitedBase(sender, e);
}
void EasterEggExited(object sender, EventArgs e)
{
GlobalFuncs.UpdateRichPresence(GlobalVars.LauncherState.InLauncher, "");

View File

@ -761,10 +761,10 @@
<TextBox x:Name="userIDBox" HorizontalAlignment="Left" Height="20" Margin="60,81,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="95" Grid.ColumnSpan="2" TextChanged="userIDBox_TextChanged"/>
<Label x:Name="userID" Content="ID:" HorizontalAlignment="Left" Margin="20,75,0,0" VerticalAlignment="Top" FontSize="12" Height="29" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Label x:Name="joinServerLabel" Content="Join Server" HorizontalAlignment="Left" Margin="30,134,0,0" VerticalAlignment="Top" FontSize="15" Width="102" Grid.ColumnSpan="2"/>
<TextBox x:Name="ipAddressBox" HorizontalAlignment="Left" Height="20" Margin="20,185,0,0" TextWrapping="Wrap" Text="localhost" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" TextChanged="ipAddressBox_TextChanged"/>
<Label x:Name="ipAddressLabel" Content="Server IP Address:" HorizontalAlignment="Left" Margin="20,165,0,0" VerticalAlignment="Top" FontSize="12" Height="29" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.ColumnSpan="2"/>
<TextBox x:Name="joinPortBox" HorizontalAlignment="Left" Height="20" Margin="20,220,0,0" TextWrapping="Wrap" Text="53640" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" TextChanged="joinPortBox_TextChanged"/>
<Label x:Name="joinPortLabel" Content="Join Port" HorizontalAlignment="Left" Margin="37,199,0,0" VerticalAlignment="Top" FontSize="12" Height="27" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="86" Grid.ColumnSpan="2"/>
<TextBox x:Name="ipAddressBox" HorizontalAlignment="Left" Height="20" Margin="19,185,0,0" TextWrapping="Wrap" Text="localhost" VerticalAlignment="Top" Width="135" Grid.ColumnSpan="2" TextChanged="ipAddressBox_TextChanged"/>
<Label x:Name="ipAddressLabel" Content="Server IP Address:" HorizontalAlignment="Left" Margin="20,163,0,0" VerticalAlignment="Top" FontSize="12" Height="29" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Grid.ColumnSpan="2" Width="135"/>
<TextBox x:Name="joinPortBox" HorizontalAlignment="Left" Height="20" Margin="20,220,0,0" TextWrapping="Wrap" Text="53640" VerticalAlignment="Top" Width="134" Grid.ColumnSpan="2" TextChanged="joinPortBox_TextChanged"/>
<Label x:Name="joinPortLabel" Content="Join Port" HorizontalAlignment="Left" Margin="20,199,0,0" VerticalAlignment="Top" FontSize="12" Height="27" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="134" Grid.ColumnSpan="2"/>
<Button x:Name="joinButton" Style="{DynamicResource JoinButton}" Content="" HorizontalAlignment="Left" Margin="122,191,0,0" VerticalAlignment="Top" Width="91" Height="33" Grid.Column="2" RenderTransformOrigin="0.227,0.54" Click="joinButton_Click"/>
<Button x:Name="serverBrowserButton" Style="{DynamicResource ImportantButton}" Content="Browse Servers" HorizontalAlignment="Left" Margin="100,225,0,0" VerticalAlignment="Top" Width="236" Height="30" Grid.Column="2" RenderTransformOrigin="0.645,-0.075" Click="serverBrowserButton_Click"/>
<Button x:Name="playSoloButton" Style="{DynamicResource SoloButton}" Content="" HorizontalAlignment="Left" Margin="218,191,0,0" VerticalAlignment="Top" Width="92" Height="32" Grid.Column="2" RenderTransformOrigin="1.3,0.863" Click="playSoloButton_Click"/>
@ -789,7 +789,7 @@
<Label x:Name="hostServerLabel" Content="Host Options" HorizontalAlignment="Left" Margin="24,0,0,0" VerticalAlignment="Top" FontSize="15" Width="105" RenderTransformOrigin="0.561,0.175"/>
<Label x:Name="serverPortLabel" Content="Host Port:" HorizontalAlignment="Left" Margin="8,34,0,0" VerticalAlignment="Top" Width="68"/>
<TextBox x:Name="serverPortBox" HorizontalAlignment="Left" Height="20" Margin="82,39,0,0" TextWrapping="Wrap" Text="53640" VerticalAlignment="Top" Width="65" TextChanged="serverPortBox_TextChanged"/>
<Label x:Name="maxPlayersLabel" Content="Max Players:" HorizontalAlignment="Left" Margin="-2,55,0,0" VerticalAlignment="Top" Width="86"/>
<Label x:Name="maxPlayersLabel" Content="Max Players:" HorizontalAlignment="Left" Margin="2,57,0,0" VerticalAlignment="Top" Width="86"/>
<TextBox x:Name="maxPlayersBox" HorizontalAlignment="Left" Height="20" Margin="82,62,0,0" TextWrapping="Wrap" Text="12" VerticalAlignment="Top" Width="66" TextChanged="maxPlayersBox_TextChanged"/>
<CheckBox x:Name="uPnPBox" Content="uPnP" HorizontalAlignment="Left" Margin="8,84,0,0" VerticalAlignment="Top" Checked="uPnPBox_Checked" Unchecked="uPnPBox_Unchecked" Click="uPnPBox_Click"/>
<CheckBox x:Name="NotifBox" Content="Join Alerts" HorizontalAlignment="Left" Margin="64,84,0,0" VerticalAlignment="Top" Checked="NotifBox_Checked" Unchecked="NotifBox_Unchecked"/>