Skip to content

Commit

Permalink
Merge pull request #19 from cryptlex/borrowing-develop
Browse files Browse the repository at this point in the history
Borrowing develop
  • Loading branch information
ahmad-kemsan authored Aug 6, 2024
2 parents 1cd54fc + 38924a9 commit 03b67c2
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cryptlex/lexfloatclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from cryptlex.lexfloatclient.lexfloatclient import LexFloatClient, LexFloatStatusCodes, LexFloatClientException
from cryptlex.lexfloatclient.lexfloatclient import LexFloatClient, LexFloatStatusCodes, LexFloatClientException, PermissionFlags
129 changes: 128 additions & 1 deletion cryptlex/lexfloatclient/lexfloatclient.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import ctypes
import json
from cryptlex.lexfloatclient import lexfloatclient_native as LexFloatClientNative
from cryptlex.lexfloatclient.lexfloatstatus_codes import LexFloatStatusCodes
from cryptlex.lexfloatclient.lexfloatclient_exception import LexFloatClientException

callback_list = []

class PermissionFlags:
LF_USER = 10
LF_ALL_USERS = 11

class HostLicenseMeterAttribute(object):
def __init__(self, name, allowed_uses, total_uses, gross_uses):
Expand All @@ -19,6 +23,11 @@ def __init__(self, name, enabled, data):
self.enabled = enabled
self.data = data

class HostConfig(object):
def __init__(self, max_offline_lease_duration):
self.max_offline_lease_duration = max_offline_lease_duration


class LexFloatClient:
@staticmethod
def SetHostProductId(product_id):
Expand Down Expand Up @@ -100,6 +109,30 @@ def SetFloatingClientMetadata(key, value):
cstring_key, cstring_value)
if LexFloatStatusCodes.LF_OK != status:
raise LexFloatClientException(status)

@staticmethod
def SetPermissionFlag(flag):
"""Sets the permission flag.
This function must be called on every start of your program after SetHostProductId()
function in case the application allows borrowing of licenses or system wide activation.
Args:
flags : depending on your application's requirements, choose one of
the following values: LF_USER, LF_ALL_USERS.
LF_USER: This flag indicates that the application does not require
admin or root permissions to run.
LF_ALL_USERS: This flag is specifically designed for Windows and should be used
for system-wide activations.
Raises:
LexFloatClientException
"""
status = LexFloatClientNative.SetPermissionFlag(flag)
if LexFloatStatusCodes.LF_OK != status:
raise LexFloatClientException(status)

@staticmethod
def GetFloatingClientLibraryVersion():
Expand All @@ -117,7 +150,30 @@ def GetFloatingClientLibraryVersion():
status = LexFloatClientNative.GetFloatingClientLibraryVersion(buffer,buffer_size)
if status != LexFloatStatusCodes.LF_OK:
raise LexFloatClientException(status)
return LexFloatClientNative.byte_to_string(buffer.value)
return LexFloatClientNative.byte_to_string(buffer.value)

@staticmethod
def GetHostConfig():
"""This function sends a network request to LexFloatServer to get the configuration details.
Raises:
LexFloatClientException
Returns:
HostConfig: host configuration.
"""
buffer_size = 1024
buffer = LexFloatClientNative.get_ctype_string_buffer(buffer_size)
status = LexFloatClientNative.GetHostConfig(buffer, buffer_size)
if status == LexFloatStatusCodes.LF_OK:
host_config_json = LexFloatClientNative.byte_to_string(buffer.value)
if not host_config_json.strip():
return None
else:
host_config = json.loads(host_config_json)
return HostConfig(host_config["maxOfflineLeaseDuration"])
else:
raise LexFloatClientException(status)

@staticmethod
def GetHostProductVersionName():
Expand Down Expand Up @@ -244,6 +300,24 @@ def GetHostLicenseExpiryDate():
return expiry_date.value
else:
raise LexFloatClientException(status)

@staticmethod
def GetFloatingClientLeaseExpiryDate():
"""Gets the lease expiry date timestamp of the floating client.
Raises:
LexFloatClientException
Returns:
int: the timestamp
"""
leaseExpiryDate = ctypes.c_uint()
status = LexFloatClientNative.GetFloatingClientLeaseExpiryDate(
ctypes.byref(leaseExpiryDate))
if status == LexFloatStatusCodes.LF_OK:
return leaseExpiryDate.value
else:
raise LexFloatClientException(status)

