AlexandrErohin / TP-Link-Archer-C6U

Python package for API access and management for TP-Link Routers. See supported routers list
GNU General Public License v3.0
43 stars 14 forks source link

Added Support for TP-link C1200 #10

Closed swwgames closed 7 months ago

swwgames commented 7 months ago

Added Support for TP-link C1200 The main difference is the login process, the C1200 doesn't expose its encryption keys.

AlexandrErohin commented 7 months ago

Thank you very much! I have one comment for encrypted password only

AlexandrErohin commented 7 months ago

Does this steps works for you to get the hashedPassword ?

  1. Go to the login page of your router. (default: 192.168.0.1)
  2. Type in the password you use to login into the password field.
  3. Click somewhere else on the page so that the password field is not selected anymore.
  4. Open the JavaScript console of your browser (usually by pressing F12 and then clicking on "Console").
  5. Type document.getElementById("login-password").value; or document.getElementById("pcPassword").value;, depending on your firmware version.
  6. Copy the returned value as password.
swwgames commented 7 months ago

yes that worked, document.getElementById("login-password").value; gave me the value. What do I need to do now?

AlexandrErohin commented 6 months ago

@swwgames Hi. Could you test please v3.4.0?

swwgames commented 6 months ago

no that version doesn't work. what i needed to change for it to work was:

class TplinkC1200Router(TplinkBaseRouter):
    def supports(self) -> bool:
        return True

    def authorize(self) -> None:
        if len(self.password) < 200:
            raise Exception('You need to use web encrypted password instead. Check the documentation!')

        url = '{}/cgi-bin/luci/;stok=/login?form=login'.format(self.host)

        response = requests.post(
            url,
            params={'operation': 'login', 'username': self.username, 'password': self.password},
            #headers={'Referer': self._login_referer, 'Content-Type': 'application/x-www-form-urlencoded'},
            timeout=self.timeout,
            verify=self._verify_ssl,
        )

Here i removed the headers. the _login_referer is empty so it did not work.

And Here i removed the &operation=read

class TplinkBaseRouter(AbstractRouter, TplinkRequest):
    def __init__(self, host: str, password: str, username: str = 'admin', logger: Logger = None,
                 verify_ssl: bool = True, timeout: int = 10) -> None:
        super().__init__(host, password, username, logger, verify_ssl, timeout)

        self._url_firmware = 'admin/firmware?form=upgrade&operation=read'
        self._url_wireless_stats = 'admin/wireless?form=statistics'
        self._url_ipv4_reservations = 'admin/dhcps?form=reservation&operation=load'
        self._url_ipv4_dhcp_leases = 'admin/dhcps?form=client&operation=load'

i also needed to change in the baserouter class in the get_status the operation from read to load.

        for item in self.request(self._url_wireless_stats, 'operation=load'):
            if item['mac'] not in devices:
                status.wifi_clients_total += 1
                type = self._map_wire_type(item.get('type'))
                devices[item['mac']] = Device(type, macaddress.EUI48(item['mac']), ipaddress.IPv4Address('0.0.0.0'), '')
            devices[item['mac']].packets_sent = item.get('txpkts')
            devices[item['mac']].packets_received = item.get('rxpkts')

first it was sending twice operation read, now it only sends operation load. and i get the data.

feel free to ask further questions.

AlexandrErohin commented 6 months ago

@swwgames Thank you! I have fixed it. Could you try v3.4.3 and let me know please?

swwgames commented 6 months ago

Yes it works!

AlexandrErohin commented 6 months ago

@swwgames Thank you!