patbef-ServiceInside/WakeUp/Program.cs

84 lines
2.7 KiB
C#
Raw Normal View History

2024-01-29 16:26:54 +01:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace WakeUp
{
internal class Program
{
static void Main(string[] args)
{
string url = null;
if (args != null && args.Length > 0 && !string.IsNullOrEmpty(args[0]))
{
Thread.Sleep(30000);
url = args[0];
}
try
{
if(string.IsNullOrEmpty(url))
{
/** try get the serviceinside url from appsettings.json **/
string appsettings = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
if(!string.IsNullOrEmpty(appsettings) && File.Exists(appsettings))
{
Console.WriteLine("appsettings.json found at " + appsettings);
string json = File.ReadAllText(appsettings);
if (!string.IsNullOrEmpty(json) && json.Contains("ServiceInsideURL"))
{
var rawObj = JObject.Parse(json);
if(rawObj != null)
{
url = rawObj.Value<string>("ServiceInsideURL");
}
}
}
else
{
Console.WriteLine("appsettings.json not found at " + appsettings);
}
}
if (!string.IsNullOrEmpty(url))
{
url = url + "/exchange/key";
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
using (WebClient client = new WebClient())
{
string content = client.UploadString(url, "post");
Console.WriteLine(content);
}
Console.WriteLine("URL found (" + url + ")");
}
else
{
Console.WriteLine("URL not defined");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}