-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_legal.py
108 lines (96 loc) · 3.39 KB
/
check_legal.py
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
import os
import sys
import base64
import subprocess
from pathlib import Path
class CheckLegalUse:
def __init__(self,
filename,
gist_url,
dir_name='punch',
n=1,
):
import FreeCAD
freecad_dir = Path(FreeCAD.ConfigGet("UserAppData"))
if not freecad_dir.exists():
freecad_dir.mkdir()
application_dir = freecad_dir / dir_name
if not application_dir.exists():
application_dir.mkdir()
self.filename = application_dir / filename
self.gist_url = gist_url
self.n = n
def allowed_to_continue(self):
if sys.platform == "win32":
if not self.is_registered:
self.serial = str(subprocess.check_output("wmic csproduct get uuid")).split("\\r\\r\\n")[1].split()[0]
if not internet():
return False, 'INTERNET'
if not self.serial_number(self.serial):
return False, 'SERIAL'
else:
self.register()
return True, 'REGISTERED'
return True, ''
return True, ''
def initiate(self):
if not self.filename.exists():
with open(self.filename, 'wb') as f:
b = base64.b64encode('0-0'.encode('utf-8'))
f.write(b)
@property
def is_registered(self):
if not Path(self.filename).exists():
self.initiate()
return True
else:
text = self.get_registered_numbers()
if text[0] == 1 or text[1] <= self.n:
return True
else:
return False
def get_registered_numbers(self):
if not Path(self.filename).exists():
self.initiate()
with open(self.filename, 'rb') as f:
b = f.read()
text = base64.b64decode(b).decode('utf-8').split('-')
return int(text[0]), int(text[1])
def add_using_feature(self):
if not Path(self.filename).exists():
self.initiate()
with open(self.filename, 'rb') as f:
b = f.read()
text = base64.b64decode(b).decode('utf-8').split('-')
text[1] = str(int(text[1]) + 1)
text = '-'.join(*[text])
with open(self.filename, 'wb') as f:
b = base64.b64encode(text.encode('utf-8'))
f.write(b)
return
def register(self):
if not Path(self.filename).exists():
self.initiate()
with open(self.filename, 'rb') as f:
b = f.read()
text = base64.b64decode(b).decode('utf-8').split('-')
text[0] = '1'
text = '-'.join([*text])
with open(self.filename, 'wb') as f:
b = base64.b64encode(text.encode('utf-8'))
f.write(b)
def serial_number(self, serial):
import urllib.request
response = urllib.request.urlopen(self.gist_url)
data = response.read() # a `bytes` object
text = data.decode('utf-8')
return serial in text
def internet(host="8.8.8.8", port=53, timeout=3):
import socket
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
# print(ex.message)
return False