|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +#--------------------------------------------------- |
| 5 | +# |
| 6 | +# Start of Extra Code |
| 7 | +# |
| 8 | +#--------------------------------------------------- |
| 9 | + |
| 10 | +# Extra import to protect dynamic library |
| 11 | +import pytransform |
| 12 | +from hashlib import md5 |
| 13 | + |
| 14 | +MD5SUM_PYTRANSFORM_PY = '46995aee690c412c8e65da764b892562' |
| 15 | +MD5SUM_PYTRANSFORM_SO = 'ca202268bbd76ffe7df10c9ef1edcb6c' |
| 16 | + |
| 17 | +# Extra import to check expired date by NTP |
| 18 | +from ntplib import NTPClient |
| 19 | +from time import mktime, strptime |
| 20 | + |
| 21 | +NTP_SERVER = 'europe.pool.ntp.org' |
| 22 | +EXPIRED_DATE = '20190202' |
| 23 | + |
| 24 | +def check_md5sum(filename, expected): |
| 25 | + print("Check md5sum for %s..." % filename) |
| 26 | + with open(filename, 'rb') as f: |
| 27 | + if not md5(f.read()).hexdigest() == expected: |
| 28 | + print("Check failed") |
| 29 | + sys.exit(1) |
| 30 | + print("Check OK.") |
| 31 | + |
| 32 | +def protect_pytransform(): |
| 33 | + # Be sure obfuscated script is not changed |
| 34 | + with open(__file__, 'r') as f: |
| 35 | + lines = f.readlines() |
| 36 | + if not ((len(lines[:4]) == 3) and |
| 37 | + (lines[0].strip() == 'from pytransform import pyarmor_runtime') and |
| 38 | + (lines[1].strip() == 'pyarmor_runtime()') and |
| 39 | + (lines[2].startswith('__pyarmor__'))): |
| 40 | + sys.eixt(1) |
| 41 | + |
| 42 | + # Be sure `pytransform.py` is not changed |
| 43 | + check_md5sum(pytransform.__file__, MD5SUM_PYTRANSFORM_PY) |
| 44 | + # Be sure `_pytransform.so` is not changed |
| 45 | + check_md5sum(pytransform._pytransform._name, MD5SUM_PYTRANSFORM_SO) |
| 46 | + |
| 47 | +def check_expired_date_by_ntp(): |
| 48 | + print("Check expired date %s by %s ..." % (EXPIRED_DATE, NTP_SERVER)) |
| 49 | + c = NTPClient() |
| 50 | + response = c.request(NTP_SERVER, version=3) |
| 51 | + if response.tx_time > mktime(strptime(EXPIRED_DATE, '%Y%m%d')): |
| 52 | + print("The license has been expired") |
| 53 | + sys.exit(1) |
| 54 | + print("The license is not expired") |
| 55 | + |
| 56 | +def show_left_days_of_license(): |
| 57 | + try: |
| 58 | + rcode = pytransform.get_license_info()['CODE'] |
| 59 | + left_days = pytransform.get_expired_days() |
| 60 | + if left_days == -1: |
| 61 | + print('This license for %s is never expired' % rcode) |
| 62 | + else: |
| 63 | + print('This license %s will be expired in %d days' % (rcode, left_days)) |
| 64 | + except Exception as e: |
| 65 | + print(e) |
| 66 | + |
| 67 | +protect_pytransform() |
| 68 | +check_expired_date_by_ntp() |
| 69 | +show_left_days_of_license() |
| 70 | + |
| 71 | +#--------------------------------------------------- |
| 72 | +# |
| 73 | +# End of Extra Code |
| 74 | +# |
| 75 | +#--------------------------------------------------- |
| 76 | + |
| 77 | +# |
| 78 | +# Business Code |
| 79 | +# |
| 80 | +def hello(): |
| 81 | + print('Hello world!') |
| 82 | + |
| 83 | +def sum(a, b): |
| 84 | + return a + b |
| 85 | + |
| 86 | +def main(): |
| 87 | + hello() |
| 88 | + print('1 + 1 = %d' % sum(1, 1)) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
0 commit comments