-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (119 loc) · 3.93 KB
/
Copy pathProgram.cs
File metadata and controls
126 lines (119 loc) · 3.93 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CodeFirstWebFramework;
namespace AccountServer {
static class Program {
static void Main(string[] args) {
Config.Load(args);
bool windows = false;
switch (Environment.OSVersion.Platform) {
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
windows = true;
break;
}
// Default to UK culture and time (specify empty culture and/or tz to use machine values)
if (Config.CommandLineFlags["culture"] != "") {
CultureInfo c = new CultureInfo(Config.CommandLineFlags["culture"] ?? "en-GB");
Thread.CurrentThread.CurrentCulture = c;
CultureInfo.DefaultThreadCurrentCulture = c;
CultureInfo.DefaultThreadCurrentUICulture = c;
}
if (!string.IsNullOrEmpty(Config.CommandLineFlags["tz"]))
Utils._tz = TimeZoneInfo.FindSystemTimeZoneById(Config.CommandLineFlags["tz"] ?? (windows ? "GMT Standard Time" : "GB"));
string startPage = "";
bool serverRunning = false;
if (windows) {
if (Config.CommandLineFlags["nolaunch"] == null) {
if (Config.CommandLineFlags["url"] != null)
startPage = Config.CommandLineFlags["url"];
// Is server already running?
try {
using (var client = new TcpClient()) {
var result = client.BeginConnect(Config.Default.DefaultServer.ServerName, Config.Default.Port, null, null);
result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
serverRunning = client.Connected;
}
} catch {
}
if(!startPage.StartsWith("http"))
startPage = "http://" + Config.Default.DefaultServer.ServerName + ":" + Config.Default.Port + "/" + startPage;
}
}
if (serverRunning) {
if (!string.IsNullOrEmpty(startPage))
System.Diagnostics.Process.Start(startPage);
} else {
WebServer server = new WebServer();
if (windows)
new Task(CheckForNewVersion).Start();
if (!string.IsNullOrEmpty(startPage))
System.Diagnostics.Process.Start(startPage);
server.Start();
}
}
public static string NewVersion;
static void CheckForNewVersion() {
for( ; ; ) {
try {
HttpWebRequest req = WebRequest.Create("https://api.github.com/repos/nikkilocke/AccountServer/releases/latest") as HttpWebRequest;
req.ServicePoint.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
req.UserAgent = "AccountServer (Windows; " + CultureInfo.CurrentCulture.Name + "; AccountServer)";
using (WebResponse resp = req.GetResponse()) {
using (var stream = resp.GetResponseStream()) {
using (var reader = new StreamReader(stream)) {
using (var jr = new JsonTextReader(reader)) {
JObject d = JObject.Load(jr);
string tag = d.AsString("tag_name");
if(tag.CompareTo("v" + WebServer.AppVersion) > 0) {
JObject asset = ((JArray)d["assets"]).Select(a => (JObject)a).FirstOrDefault(a => a.AsString("name") == "AccountServerSetup.msi");
if (asset != null)
NewVersion = asset.AsString("browser_download_url");
}
}
}
}
}
} catch(Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
}
Thread.Sleep(new TimeSpan(24, 0, 0));
}
}
/// <summary>
/// Translate NameType letter to human-readable form
/// </summary>
public static string NameType(this string type) {
switch (type) {
case "C":
return "Customer";
case "S":
return "Supplier";
case "O":
return "Other Name";
case "M":
return "Member";
case "":
case null:
return "Unknown";
default:
return "Type " + type;
}
}
}
}