tplinkrouterc6u 4.2.3__py3-none-any.whl → 5.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tplinkrouterc6u/client.py CHANGED
@@ -683,36 +683,100 @@ class TplinkC6V4Router(AbstractRouter):
683
683
 
684
684
 
685
685
  class TplinkC1200Router(TplinkBaseRouter):
686
+ username = ''
687
+ password = ''
688
+ _pwdNN = ''
689
+ _pwdEE = ''
690
+ _encryption = EncryptionWrapper()
691
+
686
692
  def supports(self) -> bool:
687
- return True
693
+ if len(self.password) > 125:
694
+ return False
695
+
696
+ try:
697
+ self._request_pwd()
698
+ return True
699
+ except ClientException:
700
+ return False
688
701
 
689
702
  def authorize(self) -> None:
690
- if len(self.password) < 200:
691
- raise Exception('You need to use web encrypted password instead. Check the documentation!')
703
+ if self._pwdNN == '':
704
+ self._request_pwd()
692
705
 
693
- url = '{}/cgi-bin/luci/;stok=/login?form=login'.format(self.host)
706
+ response = self._try_login()
694
707
 
708
+ is_valid_json = False
709
+ try:
710
+ response.json()
711
+ is_valid_json = True
712
+ except BaseException:
713
+ """Ignore"""
714
+
715
+ if is_valid_json is False or response.status_code == 403:
716
+ self._logged = False
717
+ self._request_pwd()
718
+ response = self._try_login()
719
+
720
+ data = response.text
721
+ try:
722
+ data = response.json()
723
+ data = self._decrypt_response(data)
724
+
725
+ self._stok = data[self._data_block]['stok']
726
+ regex_result = search(
727
+ 'sysauth=(.*);', response.headers['set-cookie'])
728
+ self._sysauth = regex_result.group(1)
729
+ self._logged = True
730
+
731
+ except Exception as e:
732
+ error = ("TplinkRouter - C1200 - {} - Cannot authorize! Error - {}; Response - {}"
733
+ .format(self.__class__.__name__, e, data))
734
+ if self._logger:
735
+ self._logger.error(error)
736
+ raise ClientException(error)
737
+
738
+ def _request_pwd(self) -> None:
739
+ url = '{}/cgi-bin/luci/;stok=/login?form=login'.format(self.host)
695
740
  response = post(
696
- url,
697
- params={'operation': 'login', 'username': self.username, 'password': self.password},
698
- headers=self._headers_login,
741
+ url, params={'operation': 'read'},
699
742
  timeout=self.timeout,
700
743
  verify=self._verify_ssl,
701
744
  )
702
745
 
703
746
  try:
704
- self._stok = response.json().get('data').get('stok')
705
- regex_result = search('sysauth=(.*);', response.headers['set-cookie'])
706
- self._sysauth = regex_result.group(1)
707
- self._logged = True
708
- self._smart_network = False
747
+ data = response.json()
748
+
749
+ args = data[self._data_block]['password']
750
+
751
+ self._pwdNN = args[0]
752
+ self._pwdEE = args[1]
709
753
 
710
754
  except Exception as e:
711
- error = "TplinkRouter - C1200 - Cannot authorize! Error - {}; Response - {}".format(e, response.text)
755
+ error = ('TplinkRouter - C1200 - {} - Unknown error for pwd! Error - {}; Response - {}'
756
+ .format(self.__class__.__name__, e, response.text))
712
757
  if self._logger:
713
758
  self._logger.error(error)
714
759
  raise ClientException(error)
715
760
 
761
+ def _try_login(self) -> Response:
762
+ url = '{}/cgi-bin/luci/;stok=/login?form=login'.format(self.host)
763
+
764
+ crypted_pwd = self._encryption.encrypt_password_C1200(self.password, self._pwdNN, self._pwdEE)
765
+
766
+ body = self._get_login_data(crypted_pwd)
767
+
768
+ return post(
769
+ url,
770
+ data=body,
771
+ headers=self._headers_login,
772
+ timeout=self.timeout,
773
+ verify=self._verify_ssl,
774
+ )
775
+
776
+ @staticmethod
777
+ def _get_login_data(crypted_pwd: str) -> str:
778
+ return 'operation=login&password={}'.format(crypted_pwd)
779
+
716
780
  def set_led(self, enable: bool) -> None:
