| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
""" |
| 4 |
A dummy push server that emits dummy push subscription such that Firefox can |
| 5 |
understand it, but without any data storage nor validation. |
| 6 |
|
| 7 |
How it works: |
| 8 |
1. Firefox will establish a websocket connection with the dummy server via |
| 9 |
PushServiceWebSocket.sys.mjs. |
| 10 |
The initial connection will also start up an HTTPS server that works |
| 11 |
as a push endpoint. |
| 12 |
2. A push subscription from a test via Push API will send messageType=register, |
| 13 |
which will generate a URL that points to the HTTPS server. It exposes |
| 14 |
channel ID as a URL parameter, and the server will pass through that parameter |
| 15 |
to Firefox so that it can map it to each push subscription. |
| 16 |
3. message=unregister will be sent from Firefox when unsubscription happens, |
| 17 |
but the dummy server doesn't process that as it doesn't even remember any |
| 18 |
subscription (it just passes through the channel ID and that's it). |
| 19 |
|
| 20 |
|
| 21 |
Caveat: |
| 22 |
* It sends CORS headers but that behavior is only observed in Firefox's push server |
| 23 |
(autopush-rs). |
| 24 |
* The dummy server doesn't throw any error on an invalid VAPID. |
| 25 |
* It assumes that Firefox will use websocket to connect to the push server, which is |
| 26 |
not true on Firefox for Android as it uses Google FCM. |
| 27 |
(See https://firefox-source-docs.mozilla.org/mobile/android/fenix/Firebase-Cloud-Messaging-for-WebPush.html) |
| 28 |
""" |
| 29 |
|
| 30 |
import base64 |
| 31 |
import json |
| 32 |
import pathlib |
| 33 |
import ssl |
| 34 |
import threading |
| 35 |
from http.server import BaseHTTPRequestHandler, HTTPServer |
| 36 |
from urllib.parse import parse_qs, urlparse |
| 37 |
|
| 38 |
from pywebsocket3 import msgutil |
| 39 |
|
| 40 |
from tools import localpaths |
| 41 |
|
| 42 |
UAID = "8e1c93a9-139b-419c-b200-e715bb1e8ce8" |
| 43 |
|
| 44 |
|
| 45 |
class DummyEndpointHandler(BaseHTTPRequestHandler): |
| 46 |
def do_OPTIONS(self): |
| 47 |
self.send_response(200) |
| 48 |
self.send_header("Access-Control-Allow-Origin", "*") |
| 49 |
self.send_header("Access-Control-Allow-Methods", "*") |
| 50 |
self.send_header("Access-Control-Allow-Headers", "*") |
| 51 |
self.end_headers() |
| 52 |
|
| 53 |
def do_POST(self): |
| 54 |
url = urlparse(self.path) |
| 55 |
query = parse_qs(url.query) |
| 56 |
|
| 57 |
content_len = int(self.headers.get("Content-Length")) |
| 58 |
post_body = self.rfile.read(content_len) |
| 59 |
self.send_response(200) |
| 60 |
self.send_header("Access-Control-Allow-Origin", "*") |
| 61 |
self.send_header("Access-Control-Allow-Methods", "*") |
| 62 |
self.send_header("Access-Control-Allow-Headers", "*") |
| 63 |
self.end_headers() |
| 64 |
|
| 65 |
headers = { |
| 66 |
"encoding": self.headers.get("Content-Encoding"), |
| 67 |
} |
| 68 |
msgutil.send_message( |
| 69 |
self.server.websocket_request, |
| 70 |
json.dumps( |
| 71 |
{ |
| 72 |
"messageType": "notification", |
| 73 |
"channelID": query["channelID"][0], |
| 74 |
"data": base64.urlsafe_b64encode(post_body).decode(), |
| 75 |
"headers": headers if len(post_body) > 0 else None, |
| 76 |
} |
| 77 |
), |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
def web_socket_do_extra_handshake(request): |
| 82 |
request.ws_protocol = "push-notification" |
| 83 |
|
| 84 |
|
| 85 |
def start_endpoint(server): |
| 86 |
def impl(): |
| 87 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| 88 |
|
| 89 |
certs = pathlib.Path(localpaths.repo_root) / "tools/certs" |
| 90 |
|
| 91 |
context.load_cert_chain( |
| 92 |
certfile=str(certs / "web-platform.test.pem"), |
| 93 |
keyfile=str(certs / "web-platform.test.key"), |
| 94 |
) |
| 95 |
|
| 96 |
server.socket = context.wrap_socket(server.socket, server_side=True) |
| 97 |
|
| 98 |
server.serve_forever() |
| 99 |
|
| 100 |
return impl |
| 101 |
|
| 102 |
|
| 103 |
def web_socket_transfer_data(request): |
| 104 |
server = HTTPServer(("localhost", 0), DummyEndpointHandler) |
| 105 |
server.websocket_request = request |
| 106 |
port = server.server_port |
| 107 |
|
| 108 |
endpoint_thread = threading.Thread(target=start_endpoint(server), daemon=True) |
| 109 |
endpoint_thread.start() |
| 110 |
|
| 111 |
while True: |
| 112 |
line = request.ws_stream.receive_message() |
| 113 |
if line is None: |
| 114 |
server.shutdown() |
| 115 |
return |
| 116 |
|
| 117 |
message = json.loads(line) |
| 118 |
messageType = message["messageType"] |
| 119 |
|
| 120 |
if messageType == "hello": |
| 121 |
msgutil.send_message( |
| 122 |
request, |
| 123 |
json.dumps( |
| 124 |
{ |
| 125 |
"messageType": "hello", |
| 126 |
"uaid": UAID, |
| 127 |
"status": 200, |
| 128 |
"use_webpush": True, |
| 129 |
} |
| 130 |
), |
| 131 |
) |
| 132 |
elif messageType == "register": |
| 133 |
channelID = message["channelID"] |
| 134 |
msgutil.send_message( |
| 135 |
request, |
| 136 |
json.dumps( |
| 137 |
{ |
| 138 |
"messageType": "register", |
| 139 |
"uaid": UAID, |
| 140 |
"channelID": channelID, |
| 141 |
"status": 200, |
| 142 |
"pushEndpoint": f"https://web-platform.test:{port}/push_endpoint?channelID={channelID}", |
| 143 |
} |
| 144 |
), |
| 145 |
) |
| 146 |
elif messageType == "unregister": |
| 147 |
msgutil.send_message( |
| 148 |
request, |
| 149 |
json.dumps( |
| 150 |
{ |
| 151 |
"messageType": "unregister", |
| 152 |
"channelID": message["channelID"], |
| 153 |
"status": 200, |
| 154 |
} |
| 155 |
), |
| 156 |
) |