-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObvTrojanModel.cs
149 lines (121 loc) · 4.69 KB
/
ObvTrojanModel.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
namespace ObvTrojanClient
{
public class ObvTrojanModel
{
private ObvTrojanController _controller;
private static Uri _baseUrl = new Uri(@"https://trojandns.com/rest/subdomains/");
private static Uri _keyUrl = new Uri(@"https://trojandns.com/rest/account/key/");
private static Uri _ipUrl = new Uri(@"https://trojandns.com/ip");
public ObvTrojanModel(ObvTrojanController controller, ObvTrojanView view)
{
_controller = controller;
}
public string GetDetectedIPAddress()
{
string data;
using (WebClient client = new WebClient())
{
client.Proxy = null;
data = client.DownloadString(_ipUrl);
}
return data;
}
public IList<Subdomain> GetSubdomainList(string clientKey)
{
IList<Subdomain> subdomains = new List<Subdomain>();
string url = _baseUrl.ToString() + @"?key=" + clientKey;
XmlDocument doc = new XmlDocument();
string data;
using (WebClient client = new WebClient())
{
client.Proxy = null;
data = client.DownloadString(url);
}
doc.Load(new StringReader(data));
XmlNodeList nodes = doc.SelectNodes("//item");
if (nodes != null)
{
foreach (XmlNode node in nodes)
{
string domainName = node.SelectSingleNode("descendant::domain_name").InnerText;
string subdomainName = node.SelectSingleNode("descendant::subdomain_name").InnerText;
int subdomain_id = Int32.Parse(node.SelectSingleNode("descendant::subdomain_id").InnerText);
string ip = node.SelectSingleNode("descendant::bound_ip").InnerText;
Subdomain subdomain = new Subdomain(subdomain_id, ip, subdomainName + "." + domainName);
subdomains.Add(subdomain);
}
}
return subdomains;
}
public bool TryLogin(string username, string password, out string clientKey)
{
string response;
using (WebClient wc = new WebClient())
{
wc.Proxy = null;
NameValueCollection nvc = new NameValueCollection();
nvc.Add("username", username);
nvc.Add("password", password);
byte[] byteResponse = wc.UploadValues(_keyUrl, nvc);
UTF8Encoding utf8 = new UTF8Encoding();
response = utf8.GetString(byteResponse);
}
XmlDocument xml = new XmlDocument();
xml.LoadXml(response);
if (getXmlSucces(xml))
{
clientKey = xml.SelectSingleNode("//key").InnerText.Trim();
return true;
}
else
{
clientKey = "";
return false;
}
}
public string Update(string clientKey, int subdomainID, string ip)
{
string url = _baseUrl.ToString() + subdomainID.ToString() + @"?key=" + clientKey;
byte[] data = { };
if (!String.IsNullOrEmpty(ip))
{
UTF8Encoding utf8 = new UTF8Encoding();
data = utf8.GetBytes("ip=" + ip);
}
Console.WriteLine(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
request.Method = "PUT";
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream postData = request.GetRequestStream();
postData.Write(data, 0, data.Length);
XmlDocument doc = new XmlDocument();
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
doc.Load(stream);
}
return getIPAddress(doc);
}
private static bool getXmlSucces(XmlDocument xml)
{
return xml.SelectSingleNode("//success").InnerText == "true";
}
private static string getXmlErrorMessage(XmlDocument xml)
{
return xml.SelectSingleNode("//message").InnerText;
}
private static string getIPAddress(XmlDocument xml)
{
return xml.SelectSingleNode("//ip").InnerText;
}
}
}