-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathWebServer.cs
More file actions
173 lines (129 loc) · 5.66 KB
/
Copy pathWebServer.cs
File metadata and controls
173 lines (129 loc) · 5.66 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
using BitBotBackToTheFuture;
public class WebServer
{
public static string SendResponse(HttpListenerRequest request)
{
lock (MainClass.data)
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(MainClass.location + "bd.xml");
StringBuilder sb = new StringBuilder();
sb.AppendLine(System.IO.File.ReadAllText(MainClass.location + "header.html") );
double perc = 0;
try { perc = ((double.Parse(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][2].ToString()) * 100) / double.Parse(ds.Tables[0].Rows[0][2].ToString())) - 100; }
catch { }
sb.AppendLine("<div class='row'><div class='col-sm'>Status: <b>running</b><br/>");
sb.AppendLine("Version: <b>" + MainClass.version + "</b><br/>");
sb.AppendLine("Site: <b>" + MainClass.bitmexDomain + "</b><br/>");
sb.AppendLine("Last update: <b>" + DateTime.Now.ToString() + "</b><br/>");
sb.AppendLine("Amount: <b>" + ds.Tables[1].Rows[1]["Value"].ToString() + "</b><br/>");
sb.AppendLine("Open position: <b>" + MainClass.positionContracts.ToString() + "</b><br/>");
sb.AppendLine("Amount: <b>" + ds.Tables[1].Rows[1]["Value"].ToString() + "</b> (" + double.Parse(ds.Tables[1].Rows[1]["Value"].ToString()) / 100000000 + " BTC)<br/>");
sb.AppendLine("<h3>Profit: <b>" + perc + "%</b><br/></h3>");
sb.AppendLine("Qty contracts: <b>" + MainClass.qtdyContacts.ToString() + "</b><br/>");
try
{
String graph = "";
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
graph += "['" + ds.Tables[0].Rows[i][0].ToString() + "'," + ds.Tables[0].Rows[i][2].ToString() + "],";
}
graph = graph.Substring(0, graph.Length - 1);
sb.Append("<script>google.charts.load('current', {packages: ['corechart', 'line']}); " +
" google.charts.setOnLoadCallback(drawBasic); " +
" function drawBasic() {" +
" var data = new google.visualization.DataTable();" +
" data.addColumn('string', 'Time');" +
" data.addColumn('number', '" + "BTC" + "');" +
" data.addRows([ " +
graph +
"]); " +
"var options = {" +
" hAxis: {" +
" title: 'Time'" +
" }," +
" vAxis: {" +
" title: '" + "BTC" + "'" +
" }" +
"};" +
"var chart = new google.visualization.LineChart(document.getElementById('chart_div'));" +
"chart.draw(data, options);" +
"}; </script><div id='chart_div'></div>");
}
catch
{ }
sb.AppendLine(System.IO.File.ReadAllText(MainClass.location + "footer.html"));
sb.AppendLine("</body></html>");
return sb.ToString();
}
catch (Exception ex)
{
return "";
}
}
}
private readonly HttpListener _listener = new HttpListener();
private readonly Func<HttpListenerRequest, string> _responderMethod;
public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
{
if (!HttpListener.IsSupported)
throw new NotSupportedException(
"Needs Windows XP SP2, Server 2003 or later.");
// URI prefixes are required, for example
// "http://localhost:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");
// A responder method is required
if (method == null)
throw new ArgumentException("method");
foreach (string s in prefixes)
_listener.Prefixes.Add(s);
_responderMethod = method;
_listener.Start();
}
public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
: this(prefixes, method) { }
public void Run()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Console.WriteLine("Webserver running...");
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
try
{
string rstr = _responderMethod(ctx.Request);
byte[] buf = Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64 = buf.Length;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
}
catch { } // suppress any exceptions
finally
{
// always close the stream
ctx.Response.OutputStream.Close();
}
}, _listener.GetContext());
}
}
catch { } // suppress any exceptions
});
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
}