-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from LiquidGalaxyLAB/firebase-as-host
Firebase as host
- Loading branch information
Showing
14 changed files
with
314 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,76 @@ | ||
{ | ||
"rules": { | ||
"up": { | ||
"$publicIp": { | ||
"servers": { | ||
"$uid": { | ||
".write": "auth.token.email.beginsWith($uid + '@')", | ||
".read": true, | ||
".validate": "$publicIp.matches(/^[0-9]{1,3}%[0-9]{1,3}%[0-9]{1,3}%[0-9]{1,3}$/)", | ||
"$reportUid": { | ||
".write": "!data.exists() && newData.exists()", | ||
".validate": "newData.hasChildren(['localIp', 'port', 'timestamp'])", | ||
"localIp": { | ||
".validate": "newData.val().matches(/^(10|172|192)\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/)" | ||
"displayName": { | ||
".validate": "newData.val().length < 200" | ||
}, | ||
"lastOnline": { | ||
".validate": "newData.val() === now" | ||
}, | ||
"isOnline": { | ||
".validate": "newData.isBoolean()" | ||
}, | ||
"hasPassword": { | ||
".validate": "newData.isBoolean()" | ||
}, | ||
"$other": { | ||
".validate": false | ||
} | ||
} | ||
}, | ||
"passwords": { | ||
"$serverUid": { | ||
".write": "auth.token.email.beginsWith($serverUid + '@') || newData.val() === data.val()", | ||
".read": false, | ||
".validate": "newData.isString()" | ||
} | ||
}, | ||
"queue": { | ||
"$serverUidWithKey": { | ||
".write": true, | ||
".read": true, | ||
"$queueUid": { | ||
"type": { | ||
".validate": "newData.isString()" | ||
}, | ||
"port": { | ||
".validate": "newData.val() > 0 && newData.val() < 65535" | ||
"value": { | ||
".validate": "newData.isString()" | ||
}, | ||
"timestamp": { | ||
".validate": "newData.val() === now" | ||
}, | ||
"optional": { | ||
"$other": { | ||
".validate": false | ||
} | ||
} | ||
} | ||
}, | ||
"ips": { | ||
"$ip": { | ||
".read": true, | ||
".validate": "$ip.matches(/^[0-9]{1,3}:[0-9]{1,3}:[0-9]{1,3}:[0-9]{1,3}$/)", | ||
"$serverUid": { | ||
".write": "auth.token.email.beginsWith($serverUid + '@') || newData.val() === data.val()", | ||
"displayName": { | ||
".validate": "newData.val().length < 200" | ||
}, | ||
"$other": { | ||
"lastOnline": { | ||
".validate": "newData.val() === now" | ||
}, | ||
"isOnline": { | ||
".validate": "newData.isBoolean()" | ||
}, | ||
"$other":{ | ||
".validate": false | ||
} | ||
} | ||
} | ||
}, | ||
"$other": { | ||
".validate": false | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { CronJob } = require('cron'); | ||
|
||
const log = require('../helpers/log'); | ||
|
||
class CronTask { | ||
constructor(name, cronTime, action) { | ||
const newAction = async () => { | ||
log.dev(`[CRON] Started "${name}"`); | ||
await action(); | ||
log.dev(`[CRON] Finished "${name}"`); | ||
}; | ||
this.action = newAction; | ||
this.cronJob = new CronJob(cronTime, newAction); | ||
} | ||
|
||
executeOnce() { | ||
return this.action(); | ||
} | ||
|
||
start() { | ||
this.cronJob.start(); | ||
} | ||
} | ||
|
||
module.exports = CronTask; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,3 @@ | ||
const { CronJob } = require('cron'); | ||
const CronTask = require('./CronTask'); | ||
|
||
const log = require('../helpers/log'); | ||
const { up } = require('../services'); | ||
|
||
class CronTask { | ||
constructor(name, cronTime, action) { | ||
const newAction = async () => { | ||
log.dev(`[CRON] Started "${name}"`); | ||
await action(); | ||
log.dev(`[CRON] Finished "${name}"`); | ||
}; | ||
this.cronJob = new CronJob(cronTime, newAction); | ||
} | ||
} | ||
|
||
const cronTasks = [ | ||
new CronTask('Report Alive', '0,30 * * * * *', () => up.reportAlive()), | ||
]; | ||
|
||
function startAll() { | ||
this.cronTasks.map(cronTask => cronTask.cronJob.start()); | ||
} | ||
|
||
module.exports = { | ||
cronTasks, | ||
startAll, | ||
}; | ||
module.exports = { CronTask }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"env": { | ||
"mocha": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { expect } = require('chai'); | ||
const { encodeUid } = require('../utils'); | ||
|
||
describe('Firebase Auth', () => { | ||
it('encoded email address should decode alright', () => { | ||
const UID = 'loLlipOp'; | ||
const expected = [108, 111, 76, 108, 105, 112, 79, 112].join(''); | ||
expect(encodeUid(UID)).to.equal(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Authenticate server. | ||
* Signs up new server with random credentials (stored in data/credentials.txt) | ||
* Sign in with the stored credentials. | ||
* (format deviceUid:editKey:devicePassword) | ||
*/ | ||
|
||
const Promise = require('bluebird'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const mkdirp = require('mkdirp'); | ||
const shortid = require('shortid'); | ||
const generatePassword = require('generate-password'); | ||
const firebase = require('firebase'); | ||
|
||
const { encodeUid } = require('./utils'); | ||
|
||
const readFile = Promise.promisify(fs.readFile); | ||
const writeFile = Promise.promisify(fs.writeFile); | ||
const createDir = Promise.promisify(mkdirp); | ||
|
||
const CREDENTIALS_PATH = path.join('data/credentials.txt'); | ||
|
||
async function readCredentials() { | ||
try { | ||
const contents = await readFile(CREDENTIALS_PATH, { encoding: 'utf-8' }); | ||
const [uid, editKey, password] = contents.split(':'); | ||
return [uid, editKey, password]; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
async function generateCredentials() { | ||
const uid = shortid.generate(); | ||
const editKey = generatePassword.generate({ | ||
length: 20, | ||
numbers: true, | ||
}); | ||
const password = ''; | ||
return [uid, editKey, password]; | ||
} | ||
|
||
async function saveCredentials(values) { | ||
const contents = values.join(':'); | ||
await createDir(path.join(CREDENTIALS_PATH, '..')); | ||
return writeFile(CREDENTIALS_PATH, contents); | ||
} | ||
|
||
function encodeEmail(uid) { | ||
const encodedUid = encodeUid(uid); | ||
return `${encodedUid}@firebase.com`; | ||
} | ||
|
||
function signup([uid, editKey]) { | ||
const emailVal = encodeEmail(uid); | ||
const passwordVal = editKey; | ||
return firebase.auth().createUserWithEmailAndPassword(emailVal, passwordVal); | ||
} | ||
|
||
function signin([uid, editKey]) { | ||
const emailVal = encodeEmail(uid); | ||
const passwordVal = editKey; | ||
return firebase.auth().signInWithEmailAndPassword(emailVal, passwordVal); | ||
} | ||
|
||
module.exports = async () => { | ||
let credentials = await readCredentials(); | ||
if (!credentials) { | ||
credentials = await generateCredentials(); | ||
await saveCredentials(credentials); | ||
await signup(credentials); | ||
} | ||
await signin(credentials); | ||
return credentials; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,35 @@ | ||
const firebase = require('firebase'); | ||
const config = require('config'); | ||
const log = require('../helpers/log'); | ||
|
||
const CronTask = require('../cron/CronTask'); | ||
const auth = require('./auth'); | ||
const server = require('./server'); | ||
const queue = require('./queue'); | ||
|
||
const firebaseConfig = config.get('firebase'); | ||
|
||
function initialize() { | ||
firebase.initializeApp(firebaseConfig); | ||
return auth(); | ||
} | ||
|
||
function bgReportAlive(serverUid) { | ||
const cron = new CronTask('Report Alive', '0,30 * * * * *', () => server.reportAlive(serverUid)); | ||
cron.executeOnce(); | ||
cron.start(); | ||
} | ||
|
||
function bgListenQueue(serverUid) { | ||
queue.listenQueue(serverUid); | ||
} | ||
|
||
async function start() { | ||
const [uid, , password] = await initialize(); | ||
log.info(`[Firebase] Signed in as ${uid} (${password ? `password ${password}` : 'no password'})`); | ||
|
||
bgReportAlive(uid); | ||
bgListenQueue(uid); | ||
} | ||
|
||
module.exports = { | ||
initialize, | ||
}; | ||
module.exports = { start }; |
Oops, something went wrong.