Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic mitmproxy functionality to s3s #199

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ NOTE: Read the "Token generation" section below before proceeding. [→](#token-

6. You will then be asked to navigate to a specific URL on Nintendo.com, log in, and follow simple instructions to obtain your `session_token`; this will be used to generate a `gtoken` and `bulletToken`. If you are opting against automatic token generation, enter "skip" for this step, at which point you will be asked to manually input your two tokens instead (see the [mitmproxy instructions](https://github.com/frozenpandaman/s3s/wiki/mitmproxy-instructions)).

NOTE: Currently, there is no known method of automatic token generation due to an update to Nintendo Switch Online by Nintendo. It is TBD whether a method will be found.

These tokens (used to access your SplatNet battle results) along with your stat.ink API key & language will be saved into `config.txt` for you. You're now ready to upload battles!

Have any questions, problems, or suggestions? [Create an issue](https://github.com/frozenpandaman/s3s/issues) here or contact me on [Twitter](https://twitter.com/frozenpandaman). **Please do not raise issues via Discord. It is important for discussion on the internet to be public, indexable, and searchable, able to be shared freely and benefit others – not locked behind a private platform. [Here](https://v21.io/blog/how-to-find-things-online)'s a great article about this!**
Expand Down Expand Up @@ -95,6 +97,14 @@ Users who decide against using automatic token generation may instead retrieve t

In this case, users must obtain tokens from their phone – or an emulator – by intercepting their device's web traffic and entering the tokens into s3s when prompted (or manually adding them to `config.txt` later). Follow the [mitmproxy instructions](https://github.com/frozenpandaman/s3s/wiki/mitmproxy-instructions) to obtain your tokens. To opt for manual token entry, type "skip" when prompted to enter the "Select this account" URL.

Instead of needing to copy-paste tokens, you can use s3_token_extractor.py to automatically extract tokens into config.txt. It will be automatically run through s3s.py, or you can manually run it as follows:

```
$ python s3_token_extractor.py
```



## License & copyleft statement 🏴

s3s is _free software_ licensed under [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html). This means that you have _freedom_ – to run, modify, copy, share, and redistribute this work as you see fit, as long as derivative works are also distributed under these same or equivalent terms.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ msgpack_python
packaging
pymmh3
requests
mitmproxy
67 changes: 67 additions & 0 deletions s3_token_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
Extract gToken and bulletToken for Splatoon 3 NSO.
The result will be saved in config.txt.
"""

import logging
import json
import os
import sys
from mitmproxy import ctx
from mitmproxy.tools.main import mitmdump

config_path = os.path.join(os.path.dirname(__file__), "config.txt")

try:
config_file = open(config_path, "r")
CONFIG_DATA = json.load(config_file)
config_file.close()
except (IOError, ValueError):
print('Please run s3s.py first to generate the config file.')
ctx.master.shutdown()
sys.exit()

# SET GLOBALS
API_KEY = CONFIG_DATA["api_key"] # for stat.ink
USER_LANG = CONFIG_DATA["acc_loc"][:5] # user input
USER_COUNTRY = CONFIG_DATA["acc_loc"][-2:] # nintendo account info
GTOKEN = CONFIG_DATA["gtoken"] # for accessing splatnet - base64 json web token
BULLETTOKEN = CONFIG_DATA["bullettoken"] # for accessing splatnet - base64
SESSION_TOKEN = CONFIG_DATA["session_token"] # for nintendo login
F_GEN_URL = CONFIG_DATA["f_gen"] # endpoint for generating f (imink API by default)

class Splatoon3TokenExtractor:
def __init__(self):
#self.outfile = open("gtoken_bullettoken.txt", "w")
self.web_service_token = None
self.bullet_token = None

def response(self, flow):
path = flow.request.path
if path.endswith('GetWebServiceToken'):
logging.info(f"{flow.response}")
obj = json.loads(flow.response.content.decode('utf-8'))
self.web_service_token = obj["result"]["accessToken"]
logging.info(self.web_service_token)
if path.endswith('bullet_tokens'):
logging.info(f"{flow.response}")
obj = json.loads(flow.response.content.decode('utf-8'))
self.bullet_token = obj["bulletToken"]
logging.info(self.bullet_token)
if self.web_service_token and self.bullet_token:
# write into config file
CONFIG_DATA["gtoken"] = self.web_service_token
CONFIG_DATA["bullettoken"] = self.bullet_token
config_file = open(config_path, "w")
config_file.seek(0)
config_file.write(json.dumps(CONFIG_DATA, indent=4, sort_keys=False, separators=(',', ': ')))
config_file.close()
ctx.master.shutdown()

addons = [Splatoon3TokenExtractor()]

def main():
mitmdump(['-s', __file__, '~u GetWebServiceToken | ~u bullet_tokens', '--view-filter', '~u GetWebServiceToken | ~u bullet_tokens'])

if __name__ == "__main__":
main()
11 changes: 9 additions & 2 deletions s3s.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import requests, msgpack
from packaging import version
import iksm, utils
import s3_token_extractor

A_VERSION = "0.6.7"

Expand Down Expand Up @@ -161,8 +162,14 @@ def gen_new_tokens(reason, force=False):
manual_entry = True

if manual_entry: # no session_token ever gets stored
print("\nYou have opted against automatic token generation and must manually input your tokens.\n")
new_gtoken, new_bullettoken = iksm.enter_tokens()
print("mitmdump will be used to automatically generate tokens. Go to the page below to find instructions to set up your phone:")
print("https://github.com/frozenpandaman/s3s/wiki/mitmproxy-instructions\n")
input("Press Enter to continue when your phone is set up.\nYou do not need to use mitmweb, or manually copy any tokens.")
s3_token_extractor.main()
config_file = open(config_path, "r")
config_temp = json.load(config_file)
config_file.close()
new_gtoken, new_bullettoken = config_temp["gtoken"], config_temp["bullettoken"]
acc_lang = "en-US" # overwritten by user setting
acc_country = "US"
print("Using `US` for country by default. This can be changed in config.txt.")
Expand Down