Skip to content

Commit

Permalink
Added support for multiple Thorium servers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Jan 24, 2018
1 parent 881f1bc commit c682ca3
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 162 deletions.
147 changes: 104 additions & 43 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,83 +1,144 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thorium</title>
<style>
body {
background: url(background.jpg);
background-size: cover;
display: flex;
flex-direction: column;
align-items: center;
margin: 40px;
color: white;
font-size: 18px;
font-family: sans-serif;
text-shadow: 5px 5px 5px rgba(0,0,0,0.5);
}
h1 {
font-size: 36px;
}
img {
width: 200px;
filter: drop-shadow(3px 3px 5px rgba(0,0,0,0.5));
}
</style>
</head>
<body style="background-color: rgba(255,0,0,1)">
<img src="logo.png" draggable="false" />
<h1>Thorium</h1>
<svg class="lds-spinner" width="55px" height="55px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" style="background-image: none; background-position: initial initial; background-repeat: initial initial;"><g transform="rotate(0 50 50)">

<head>
<meta charset="UTF-8">
<title>Thorium</title>
<style>
body {
background: url(background.jpg);
background-size: 100vw 100vh;
display: flex;
flex-direction: column;
align-items: center;
margin: 40px;
color: white;
font-size: 18px;
font-family: sans-serif;
text-shadow: 5px 5px 5px rgba(0, 0, 0, 0.5);
}
#loading {
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
font-size: 36px;
}

img {
width: 200px;
filter: drop-shadow(3px 3px 5px rgba(0, 0, 0, 0.5));
}
button {
color: #fff;
background-color: #007bff;
border-color: #007bff;
-webkit-appearance: button;
cursor: pointer;
display: block;
font-weight: 400;
text-align: center;
white-space: nowrap;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
text-transform: none;
margin: 0;
font-family: inherit;
}
#loading.hidden, .hidden {
display: none;
}
</style>
</head>

<body>
<img src="logo.png" draggable="false" />
<h1>Thorium</h1>
<div id="loading">
<svg class="lds-spinner" width="55px" height="55px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" style="background-image: none; background-position: initial initial; background-repeat: initial initial;">
<g transform="rotate(0 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(30 50 50)">
</g>
<g transform="rotate(30 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(60 50 50)">
</g>
<g transform="rotate(60 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(90 50 50)">
</g>
<g transform="rotate(90 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(120 50 50)">
</g>
<g transform="rotate(120 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(150 50 50)">
</g>
<g transform="rotate(150 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(180 50 50)">
</g>
<g transform="rotate(180 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(210 50 50)">
</g>
<g transform="rotate(210 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(240 50 50)">
</g>
<g transform="rotate(240 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(270 50 50)">
</g>
<g transform="rotate(270 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(300 50 50)">
</g>
<g transform="rotate(300 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite"></animate>
</rect>
</g><g transform="rotate(330 50 50)">
</g>
<g transform="rotate(330 50 50)">
<rect x="47" y="24" rx="9.4" ry="4.8" width="6" height="12" fill="#d672ff">
<animate attributeName="opacity" values="1;0" times="0;1" dur="1s" begin="0s" repeatCount="indefinite"></animate>
</rect>
</g></svg>
</g>
</svg>
<p>Searching for Thorium Server...</p>
<p><em><strong>⌘ + D</strong></em> to enter server address</p>
</body>
</div>
<div id="servers" class="hidden">
<h4>Select a server:</h4>
<div id="serverList">

</div>
</div>
<p>
<em>
<strong>⌘ + D</strong>
</em> to enter server address</p>
</body>

</html>
101 changes: 49 additions & 52 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ const electron = require("electron");
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
const os = require("os");
const prompt = require("electron-prompt");
const path = require("path");
const url = require("url");
const powerOff = require("power-off");
const sleepMode = require("sleep-mode");
const shell = electron.shell;
const storage = require("electron-json-storage");
const freakout = require("./freakout");
const dialog = electron.dialog;
const globalShortcut = electron.globalShortcut;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let uri;

