diff --git a/src/modules/pageRender/page.js b/src/modules/pageRender/page.js index 82f6dd52..56e7eeee 100644 --- a/src/modules/pageRender/page.js +++ b/src/modules/pageRender/page.js @@ -314,7 +314,7 @@ export default function(obj) { ${!isFirefox ? `
- +
` : ''} diff --git a/src/modules/setup.js b/src/modules/setup.js index 4aa98a91..d1aa99cc 100644 --- a/src/modules/setup.js +++ b/src/modules/setup.js @@ -1,14 +1,37 @@ import { randomBytes } from "crypto"; import { existsSync, unlinkSync, appendFileSync } from "fs"; import { createInterface } from "readline"; -import { Cyan, Bright, Green } from "./sub/consoleText.js"; +import { Cyan, Bright, Green , Red} from "./sub/consoleText.js"; import { execSync } from "child_process"; +let ip = false let envPath = './.env'; let q = `${Cyan('?')} \x1b[1m`; let ob = { streamSalt: randomBytes(64).toString('hex') } let rl = createInterface({ input: process.stdin, output: process.stdout }); +const badInputError = "\nNext time enter valid input, ok?" +const unknownError = "\nUnknown error. Please report: https://github.com/wukko/cobalt/issues or can try againg." + +const validNumericPortRegex = "([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])"; +const validIPAddressRegex = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; +const validHostnameRegex = "(((?!\-))(xn\-\-)?[a-z0-9\-_]{0,61}[a-z0-9]{1,1}\.)*(xn\-\-)?([a-z0-9\-]{1,61}|[a-z0-9\-]{1,30})\.[a-z]{2,}"; + +function fullMatchRegexp(s, r) { + var sum_length = 0 + var found = s.match(r) + + if (found !== null) { + sum_length += found[0].length + } + + if (sum_length == s.length) { + return true + } else { + return false + } +} + let final = () => { if (existsSync(envPath)) { unlinkSync(envPath) @@ -22,33 +45,98 @@ let final = () => { console.log(`\n\n${Green("All done!\n")}`) console.log("You can re-run this script any time to update the configuration.") console.log("\nYou're now ready to start the main project.\nHave fun!") - rl.close() } console.log( `${Cyan("Welcome to cobalt!")}\n${Bright("We'll get you up and running in no time.\nLet's start by creating a ")}${Cyan(".env")}${Bright(" file. You can always change it later.")}` ) + console.log( - Bright("\nWhat's the selfURL we'll be running on? (localhost)") + Bright("\nDo you prefer:\n 1) 'http'\n 2) 'https'\n\nFor 'https' on '127.0.0.1', 'localhost', you must turn off 'HTTPS-Only Mode' in your browser.") ) rl.question(q, r1 => { - if (r1) { - ob['selfURL'] = `https://${r1}/` - } else { - ob['selfURL'] = `http://localhost` - } - console.log(Bright("\nGreat! Now, what's the port we'll be running on? (9000)")) - rl.question(q, r2 => { - if (!r1 && !r2) { - ob['selfURL'] = `http://localhost:9000/` - ob['port'] = 9000 - } else if (!r1 && r2) { - ob['selfURL'] = `http://localhost:${r2}/` - ob['port'] = r2 + if (r1.length == 1){ + if (r1 == 1) { // HTTP + ob['selfURL'] = 'http://' + } else if (r1 == 2){ // HTTP(S) + ob['selfURL'] = 'https://' } else { - ob['port'] = r2 + console.error(Red(badInputError)) + process.exit(1) } - final() + } else { + console.error(Red(badInputError)) + process.exit(1) + } + + console.log("\nDo you prefer to specify:\n 1) 'IP'\n 2) 'Domain'") + rl.question(q, r2 => { + console.log(r2) + if (r2.length == 1) { + if (r2 == 1) { // IP + ip = true + } else if (r2 == 2) { // Domain + ip = false + } else { + console.error(Red(badInputError)) + process.exit(1) + } + } else { + console.error(Red(badInputError)) + process.exit(1) + } + + console.log(Bright("\nOn which domain name or IP address will it run? (For local network usage: '127.0.0.1', 'localhost')")) + rl.question(q, r3 => { + if (ip) { // IP + if (r3 === "127.0.0.1") { // '127.0.0.1' + ob['selfURL'] += "127.0.0.1" + } else if (fullMatchRegexp(r3, validIPAddressRegex) && !fullMatchRegexp(r3, validHostnameRegex)) { + console.log(r3) + ob['selfURL'] += `${r3}` + } else { + console.error(Red("You entered the wrong IP-address. If you need to use a domain name, select the correct item. In case of an unknown error and/or regexp is wrong, report: https://github.com/wukko/cobalt/issues.")) + process.exit(1) + } + } else if (!ip) { // Domain + if (r3 === 'localhost') { // 'localhost' is not the same as '127.0.0.1' is 'local domain' + ob['selfURL'] += "localhost" + } else if (!fullMatchRegexp(r3, validIPAddressRegex) && fullMatchRegexp(r3, validHostnameRegex)) { + console.log(r3) + ob['selfURL'] += `${r3}` + } else { + console.error(Red("You entered the wrong domain name. If you need to use a domain name, select the correct item. In case of an unknown error and/or regexp is wrong, report: https://github.com/wukko/cobalt/issues.")) + process.exit(1) + } + } else { + console.error(Red(unknownError)) + process.exit(1) + } + + console.log(Bright("\nGreat! Now, what's the port we'll be running on?")) + rl.question(q, r4 => { + if (!r4.includes(".")){ + if (r4.length <= 5) { + if (fullMatchRegexp(r4, validNumericPortRegex) && parseInt(r4, 10) <= 65535 && parseInt(r4, 10) > 0) { + ob['selfURL'] += `:${r4}/` + ob['port'] = `${r4}` + final() + console.log(Bright("\nGreat! You did it! After start you can access and use the application at only this URL:\n"), ob['selfURL']) + } else { + console.error(Red(badInputError)) + process.exit(1) + } + } else { + console.error(Red(badInputError)) + process.exit(1) + } + } else { + console.error(Red(badInputError)) + process.exit(1) + } + rl.close() + }); + }); }); })