From 2621367248ada9fd96efa5ad8e5a49cc93e3e6fd Mon Sep 17 00:00:00 2001 From: muppet3000 Date: Tue, 29 Nov 2022 13:06:27 +0000 Subject: [PATCH] Add randomly generated user agents (#42) --- README.md | 20 ++++++++++++- examples/user_agent_options.py | 51 ++++++++++++++++++++++++++++++++++ growattServer/__init__.py | 15 ++++++++-- 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100755 examples/user_agent_options.py diff --git a/README.md b/README.md index c0cc699..7df309b 100755 --- a/README.md +++ b/README.md @@ -68,12 +68,30 @@ Any methods that may be useful. Some variables you may want to set. -`api.server_url` The growatt server URL, default: 'https://server.growatt.com/' +`api.server_url` The growatt server URL, default: 'https://server-api.growatt.com/' ## Note This is based on the endpoints used on the mobile app and could be changed without notice. +## Initialisation + +The library can be initialised to introduce randomness into the User Agent field that is used when communicating with the servers. + +This has been added since the Growatt servers started checking for the presence of a `User-Agent` field in the headers that are sent. + +By default the library will use a pre-set `User-Agent` value which identifies this library while also appearing like an Android device. However, it is also possible to pass in parameters to the intialisation of the library to override this entirely, or just add a random ID to the value. e.g. + +```python +api = growattServer.GrowattApi() # The default way to initialise + +api = growattServer.GrowattApi(True) # Adds a randomly generated User ID to the default User-Agent + +api = growattServer.GrowattApi(False, "my_user_agent_value") # Overrides the default and uses "my_user_agent_value" in the User-Agent header +``` + +Please see the `user_agent_options.py` example in the `examples` directory if you wish to investigate further. + ## Examples The `examples` directory contains example usage for the library. You are required to have the library installed to use them `pip install growattServer`. However, if you are contributing to the library and want to use the latest version from the git repository, simply create a symlink to the growattServer directory inside the `examples` directory. diff --git a/examples/user_agent_options.py b/examples/user_agent_options.py new file mode 100755 index 0000000..a9b6fac --- /dev/null +++ b/examples/user_agent_options.py @@ -0,0 +1,51 @@ +import growattServer +import getpass + +""" +This is a simple script that demonstrates the various ways to initialise the library to set a User Agent +""" + +#Prompt user for username +username=input("Enter username:") + +#Prompt user to input password +user_pass=getpass.getpass("Enter password:") + + + +api = growattServer.GrowattApi() +login_response = api.login(username, user_pass) +print("Default initialisation") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + +api = growattServer.GrowattApi(True) +login_response = api.login(username, user_pass) +print("Add random ID to default User-Agent") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + +api = growattServer.GrowattApi(False, "my-user-id") +login_response = api.login(username, user_pass) +print("Override default User-Agent") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + +api = growattServer.GrowattApi(True, "my-user-id") +login_response = api.login(username, user_pass) +print("Override default User-Agent and add random ID") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + +api = growattServer.GrowattApi(False, growattServer.GrowattApi.agent_identifier + " - my-user-id") +login_response = api.login(username, user_pass) +print("Extend default User-Agent") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + +api = growattServer.GrowattApi(True, growattServer.GrowattApi.agent_identifier + " - my-user-id") +login_response = api.login(username, user_pass) +print("Extend default User-Agent and add random ID") +print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId'])) +print("") + diff --git a/growattServer/__init__.py b/growattServer/__init__.py index febc57f..6ce22cd 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -6,6 +6,7 @@ import json import requests import warnings +from random import randint def hash_password(password): """ @@ -24,10 +25,20 @@ class Timespan(IntEnum): class GrowattApi: server_url = 'https://server-api.growatt.com/' + agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)" + + def __init__(self, add_random_user_id=False, agent_identifier=None): + if (agent_identifier != None): + self.agent_identifier = agent_identifier + + #If a random user id is required, generate a 5 digit number and add it to the user agent + if (add_random_user_id): + random_number = ''.join(["{}".format(randint(0,9)) for num in range(0,5)]) + self.agent_identifier += " - " + random_number - def __init__(self): self.session = requests.Session() - headers = {'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android Device)'} + + headers = {'User-Agent': self.agent_identifier} self.session.headers.update(headers) def __get_date_string(self, timespan=None, date=None):