Skip to content

Commit

Permalink
added VPN stats
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrErohin committed Nov 18, 2024
1 parent 7312b23 commit 7a9fbfe
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 34 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
The best way would be great if you create Pull Request with a new class for your router witch extends [AbstractRouter](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client_abstract.py#L8)

For example
- [Class for routers XDR series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/xdr.py#L12)
- [Class for routers DECO series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/deco.py#L12)
- [Class for routers AX series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/api_cgi_bin.py#L223)
- [Class for routers AX series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/c6u#L223)
- [Class for routers MR and VR series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/mr.py#L368)
- [Class for routers EX series](https://github.com/AlexandrErohin/TP-Link-Archer-C6U/blob/main/tplinkrouterc6u/client/ex.py#L13)

39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ from tplinkrouterc6u import (
TplinkC5400XRouter,
TPLinkMRClient,
TPLinkEXClient,
TPLinkXDRClient,
TPLinkDecoClient,
Connection
)
Expand Down Expand Up @@ -85,14 +86,15 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
| get_ipv4_reservations | | Gets IPv4 reserved addresses (static) | [[IPv4Reservation]](#IPv4Reservation) |
| get_ipv4_dhcp_leases | | Gets IPv4 addresses assigned via DHCP | [[IPv4DHCPLease]](#IPv4DHCPLease) |
| set_wifi | wifi: [Connection](#connection), enable: bool | Allow to turn on/of 4 wifi networks | |
| reboot | | reboot router |
| authorize | | authorize for actions |
| logout | | logout after all is done |
| send_sms | phone_number: str, message: str | Send sms for LTE routers | |
| send_ussd | command: str | Send USSD command for LTE routers | str |
| get_sms | | Get sms messages from the first page for LTE routers | [[SMS]](#sms) |
| set_sms_read | sms: [SMS](#sms) | Set sms message read from the first page for LTE routers | |
| delete_sms | sms: [SMS](#sms) | Delete sms message from the first page for LTE routers | |
| reboot | | reboot router |
| authorize | | authorize for actions |
| logout | | logout after all is done |
| get_lte_status | | Get lte info for LTE routers | [LTEStatus](#lte_status) |

## Dataclass
### <a id="firmware">Firmware</a>
Expand Down Expand Up @@ -147,6 +149,7 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
| packets_received | total packets received | int, None |
| down_speed | download speed | int, None |
| up_speed | upload speed | int, None |
| signal | Signal strength | int, None |

### <a id="IPv4Reservation">IPv4Reservation</a>
| Field | Description | Type |
Expand Down Expand Up @@ -193,6 +196,14 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
| lan_ipv4_netmask_address | router LAN gateway IP netmask | ipaddress |
| remote | router remote | bool, None |

### <a id="vpn_status">VPNStatus</a>
| Field | Description | Type |
| --- |---|---|
| openvpn_enable | OpenVPN is enabled | bool |
| pptpvpn_enable | PPTPVPN is enabled | bool |
| openvpn_clients_total | OpenVPN clients connected | int |
| pptpvpn_clients_total | PPTPVPN clients connected | int |

### <a id="sms">SMS</a>
| Field | Description | Type |
| --- |---|---|
Expand All @@ -202,6 +213,23 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
| received_at| received datetime | datetime |
| unread| is message unread | bool |

### <a id="lte_status">LTEStatus</a>
| Field | Description | Type |
| --- |---|---|
| enable | is enabled | int |
| connect_status | connect status | int |
| network_type | network type | int |
| sim_status | sim status | int |
| total_statistics | total statistics in bytes | int |
| cur_rx_speed | current download speed in bytes per second | int |
| cur_tx_speed | current upload speed in bytes per second | int |
| sms_unread_count | sms unread amount | int |
| sig_level | signal level | int |
| rsrp | RSRP | int |
| rsrq | RSRQ | int |
| snr | SNR | int |
| isp_name | ISP name | str |

## Enum
### <a id="connection">Connection</a>
- Connection.HOST_2G - host wifi 2.4G
Expand All @@ -215,6 +243,10 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
- Connection.IOT_6G - IoT wifi 6G
- Connection.WIRED - Wired

### <a id="vpn">VPN</a>
- VPN.OPEN_VPN
- VPN.PPTP_VPN

## <a id="supports">Supported routers</a>
### Fully tested Hardware Versions
- Archer A7 V5
Expand Down Expand Up @@ -269,6 +301,7 @@ or you have TP-link C5400X or similar router you need to get web encrypted passw
- TL-MR105
- TL-MR6400 (v5, v5.3)
- TL-MR6500v
- TL-XDR3010 V2
- TL-WA3001 v1.0

### Not fully tested Hardware Versions
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tplinkrouterc6u",
version="5.1.0",
version="5.2.0",
author="Alex Erohin",
author_email="[email protected]",
description="TP-Link Router API",
Expand Down
3 changes: 2 additions & 1 deletion test/test_client.py → test/test_client_c6u.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def test_get_status_with_game_accelerator(self) -> None:
{"mac": "54:b3:a2:f7:be:ea", "deviceTag":"iot_5G", "isGuest":false, "ip":"192.168.1.188",
"deviceName":"name3"},
{"mac": "3c:ae:e1:83:94:9d", "deviceTag":"iot_6G", "isGuest":false, "ip":"192.168.1.189",
"deviceName":"name4"}
"deviceName":"name4", "signal": -52}
],
"timeout": false,
"success": true
Expand Down Expand Up @@ -570,6 +570,7 @@ def request(self, path: str, data: str,
self.assertEqual(status.devices[5].hostname, 'name4')
self.assertEqual(status.devices[5].packets_sent, None)
self.assertEqual(status.devices[5].packets_received, None)
self.assertEqual(status.devices[5].signal, -52)
self.assertIsInstance(status.devices[6], Device)
self.assertEqual(status.devices[6].type, Connection.HOST_5G)
self.assertEqual(status.devices[6].macaddr, '1F-7A-BD-F7-20-0D')
Expand Down
180 changes: 180 additions & 0 deletions test/test_client_mr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
IPv4Status,
ClientError,
SMS,
LTEStatus,
VPNStatus,
VPN,
)


Expand Down Expand Up @@ -864,6 +867,183 @@ def _request(self, url, method='POST', data_str=None, encrypt=False):
self.assertIn('http:///cgi_gdpr?_=', check_url)
self.assertEqual(check_data, '4\r\n[LTE_SMS_RECVMSGENTRY#2,0,0,0,0,0#0,0,0,0,0,0]0,0\r\n\r\n')

def test_get_lte_status(self) -> None:
response = '''[2,1,0,0,0,0]0
enable=1
connectStatus=4
networkType=3
roamingStatus=0
simStatus=3
[2,0,0,0,0,0]1
dataLimit=0
enablePaymentDay=0
curStatistics=0
totalStatistics=32779416.0000
enableDataLimit=0
limitation=0
curRxSpeed=85
curTxSpeed=1492
[2,1,0,0,0,0]2
smsUnreadCount=0
ussdStatus=0
smsSendResult=3
sigLevel=0
rfInfoRsrp=-105
rfInfoRsrq=-20
rfInfoSnr=-44
[2,1,0,0,0,0]3
spn=Full name
ispName=Name
[error]0
'''

class TPLinkMRClientTest(TPLinkMRClient):
def _request(self, url, method='POST', data_str=None, encrypt=False):
return 200, response

client = TPLinkMRClientTest('', '')
status = client.get_lte_status()

self.assertIsInstance(status, LTEStatus)
self.assertEqual(status.enable, 1)
self.assertEqual(status.connect_status, 4)
self.assertEqual(status.network_type, 3)
self.assertEqual(status.sim_status, 3)
self.assertEqual(status.total_statistics, 32779416)
self.assertEqual(status.cur_rx_speed, 85)
self.assertEqual(status.cur_tx_speed, 1492)
self.assertEqual(status.sms_unread_count, 0)
self.assertEqual(status.sig_level, 0)
self.assertEqual(status.rsrp, -105)
self.assertEqual(status.rsrq, -20)
self.assertEqual(status.snr, -44)
self.assertEqual(status.isp_name, 'Name')

def test_get_lte_status_wrong(self) -> None:
response = '''[2,1,0,0,0,0]0
enable=1
connectStatus=1
networkType=2
roamingStatus=0
simStatus=1
[2,0,0,0,0,0]1
dataLimit=0
enablePaymentDay=0
curStatistics=0
totalStatistics=32779416.0000
enableDataLimit=0
limitation=0
curRxSpeed=0
curTxSpeed=0
[2,1,0,0,0,0]2
smsUnreadCount=0
ussdStatus=0
smsSendResult=3
sigLevel=2
rfInfoRsrp=0
rfInfoRsrq=0
rfInfoSnr=0
[2,1,0,0,0,0]3
spn=Full name
ispName=Name
[error]0
'''

class TPLinkMRClientTest(TPLinkMRClient):
def _request(self, url, method='POST', data_str=None, encrypt=False):
return 200, response

client = TPLinkMRClientTest('', '')
status = client.get_lte_status()

self.assertIsInstance(status, LTEStatus)

def test_get_vpn_status(self) -> None:
response = '''[0,0,0,0,0,0]0
enable=1
[0,0,0,0,0,0]1
enable=0
[1,0,0,0,0,0]2
connAct=0
[2,0,0,0,0,0]2
connAct=0
[3,0,0,0,0,0]2
connAct=0
[4,0,0,0,0,0]2
connAct=0
[5,0,0,0,0,0]2
connAct=1
[6,0,0,0,0,0]2
connAct=1
[7,0,0,0,0,0]2
connAct=0
[8,0,0,0,0,0]2
connAct=0
[9,0,0,0,0,0]2
connAct=0
[10,0,0,0,0,0]2
connAct=0
[1,0,0,0,0,0]3
connAct=0
[2,0,0,0,0,0]3
connAct=0
[3,0,0,0,0,0]3
connAct=0
[4,0,0,0,0,0]3
connAct=0
[5,0,0,0,0,0]3
connAct=0
[6,0,0,0,0,0]3
connAct=0
[7,0,0,0,0,0]3
connAct=0
[8,0,0,0,0,0]3
connAct=0
[9,0,0,0,0,0]3
connAct=0
[10,0,0,0,0,0]3
connAct=0
[error]0
'''

class TPLinkMRClientTest(TPLinkMRClient):
def _request(self, url, method='POST', data_str=None, encrypt=False):
return 200, response

client = TPLinkMRClientTest('', '')
status = client.get_vpn_status()

self.assertIsInstance(status, VPNStatus)
self.assertEqual(status.openvpn_enable, True)
self.assertEqual(status.pptpvpn_enable, False)
self.assertEqual(status.openvpn_clients_total, 2)
self.assertEqual(status.pptpvpn_clients_total, 0)

def test_set_vpn(self) -> None:
response = '''
[error]0
'''

check_url = ''
check_data = ''

class TPLinkMRClientTest(TPLinkMRClient):
def _request(self, url, method='POST', data_str=None, encrypt=False):
nonlocal check_url, check_data
check_url = url
check_data = data_str
return 200, response

client = TPLinkMRClientTest('', '')
client.set_vpn(VPN.OPEN_VPN, True)

self.assertIn('http:///cgi_gdpr?_=', check_url)
self.assertEqual(check_data, '2\r\n[OPENVPN#0,0,0,0,0,0#0,0,0,0,0,0]0,1\r\nenable=1\r\n')


if __name__ == '__main__':
main()
30 changes: 9 additions & 21 deletions test/test_client_xdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def _request(self, payload: dict) -> dict:
self.assertEqual(firmware.model, 'TL-XDR3010易展版')

def test_get_status(self) -> None:
mock_data = json.loads('''
{
mock_data = json.loads('''{
"hosts_info": {
"host_info": [
{
Expand All @@ -96,9 +95,7 @@ def test_get_status(self) -> None:
"ssid": "",
"forbid_domain": "",
"limit_time": "",
"plan_rule": [
]
"plan_rule": []
}
},
{
Expand All @@ -119,9 +116,7 @@ def test_get_status(self) -> None:
"ssid": "",
"forbid_domain": "",
"limit_time": "",
"plan_rule": [
]
"plan_rule": []
}
}
]
Expand Down Expand Up @@ -359,19 +354,12 @@ def _request(self, payload: dict) -> dict:
self.assertEqual(check_payload['wireless']['wlan_host_5g']['enable'], 0)

def test_get_ipv4_reservations(self):
mock_data = json.loads('''
{
"ip_mac_bind": {
"user_bind": [{
"user_bind_3": {
"mac": "24-59-E5-D0-21-8C",
"ip": "192.168.2.202",
"hostname": "midea_ac_0361"
}
}]
},
"error_code": 0
}''')
mock_data = json.loads('''{
"ip_mac_bind": {
"user_bind": [{"user_bind_3": {"mac": "24-59-E5-D0-21-8C", "ip": "192.168.2.202", "hostname": "midea_ac_0361"}}]
},
"error_code": 0}
''')
check_payload = {}

class TPLinkXDRClientTest(TPLinkXDRClient):
Expand Down
Loading

0 comments on commit 7a9fbfe

Please sign in to comment.