-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDotNS.cs
88 lines (72 loc) · 2.6 KB
/
DotNS.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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
namespace dotNS
{
public class DotNS
{
protected string _Nation = null;
public string Nation { get { return _Nation; } }
protected bool _IsAuthed = false;
public bool IsAuthed { get { return _IsAuthed; } }
public string Pin { get; protected set; } = "-1";
protected string _UserAgent;
public string UserAgent { get { return _UserAgent; } set { _UserAgent = value; } }
protected List<long> _RL_Requests = new List<long>();
protected bool _RateLimit = true;
public bool RateLimit { get { return _RateLimit; } set { _RateLimit = value; _RL_Requests = new List<long>(); } }
public static string DefaultUA = "DotNS Default UserAgent nk.ax";
public DotNS()
{
UserAgent = DefaultUA;
}
public bool UpdatePin(string nation, string password)
{
var nvc = new NameValueCollection();
nvc.Add("nation", nation);
nvc.Add("q", "unread");
var resp = Utilities.API(nvc, password);
if (resp.StatusCode == HttpStatusCode.OK)
{
Pin = resp.Headers.GetValues("X-Pin").First();
_IsAuthed = true;
_Nation = nation;
return true;
}
return false;
}
public DotNS(string nation, string password, string useragent = null)
{
_Nation = nation;
UserAgent = useragent is null ? DefaultUA : useragent;
UpdatePin(nation, password);
}
public void ProcessRatelimit(bool addValue = false)
{
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (_RateLimit)
{
_RL_Requests.RemoveAll((z) => { return z + 30000 <= now; });
int currReq = _RL_Requests.Count;
if (currReq >= 49)
{
var el = _RL_Requests.First();
Thread.Sleep((int)(31000 - (now - el)));
}
}
if (addValue)
{
_RL_Requests.Add(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
}
}
public HttpResponseMessage API(NameValueCollection nvc, string pass = null, string pin = "0")
{
ProcessRatelimit(true);
return Utilities.API(nvc, pass, pin, UserAgent);
}
}
}