Skip to content

Commit

Permalink
feat: add api key
Browse files Browse the repository at this point in the history
feat: add api key
  • Loading branch information
Cafe137 authored Apr 4, 2022
2 parents 9639279 + 12f5ead commit 9982791
Show file tree
Hide file tree
Showing 30 changed files with 129 additions and 91 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ build
bee
data-dir
config.yaml
api-key.txt

# secret
.env
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Try out the packaged version

`./release.sh`
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { shell } = require('electron')
const { getApiKey } = require('./src/api-key')
const { runElectronTray } = require('./src/electron')
const { runLauncher } = require('./src/launcher')
const { findFreePort, port } = require('./src/port')
Expand All @@ -7,13 +8,13 @@ const { getStatus } = require('./src/status')

async function main() {
await findFreePort()
runServer()
runElectronTray()
if (getStatus().status === 2) {
runLauncher()
} else {
shell.openExternal(`http://localhost:${port.value}/installer/`)
shell.openExternal(`http://localhost:${port.value}/installer/?v=${getApiKey()}`)
}
runElectronTray()
runServer()
}

main()
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-static": "^5.0.0",
"node-fetch": "^2.6.7"
"node-fetch": "^2.6.7",
"uuid": "^8.3.2"
},
"devDependencies": {
"electron": "^17.1.0",
"electron-builder": "^22.14.13"
},
"build": {
"mac": {
"target": "mas"
}
}
}
6 changes: 6 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
npm run dist
cp bee dist/mas-arm64/
cp icon.png dist/mas-arm64/
cp tray.png dist/mas-arm64/
cp [email protected] dist/mas-arm64/
cp -r static dist/mas-arm64/
14 changes: 14 additions & 0 deletions src/api-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { existsSync, writeFileSync, readFileSync } = require('fs')
const { v4 } = require('uuid')
const { resolvePath } = require('./path')

function getApiKey() {
if (!existsSync(resolvePath('api-key.txt'))) {
const apiKey = v4()
writeFileSync(resolvePath('api-key.txt'), apiKey)
return apiKey
}
return readFileSync(resolvePath('api-key.txt'), 'utf-8')
}

module.exports = { getApiKey }
3 changes: 2 additions & 1 deletion src/config-yaml.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { load, dump } = require('js-yaml')
const { readFileSync, writeFileSync } = require('fs')
const { FAILSAFE_SCHEMA } = require('js-yaml')
const { resolvePath } = require('./path')

function getPath() {
return 'config.yaml'
return resolvePath('config.yaml')
}

