Skip to content

Commit

Permalink
Add randomly generated user agents (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
muppet3000 authored Nov 29, 2022
1 parent 887655a commit 2621367
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions examples/user_agent_options.py
Original file line number Diff line number Diff line change
@@ -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("")

15 changes: 13 additions & 2 deletions growattServer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import requests
import warnings
from random import randint

def hash_password(password):
"""
Expand All @@ -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):
Expand Down

0 comments on commit 2621367

Please sign in to comment.