Removed custom ipv4. Small changes

This commit is contained in:
EntenKoeniq
2022-04-19 03:38:48 +02:00
parent 406248a05f
commit e8c376154a
2 changed files with 55 additions and 90 deletions

View File

@ -8,18 +8,10 @@ using System.Reflection;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using Lidgren.Network;
namespace CoopServer
{
internal class IpInfo
{
[JsonProperty("country")]
public string Country { get; set; }
}
internal class Server
{
private static readonly string _compatibleVersion = "V1_4";
@ -39,18 +31,8 @@ namespace CoopServer
public Server()
{
if (IPAddress.TryParse(MainSettings.Address, out IPAddress parsedAddress) == false)
{
throw new Exception($"Something went wrong while parsing IP {MainSettings.Address}!");
}
if (parsedAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
{
throw new Exception("Please use a valid IPv4 address!");
}
Logging.Info("================");
Logging.Info($"Server bound to: {MainSettings.Address}:{MainSettings.Port}");
Logging.Info($"Server bound to: 0.0.0.0:{MainSettings.Port}");
Logging.Info($"Server version: {Assembly.GetCallingAssembly().GetName().Version}");
Logging.Info($"Compatible RAGECOOP versions: {_compatibleVersion.Replace('_', '.')}.x");
Logging.Info("================");
@ -58,8 +40,6 @@ namespace CoopServer
// 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP
NetPeerConfiguration config = new("623c92c287cc392406e7aaaac1c0f3b0")
{
LocalAddress = parsedAddress,
BroadcastAddress = parsedAddress,
Port = MainSettings.Port,
MaximumConnections = MainSettings.MaxPlayers,
EnableUPnP = MainSettings.UPnP
@ -92,12 +72,6 @@ namespace CoopServer
{
Logging.Info("Announcing to master server...");
if (new string[5] { "127.0.0.1", "192.168.", "0.0.0.0", "1.1.1.1", "1.2.3.4" }.Any(el => MainSettings.Address.Contains(el, StringComparison.CurrentCultureIgnoreCase)))
{
Logging.Error($"Announcing to the master server failed because this is not a public address!");
}
else
{
#region -- MASTERSERVER --
new Thread(async () =>
{
@ -110,24 +84,10 @@ namespace CoopServer
HttpClient httpClient = new();
IpInfo info;
try
{
string data = await httpClient.GetStringAsync("https://ipinfo.io/json");
info = JsonConvert.DeserializeObject<IpInfo>(data);
}
catch
{
info = new() { Country = "?" };
}
while (!Program.ReadyToStop)
{
string msg =
"{ " +
"\"address\": \"" + MainSettings.Address + "\", " +
"\"port\": \"" + MainSettings.Port + "\", " +
"\"name\": \"" + MainSettings.Name + "\", " +
"\"version\": \"" + _compatibleVersion.Replace("_", ".") + "\", " +
@ -135,8 +95,7 @@ namespace CoopServer
"\"maxPlayers\": \"" + MainSettings.MaxPlayers + "\", " +
"\"allowlist\": \"" + _mainAllowlist.Username.Any() + "\", " +
"\"mods\": \"" + MainSettings.ModsAllowed + "\", " +
"\"npcs\": \"" + MainSettings.NpcsAllowed + "\", " +
"\"country\": \"" + info.Country + "\"" +
"\"npcs\": \"" + MainSettings.NpcsAllowed + "\"" +
" }";
HttpResponseMessage response = null;
@ -157,10 +116,18 @@ namespace CoopServer
{
Logging.Error("MasterServer: Something went wrong!");
}
else if (response.StatusCode != System.Net.HttpStatusCode.OK)
else if (response.StatusCode != HttpStatusCode.OK)
{
if (response.StatusCode == HttpStatusCode.BadRequest)
{
string requestContent = await response.Content.ReadAsStringAsync();
Logging.Error($"MasterServer: [{(int)response.StatusCode}], {requestContent}");
}
else
{
Logging.Error($"MasterServer: [{(int)response.StatusCode}]");
}
}
// Sleep for 10s
Thread.Sleep(10000);
@ -177,7 +144,6 @@ namespace CoopServer
}).Start();
#endregion
}
}
if (!string.IsNullOrEmpty(MainSettings.Resource))
{

View File

@ -2,7 +2,6 @@
{
public class Settings
{
public string Address { get; set; } = "127.0.0.1";
public int Port { get; set; } = 4499;
public int MaxPlayers { get; set; } = 16;
public int MaxLatency { get; set; } = 300;