@staticmethod
def GetFloatingClientMeterAttributeUses(name):
Expand All @@ -266,6 +340,45 @@ def GetFloatingClientMeterAttributeUses(name):
return uses.value
else:
raise LexFloatClientException(status)

@staticmethod
def GetFloatingLicenseMode():
"""Gets the mode of the floating license (online or offline).
Raises:
LexActivatorException
Returns:
ActivationMode: mode of floating license.
"""
buffer_size = 256
buffer = LexFloatClientNative.get_ctype_string_buffer(buffer_size)
status = LexFloatClientNative.GetFloatingLicenseMode(buffer,buffer_size)
if status != LexFloatStatusCodes.LF_OK:
raise LexFloatClientException(status)
return LexFloatClientNative.byte_to_string(buffer.value)

@staticmethod
def GetFloatingClientMetadata(key):
"""Gets the value of the floating client metadata.
Args:
key (str): metadata key to retrieve the value
Raises:
LexFloatClientException
Returns:
str: value of the floating client metadata
"""
cstring_key = LexFloatClientNative.get_ctype_string(key)
buffer_size = 4096
buffer = LexFloatClientNative.get_ctype_string_buffer(buffer_size)
status = LexFloatClientNative.GetFloatingClientMetadata(
cstring_key, buffer, buffer_size)
if status != LexFloatStatusCodes.LF_OK:
raise LexFloatClientException(status)
return LexFloatClientNative.byte_to_string(buffer.value)

@staticmethod
def RequestFloatingLicense():
Expand All @@ -278,6 +391,20 @@ def RequestFloatingLicense():
if LexFloatStatusCodes.LF_OK != status:
raise LexFloatClientException(status)

@staticmethod
def RequestOfflineFloatingLicense(lease_duration):
"""Sends the request to lease the license from the LexFloatServer for offline usage.
Args:
leaseDuration (int): seconds for which the lease should be obtained.
Raises:
LexFloatClientException
"""
status = LexFloatClientNative.RequestOfflineFloatingLicense(lease_duration)
if LexFloatStatusCodes.LF_OK != status:
raise LexFloatClientException(status)

