mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-16 07:30:01 -04:00
autoupdater, use ~/.config or windows equivalent for config
This commit is contained in:
parent
ba77d77036
commit
def0854064
5 changed files with 185 additions and 124 deletions
|
@ -16,6 +16,7 @@
|
|||
"@esbuild-plugins/node-resolve": "^0.2.2",
|
||||
"@inquirer/prompts": "^3.0.2",
|
||||
"@inquirer/select": "^1.2.7",
|
||||
"@types/follow-redirects": "^1.14.1",
|
||||
"boxen": "^7.1.1",
|
||||
"chalk": "^5.3.0",
|
||||
"dotenv": "^16.3.1",
|
||||
|
@ -23,6 +24,7 @@
|
|||
"express": "^4.18.2",
|
||||
"express-ws": "^5.0.2",
|
||||
"firebase": "^10.1.0",
|
||||
"follow-redirects": "^1.15.2",
|
||||
"inquirer": "^9.2.10",
|
||||
"ipaddr.js": "^2.1.0",
|
||||
"isomorphic-ws": "^5.0.0",
|
||||
|
|
24
server/src/autoupdater.ts
Normal file
24
server/src/autoupdater.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { datadir } from "./lib";
|
||||
import { spawn } from "child_process";
|
||||
import fs from "fs";
|
||||
import { https } from 'follow-redirects';
|
||||
let dir = datadir();
|
||||
let platform = `${process.platform}-${process.arch}`
|
||||
let appname = `adrift-server-${platform}`;
|
||||
if (process.platform == "win32") {
|
||||
appname += ".exe";
|
||||
}
|
||||
https.get(
|
||||
"https://github.com/MercuryWorkshop/adrift/releases/latest/download/adrift-server-${}", resp => {
|
||||
let file = fs.createWriteStream(`${dir}/${appname}`);
|
||||
resp.pipe(file);
|
||||
|
||||
|
||||
file.on("finish", () => {
|
||||
fs.chmodSync(`${dir}/${appname}`, "755");
|
||||
setTimeout(() => {
|
||||
// this timeout shouldn't be needed, but it is
|
||||
spawn(`${dir}/${appname}`, [], { stdio: "inherit" });
|
||||
}, 2000);
|
||||
});
|
||||
})
|
8
server/src/lib.ts
Normal file
8
server/src/lib.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import fs from "fs";
|
||||
|
||||
export function datadir(): string {
|
||||
let base = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.config")) + "/adrift-server"
|
||||
if (!fs.existsSync(base))
|
||||
fs.mkdirSync(base);
|
||||
return base;
|
||||
}
|
|
@ -17,134 +17,139 @@ import TrackerList from "tracker-list";
|
|||
|
||||
import fs from "fs";
|
||||
import { exit } from "process";
|
||||
import { datadir } from "./lib";
|
||||
|
||||
async function config() {
|
||||
if (
|
||||
!(await confirm({
|
||||
message:
|
||||
"No config.json found. Would you like to go through first-time setup?",
|
||||
}))
|
||||
)
|
||||
exit(1);
|
||||
console.log(
|
||||
boxen(
|
||||
`${chalk.yellow("")} ${chalk.blue(
|
||||
"Adrift Server Setup"
|
||||
)} ${chalk.yellow("")}`,
|
||||
{ padding: 1 }
|
||||
let dir = datadir();
|
||||
|
||||
if (
|
||||
!(await confirm({
|
||||
message:
|
||||
"No config.json found. Would you like to go through first-time setup?",
|
||||
}))
|
||||
)
|
||||
);
|
||||
const tracker = (await select({
|
||||
message: "Select a central tracker",
|
||||
choices: Object.keys(TrackerList).map((name) => ({ name, value: name })),
|
||||
})) as keyof typeof TrackerList;
|
||||
const type = await select({
|
||||
message: "Select a central tracker",
|
||||
choices: [
|
||||
{
|
||||
value: "swarm",
|
||||
name: "Join global swarm",
|
||||
description:
|
||||
"Allow requests from any Adrift user to connect to your server",
|
||||
},
|
||||
{
|
||||
value: "account",
|
||||
name: "Link to a personal account",
|
||||
description:
|
||||
"Connect to your account, no one but you will be able to connect to the server",
|
||||
},
|
||||
],
|
||||
});
|
||||
exit(1);
|
||||
console.log(
|
||||
boxen(
|
||||
`${chalk.yellow("")} ${chalk.blue(
|
||||
"Adrift Server Setup"
|
||||
)} ${chalk.yellow("")}`,
|
||||
{ padding: 1 }
|
||||
)
|
||||
);
|
||||
const tracker = (await select({
|
||||
message: "Select a central tracker",
|
||||
choices: Object.keys(TrackerList).map((name) => ({ name, value: name })),
|
||||
})) as keyof typeof TrackerList;
|
||||
const type = await select({
|
||||
message: "Select a central tracker",
|
||||
choices: [
|
||||
{
|
||||
value: "swarm",
|
||||
name: "Join global swarm",
|
||||
description:
|
||||
"Allow requests from any Adrift user to connect to your server",
|
||||
},
|
||||
{
|
||||
value: "account",
|
||||
name: "Link to a personal account",
|
||||
description:
|
||||
"Connect to your account, no one but you will be able to connect to the server",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let credentials: any = {};
|
||||
if (type == "account") {
|
||||
initializeApp(TrackerList[tracker].firebase);
|
||||
await login(credentials);
|
||||
}
|
||||
let credentials: any = {};
|
||||
if (type == "account") {
|
||||
initializeApp(TrackerList[tracker].firebase);
|
||||
await login(credentials);
|
||||
}
|
||||
|
||||
let conf = {
|
||||
tracker,
|
||||
type,
|
||||
credentials,
|
||||
};
|
||||
console.log(chalk.bold("Writing choices to config.json..."));
|
||||
let conf = {
|
||||
tracker,
|
||||
type,
|
||||
credentials,
|
||||
};
|
||||
console.log(chalk.bold(`Writing choices to ${dir}/config.json...`));
|
||||
|
||||
fs.writeFile("config.json", JSON.stringify(conf), () => {});
|
||||
return conf;
|
||||
fs.writeFile(`${dir}/config.json`, JSON.stringify(conf), () => { });
|
||||
return conf;
|
||||
}
|
||||
async function login(credentials: any) {
|
||||
for (;;) {
|
||||
credentials.email = await input({
|
||||
message: "Enter your account's email address",
|
||||
});
|
||||
credentials.password = await password({
|
||||
message: "Enter your account's password",
|
||||
});
|
||||
for (; ;) {
|
||||
credentials.email = await input({
|
||||
message: "Enter your account's email address",
|
||||
});
|
||||
credentials.password = await password({
|
||||
message: "Enter your account's password",
|
||||
});
|
||||
|
||||
let auth = getAuth();
|
||||
try {
|
||||
let creds = await signInWithEmailAndPassword(
|
||||
auth,
|
||||
credentials.email,
|
||||
credentials.password
|
||||
);
|
||||
return creds;
|
||||
} catch (err) {
|
||||
console.error(chalk.red(`Error signing in: ${err.code}`));
|
||||
let auth = getAuth();
|
||||
try {
|
||||
let creds = await signInWithEmailAndPassword(
|
||||
auth,
|
||||
credentials.email,
|
||||
credentials.password
|
||||
);
|
||||
return creds;
|
||||
} catch (err) {
|
||||
console.error(chalk.red(`Error signing in: ${err.code}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(async () => {
|
||||
let conf;
|
||||
try {
|
||||
conf = JSON.parse(fs.readFileSync("config.json").toString());
|
||||
} catch {
|
||||
conf = await config();
|
||||
}
|
||||
let tracker = TrackerList[conf.tracker as keyof typeof TrackerList];
|
||||
let dir = datadir();
|
||||
|
||||
console.log(chalk.blue("Starting server!"));
|
||||
if (conf.type == "swarm") {
|
||||
let connect = () => {
|
||||
let trackerws = new WebSocket(tracker.tracker + "/join");
|
||||
trackerws.onclose = () => {
|
||||
console.log(`Disconnected from tracker. Retrying...`);
|
||||
setTimeout(() => {
|
||||
connect();
|
||||
}, 10000);
|
||||
};
|
||||
trackerws.onopen = () => {
|
||||
console.log(`Connected to tracker ${tracker.tracker}`);
|
||||
};
|
||||
connectTracker(trackerws);
|
||||
};
|
||||
connect();
|
||||
} else {
|
||||
initializeApp(tracker.firebase);
|
||||
let conf;
|
||||
try {
|
||||
conf = JSON.parse(fs.readFileSync(`${dir}/config.json`).toString());
|
||||
} catch {
|
||||
conf = await config();
|
||||
}
|
||||
let tracker = TrackerList[conf.tracker as keyof typeof TrackerList];
|
||||
|
||||
let creds = await signInWithEmailAndPassword(
|
||||
getAuth(),
|
||||
conf.credentials.email,
|
||||
conf.credentials.password
|
||||
);
|
||||
console.log(chalk.blue("Starting server!"));
|
||||
if (conf.type == "swarm") {
|
||||
let connect = () => {
|
||||
let trackerws = new WebSocket(tracker.tracker + "/join");
|
||||
trackerws.onclose = () => {
|
||||
console.log(`Disconnected from tracker. Retrying...`);
|
||||
setTimeout(() => {
|
||||
connect();
|
||||
}, 10000);
|
||||
};
|
||||
trackerws.onopen = () => {
|
||||
console.log(`Connected to tracker ${tracker.tracker}`);
|
||||
};
|
||||
connectTracker(trackerws);
|
||||
};
|
||||
connect();
|
||||
} else {
|
||||
initializeApp(tracker.firebase);
|
||||
|
||||
const db = getDatabase();
|
||||
let peer = ref(db, `/peers/${creds.user.uid}`);
|
||||
let creds = await signInWithEmailAndPassword(
|
||||
getAuth(),
|
||||
conf.credentials.email,
|
||||
conf.credentials.password
|
||||
);
|
||||
|
||||
set(peer, "");
|
||||
const db = getDatabase();
|
||||
let peer = ref(db, `/peers/${creds.user.uid}`);
|
||||
|
||||
onValue(peer, (snapshot) => {
|
||||
const str = snapshot.val();
|
||||
set(peer, "");
|
||||
|
||||
if (str) {
|
||||
let data = JSON.parse(str);
|
||||
if (data && data.offer && data.localCandidates) {
|
||||
answerRtc(data, (answer) => {
|
||||
console.log("answering");
|
||||
set(peer, JSON.stringify(answer));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
onValue(peer, (snapshot) => {
|
||||
const str = snapshot.val();
|
||||
|
||||
if (str) {
|
||||
let data = JSON.parse(str);
|
||||
if (data && data.offer && data.localCandidates) {
|
||||
answerRtc(data, (answer) => {
|
||||
console.log("answering");
|
||||
set(peer, JSON.stringify(answer));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue