forked from ajkagy/xls20-bridge-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiServerSession.cs
131 lines (123 loc) · 5.51 KB
/
ApiServerSession.cs
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
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Text;
using NetCoreServer;
using System.Collections.Generic;
using System.Text.Json;
using System.Web;
namespace XLS_20_Bridge_MasterProcess
{
class ApiServerSession : HttpSession
{
private string ApiKey;
private database db;
private Settings config;
public ApiServerSession(NetCoreServer.HttpServer server, database _db, Settings _config) : base(server)
{
ApiKey = _config._apiKey;
db = _db;
config = _config;
}
protected override void OnReceivedRequest(HttpRequest request)
{
try
{
string tempApiKeyStorage = "";
// Show HTTP request content
Console.WriteLine(request);
long totalHeaders = request.Headers;
for (long j = 0; j < totalHeaders; j++)
{
if (request.Header((int)j).Item1 == "x-api-key")
{
tempApiKeyStorage = request.Header((int)j).Item2;
}
}
if (tempApiKeyStorage != ApiKey)
{
SendResponseAsync(Response.MakeErrorResponse(401, "Not Authorized"));
}
else
{
if (request.Method == "GET")
{
string url = request.Url;
if (url.StartsWith("/api/xls20bridge/getBridgeInfoByXRPAddress/"))
{
try
{
string[] splitVal = url.Split("/api/xls20bridge/getBridgeInfoByXRPAddress/");
List<BridgeNFT> list = db.GetNFTsReadyToBeClaimed(splitVal[1]);
List<ResponseObjectNFT> returnObj = new List<ResponseObjectNFT>();
foreach (BridgeNFT b in list)
{
ResponseObjectNFT r = new ResponseObjectNFT();
r.xrplTokenId = b.xrplTokenId;
r.tokenUri = b.tokenUri;
returnObj.Add(r);
}
SendResponseAsync(Response.MakeGetResponse(JsonSerializer.Serialize(returnObj), "application/json; charset=UTF-8"));
}
catch (Exception)
{
SendResponseAsync(Response.MakeErrorResponse(500, "Server Error"));
}
}
else if (url == "/api/xls20bridge/bridgeStatus")
{
List<string> validators = db.GetValidatorsToPing(6);
string status = "";
if (validators.Count == 0)
{
status = "Online";
SendResponseAsync(Response.MakeGetResponse("{\"status\": \"" + status + "\"}", "application/json; charset=UTF-8"));
}
else
{
status = "Validation Offline";
SendResponseAsync(Response.MakeGetResponse("{\"status\": \"" + status + "\"}", "application/json; charset=UTF-8"));
}
}
else if (url.StartsWith("/api/xls20bridge/getStatus"))
{
try
{
string[] splitVal = url.Split("?");
string tokenId = HttpUtility.ParseQueryString(splitVal[1]).Get("tokenId");
string tokenAddress = HttpUtility.ParseQueryString(splitVal[1]).Get("tokenAddress");
ResponseObjectStatus r = new ResponseObjectStatus();
string status = db.GetStatus(Convert.ToInt32(tokenId), tokenAddress);
r.status = status;
SendResponseAsync(Response.MakeGetResponse(JsonSerializer.Serialize(r), "application/json; charset=UTF-8"));
}
catch (Exception)
{
SendResponseAsync(Response.MakeErrorResponse(500, "Server Error"));
}
}
else
{
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
}
}
else
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected override void OnReceivedRequestError(HttpRequest request, string error)
{
Console.WriteLine($"Request error: {error}");
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"HTTP session caught an error: {error}");
}
}
}