717
781
  current_state = (self.request('admin/ledgeneral?form=setting&operation=read', 'operation=read')
718
782
  .get('enable', 'off') == 'on')
@@ -1340,4 +1404,7 @@ class TplinkRouterProvider:
1340
1404
  if router.supports():
1341
1405
  return router
1342
1406
 
1343
- return None
1407
+ raise ClientException(('Your router is not supported. Please add your router support to '
1408
+ 'https://github.com/AlexandrErohin/TP-Link-Archer-C6U'
1409
+ 'by implementing methods for AbstractRouter class'
1410
+ ))
@@ -64,6 +64,24 @@ class EncryptionWrapper:
64
64
  def _get_aes_string(self) -> str:
65
65
  return 'k={}&i={}'.format(self._key.decode(), self._iv.decode())
66
66
 
67
+ @staticmethod
68
+ def encrypt_password_C1200(password: str, nn: str, ee: str) -> str:
69
+ n = int(nn, 16)
70
+ e = int(ee, 16)
71
+ key = RSA.construct((n, e))
72
+
73
+ modulus_byte_length = (key.size_in_bits() + 7) // 8
74
+ password_bytes = password.encode('utf-8')
75
+ if len(password_bytes) > modulus_byte_length:
76
+ raise ValueError("Password too long for the RSA key size.")
77
+
78
+ padded_password = password_bytes.ljust(modulus_byte_length, b'\x00')
79
+ message_int = bytes_to_long(padded_password)
80
+ encrypted_int = pow(message_int, e, n)
81
+ encrypted_hex = format(encrypted_int, 'x').zfill(256)
82
+
83
+ return encrypted_hex
84
+
67
85
 
68
86
  class EncryptionWrapperMR:
69
87
  RSA_USE_PKCS_V1_5 = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tplinkrouterc6u
3
- Version: 4.2.3
3
+ Version: 5.0.0
4
4
  Summary: TP-Link Router API
5
5
  Home-page: https://github.com/AlexandrErohin/TP-Link-Archer-C6U
6
6
  Author: Alex Erohin
@@ -53,10 +53,6 @@ router = TplinkRouterProvider.get_client('http://192.168.0.1', 'password')
53
53
  # router = TplinkRouter('http://192.168.0.1', 'password')
54
54
  # You may also pass username if it is different and a logger to log errors as
55
55
  # router = TplinkRouter('http://192.168.0.1','password','admin2', Logger('test'))
56
- # If you have the TP-link C1200 V2 or similar, you can use the TplinkC1200Router class instead of the TplinkRouter class.
57
- # Remember that the password for this router is different, here you need to use the web encrypted password.
58
- # To get web encrypted password, read Web Encrypted Password section
59
- # router = TplinkC1200Router('http://192.168.0.1','WebEncryptedPassword', Logger('test'))
60
56
 
61
57
  try:
62
58
  router.authorize() # authorizing
@@ -86,16 +82,6 @@ finally:
86
82
  The TP-Link Web Interface only supports upto 1 user logged in at a time (for security reasons, apparently).
87
83
  So before action you need to authorize and after logout
88
84
 
89
- ### <a id="encrypted_pass">Web Encrypted Password</a>
90
- If you got exception - `You need to use web encrypted password instead. Check the documentation!`
91
- or you have TP-link C1200 V2 or similar router you need to get web encrypted password by these actions:
92
- 1. Go to the login page of your router. (default: 192.168.0.1).
93
- 2. Type in the password you use to login into the password field.
94
- 3. Click somewhere else on the page so that the password field is not selected anymore.
95
- 4. Open the JavaScript console of your browser (usually by pressing F12 and then clicking on "Console").
96
- 5. Type `document.getElementById("login-password").value;`
97
- 6. Copy the returned value as password and use it.
98
-
99
85
  ## Functions
100
86
  | Function | Args | Description | Return |
101
87
  |---|---|---|---|
@@ -246,8 +232,8 @@ or you have TP-link C1200 V2 or similar router you need to get web encrypted pas
246
232
  - Archer AX11000 V1
