Skip to content

Commit

Permalink
Version 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Crypto-Python committed Jun 7, 2022
1 parent aaf54e4 commit 7815055
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 391 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased <https://github.com/Cryptnox-Software/cryptnoxpro/compare/v2.2.1...HEAD>`_
--------------------------------------------------------------------------------------

`2.3.0 <https://github.com/Cryptnox-Software/cryptnoxpro/compare/v2.2.2...2.3.0>`_ - 2022-06-07
------------------------------------------------------------------------------------------------

Added
^^^^^

- Remote mode for connecting over a port

Changed
^^^^^^^

Expand Down
466 changes: 250 additions & 216 deletions Pipfile.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ The later will also start if the executable is called from a GUI, like Windows E

Remote mode
^^^^^^^^^^^
While running the application without arguments starts a local mode, a port number can be provided as argument to start remote mode.
This enables the application to connect to an interface on the same machine via the provided port.
While running the application without arguments starts a local mode, a port number can be provided
as argument to start remote mode. This enables the application to connect to an interface on the
same machine via the provided port.

An example below:

Expand Down
2 changes: 1 addition & 1 deletion cryptnoxpro/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "2.2.2"
__version__ = "2.3.0"
14 changes: 8 additions & 6 deletions cryptnoxpro/command/helper/cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

from . import security
from .ui import print_warning
from ... import config
try:
from ... import config
except ImportError:
import config


class ExitException(Exception):
Expand Down Expand Up @@ -59,7 +62,6 @@ def __getitem__(self, key: int) -> cryptnoxpy.Card:
self._remove_card(card.serial_number)

self.refresh()
card = None

try:
card = self._cards_by_index[key]
Expand All @@ -74,7 +76,7 @@ def __getitem__(self, key: int) -> cryptnoxpy.Card:
def __len__(self):
return len(self._cards)

def refresh(self,remote: bool = False) -> None:
def refresh(self, remote: bool = False) -> None:
index = 0

while True:
Expand All @@ -90,7 +92,7 @@ def refresh(self,remote: bool = False) -> None:
self._remove_card(card.serial_number)

try:
self._open_card(index,remote)
self._open_card(index, remote)
except cryptnoxpy.ReaderException:
break
except cryptnoxpy.CryptnoxException:
Expand Down Expand Up @@ -164,8 +166,8 @@ def _index(self, serial_number: int) -> int:
return index
raise ValueError

def _open_card(self, index: int,remote: bool = False) -> cryptnoxpy.Card:
connection = cryptnoxpy.Connection(index, self.debug,config._REMOTE_CONNECTIONS,remote)
def _open_card(self, index: int, remote: bool = False) -> cryptnoxpy.Card:
connection = cryptnoxpy.Connection(index, self.debug, config.REMOTE_CONNECTIONS, remote)
card = cryptnoxpy.factory.get_card(connection, self.debug)
self._cards[card.serial_number] = self._cards_by_index[index] = card

Expand Down
4 changes: 1 addition & 3 deletions cryptnoxpro/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
from .command.helper import helper_methods

_CONFIGURATION = {}
_REMOTE_CONNECTIONS = []
_REMOTE = False
_STOP_THREADS = False
REMOTE_CONNECTIONS = []


