Skip to content

Commit

Permalink
Merge pull request #423 from uzlonewolf/misc
Browse files Browse the repository at this point in the history
Crypto lib changes and misc fixes
  • Loading branch information
jasonacox authored Nov 15, 2023
2 parents b18c444 + 6194d0a commit 9953f39
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/contrib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
python -VV
python -m site
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade pycryptodome requests colorama
python -m pip install --upgrade cryptography requests colorama
- name: "Run testcontrib.py on ${{ matrix.python-version }}"
run: "python -m testcontrib.py"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
python -VV
python -m site
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade pycryptodome requests colorama
python -m pip install --upgrade cryptography requests colorama
- name: "Run test.py on ${{ matrix.python-version }}"
run: "python -m test.py"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TinyTuya supports python versions 2.7 and 3.x (recommended).
python -m pip install tinytuya
```

Pip will attempt to install `pycryptodome`, `requests` and `colorama` if not already installed.
Pip will attempt to install `cryptography`, `requests` and `colorama` if not already installed.

## Tuya Device Preparation

Expand Down Expand Up @@ -419,7 +419,7 @@ print( json.dumps(r, indent=2) )
### Encryption Notes
Tuya devices use AES encryption which is not available in the Python standard library. **PyCryptodome** is recommended and installed by default. Other options include **PyCrypto** and **pyaes**.
Tuya devices use AES encryption which is not available in the Python standard library. **PyCA/cryptography** is recommended and installed by default. Other options include **PyCryptodome** , **PyCrypto** and **pyaes**.
* Deprecation notice for pyaes: The pyaes library works for Tuya Protocol <= 3.4 but will not work for 3.5 devices. This is because pyaes does not support GCM which is required for v3.5 devices.
Expand Down
7 changes: 7 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# RELEASE NOTES

## v1.13.0 - Crypto Library Update

* PyPI 1.13.0
* Updates AESCipher() to make it a bit easier to add additional crypto libraries. It also adds pyca/cryptography as the default. By @uzlonewolf in https://github.com/jasonacox/tinytuya/pull/423
* Fixes issue with tinytuya.find_device() for v3.1 devices and the infinite loop in Contrib/IRRemoteControlDevice.py (Closes #403).
* Officially removes Python 2.7 support.

## v1.12.11 - Bug Fix for _get_socket()

* PyPI 1.12.11
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#
pycryptodome # Encryption - AES can also be provided via PyCrypto or pyaes
cryptography # Encryption - AES can also be provided via PyCryptodome or pyaes or pyca/cryptography
requests # Used for Setup Wizard - Tuya IoT Platform calls
colorama # Makes ANSI escape character sequences work under MS Windows.
8 changes: 2 additions & 6 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

import tinytuya

BUILD = "t8"
BUILD = "t9"

# Defaults
APIPORT = 8888
Expand Down Expand Up @@ -293,11 +293,7 @@ def tuyalisten(port):
gwId = dname = dkey = mac = ""
result = data
try:
result = data[20:-8]
try:
result = tinytuya.decrypt_udp(result)
except:
result = result.decode()
result = tinytuya.decrypt_udp( data )
result = json.loads(result)
#log.debug("Received valid UDP packet: %r", result)
ip = result["ip"]
Expand Down
31 changes: 25 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import setuptools
from pkg_resources import DistributionNotFound, get_distribution

from tinytuya import __version__

with open("README.md", "r") as fh:
long_description = fh.read()

INSTALL_REQUIRES = [
'requests', # Used for Setup Wizard - Tuya IoT Platform calls
'colorama', # Makes ANSI escape character sequences work under MS Windows.
]

CHOOSE_CRYPTO_LIB = [
'cryptography', # pyca/cryptography - https://cryptography.io/en/latest/
'pycryptodome', # PyCryptodome - https://pycryptodome.readthedocs.io/en/latest/
'pyaes', # pyaes - https://github.com/ricmoo/pyaes
'pycrypto', # PyCrypto - https://www.pycrypto.org/
]

pref_lib = CHOOSE_CRYPTO_LIB[0]
for cryptolib in CHOOSE_CRYPTO_LIB:
try:
get_distribution(cryptolib)
pref_lib = cryptolib
break
except DistributionNotFound:
pass

INSTALL_REQUIRES.append( pref_lib )

setuptools.setup(
name="tinytuya",
version=__version__,
Expand All @@ -15,13 +39,8 @@
long_description_content_type="text/markdown",
url='https://github.com/jasonacox/tinytuya',
packages=setuptools.find_packages(exclude=("sandbox",)),
install_requires=[
'pycryptodome', # Encryption - AES can also be provided via PyCrypto or pyaes
'requests', # Used for Setup Wizard - Tuya IoT Platform calls
'colorama', # Makes ANSI escape character sequences work under MS Windows.
],
install_requires=INSTALL_REQUIRES,
classifiers=[
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
Expand Down
24 changes: 9 additions & 15 deletions tinytuya/Contrib/IRRemoteControlDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,18 @@ def detect_control_type( self ):
self.study_end()
self.control_type = 0
status = self.status()
while status:
if status and 'dps' in status:
# original devices using DPS 201/202
if self.DP_SEND_IR in status['dps']:
log.debug( 'Detected control type 1' )
self.control_type = 1
break
# newer devices using DPS 1-13
elif self.DP_MODE in status['dps']:
log.debug( 'Detected control type 2' )
self.control_type = 2
break
while status and 'dps' in status:
# original devices using DPS 201/202
if self.DP_SEND_IR in status['dps']:
log.debug( 'Detected control type 1' )
self.control_type = 1
# newer devices using DPS 1-13
elif self.DP_MODE in status['dps']:
log.debug( 'Detected control type 2' )
self.control_type = 2
status = self._send_receive(None)
if not self.control_type:
log.warning( 'Detect control type failed! control_type= must be set manually' )
elif status:
# try and make sure no data is waiting to be read
status = self._send_receive(None)
self.set_socketTimeout( old_timeout )
self.set_socketPersistent( old_persist )

Expand Down
Loading

0 comments on commit 9953f39

Please sign in to comment.