247
233
  - Archer BE800 v1.0
248
234
  - Archer BE805 v1.0
249
- - Archer C1200 (v1.0, v2.0) (You need to use [web encrypted password](#encrypted_pass))
250
- - Archer C2300 v1.0 (You need to use [web encrypted password](#encrypted_pass))
235
+ - Archer C1200 (v1.0, v2.0)
236
+ - Archer C2300 v1.0
251
237
  - Archer C6 (v2.0, v3.0)
252
238
  - Archer C6U v1.0
253
239
  - Archer C7 (v4.0, v5.0)
@@ -4,14 +4,14 @@ test/test_client_c1200.py,sha256=O6NK9uXIEb_vks6lI3g7j6q8TYJ9MSDFYzGA_wOBhOA,462
4
4
  test/test_client_deco.py,sha256=OqJ9e6_TcX60Ccqj_UH9AQ3Ty25hfdbtPvtzLnWpMn0,30464
5
5
  test/test_client_mr.py,sha256=znDA8PBxHOGKbZHzsyERbEEWjkP0U5KpV8Z-kaRnXIM,23046
6
6
  tplinkrouterc6u/__init__.py,sha256=EpcrPHORAk_m76WDHMLea_RzCPjqZYAs_774JZChLGs,410
7
- tplinkrouterc6u/client.py,sha256=dUV4jbx_r4RP_vjvUePxjaZ5LW1D1hJ9BtISWXlPIqM,51458
7
+ tplinkrouterc6u/client.py,sha256=CWWU_IXA33PADfxA3XEWeXGyDEonhPDaTlc4f3iMLq8,53434
8
8
  tplinkrouterc6u/dataclass.py,sha256=SuAOqKun_ThaeUEa8F9wi6qu4ucUatYMzebo39Kk10s,6617
9
- tplinkrouterc6u/encryption.py,sha256=2InKlHLQSWkcI8yP8EIGSG-bGXft_KU-bplGjcoeE-k,5546
9
+ tplinkrouterc6u/encryption.py,sha256=4HelTxzN6esMfDZRBt3m8bwB9Nj_biKijnCnrGWPWKg,6228
10
10
  tplinkrouterc6u/exception.py,sha256=PUTPsadxiylQhIRUMfkN1RWXh07cHmdav9Pdgxs3Lmw,90
11
11
  tplinkrouterc6u/helper.py,sha256=mbPkS_9GfOYMxEIx-q-xLlupR0-m_MDgCWQgx8xxOA8,172
12
12
  tplinkrouterc6u/package_enum.py,sha256=bqmnu4gQMEntMCgsya2R7dRcrSJIDPWMj8vNN6JxeME,1382
13
- tplinkrouterc6u-4.2.3.dist-info/LICENSE,sha256=YF6QR6Vjxcg5b_sYIyqkME7FZYau5TfEUGTG-0JeRK0,35129
14
- tplinkrouterc6u-4.2.3.dist-info/METADATA,sha256=v6KaqYCwRYqjwvMouDqkLgbp0_X9nz4y1K-qcMoTh4Y,13058
15
- tplinkrouterc6u-4.2.3.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
16
- tplinkrouterc6u-4.2.3.dist-info/top_level.txt,sha256=1iSCCIueqgEkrTxtQ-jiHe99jAB10zqrVdBcwvNfe_M,21
17
- tplinkrouterc6u-4.2.3.dist-info/RECORD,,
13
+ tplinkrouterc6u-5.0.0.dist-info/LICENSE,sha256=YF6QR6Vjxcg5b_sYIyqkME7FZYau5TfEUGTG-0JeRK0,35129
14
+ tplinkrouterc6u-5.0.0.dist-info/METADATA,sha256=z8zgcEfcEO358npxVgWcDCDDuHsEoKtusw428SU_mt0,11859
15
+ tplinkrouterc6u-5.0.0.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
16
+ tplinkrouterc6u-5.0.0.dist-info/top_level.txt,sha256=1iSCCIueqgEkrTxtQ-jiHe99jAB10zqrVdBcwvNfe_M,21
17
+ tplinkrouterc6u-5.0.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (75.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5