function readConfigYaml() {
Expand Down
16 changes: 12 additions & 4 deletions src/electron.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { app, Tray, Menu, shell } = require('electron')
const { getApiKey } = require('./api-key')
const { runLauncher } = require('./launcher')
const { BeeManager } = require('./lifecycle')
const { resolvePath } = require('./path')
const { port } = require('./port')
const { getStatus } = require('./status')

Expand All @@ -12,7 +14,10 @@ function rebuildElectronTray() {
}
if (getStatus().status !== 2) {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open Installer', click: () => shell.openExternal(`http://localhost:${port.value}/installer/`) },
{
label: 'Open Installer',
click: () => shell.openExternal(`http://localhost:${port.value}/installer/?v=${getApiKey()}`)
},
{ type: 'separator' },
{
label: 'Exit',
Expand All @@ -36,7 +41,10 @@ function rebuildElectronTray() {
}
},
{ type: 'separator' },
{ label: 'Open Web UI', click: () => shell.openExternal(`http://localhost:${port.value}/dashboard/#/`) },
{
label: 'Open Web UI',
click: () => shell.openExternal(`http://localhost:${port.value}/dashboard/?v=${getApiKey()}#/`)
},
{ type: 'separator' },
{
label: 'Exit',
Expand All @@ -52,9 +60,9 @@ function rebuildElectronTray() {

function main() {
app.whenReady().then(() => {
app.dock.setIcon('icon.png')
app.dock.setIcon(resolvePath('icon.png'))
app.dock.hide()
tray = new Tray('tray.png')
tray = new Tray(resolvePath('tray.png'))
rebuildElectronTray()
})
}
Expand Down
28 changes: 14 additions & 14 deletions src/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,45 @@ const { exit } = require('process')
const { resolve } = require('path')
const { spawn } = require('child_process')
const { BeeManager } = require('./lifecycle')
const { resolvePath } = require('./path')

async function createConfigFileAndAddress() {
writeFileSync('config.yaml', createStubConfiguration())
writeFileSync(resolvePath('config.yaml'), createStubConfiguration())
await initializeBee()
}

async function createInitialTransaction() {
const config = readFileSync('config.yaml', 'utf-8')
const config = readFileSync(resolvePath('config.yaml'), 'utf-8')
if (!config.includes('block-hash')) {
const { address } = JSON.parse(readFileSync('data-dir/keys/swarm.key'))
const { address } = JSON.parse(readFileSync(resolvePath('data-dir/keys/swarm.key')))
console.log('Sending transaction to address', address)
const { transaction, blockHash } = await sendTransaction(address)
writeFileSync('config.yaml', createConfiguration(transaction, blockHash))
writeFileSync(resolvePath('config.yaml'), createConfiguration(transaction, blockHash))
}
}

async function main() {
const { rebuildElectronTray } = require('./electron')
const abortController = new AbortController()
if (!existsSync('bee')) {
if (!existsSync(resolvePath('bee'))) {
console.error(`Please compile bee and place it as follows: ${resolve('bee')}`)
exit(1)
}
if (!existsSync('data-dir')) {
mkdirSync('data-dir')
if (!existsSync(resolvePath('data-dir'))) {
mkdirSync(resolvePath('data-dir'))
}
if (!existsSync('config.yaml')) {
writeFileSync('config.yaml', createStubConfiguration())
if (!existsSync(resolvePath('config.yaml'))) {
writeFileSync(resolvePath('config.yaml'), createStubConfiguration())
}
if (!existsSync('data-dir/keys/swarm.key')) {
if (!existsSync(resolvePath('data-dir/keys/swarm.key'))) {
await launchBee().catch(() => {})
}
const config = readFileSync('config.yaml', 'utf-8')
const config = readFileSync(resolvePath('config.yaml'), 'utf-8')
if (!config.includes('block-hash')) {
const { address } = JSON.parse(readFileSync('data-dir/keys/swarm.key'))
const { address } = JSON.parse(readFileSync(resolvePath('data-dir/keys/swarm.key')))
console.log('Sending transaction to address', address)
const { transaction, blockHash } = await sendTransaction(address)
writeFileSync('config.yaml', createConfiguration(transaction, blockHash))
writeFileSync(resolvePath('config.yaml'), createConfiguration(transaction, blockHash))
}
const subprocess = launchBee(abortController).catch(reason => {
console.error(reason)
Expand Down Expand Up @@ -72,7 +73,6 @@ mainnet: true
full-node: false
chain-enable: false
cors-allowed-origins: '*'
resolver-options: https://cloudflare-eth.com
use-postage-snapshot: true
data-dir: ${resolve('data-dir')}`
}
Expand Down
18 changes: 18 additions & 0 deletions src/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { join } = require('path')
const { app } = require('electron')

function resolvePath(path) {
if (process.execPath.includes('node_modules/electron/dist/Electron.app')) {
return path
}
const appName = `${app.getName()}.app`
let execPath = process.execPath
if (execPath.includes(appName)) {
execPath = execPath.split(appName)[0]
}
return join(execPath, path)
}

module.exports = {
resolvePath
}
13 changes: 12 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ const Router = require('@koa/router')
const Koa = require('koa')
const koaBodyparser = require('koa-bodyparser')
const serve = require('koa-static')
const { getApiKey } = require('./api-key')
const { writeConfigYaml, readConfigYaml } = require('./config-yaml')
const { createInitialTransaction, createConfigFileAndAddress, runLauncher } = require('./launcher')
const { BeeManager } = require('./lifecycle')
const { resolvePath } = require('./path')
const { port } = require('./port')
const { getStatus } = require('./status')

function runServer() {
const app = new Koa()
app.use(serve('static'))
app.use(serve(resolvePath('static')))
app.use(async (context, next) => {
context.set('Access-Control-Allow-Origin', `http://localhost:${port.value}`)
context.set('Access-Control-Allow-Credentials', 'true')
Expand All @@ -21,6 +23,15 @@ function runServer() {
context.set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS')
await next()
})
app.use(async (context, next) => {
const { authorization } = context.headers
if (authorization !== getApiKey()) {
context.status = 401
context.body = 'Unauthorized'
return
}
await next()
})
app.use(koaBodyparser())
const router = new Router()
router.get('/status', context => {
Expand Down
6 changes: 3 additions & 3 deletions src/status.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const { readFileSync, existsSync } = require('fs')
const { load } = require('js-yaml')
const { readConfigYaml } = require('./config-yaml')
const { resolvePath } = require('./path')

function getStatus() {
const statusObject = {
status: 0,
address: null,
config: null
}
if (!existsSync('config.yaml') || !existsSync('data-dir')) {
if (!existsSync(resolvePath('config.yaml')) || !existsSync(resolvePath('data-dir'))) {
return statusObject
}
statusObject.config = readConfigYaml()
const { address } = JSON.parse(readFileSync('data-dir/keys/swarm.key'))
const { address } = JSON.parse(readFileSync(resolvePath('data-dir/keys/swarm.key')))
statusObject.address = address
if (!statusObject.config['block-hash']) {
statusObject.status = 1
Expand Down
6 changes: 3 additions & 3 deletions static/dashboard/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"files": {
"main.css": "./static/css/main.59b0af6f.chunk.css",
"main.js": "./static/js/main.16628c3e.chunk.js",
"main.js.map": "./static/js/main.16628c3e.chunk.js.map",
"main.js": "./static/js/main.cfa2d25c.chunk.js",
"main.js.map": "./static/js/main.cfa2d25c.chunk.js.map",
"runtime-main.js": "./static/js/runtime-main.fb7b86cd.js",
"runtime-main.js.map": "./static/js/runtime-main.fb7b86cd.js.map",
"static/js/2.b42b2095.chunk.js": "./static/js/2.b42b2095.chunk.js",
Expand All @@ -19,6 +19,6 @@
"static/js/runtime-main.fb7b86cd.js",
"static/js/2.b42b2095.chunk.js",
"static/css/main.59b0af6f.chunk.css",
"static/js/main.16628c3e.chunk.js"
"static/js/main.cfa2d25c.chunk.js"
]
}
2 changes: 1 addition & 1 deletion static/dashboard/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Bee Dashboard"/><link rel="apple-touch-icon" href="./logo192.png"/><link rel="manifest" href="./manifest.json"/><title>Bee Dashboard</title><link href="./static/css/main.59b0af6f.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,a,i=r[0],c=r[1],l=r[2],f=0,p=[];f<i.length;f++)a=i[f],Object.prototype.hasOwnProperty.call(o,a)&&o[a]&&p.push(o[a][0]),o[a]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(s&&s(r);p.length;)p.shift()();return u.push.apply(u,l||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,i=1;i<t.length;i++){var c=t[i];0!==o[c]&&(n=!1)}n&&(u.splice(r--,1),e=a(a.s=t[0]))}return e}var n={},o={1:0},u=[];function a(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,a),t.l=!0,t.exports}a.e=function(e){var r=[],t=o[e];if(0!==t)if(t)r.push(t[2]);else{var n=new Promise((function(r,n){t=o[e]=[r,n]}));r.push(t[2]=n);var u,i=document.createElement("script");i.charset="utf-8",i.timeout=120,a.nc&&i.setAttribute("nonce",a.nc),i.src=function(e){return a.p+"static/js/"+({}[e]||e)+"."+{3:"d87d34cb"}[e]+".chunk.js"}(e);var c=new Error;u=function(r){i.onerror=i.onload=null,clearTimeout(l);var t=o[e];if(0!==t){if(t){var n=r&&("load"===r.type?"missing":r.type),u=r&&r.target&&r.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,t[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:i})}),12e4);i.onerror=i.onload=u,document.head.appendChild(i)}return Promise.all(r)},a.m=e,a.c=n,a.d=function(e,r,t){a.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,r){if(1&r&&(e=a(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(a.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)a.d(t,n,function(r){return e[r]}.bind(null,n));return t},a.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(r,"a",r),r},a.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},a.p="./",a.oe=function(e){throw console.error(e),e};var i=this["webpackJsonp@ethersphere/bee-dashboard"]=this["webpackJsonp@ethersphere/bee-dashboard"]||[],c=i.push.bind(i);i.push=r,i=i.slice();for(var l=0;l<i.length;l++)r(i[l]);var s=c;t()}([])</script><script src="./static/js/2.b42b2095.chunk.js"></script><script src="./static/js/main.16628c3e.chunk.js"></script></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Bee Dashboard"/><link rel="apple-touch-icon" href="./logo192.png"/><link rel="manifest" href="./manifest.json"/><title>Bee Dashboard</title><link href="./static/css/main.59b0af6f.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,a,i=r[0],c=r[1],l=r[2],f=0,p=[];f<i.length;f++)a=i[f],Object.prototype.hasOwnProperty.call(o,a)&&o[a]&&p.push(o[a][0]),o[a]=0;for(n in c)Object.prototype.hasOwnProperty.call(c,n)&&(e[n]=c[n]);for(s&&s(r);p.length;)p.shift()();return u.push.apply(u,l||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,i=1;i<t.length;i++){var c=t[i];0!==o[c]&&(n=!1)}n&&(u.splice(r--,1),e=a(a.s=t[0]))}return e}var n={},o={1:0},u=[];function a(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,a),t.l=!0,t.exports}a.e=function(e){var r=[],t=o[e];if(0!==t)if(t)r.push(t[2]);else{var n=new Promise((function(r,n){t=o[e]=[r,n]}));r.push(t[2]=n);var u,i=document.createElement("script");i.charset="utf-8",i.timeout=120,a.nc&&i.setAttribute("nonce",a.nc),i.src=function(e){return a.p+"static/js/"+({}[e]||e)+"."+{3:"d87d34cb"}[e]+".chunk.js"}(e);var c=new Error;u=function(r){i.onerror=i.onload=null,clearTimeout(l);var t=o[e];if(0!==t){if(t){var n=r&&("load"===r.type?"missing":r.type),u=r&&r.target&&r.target.src;c.message="Loading chunk "+e+" failed.\n("+n+": "+u+")",c.name="ChunkLoadError",c.type=n,c.request=u,t[1](c)}o[e]=void 0}};var l=setTimeout((function(){u({type:"timeout",target:i})}),12e4);i.onerror=i.onload=u,document.head.appendChild(i)}return Promise.all(r)},a.m=e,a.c=n,a.d=function(e,r,t){a.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,r){if(1&r&&(e=a(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(a.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)a.d(t,n,function(r){return e[r]}.bind(null,n));return t},a.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(r,"a",r),r},a.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},a.p="./",a.oe=function(e){throw console.error(e),e};var i=this["webpackJsonp@ethersphere/bee-dashboard"]=this["webpackJsonp@ethersphere/bee-dashboard"]||[],c=i.push.bind(i);i.push=r,i=i.slice();for(var l=0;l<i.length;l++)r(i[l]);var s=c;t()}([])</script><script src="./static/js/2.b42b2095.chunk.js"></script><script src="./static/js/main.cfa2d25c.chunk.js"></script></body></html>
2 changes: 0 additions & 2 deletions static/dashboard/static/js/main.16628c3e.chunk.js

This file was deleted.

1 change: 0 additions & 1 deletion static/dashboard/static/js/main.16628c3e.chunk.js.map

This file was deleted.

2 changes: 2 additions & 0 deletions static/dashboard/static/js/main.cfa2d25c.chunk.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/dashboard/static/js/main.cfa2d25c.chunk.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions static/installer/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"files": {
"main.css": "/installer/static/css/main.cfed29a4.css",
"main.js": "/installer/static/js/main.443fc15b.js",
"main.js": "/installer/static/js/main.2a4a9dc7.js",
"index.html": "/installer/index.html",
"main.cfed29a4.css.map": "/installer/static/css/main.cfed29a4.css.map",
"main.443fc15b.js.map": "/installer/static/js/main.443fc15b.js.map"
"main.2a4a9dc7.js.map": "/installer/static/js/main.2a4a9dc7.js.map"
},
"entrypoints": [
"static/css/main.cfed29a4.css",
"static/js/main.443fc15b.js"
"static/js/main.2a4a9dc7.js"
]
}
2 changes: 0 additions & 2 deletions static/installer/css/main.cfed29a4.css

This file was deleted.

Loading

0 comments on commit 9982791

Please sign in to comment.