개정판 7b476006
sppid web api license check test
Change-Id: I3575c0f9f3e0db7a7fafbef852161b7024b62bef
DTI_PID/SPPIDConverter/License.cs | ||
---|---|---|
7 | 7 |
using System.IO; |
8 | 8 |
using Microsoft.Win32; |
9 | 9 |
using System.Net.NetworkInformation; |
10 |
using Converter.SPPID.Form; |
|
11 |
using System.Windows.Forms; |
|
12 |
using Converter.SPPID.Properties; |
|
13 |
using System.Net; |
|
14 |
using JWT; |
|
15 |
using JWT.Algorithms; |
|
16 |
using JWT.Serializers; |
|
17 |
using JWT.Exceptions; |
|
18 |
using Newtonsoft.Json; |
|
10 | 19 | |
11 | 20 |
namespace DOFTECH.License |
12 | 21 |
{ |
22 |
public class LicenseDTO |
|
23 |
{ |
|
24 |
/// <summary> |
|
25 |
/// 라이센스 고유 값 |
|
26 |
/// </summary> |
|
27 |
public int uid { get; set; } |
|
28 | ||
29 |
/// <summary> |
|
30 |
/// MAC 주소 |
|
31 |
/// </summary> |
|
32 |
public string mac { get; set; } |
|
33 | ||
34 |
/// <summary> |
|
35 |
/// 메일 주소 |
|
36 |
/// </summary> |
|
37 |
public string email { get; set; } |
|
38 | ||
39 |
/// <summary> |
|
40 |
/// 어플리케이션 |
|
41 |
/// </summary> |
|
42 |
public string app { get; set; } |
|
43 | ||
44 |
/// <summary> |
|
45 |
/// 라이센스 코드 |
|
46 |
/// </summary> |
|
47 |
public string code { get; set; } |
|
48 | ||
49 |
/// <summary> |
|
50 |
/// api name. |
|
51 |
/// </summary> |
|
52 |
public string api { get; set; } |
|
53 | ||
54 |
/// <summary> |
|
55 |
/// 만료 시간. |
|
56 |
/// </summary> |
|
57 |
public string expiration_date { get; set; } |
|
58 | ||
59 |
/// <summary> |
|
60 |
/// 만료 시간. |
|
61 |
/// </summary> |
|
62 |
public string exp { get; set; } |
|
63 | ||
64 |
/// <summary> |
|
65 |
/// License 개수 |
|
66 |
/// </summary> |
|
67 |
public int count { get; set; } |
|
68 |
} |
|
69 | ||
70 |
public class LicenseReturn |
|
71 |
{ |
|
72 |
/// <summary> |
|
73 |
/// STATUS |
|
74 |
/// </summary> |
|
75 |
public bool success { get; set; } |
|
76 | ||
77 |
/// <summary> |
|
78 |
/// CODE |
|
79 |
/// </summary> |
|
80 |
public int code { get; set; } |
|
81 | ||
82 |
/// <summary> |
|
83 |
/// MSG |
|
84 |
/// </summary> |
|
85 |
public string msg { get; set; } |
|
86 | ||
87 |
/// <summary> |
|
88 |
/// DATA |
|
89 |
/// </summary> |
|
90 |
public string data { get; set; } |
|
91 |
} |
|
92 | ||
13 | 93 |
public static class License |
14 | 94 |
{ |
95 |
//public static bool IsEnabled() |
|
96 |
//{ |
|
97 |
// bool result = false; |
|
98 |
// string path = string.Empty; |
|
99 | ||
100 |
// try |
|
101 |
// { |
|
102 |
// RegistryKey key = Registry.LocalMachine; |
|
103 |
// RegistryKey software = key.OpenSubKey("SOFTWARE"); |
|
104 |
// RegistryKey DOFTECH = software.OpenSubKey("DOFTECH"); |
|
105 |
// RegistryKey SPPIDConverter = DOFTECH.OpenSubKey("SPPIDConverter", true); |
|
106 |
// path = SPPIDConverter.GetValue("path") + "licenses.licx"; |
|
107 |
// if (File.Exists(path)) |
|
108 |
// { |
|
109 |
// string data = File.ReadAllText(path); |
|
110 |
// data = Crypt.decrypt(data, "dof1073#"); |
|
111 |
// if (data.StartsWith("Date:")) |
|
112 |
// { |
|
113 |
// data = data.Remove(0, "Date:".Length); |
|
114 | ||
115 |
// if (string.IsNullOrEmpty(data)) |
|
116 |
// { |
|
117 |
// DateTime dataTime = new DateTime(2000, 1, 1); |
|
118 |
// DateTime currentTime = DateTime.Now; |
|
119 |
// if (DateTime.TryParse(Converter.SPPID.Properties.Resources.lic, out dataTime) && DateTime.Compare(dataTime.AddDays(150), currentTime) >= 0) |
|
120 |
// result = true; |
|
121 |
// } |
|
122 |
// } |
|
123 |
// else if (data.StartsWith("Absolute:")) |
|
124 |
// { |
|
125 |
// data = data.Remove(0, "Absolute:".Length); |
|
126 |
// string macAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString(); |
|
127 |
// if (string.IsNullOrEmpty(data)) |
|
128 |
// { |
|
129 |
// data = "Absolute:" + macAddress; |
|
130 |
// data = Crypt.encrypt(data, "dof1073#"); |
|
131 |
// File.WriteAllText(path, data); |
|
132 |
// result = true; |
|
133 |
// } |
|
134 |
// else if (macAddress == data) |
|
135 |
// result = true; |
|
136 |
// } |
|
137 |
// } |
|
138 |
// } |
|
139 |
// catch (Exception ex) |
|
140 |
// { |
|
141 | ||
142 |
// } |
|
143 | ||
144 |
// return result; |
|
145 |
//} |
|
146 | ||
15 | 147 |
public static bool IsEnabled() |
16 | 148 |
{ |
17 | 149 |
bool result = false; |
18 |
string path = string.Empty; |
|
19 | 150 | |
20 | 151 |
try |
21 | 152 |
{ |
22 |
RegistryKey key = Registry.LocalMachine; |
|
23 |
RegistryKey software = key.OpenSubKey("SOFTWARE"); |
|
24 |
RegistryKey DOFTECH = software.OpenSubKey("DOFTECH"); |
|
25 |
RegistryKey SPPIDConverter = DOFTECH.OpenSubKey("SPPIDConverter", true); |
|
26 |
path = SPPIDConverter.GetValue("path") + "licenses.licx"; |
|
27 |
if (File.Exists(path)) |
|
153 |
string key = Settings.Default.LicenseKey; |
|
154 |
if (!string.IsNullOrEmpty(key)) |
|
28 | 155 |
{ |
29 |
string data = File.ReadAllText(path); |
|
30 |
data = Crypt.decrypt(data, "dof1073#"); |
|
31 |
if (data.StartsWith("Date:")) |
|
156 |
key = Crypt.decrypt(key, "dof1073#"); |
|
157 |
} |
|
158 |
else |
|
159 |
{ |
|
160 |
key = "dummy"; // must fail |
|
161 |
} |
|
162 | ||
163 |
if (!CheckLicense(key)) |
|
164 |
{ |
|
165 |
var dlg = new LicenseForm(); |
|
166 |
if (dlg.ShowDialog() == DialogResult.OK) |
|
32 | 167 |
{ |
33 |
data = data.Remove(0, "Date:".Length); |
|
168 |
key = dlg.key; |
|
169 |
} |
|
34 | 170 | |
35 |
if (string.IsNullOrEmpty(data)) |
|
36 |
{ |
|
37 |
DateTime dataTime = new DateTime(2000, 1, 1); |
|
38 |
DateTime currentTime = DateTime.Now; |
|
39 |
if (DateTime.TryParse(Converter.SPPID.Properties.Resources.lic, out dataTime) && DateTime.Compare(dataTime.AddDays(150), currentTime) >= 0) |
|
40 |
result = true; |
|
41 |
} |
|
171 |
if (CheckLicense(key)) |
|
172 |
{ |
|
173 |
Settings.Default.LicenseKey = Crypt.encrypt(key, "dof1073#"); |
|
174 |
Settings.Default.Save(); |
|
175 | ||
176 |
result = true; |
|
177 |
} |
|
178 |
} |
|
179 |
else |
|
180 |
{ |
|
181 |
result = true; |
|
182 |
} |
|
183 |
} |
|
184 |
catch (Exception ex) |
|
185 |
{ |
|
186 | ||
187 |
} |
|
188 | ||
189 |
return result; |
|
190 |
} |
|
191 | ||
192 |
public static bool CheckLicense(string key) |
|
193 |
{ |
|
194 |
try |
|
195 |
{ |
|
196 |
const string secret = "795hi#(7qq5&p#f3ysa#n-449h8g_n95olca)b%t23s7!w%v0m"; |
|
197 | ||
198 |
if (key != "dummy") |
|
199 |
{ |
|
200 |
IJsonSerializer serializer = new JsonNetSerializer(); |
|
201 |
var provider = new UtcDateTimeProvider(); |
|
202 |
IJwtValidator validator = new JwtValidator(serializer, provider); |
|
203 |
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); |
|
204 |
IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric |
|
205 |
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm); |
|
206 |
var json = decoder.Decode(key, secret, verify: true); |
|
207 |
var licenseDto = JsonConvert.DeserializeObject<LicenseDTO>(json); |
|
208 | ||
209 |
if (NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString() != licenseDto.mac) |
|
210 |
{ |
|
211 |
return false; |
|
42 | 212 |
} |
43 |
else if (data.StartsWith("Absolute:")) |
|
213 | ||
214 |
string url = licenseDto.api; |
|
215 |
string responseText = string.Empty; |
|
216 | ||
217 |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
|
218 |
request.Method = "GET"; |
|
219 |
request.Timeout = 5 * 1000; |
|
220 |
request.ContentType = "application/json"; |
|
221 |
request.Headers.Add("x-auth-token:" + key); |
|
222 | ||
223 |
bool validKey = true; |
|
224 | ||
225 |
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) |
|
44 | 226 |
{ |
45 |
data = data.Remove(0, "Absolute:".Length); |
|
46 |
string macAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString(); |
|
47 |
if (string.IsNullOrEmpty(data)) |
|
227 |
HttpStatusCode status = resp.StatusCode; |
|
228 |
Console.WriteLine(status); |
|
229 | ||
230 |
Stream respStream = resp.GetResponseStream(); |
|
231 |
using (StreamReader sr = new StreamReader(respStream)) |
|
48 | 232 |
{ |
49 |
data = "Absolute:" + macAddress; |
|
50 |
data = Crypt.encrypt(data, "dof1073#"); |
|
51 |
File.WriteAllText(path, data); |
|
52 |
result = true; |
|
233 |
responseText = sr.ReadToEnd(); |
|
234 |
var licenseReturn = JsonConvert.DeserializeObject<LicenseReturn>(responseText); |
|
235 | ||
236 |
if (!licenseReturn.success) |
|
237 |
{ |
|
238 |
MessageBox.Show(licenseReturn.msg, "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
|
239 |
validKey = false; |
|
240 |
} |
|
53 | 241 |
} |
54 |
else if (macAddress == data) |
|
55 |
result = true; |
|
56 | 242 |
} |
243 | ||
244 |
return validKey; |
|
57 | 245 |
} |
58 | 246 |
} |
59 | 247 |
catch (Exception ex) |
... | ... | |
61 | 249 | |
62 | 250 |
} |
63 | 251 | |
64 |
return result;
|
|
252 |
return false;
|
|
65 | 253 |
} |
66 | 254 | |
67 | 255 |
public static string SHA256Hash(string data) |
내보내기 Unified diff