def get_default_configuration() -> Dict:
Expand Down
78 changes: 44 additions & 34 deletions cryptnoxpro/interactive_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
Module for application that behaves as command line interface
"""
import shutil
import socket
import sys
import traceback
import socket
from pathlib import Path
from typing import List
try:
from . import config
except:
import config

import argparse
import cryptnoxpy

try:
import config
import enums
from command.options import options
from command.helper import security
Expand All @@ -26,6 +23,7 @@
TimeoutException
)
except ImportError:
from . import config
from . import enums
from .command.options import options
from .command.helper import security
Expand Down Expand Up @@ -209,8 +207,7 @@ class InteractiveCli:
Application running a command line interface.
:param str version: Version of the application the interface should return
:param bool debug: Print out debug information in regards to the
communication to the card
:param bool debug: Print out debug information in regard to the communication to the card
:param bool remote: Determines whether the application should use remote feature
or look for local readers
"""
Expand All @@ -220,9 +217,8 @@ class ExitException(Exception):
Exception to handle when the user requests to exit the application.
"""

def __init__(self, version: str, card_type: int = 0,debug: bool = False,port: int = None):
def __init__(self, version: str, debug: bool = False, port: int = None):
self.version = version
self.card_type = card_type
self.debug: bool = debug
self.port: int = port

Expand Down Expand Up @@ -266,45 +262,59 @@ def run(self) -> int:

return 0

def _client(self):
if not self.port:
return None

print(f"Found port {self.port} defined, searching remotely.")
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server = socket.gethostbyname(socket.gethostname() + ".local")
client.connect((server, self.port))
except Exception as error:
client = None
print(f'Remote card connection not found: {error}')
else:
config.REMOTE_CONNECTIONS.append(client)

return client

def _close_client(self, client):
if not self.port or not client:
return

message = '!Close'.encode('utf-8')
msg_length = len(message)
send_length = str(msg_length).encode('utf-8')
send_length += b' ' * (64 - len(send_length))

try:
client.send(send_length)
client.send(message)
client.close()
except Exception as e:
print(f'Probably no socket to close: {e}')

def _run(self):
print("Loading cards...")
if self.port:
print(f"Found port {self.port} defined, searching remotely.")
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server = socket.gethostbyname(socket.gethostname() + ".local")
client.connect((server,self.port))
config._REMOTE_CONNECTIONS.append(client)
except Exception as e:
print(f'Remote card connection not found: {e}')
client = self._client()

try:
self._cards.refresh(self.port!=None)
self._cards.refresh(self.port and client is not None)
self._card_info = list(self._cards.values())[0].info
except IndexError:
pass

self._cards.print_card_list(show_warnings=True, print_with_one_card=True)
print("\n\nType help for list of commands.\n\n")
print("\n\nWith any input that is requested from you, to exit the current command "
"type: exit \n\n")
print("\n\nWith any input that is requested from you, to exit the current command type: exit \n\n")

while True:
try:
self._process_command()
except InteractiveCli.ExitException:
if self.port:
try:
message = '!Close'.encode('utf-8')
msg_length = len(message)
send_length = str(msg_length).encode('utf-8')
send_length += b' ' * (64 - len(send_length))
client.send(send_length)
client.send(message)
client.close()
except Exception as e:
print(f'Probably no socket to close: {e}')
self._close_client(client)
break

def is_valid_subcommand(self, new_subcommand: List[str],
Expand Down Expand Up @@ -425,7 +435,7 @@ def _run_command(self, args: argparse.Namespace, to_always_run: List = None) ->

if not self._card_info and not always_run:
try:
self._cards.refresh(self.port!=None)
self._cards.refresh(self.port != None)
self._card_info = list(self._cards.values())[0].info
except IndexError:
print("No cards found.\n")
Expand Down
7 changes: 4 additions & 3 deletions cryptnoxpro/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""
Command line interface for Cryptnox Cards
"""
import lazy_import
import sys

import argparse
import lazy_import

try:
from __init__ import __version__
Expand Down Expand Up @@ -44,7 +44,8 @@ def get_parser() -> argparse.ArgumentParser:

parser.add_argument("-v", "--version", action="version", version=f"Cryptnox Pro {__version__}")
parser.add_argument("--verbose", action="store_true", help="Turn on logging")
parser.add_argument('--port', nargs='?', type=int, default=None, help='Define port to enable remote feature')
parser.add_argument('--port', nargs='?', type=int, default=None,
help='Define port to enable remote feature')
serial_index_parser = parser.add_mutually_exclusive_group()
serial_index_parser.add_argument("-s", "--serial", type=int,
help="Serial number of the card to be used for the command")
Expand Down Expand Up @@ -74,7 +75,7 @@ def main() -> int:
result = factory.command(args).execute()
else:
print(f'Port: {args.port}')
result = interactive_cli.InteractiveCli(__version__, args.verbose,port=args.port).run()
result = interactive_cli.InteractiveCli(__version__, args.verbose, args.port).run()

return result

Expand Down
Loading

0 comments on commit 7815055

Please sign in to comment.