|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import urllib.parse |
| 4 | + |
| 5 | +import httpx |
| 6 | +from flask import Flask, request, make_response |
| 7 | +from saic_ismart_client_ng.net.crypto import decrypt_request, encrypt_response |
| 8 | +from saic_ismart_client_ng.net.httpx import encrypt_httpx_request, decrypt_httpx_response |
| 9 | + |
| 10 | +app = Flask(__name__) |
| 11 | + |
| 12 | +cached_tokens = dict() |
| 13 | +base_uri = 'https://gateway-mg-eu.soimt.com/api.app/v1/' |
| 14 | + |
| 15 | + |
| 16 | +async def encrypt_httpx_request_wrapper( |
| 17 | + req: httpx.Request |
| 18 | +): |
| 19 | + return await encrypt_httpx_request( |
| 20 | + modified_request=req, |
| 21 | + request_timestamp=datetime.datetime.now(), |
| 22 | + base_uri=base_uri, |
| 23 | + region='eu', |
| 24 | + tenant_id='459771', |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@app.route('/api.app/v1/', defaults={'path': ''}) |
| 29 | +@app.route('/api.app/v1/<path:path>', methods=['GET']) |
| 30 | +def do_get(path): |
| 31 | + print(request) |
| 32 | + return 'You want GET path: %s' % path |
| 33 | + |
| 34 | + |
| 35 | +@app.route('/api.app/v1/', defaults={'path': ''}, methods=['POST']) |
| 36 | +@app.route('/api.app/v1/<path:path>', methods=['POST']) |
| 37 | +async def do_post(path): |
| 38 | + if path == 'oauth/token': |
| 39 | + raw_data = request.get_data(parse_form_data=False).decode('utf-8') |
| 40 | + decrypted = decrypt_request( |
| 41 | + original_request_url=request.url, |
| 42 | + original_request_headers=request.headers, |
| 43 | + original_request_content=raw_data, |
| 44 | + base_uri=request.url.removesuffix(path), |
| 45 | + ) |
| 46 | + unquoted = urllib.parse.parse_qs(decrypted) |
| 47 | + username = unquoted[b'username'][0].decode('utf-8') |
| 48 | + if username in cached_tokens: |
| 49 | + response = cached_tokens[username] |
| 50 | + else: |
| 51 | + password = unquoted[b'password'][0].decode('utf-8') |
| 52 | + login_type = unquoted[b'loginType'][0].decode('utf-8') |
| 53 | + country_code = unquoted[b'countryCode'][0].decode('utf-8') if b'countryCode' in unquoted else '' |
| 54 | + headers = { |
| 55 | + "Content-Type": "application/x-www-form-urlencoded", |
| 56 | + "Accept": "application/json", |
| 57 | + "Authorization": request.headers['Authorization'] |
| 58 | + } |
| 59 | + firebase_device_id = "cqSHOMG1SmK4k-fzAeK6hr:APA91bGtGihOG5SEQ9hPx3Dtr9o9mQguNiKZrQzboa-1C_UBlRZYdFcMmdfLvh9Q_xA8A0dGFIjkMhZbdIXOYnKfHCeWafAfLXOrxBS3N18T4Slr-x9qpV6FHLMhE9s7I6s89k9lU7DD" |
| 60 | + form_body = { |
| 61 | + "grant_type": "password", |
| 62 | + "username": username, |
| 63 | + "password": password, |
| 64 | + "scope": "all", |
| 65 | + "deviceId": f"{firebase_device_id}###europecar", |
| 66 | + "deviceType": "1", # 2 for huawei |
| 67 | + "loginType": login_type, |
| 68 | + "countryCode": country_code |
| 69 | + } |
| 70 | + client = httpx.AsyncClient( |
| 71 | + event_hooks={ |
| 72 | + "request": [encrypt_httpx_request_wrapper], |
| 73 | + "response": [decrypt_httpx_response] |
| 74 | + }, |
| 75 | + ) |
| 76 | + response = await client.post(url=f'{base_uri}{path}', data=form_body, headers=headers) |
| 77 | + if response.is_success: |
| 78 | + response_json = response.json() |
| 79 | + cached_tokens[username] = response_json |
| 80 | + text_content = json.dumps(response_json) |
| 81 | + ts = response.headers['app-send-date'] |
| 82 | + their_verification = response.headers['app-verification-string'] |
| 83 | + new_content, new_headers = encrypt_response( |
| 84 | + original_request_url=str(response.url), |
| 85 | + original_response_headers=response.headers, |
| 86 | + original_response_content=response.text, |
| 87 | + response_timestamp_ms=ts, |
| 88 | + base_uri=base_uri, |
| 89 | + tenant_id='459771', |
| 90 | + user_token='' |
| 91 | + ) |
| 92 | + response = make_response(new_content) |
| 93 | + response.headers.update(new_headers) |
| 94 | + return response |
| 95 | + else: |
| 96 | + return (response.status_code, response.content) |
| 97 | + |
| 98 | + return 'You want POST path: %s' % path |
| 99 | + |
| 100 | + |
| 101 | +@app.route('/api.app/v1/', defaults={'path': ''}, methods=['DELETE']) |
| 102 | +@app.route('/api.app/v1/<path:path>', methods=['DELETE']) |
| 103 | +def do_delete(path): |
| 104 | + print(request) |
| 105 | + return 'You want GET path: %s' % path |
| 106 | + |
| 107 | + |
| 108 | +@app.route('/api.app/v1/', defaults={'path': ''}, methods=['PUT']) |
| 109 | +@app.route('/api.app/v1/<path:path>', methods=['PUT']) |
| 110 | +def do_put(path): |
| 111 | + print(request) |
| 112 | + return 'You want POST path: %s' % path |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + app.run() |
0 commit comments