From bae21a877d7901e1b462c0d262727bf3e26837b9 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Tue, 19 Apr 2022 05:33:18 +0200 Subject: [PATCH] Welcome back IPv4 request! --- Server/Server.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Server/Server.cs b/Server/Server.cs index db71ed6..368ccfd 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -8,10 +8,18 @@ using System.Reflection; using System.IO; using System.Net.Http; +using Newtonsoft.Json; + using Lidgren.Network; namespace CoopServer { + internal struct IpInfo + { + [JsonProperty("ip")] + public string Address { get; set; } + } + internal class Server { private static readonly string _compatibleVersion = "V1_4"; @@ -84,10 +92,29 @@ namespace CoopServer HttpClient httpClient = new(); + IpInfo info; + try + { + HttpResponseMessage response = await httpClient.GetAsync("https://ipinfo.io/json"); + if (!response.IsSuccessStatusCode) + { + throw new Exception("IPv4 request failed!"); + } + + string content = await response.Content.ReadAsStringAsync(); + info = JsonConvert.DeserializeObject(content); + } + catch (Exception ex) + { + Logging.Error(ex.InnerException?.Message ?? ex.Message); + return; + } + while (!Program.ReadyToStop) { string msg = "{ " + + "\"address\": \"" + info.Address + "\", " + "\"port\": \"" + MainSettings.Port + "\", " + "\"name\": \"" + MainSettings.Name + "\", " + "\"version\": \"" + _compatibleVersion.Replace("_", ".") + "\", " +