From ccf4f337345c73b8411c00ac994b59f0944b79cc Mon Sep 17 00:00:00 2001
From: Stefan Kraus <stefan.kraus@methodpark.de>
Date: Mon, 5 Apr 2021 13:53:48 +0200
Subject: [PATCH] Make things work together with reverse proxy

---
 client/client.py              |  6 ++++--
 frontend/src/store/store.ts   | 13 +++++++++++--
 frontend/src/url/InputBar.tsx |  4 ++--
 mpvsync.conf                  | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 6 deletions(-)
 create mode 100644 mpvsync.conf

diff --git a/client/client.py b/client/client.py
index 310abef..36739e1 100755
--- a/client/client.py
+++ b/client/client.py
@@ -112,7 +112,7 @@ Relays command from the server to the mpv instance, but sanitizes the commands."
     )
 
     parser.add_argument(
-        "--port", help="The port of the server.", default=8432, type=int
+        "--port", help="The port of the server.", default=443, type=int
     )
 
     parser.add_argument("--ca-file", help="Path to the certicate file.", required=False)
@@ -202,7 +202,9 @@ async def handle_server(hostname, port, group, ignore_cert):
     if ignore_cert:
         ssl_context.check_hostname = False
         ssl_context.verify_mode = ssl.CERT_NONE
-    uri = f"wss://{hostname}:{port}/{group}"
+
+    uri = f"wss://{hostname}/api/ws/{group}"
+
     while True:
         try:
             async with websockets.connect(uri, ssl=ssl_context) as ws:
diff --git a/frontend/src/store/store.ts b/frontend/src/store/store.ts
index b6d0f2d..7a85352 100644
--- a/frontend/src/store/store.ts
+++ b/frontend/src/store/store.ts
@@ -57,8 +57,17 @@ export const store = configureStore({
   ],
 });
 
-const path = window.location.pathname;
-store.dispatch(connect("wss://mpvsync.de:8432/" + path));
+const domain = window.location.hostname;
+const group = window.location.pathname;
+
+let ws_url = `wss://${domain}/api/ws/${group}`
+
+// It's localhost, no SSL needed / possible.
+if(domain === 'localhost'){
+  ws_url = `ws://${domain}:8432/${group}`
+}
+
+store.dispatch(connect(ws_url));
 sagaMiddleware.run(rootSaga);
 
 export type RootState = ReturnType<typeof store.getState>;
diff --git a/frontend/src/url/InputBar.tsx b/frontend/src/url/InputBar.tsx
index 3fad070..bd35e84 100644
--- a/frontend/src/url/InputBar.tsx
+++ b/frontend/src/url/InputBar.tsx
@@ -51,8 +51,8 @@ class UrlBox extends React.Component<Props> {
   }
 
   handleServerClick(): void | undefined {
-    this.props.dispatchUrl("https://mpvsync.de/video/index.mp4");
-    this.props.dispatchSubtitleFile("https://mpvsync.de/video/index.vtt");
+    this.props.dispatchUrl("/video/index.mp4");
+    this.props.dispatchSubtitleFile("/video/index.vtt");
   }
 
   async handleSubtitleClick(): Promise<void | undefined> {
diff --git a/mpvsync.conf b/mpvsync.conf
new file mode 100644
index 0000000..8260fb5
--- /dev/null
+++ b/mpvsync.conf
@@ -0,0 +1,33 @@
+server {
+
+	# TODO CHANGE :)
+	server_name your_servername.com;
+
+	listen 443 ssl; # managed by Certbot
+	listen [::]:443 ssl; # managed by Certbot
+
+	ssl_session_timeout 5m;
+	ssl_protocols  SSLv2 SSLv3 TLSv1;
+	ssl_ciphers  HIGH:!aNULL:!MD5;
+	ssl_prefer_server_ciphers   on;
+
+	location / {
+		proxy_pass http://127.0.0.1:8430;
+	}
+
+	location /api/ws/ {
+		proxy_pass http://127.0.0.1:8432/;
+
+		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+		proxy_set_header Host $host;
+
+		proxy_http_version 1.1;
+		proxy_set_header Upgrade $http_upgrade;
+		proxy_set_header Connection "upgrade";
+	}
+
+
+	# TODO CHANGE :)
+	ssl_certificate /path/to/your/fullchain.pem # Can be managed by Certbot
+	ssl_certificate_key /path/to/your/certificate_key.pem # Can be managed by Certbot
+}
-- 
GitLab