@staticmethod
def DropFloatingLicense():
"""Sends the request to the LexFloatServer to free the license.
Expand Down
16 changes: 16 additions & 0 deletions cryptlex/lexfloatclient/lexfloatclient_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ def get_error_message(code):
return 'The server license has been suspended.'
if code == LexFloatStatusCodes.LF_E_SERVER_LICENSE_GRACE_PERIOD_OVER:
return 'The grace period for server license is over.'
if code == LexFloatStatusCodes.LF_E_SYSTEM_PERMISSION:
return 'Insufficient system permissions.'
if code == LexFloatStatusCodes.LF_E_INVALID_PERMISSION_FLAG:
return 'Invalid permission flag.'
if code == LexFloatStatusCodes.LF_E_OFFLINE_FLOATING_LICENSE_NOT_ALLOWED:
return 'Offline floating license is not allowed for per-instance leasing strategy.'
if code == LexFloatStatusCodes.LF_E_MAX_OFFLINE_LEASE_DURATION_EXCEEDED:
return 'Maximum offline lease duration exceeded.'
if code == LexFloatStatusCodes.LF_E_ALLOWED_OFFLINE_FLOATING_CLIENTS_LIMIT_REACHED:
return 'Allowed offline floating clients limit reached.'
if code == LexFloatStatusCodes.LF_E_WMIC:
return "Fingerprint couldn't be generated because Windows Management Instrumentation (WMI) service has been disabled. This error is specific to Windows only."
if code == LexFloatStatusCodes.LF_E_MACHINE_FINGERPRINT:
return 'Machine fingerprint has changed since activation.'
if code == LexFloatStatusCodes.LF_E_PROXY_NOT_TRUSTED:
return 'Request blocked due to untrusted proxy.'
return 'Unknown error!'
24 changes: 24 additions & 0 deletions cryptlex/lexfloatclient/lexfloatclient_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def byte_to_string(input):
SetFloatingClientMetadata.argtypes = [CSTRTYPE, CSTRTYPE]
SetFloatingClientMetadata.restype = c_int

SetPermissionFlag = library.SetPermissionFlag
SetPermissionFlag.argtypes = [c_uint32]
SetPermissionFlag.restype = c_int

GetFloatingClientLibraryVersion = library.GetFloatingClientLibraryVersion
GetFloatingClientLibraryVersion.argtypes = [STRTYPE, c_uint32]
GetFloatingClientLibraryVersion.restype = c_int
Expand All @@ -139,10 +143,18 @@ def byte_to_string(input):
GetHostProductVersionDisplayName.argtypes = [STRTYPE, c_uint32]
GetHostProductVersionDisplayName.restype = c_int

GetFloatingLicenseMode = library.GetFloatingLicenseMode
GetFloatingLicenseMode.argtypes = [STRTYPE, c_uint32]
GetFloatingLicenseMode.restype = c_int

GetHostProductVersionFeatureFlag = library.GetHostProductVersionFeatureFlag
GetHostProductVersionFeatureFlag.argtypes = [CSTRTYPE, POINTER(c_uint32), STRTYPE, c_uint32]
GetHostProductVersionFeatureFlag.restype = c_int

GetHostConfig = library.GetHostConfigInternal
GetHostConfig.argtypes = [STRTYPE, c_uint32]
GetHostConfig.restype = c_int

GetHostLicenseMetadata = library.GetHostLicenseMetadata
GetHostLicenseMetadata.argtypes = [CSTRTYPE, STRTYPE, c_uint32]
GetHostLicenseMetadata.restype = c_int
Expand All @@ -155,14 +167,26 @@ def byte_to_string(input):
GetHostLicenseExpiryDate.argtypes = [POINTER(c_uint32)]
GetHostLicenseExpiryDate.restype = c_int

GetFloatingClientLeaseExpiryDate = library.GetFloatingClientLeaseExpiryDate
GetFloatingClientLeaseExpiryDate.argtypes = [POINTER(c_uint32)]
GetFloatingClientLeaseExpiryDate.restype = c_int

GetFloatingClientMeterAttributeUses = library.GetFloatingClientMeterAttributeUses
GetFloatingClientMeterAttributeUses.argtypes = [CSTRTYPE, POINTER(c_uint32)]
GetFloatingClientMeterAttributeUses.restype = c_int

GetFloatingClientMetadata = library.GetFloatingClientMetadata
GetFloatingClientMetadata.argtypes = [CSTRTYPE, STRTYPE, c_uint32]
GetFloatingClientMetadata.restype = c_int

RequestFloatingLicense = library.RequestFloatingLicense
RequestFloatingLicense.argtypes = []
RequestFloatingLicense.restype = c_int

RequestOfflineFloatingLicense = library.RequestOfflineFloatingLicense
RequestOfflineFloatingLicense.argtypes = [c_uint32]
RequestOfflineFloatingLicense.restype = c_int

DropFloatingLicense = library.DropFloatingLicense
DropFloatingLicense.argtypes = []
DropFloatingLicense.restype = c_int
Expand Down
18 changes: 17 additions & 1 deletion cryptlex/lexfloatclient/lexfloatstatus_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,26 @@ class LexFloatStatusCodes:
LF_E_METER_ATTRIBUTE_USES_LIMIT_REACHED = 56

LF_E_PRODUCT_VERSION_NOT_LINKED = 57

LF_E_FEATURE_FLAG_NOT_FOUND = 58

LF_E_SYSTEM_PERMISSION = 59

LF_E_IP = 60

LF_E_INVALID_PERMISSION_FLAG = 61

LF_E_OFFLINE_FLOATING_LICENSE_NOT_ALLOWED = 62

LF_E_MAX_OFFLINE_LEASE_DURATION_EXCEEDED = 63

LF_E_ALLOWED_OFFLINE_FLOATING_CLIENTS_LIMIT_REACHED = 64

LF_E_WMIC = 65

LF_E_MACHINE_FINGERPRINT = 66

LF_E_PROXY_NOT_TRUSTED = 67

LF_E_CLIENT = 70

Expand Down

0 comments on commit 03b67c2

Please sign in to comment.