-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (86 loc) · 4.1 KB
/
Program.cs
File metadata and controls
108 lines (86 loc) · 4.1 KB
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
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
namespace SmartChain.Web
{
public class Program
{
public static string client_version = "1";
public static ManualResetEvent shutdown_Event = new ManualResetEvent(false);
public static ConfigFileReader config1 = new ConfigFileReader();
public static int language { get; set; } = 1;
private static X509Certificate2 cert;
private static bool useSSL = bool.Parse(config1.lookup("useSSL"));
public static JArray listen_urls = new JArray();
public static int listen_port = 80;
public static void Main(string[] args)
{
IConfigurationRoot config;
if (useSSL)
{
config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
.AddJsonFile($"certificate.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
.Build();
var certificateSettings = config.GetSection("certificateSettings");
string certificateFileName = certificateSettings.GetValue<string>("filename");
string certificatePassword = certificateSettings.GetValue<string>("password");
cert = new X509Certificate2(certificateFileName, certificatePassword);
}
else
{
config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.Build();
}
listen_urls = JArray.Parse(config1.lookup("listens"));
// route all unhandled exceptions to the logger
AppDomain currentDomain = AppDomain.CurrentDomain;
BuildWebHost(args, config).RunAsync();
// setup logging
shutdown_Event.Reset();
shutdown_Event.WaitOne();
}
private static void OpenBrowser(string ip)
{
System.Diagnostics.Process.Start("cmd", "/C start " + ip);
}
public static IWebHost BuildWebHost(string[] args, IConfiguration config) =>
WebHost.CreateDefaultBuilder(args)
// swithc to easy turn off kestral to enable chrome debugging
#if false
.UseKestrel(
options =>
{
options.AddServerHeader = false;
// if SSL is to be used, make sure we listen on SSL port 443 and use our SSL cert.
foreach (JObject listen in listen_urls)
{
if ((bool)listen["SSL"] && useSSL)
options.Listen(IPAddress.Parse(listen["listen_url"].ToString()), (int)listen["listen_port"], listenOptions => { listenOptions.UseHttps(cert); });
else
options.Listen(IPAddress.Parse(listen["listen_url"].ToString()), (int)listen["listen_port"]);
}
}
)
#endif
// set the port non-ssl requests will be forwarded to, if we are using SSL
.UseSetting("https_port", "443")
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
// not used due to kestral setting listen urls/ports
//.UseElectron(args)
.UseStartup<Startup>()
//.UseSetting("https_port", "8080")
.Build();
}
}