📄 octuna / setup.js
| 1 | const crypto = require('crypto'); |
| 2 | const readline = require('readline'); |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | const config = require('./lib/config'); |
| 7 | const auth = require('./lib/auth'); |
| 8 | const data = require('./lib/data'); |
| 9 | const validate = require('./lib/validate'); |
| 10 | |
| 11 | const WEAK_PASSWORDS = new Set([ |
| 12 | 'password', 'password1', '1234567890', 'qwertyuiop', 'admin12345', |
| 13 | 'letmein123', 'welcome123', 'iloveyou1', 'sunshine1', 'princess1' |
| 14 | ]); |
| 15 | |
| 16 | function ask(rl, question) { |
| 17 | return new Promise(resolve => rl.question(question, resolve)); |
| 18 | } |
| 19 | |
| 20 | async function main() { |
| 21 | const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); |
| 22 | |
| 23 | console.log('otgit setup'); |
| 24 | console.log('-----------'); |
| 25 | console.log(''); |
| 26 | |
| 27 | let username = (await ask(rl, 'Admin username [admin]: ')).trim() || 'admin'; |
| 28 | username = username.toLowerCase(); |
| 29 | if (!validate.isValidName(username)) { |
| 30 | console.error('Invalid username. Use lowercase letters, numbers, hyphens, underscores.'); |
| 31 | rl.close(); process.exit(1); |
| 32 | } |
| 33 | |
| 34 | let password; |
| 35 | while (true) { |
| 36 | password = (await ask(rl, 'Admin password: ')).trim(); |
| 37 | if (password.length < 10) { console.log('Too short. Minimum 10 characters.'); continue; } |
| 38 | if (WEAK_PASSWORDS.has(password.toLowerCase())) { console.log('Password too common.'); continue; } |
| 39 | const confirm = (await ask(rl, 'Confirm password: ')).trim(); |
| 40 | if (password !== confirm) { console.log('Passwords do not match.'); continue; } |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | let publicUrl = (await ask(rl, 'Public URL [http://localhost:8080]: ')).trim() |
| 45 | || 'http://localhost:8080'; |
| 46 | publicUrl = publicUrl.replace(/\/$/, ''); |
| 47 | |
| 48 | let port = parseInt((await ask(rl, 'Port [8080]: ')).trim(), 10); |
| 49 | if (!port || port < 1 || port > 65535) port = 8080; |
| 50 | |
| 51 | rl.close(); |
| 52 | |
| 53 | const sessionSecret = crypto.randomBytes(32).toString('base64'); |
| 54 | const { passwordHash, passwordSalt } = auth.createPasswordRecord(password); |
| 55 | |
| 56 | const configUpdates = { |
| 57 | 'admin.user': username, |
| 58 | 'admin.passwordHash': passwordHash, |
| 59 | 'admin.passwordSalt': passwordSalt, |
| 60 | 'auth.sessionSecret': sessionSecret, |
| 61 | 'site.publicUrl': publicUrl, |
| 62 | 'site.port': port |
| 63 | }; |
| 64 | |
| 65 | const configPath = path.join(__dirname, 'config.json'); |
| 66 | if (!fs.existsSync(configPath)) { |
| 67 | fs.copyFileSync(path.join(__dirname, 'config.example.json'), configPath); |
| 68 | config.reload(); |
| 69 | } |
| 70 | |
| 71 | config.setMany(configUpdates); |
| 72 | |
| 73 | const users = data.users(); |
| 74 | if (!users.has(username)) { |
| 75 | users.set(username, { |
| 76 | username, |
| 77 | passwordHash, |
| 78 | passwordSalt, |
| 79 | displayName: username, |
| 80 | bio: '', |
| 81 | createdAt: Date.now() |
| 82 | }); |
| 83 | users.flush(); |
| 84 | } |
| 85 | |
| 86 | const dataDir = path.join(__dirname, 'data'); |
| 87 | fs.mkdirSync(dataDir, { recursive: true }); |
| 88 | fs.mkdirSync(path.join(dataDir, 'repos'), { recursive: true }); |
| 89 | |
| 90 | console.log(''); |
| 91 | console.log('Setup complete.'); |
| 92 | console.log(`Admin user: ${username}`); |
| 93 | console.log(`Run: npm start`); |
| 94 | } |
| 95 | |
| 96 | main().catch(e => { |
| 97 | console.error('Setup failed:', e.message); |
| 98 | process.exit(1); |
| 99 | }); |
| 100 |