app.on("ready", function() {
const ipcMain = electron.ipcMain;
ipcMain.on('loadPage', function(evt, uri) {
mainWindow && mainWindow.loadURL(uri);
triggerWindow();
});
ipcMain.on("synchronous-message", function(event, arg) {
if (arg) {
switch (arg.action) {
Expand Down Expand Up @@ -49,6 +49,8 @@ app.on("ready", function() {
case "quit":
app.quit();
break;
default:
break;
}
}
event.returnValue = "pong";
Expand All @@ -66,26 +68,14 @@ app.on("ready", function() {
});
let webContents = mainWindow.webContents;

storage.get("url", function(err, res) {
if (res && res.url) {
mainWindow.loadURL(res.url);
triggerWindow();
} else {
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
})
);
}
});
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
})
);

webContents.on("did-start-loading", () => {
mainWindow.webContents.executeJavaScript(
"localStorage.setItem('thorium_clientId','" + os.hostname() + "');"
);
});
webContents.on("did-fail-load", () => {
// Load the default page
mainWindow &&
Expand All @@ -98,45 +88,51 @@ app.on("ready", function() {
);
});
globalShortcut.register("CommandOrControl+D", function() {
storage.get("url", function(err, res) {
prompt({
title: "Enter the IP address of the server",
label: "URL:",
value: res.url,
inputAttrs: {
type: "text"
}
prompt({
title: "Enter the IP address of the server, including the port",
label: "URL:",
value: "",
inputAttrs: {
type: "text"
}
})
.then(r => {
r = r.replace("http://", "").replace(/:[0-9]{4}\/client/gi, "");
mainWindow && mainWindow.loadURL(`http://${r}/client`);
triggerWindow();
})
.then(r => {
r = r.replace("http://", "").replace(":3000/client", "");
storage.set("url", { url: `http://${r}:3000/client` });
mainWindow && mainWindow.loadURL(`http://${r}:3000/client`);
triggerWindow();
})
.catch(console.error);
});
.catch(console.error);
});

// Auto-discovery
const bonjour = require('bonjour')();
bonjour.find({ type: 'http' }, newService);

const bonjour = require("bonjour")();
bonjour.find({ type: "thorium-http" }, newService);
const servers = [];
const autoLoad = () => {
if (servers.length === 1) {
mainWindow.loadURL(servers[0].url);
triggerWindow();
} else if (servers.length === 0) {
setTimeout(autoLoad, 3000);
}
};
setTimeout(autoLoad, 3000);
function newService(service) {
if (service.name === "Thorium" && service.type === 'http') {
const ipregex = /[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}/gi
if (service.type === "app") {
const ipregex = /[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}\.[0-2]?[0-9]{1,2}/gi;
const address = service.addresses.find(a => ipregex.test(a));
const uri = `http://${address}:${service.port || 3000}/client`;
if (uri !== mainWindow.webContents.getURL().replace(`#`, ``)) {
mainWindow.loadURL(uri);
storage.set("url", { url: uri });
triggerWindow();
}
servers.push({
name: service.name,
url: uri
});
setTimeout(() => {
mainWindow.webContents.send("updateServers", servers);
}, 500);
}
}
});



function triggerWindow() {
mainWindow.setKiosk(true);

Expand All @@ -146,7 +142,7 @@ function triggerWindow() {
mainWindow.webContents.openDevTools();
});

globalShortcut.register("CommandOrControl+Q", function(evt) {
globalShortcut.register("CommandOrControl+Q", function() {
// Do nothing.
});

Expand All @@ -160,6 +156,7 @@ function triggerWindow() {

globalShortcut.register("CommandOrControl+Alt+I", function() {
// Do nothing.
mainWindow.webContents.openDevTools();
});
globalShortcut.register("CommandOrControl+Alt+K", function() {
if (mainWindow.isKiosk()) {
Expand Down
Loading

0 comments on commit c682ca3

Please sign in to comment.