Compare commits
10 commits
feature/vu
...
master
Author | SHA1 | Date | |
---|---|---|---|
e538eab674 | |||
b3abb27446 | |||
4f3d74c2e5 | |||
19880a1a7a | |||
9f177f325e | |||
bbe46961b5 | |||
ea7d97930f | |||
00bac21837 | |||
d8686c94ee | |||
02b4a0a320 |
44 changed files with 416 additions and 8329 deletions
|
@ -1,4 +1,4 @@
|
||||||
FROM node:latest as devStage
|
FROM node:latest
|
||||||
|
|
||||||
# some dir for our code
|
# some dir for our code
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
@ -9,5 +9,8 @@ EXPOSE 3000
|
||||||
COPY package*.json .
|
COPY package*.json .
|
||||||
RUN yarn
|
RUN yarn
|
||||||
|
|
||||||
|
# copy code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
# this is how we start
|
# this is how we start
|
||||||
CMD ["yarn", "watch"]
|
CMD ["yarn", "start"]
|
|
@ -1,108 +0,0 @@
|
||||||
# libraries
|
|
||||||
bcrypt = (require 'bcrypt')
|
|
||||||
sqlite3 = (require 'sqlite3').verbose()
|
|
||||||
|
|
||||||
# bruteforce countermeasure
|
|
||||||
saltRounds = 13
|
|
||||||
|
|
||||||
FFTCGDB = (filename) ->
|
|
||||||
@filename = filename
|
|
||||||
|
|
||||||
@db = new sqlite3.Database @filename, (err) ->
|
|
||||||
if err
|
|
||||||
console.error err.message
|
|
||||||
|
|
||||||
@db.run """
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
login text NOT NULL COLLATE NOCASE,
|
|
||||||
pwdhash text NOT NULL,
|
|
||||||
UNIQUE(login)
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
console.log "[FFTCGDB] Connected to '#{@filename}'"
|
|
||||||
return
|
|
||||||
|
|
||||||
FFTCGDB::close = ->
|
|
||||||
new Promise (resolve, reject) ->
|
|
||||||
@db.close (err) ->
|
|
||||||
if err
|
|
||||||
resolve "[FFTCGDB] Error closing: '#{err.message}'"
|
|
||||||
else
|
|
||||||
reject "[FFTCGDB] Closed '#{@filename}'"
|
|
||||||
|
|
||||||
FFTCGDB::register = (login, password) ->
|
|
||||||
that = @
|
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
|
||||||
# validate user input
|
|
||||||
if login == '' or password == ''
|
|
||||||
# no user name or password given
|
|
||||||
console.log "[FFTCGDB] reg: user name '#{login}' or password empty"
|
|
||||||
reject 'invalid'
|
|
||||||
|
|
||||||
# hash password
|
|
||||||
bcrypt.hash password, saltRounds, (err, hash) ->
|
|
||||||
if err
|
|
||||||
console.log "[FFTCGDB] reg: hash fail for name '#{login}'"
|
|
||||||
reject 'hash'
|
|
||||||
|
|
||||||
# try creating row in users table
|
|
||||||
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
|
|
||||||
stmt.run [login, hash], (err) ->
|
|
||||||
if err
|
|
||||||
console.log "[FFTCGDB] reg: DB fail '#{err.code}' for name '#{login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
# reduce attack surface, don't disclose user names
|
|
||||||
reject 'db' # user already exists
|
|
||||||
|
|
||||||
else
|
|
||||||
console.log "[FFTCGDB] reg: OK '#{login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
# registration successful
|
|
||||||
resolve
|
|
||||||
uid: @lastID
|
|
||||||
login: login
|
|
||||||
|
|
||||||
FFTCGDB::login = (login, password) ->
|
|
||||||
that = @
|
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
|
||||||
# get users table row
|
|
||||||
stmt = that.db.prepare 'SELECT rowid, login, pwdhash FROM users WHERE login = ?'
|
|
||||||
stmt.get [login], (err, row) ->
|
|
||||||
if err
|
|
||||||
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
reject 'db'
|
|
||||||
|
|
||||||
else if not row
|
|
||||||
# hash the password for timing attack reasons
|
|
||||||
bcrypt.hash password, saltRounds, (err, hash) ->
|
|
||||||
console.log "[FFTCGDB] login: nonexistent '#{login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
# reduce attack surface, don't disclose user names
|
|
||||||
reject 'login' # user doesnt exist
|
|
||||||
|
|
||||||
else
|
|
||||||
bcrypt.compare password, row.pwdhash, (err, res) ->
|
|
||||||
if err
|
|
||||||
console.log "[FFTCGDB] login: hash fail for name '#{login}'"
|
|
||||||
reject 'hash'
|
|
||||||
|
|
||||||
if res == true
|
|
||||||
console.log "[FFTCGDB] login: OK '#{row.login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
# login successful
|
|
||||||
resolve
|
|
||||||
uid: row.rowid
|
|
||||||
login: row.login
|
|
||||||
|
|
||||||
else
|
|
||||||
console.log "[FFTCGDB] login: wrong password for '#{login}'"
|
|
||||||
stmt.finalize()
|
|
||||||
# login failed
|
|
||||||
reject 'login'
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = FFTCGDB
|
|
|
@ -1,34 +0,0 @@
|
||||||
{
|
|
||||||
"name": "yvk-fftcg-backend",
|
|
||||||
"version": "0.1.0",
|
|
||||||
|
|
||||||
"description": "FFTCG online using Socket.IO and CraftyJS on Node.js on Docker",
|
|
||||||
"author": "JMM <jmm@yavook.de>",
|
|
||||||
|
|
||||||
"main": "server.coffee",
|
|
||||||
"private": true,
|
|
||||||
"license": "UNLICENSED",
|
|
||||||
|
|
||||||
"scripts": {
|
|
||||||
"start": "coffee server.coffee",
|
|
||||||
"watch": "nodemon server.coffee",
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
|
|
||||||
"dependencies": {
|
|
||||||
"bcrypt": "^3.0.2",
|
|
||||||
"body-parser": "^1.18.3",
|
|
||||||
"coffeescript": "^2.3.2",
|
|
||||||
"connect-redis": "^3.4.0",
|
|
||||||
"express": "^4.16.4",
|
|
||||||
"express-session": "^1.15.6",
|
|
||||||
"express-socket.io-session": "^1.3.5",
|
|
||||||
"helmet": "^3.15.0",
|
|
||||||
"socket.io": "^2.2.0",
|
|
||||||
"sqlite3": "^4.0.4"
|
|
||||||
},
|
|
||||||
|
|
||||||
"devDependencies": {
|
|
||||||
"nodemon": "^1.18.9"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,18 +1,20 @@
|
||||||
version: "2"
|
version: "2"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
fftcg:
|
||||||
|
build: .
|
||||||
|
command: "yarn debug"
|
||||||
|
restart: "no"
|
||||||
|
volumes:
|
||||||
|
- "${PWD}/src:/app/src"
|
||||||
|
- "${PWD}/views:/app/views:ro"
|
||||||
|
- "${PWD}/public_html:/app/public_html"
|
||||||
|
- "${PWD}/inc:/app/inc:ro"
|
||||||
|
- "${PWD}/server.coffee:/app/server.coffee:ro"
|
||||||
|
# - "${PWD}/fftcg.db:/app/fftcg.db"
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:alpine
|
image: redis:alpine
|
||||||
restart: "no"
|
restart: "no"
|
||||||
|
|
||||||
backend:
|
|
||||||
build:
|
|
||||||
context: ./backend
|
|
||||||
target: devStage
|
|
||||||
restart: "no"
|
|
||||||
volumes:
|
|
||||||
- "${PWD}/backend/inc:/app/inc:ro"
|
|
||||||
- "${PWD}/backend/server.coffee:/app/server.coffee:ro"
|
|
||||||
# - "${PWD}/backend/fftcg.db:/app/fftcg.db"
|
|
||||||
ports:
|
|
||||||
- "3000:3000"
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
> 1%
|
|
||||||
last 2 versions
|
|
||||||
not ie <= 8
|
|
|
@ -1,17 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
root: true,
|
|
||||||
env: {
|
|
||||||
node: true
|
|
||||||
},
|
|
||||||
'extends': [
|
|
||||||
'plugin:vue/essential',
|
|
||||||
'eslint:recommended'
|
|
||||||
],
|
|
||||||
rules: {
|
|
||||||
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
|
|
||||||
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
|
|
||||||
},
|
|
||||||
parserOptions: {
|
|
||||||
parser: 'babel-eslint'
|
|
||||||
}
|
|
||||||
}
|
|
21
frontend/.gitignore
vendored
21
frontend/.gitignore
vendored
|
@ -1,21 +0,0 @@
|
||||||
.DS_Store
|
|
||||||
node_modules
|
|
||||||
/dist
|
|
||||||
|
|
||||||
# local env files
|
|
||||||
.env.local
|
|
||||||
.env.*.local
|
|
||||||
|
|
||||||
# Log files
|
|
||||||
npm-debug.log*
|
|
||||||
yarn-debug.log*
|
|
||||||
yarn-error.log*
|
|
||||||
|
|
||||||
# Editor directories and files
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
*.suo
|
|
||||||
*.ntvs*
|
|
||||||
*.njsproj
|
|
||||||
*.sln
|
|
||||||
*.sw*
|
|
|
@ -1,29 +0,0 @@
|
||||||
# frontend
|
|
||||||
|
|
||||||
## Project setup
|
|
||||||
```
|
|
||||||
yarn install
|
|
||||||
```
|
|
||||||
|
|
||||||
### Compiles and hot-reloads for development
|
|
||||||
```
|
|
||||||
yarn run serve
|
|
||||||
```
|
|
||||||
|
|
||||||
### Compiles and minifies for production
|
|
||||||
```
|
|
||||||
yarn run build
|
|
||||||
```
|
|
||||||
|
|
||||||
### Run your tests
|
|
||||||
```
|
|
||||||
yarn run test
|
|
||||||
```
|
|
||||||
|
|
||||||
### Lints and fixes files
|
|
||||||
```
|
|
||||||
yarn run lint
|
|
||||||
```
|
|
||||||
|
|
||||||
### Customize configuration
|
|
||||||
See [Configuration Reference](https://cli.vuejs.org/config/).
|
|
|
@ -1,5 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
presets: [
|
|
||||||
'@vue/app'
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
{
|
|
||||||
"name": "yvk-fftcg-frontend",
|
|
||||||
"version": "0.1.0",
|
|
||||||
|
|
||||||
"description": "FFTCG online using Socket.IO and CraftyJS on Node.js on Docker",
|
|
||||||
"author": "JMM <jmm@yavook.de>",
|
|
||||||
|
|
||||||
"private": true,
|
|
||||||
"license": "UNLICENSED",
|
|
||||||
|
|
||||||
"scripts": {
|
|
||||||
"serve": "vue-cli-service serve",
|
|
||||||
"build": "vue-cli-service build",
|
|
||||||
"lint": "vue-cli-service lint"
|
|
||||||
},
|
|
||||||
|
|
||||||
"dependencies": {
|
|
||||||
"vue": "^2.5.21"
|
|
||||||
},
|
|
||||||
|
|
||||||
"devDependencies": {
|
|
||||||
"craftyjs": "^0.9.0",
|
|
||||||
"bootstrap": "^4.1.3",
|
|
||||||
"jquery": "^3.3.1",
|
|
||||||
"popper.js": "^1.14.5",
|
|
||||||
"socket.io-client": "^2.2.0",
|
|
||||||
|
|
||||||
"@vue/cli-plugin-babel": "^3.3.0",
|
|
||||||
"@vue/cli-plugin-eslint": "^3.3.0",
|
|
||||||
"@vue/cli-service": "^3.3.0",
|
|
||||||
"babel-eslint": "^10.0.1",
|
|
||||||
"eslint": "^5.8.0",
|
|
||||||
"eslint-plugin-vue": "^5.0.0",
|
|
||||||
"pug": "^2.0.3",
|
|
||||||
"pug-plain-loader": "^1.0.0",
|
|
||||||
"vue-template-compiler": "^2.5.21"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
plugins: {
|
|
||||||
autoprefixer: {}
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
Before Width: | Height: | Size: 1.1 KiB |
|
@ -1,17 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
|
||||||
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
|
|
||||||
<title>frontend</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<noscript>
|
|
||||||
<strong>We're sorry but frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
|
|
||||||
</noscript>
|
|
||||||
<div id="app"></div>
|
|
||||||
<!-- built files will be auto injected -->
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,28 +0,0 @@
|
||||||
<template>
|
|
||||||
<div id="app">
|
|
||||||
<img alt="Vue logo" src="./assets/logo.png">
|
|
||||||
<HelloWorld msg="Welcome to Your Vue.js App"/>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import HelloWorld from './components/HelloWorld.vue'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'app',
|
|
||||||
components: {
|
|
||||||
HelloWorld
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
#app {
|
|
||||||
font-family: 'Avenir', Helvetica, Arial, sans-serif;
|
|
||||||
-webkit-font-smoothing: antialiased;
|
|
||||||
-moz-osx-font-smoothing: grayscale;
|
|
||||||
text-align: center;
|
|
||||||
color: #2c3e50;
|
|
||||||
margin-top: 60px;
|
|
||||||
}
|
|
||||||
</style>
|
|
Binary file not shown.
Before Width: | Height: | Size: 6.7 KiB |
|
@ -1,58 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="hello">
|
|
||||||
<h1>{{ msg }}</h1>
|
|
||||||
<p>
|
|
||||||
For a guide and recipes on how to configure / customize this project,<br>
|
|
||||||
check out the
|
|
||||||
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
|
|
||||||
</p>
|
|
||||||
<h3>Installed CLI Plugins</h3>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
|
|
||||||
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
|
|
||||||
</ul>
|
|
||||||
<h3>Essential Links</h3>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
|
|
||||||
<li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
|
|
||||||
<li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
|
|
||||||
<li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
|
|
||||||
<li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
|
|
||||||
</ul>
|
|
||||||
<h3>Ecosystem</h3>
|
|
||||||
<ul>
|
|
||||||
<li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
|
|
||||||
<li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
|
|
||||||
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
|
|
||||||
<li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
|
|
||||||
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
name: 'HelloWorld',
|
|
||||||
props: {
|
|
||||||
msg: String
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<!-- Add "scoped" attribute to limit CSS to this component only -->
|
|
||||||
<style scoped>
|
|
||||||
h3 {
|
|
||||||
margin: 40px 0 0;
|
|
||||||
}
|
|
||||||
ul {
|
|
||||||
list-style-type: none;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
li {
|
|
||||||
display: inline-block;
|
|
||||||
margin: 0 10px;
|
|
||||||
}
|
|
||||||
a {
|
|
||||||
color: #42b983;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -1,8 +0,0 @@
|
||||||
import Vue from 'vue'
|
|
||||||
import App from './App.vue'
|
|
||||||
|
|
||||||
Vue.config.productionTip = false
|
|
||||||
|
|
||||||
new Vue({
|
|
||||||
render: h => h(App),
|
|
||||||
}).$mount('#app')
|
|
7920
frontend/yarn.lock
7920
frontend/yarn.lock
File diff suppressed because it is too large
Load diff
11
inc/console.coffee
Normal file
11
inc/console.coffee
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
FFTCGLOG = (unit) ->
|
||||||
|
@unit = unit
|
||||||
|
return
|
||||||
|
|
||||||
|
FFTCGLOG::log = (msg) ->
|
||||||
|
console.log "[#{@unit}] #{msg}"
|
||||||
|
|
||||||
|
FFTCGLOG::error = (msg) ->
|
||||||
|
console.error "[#{@unit}] #{msg}"
|
||||||
|
|
||||||
|
module.exports = FFTCGLOG
|
222
inc/db.coffee
Normal file
222
inc/db.coffee
Normal file
|
@ -0,0 +1,222 @@
|
||||||
|
# libraries
|
||||||
|
bcrypt = (require 'bcrypt')
|
||||||
|
sqlite3 = (require 'sqlite3').verbose()
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGDB')
|
||||||
|
|
||||||
|
# bruteforce countermeasure
|
||||||
|
saltRounds = 13
|
||||||
|
|
||||||
|
FFTCGDB = (filename, truncate) ->
|
||||||
|
that = @
|
||||||
|
@filename = filename
|
||||||
|
|
||||||
|
@db = new sqlite3.Database @filename, (err) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.error err.message
|
||||||
|
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "Connected to '#{that.filename}'"
|
||||||
|
|
||||||
|
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
|
||||||
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
|
if truncate == true
|
||||||
|
that.db.run 'DROP TABLE IF EXISTS users;', (err) ->
|
||||||
|
FFTCGLOG.error err.message if err
|
||||||
|
that.db.run '''
|
||||||
|
CREATE TABLE users (
|
||||||
|
user integer PRIMARY KEY,
|
||||||
|
login text NOT NULL COLLATE NOCASE,
|
||||||
|
pwdhash text NOT NULL,
|
||||||
|
UNIQUE(login)
|
||||||
|
);
|
||||||
|
''', (err) ->
|
||||||
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
|
that.db.run 'DROP TABLE IF EXISTS decks;', (err) ->
|
||||||
|
FFTCGLOG.error err.message if err
|
||||||
|
that.db.run '''
|
||||||
|
CREATE TABLE decks (
|
||||||
|
deck integer PRIMARY KEY,
|
||||||
|
user integer NOT NULL,
|
||||||
|
json text,
|
||||||
|
FOREIGN KEY (user) REFERENCES users (user)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
''', (err) ->
|
||||||
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
|
FFTCGLOG.log 'recreated DB'
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
FFTCGDB::close = ->
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
@db.close (err) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "Error closing: '#{err.message}'"
|
||||||
|
resolve 'ok'
|
||||||
|
else
|
||||||
|
FFTCGLOG.error "Closed '#{@filename}'"
|
||||||
|
reject 'db'
|
||||||
|
|
||||||
|
FFTCGDB::register = (login, password) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# validate user input
|
||||||
|
if login == '' or password == ''
|
||||||
|
# no user name or password given
|
||||||
|
FFTCGLOG.log "reg: user name '#{login}' or password empty"
|
||||||
|
reject 'invalid'
|
||||||
|
|
||||||
|
# hash password
|
||||||
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "reg: hash fail for name '#{login}'"
|
||||||
|
reject 'hash'
|
||||||
|
|
||||||
|
# try creating row in users table
|
||||||
|
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
|
||||||
|
stmt.run [login, hash], (err) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "reg: DB fail '#{err.code}' for name '#{login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
# reduce attack surface, don't disclose user names
|
||||||
|
reject 'db' # user already exists
|
||||||
|
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "reg: OK '#{login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
# registration successful
|
||||||
|
resolve
|
||||||
|
user: @lastID
|
||||||
|
login: login
|
||||||
|
|
||||||
|
FFTCGDB::login = (login, password) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# get users table row
|
||||||
|
stmt = that.db.prepare 'SELECT user, login, pwdhash FROM users WHERE login = ?'
|
||||||
|
stmt.get [login], (err, row) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "login: DB fail '#{err.code}' for name '#{login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
reject 'db'
|
||||||
|
|
||||||
|
else if not row
|
||||||
|
# hash the password for timing attack reasons
|
||||||
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||||||
|
FFTCGLOG.log "login: nonexistent '#{login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
# reduce attack surface, don't disclose user names
|
||||||
|
reject 'login' # user doesnt exist
|
||||||
|
|
||||||
|
else
|
||||||
|
bcrypt.compare password, row.pwdhash, (err, res) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "login: hash fail for name '#{login}'"
|
||||||
|
reject 'hash'
|
||||||
|
|
||||||
|
if res == true
|
||||||
|
FFTCGLOG.log "login: OK '#{row.login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
# login successful
|
||||||
|
resolve
|
||||||
|
user: row.user
|
||||||
|
login: row.login
|
||||||
|
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "login: wrong password for '#{login}'"
|
||||||
|
stmt.finalize()
|
||||||
|
# login failed
|
||||||
|
reject 'login'
|
||||||
|
|
||||||
|
FFTCGDB::addDeck = (user, deckCards) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# try creating row in decks table
|
||||||
|
stmt = that.db.prepare 'INSERT INTO decks (user, json) VALUES (?, ?)'
|
||||||
|
stmt.run [user, JSON.stringify deckCards], (err) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "addDeck: DB fail '#{err.code}' for id '#{user}'"
|
||||||
|
stmt.finalize()
|
||||||
|
reject 'db'
|
||||||
|
|
||||||
|
else
|
||||||
|
stmt.finalize()
|
||||||
|
# deck added successfully, now add cards
|
||||||
|
that.modDeck(@lastID, deckCards)
|
||||||
|
.then (deckID) ->
|
||||||
|
resolve deckID
|
||||||
|
.catch (error) ->
|
||||||
|
reject error
|
||||||
|
|
||||||
|
FFTCGDB::modDeck = (deckID, deckCards) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# delete old deck cards
|
||||||
|
stmt = that.db.prepare 'DELETE FROM decks_cards WHERE deck = ?'
|
||||||
|
stmt.run [deckID], (err) ->
|
||||||
|
stmt.finalize()
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "modDeck: DB fail '#{err.code}' for deck '#{deckID}'"
|
||||||
|
reject 'db'
|
||||||
|
else
|
||||||
|
stmt = that.db.prepare 'INSERT INTO decks_cards (deck, card, quant) VALUES (?, ?, ?)'
|
||||||
|
# add new cards
|
||||||
|
that.db.parallelize ->
|
||||||
|
# needs to be done in several queries
|
||||||
|
promiseCount = deckCards.length
|
||||||
|
deckCards.forEach (card) ->
|
||||||
|
stmt.run [deckID, card.id, card.quant], (err) ->
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "modDeck: DB fail '#{err.code}' for card '#{deckID}', '#{card.id}', '#{card.quant}'"
|
||||||
|
stmt.finalize()
|
||||||
|
reject 'db'
|
||||||
|
else
|
||||||
|
# check if all queries are done
|
||||||
|
promiseCount -= 1
|
||||||
|
if promiseCount == 0
|
||||||
|
FFTCGLOG.log "modDeck: OK '#{deckID}'"
|
||||||
|
stmt.finalize()
|
||||||
|
resolve deckID
|
||||||
|
|
||||||
|
FFTCGDB::getDecks = (user) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# try deleting correct row in decks table
|
||||||
|
decks = {}
|
||||||
|
stmt = that.db.prepare 'SELECT decks.deck, decks.json FROM decks INNER JOIN users ON decks.user = users.user WHERE users.user = ?'
|
||||||
|
stmt.all [user], (err, rows) ->
|
||||||
|
stmt.finalize()
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "getDeck: DB fail '#{err.code}' for deck '#{deckID}'"
|
||||||
|
reject 'db'
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "getDeck: OK '#{deckID}'"
|
||||||
|
for row in rows
|
||||||
|
decks[row.deck] = JSON.parse row.json
|
||||||
|
resolve decks
|
||||||
|
|
||||||
|
FFTCGDB::delDeck = (deckID) ->
|
||||||
|
that = @
|
||||||
|
|
||||||
|
new Promise (resolve, reject) ->
|
||||||
|
# try deleting correct row in decks table
|
||||||
|
stmt = that.db.prepare 'DELETE FROM decks WHERE deck = ?'
|
||||||
|
stmt.run [deckID], (err) ->
|
||||||
|
stmt.finalize()
|
||||||
|
if err
|
||||||
|
FFTCGLOG.log "delDeck: DB fail '#{err.code}' for deck '#{deckID}'"
|
||||||
|
reject 'db'
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "delDeck: OK '#{deckID}'"
|
||||||
|
resolve deckID
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = FFTCGDB
|
|
@ -3,14 +3,26 @@ express = (require 'express')
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
FFTCGDB = (require './fftcgdb')
|
FFTCGDB = (require './db')
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGROUTER')
|
||||||
|
|
||||||
# open fftcg db
|
# open fftcg db
|
||||||
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
|
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
|
||||||
|
|
||||||
# create router
|
# create router
|
||||||
FFTCGROUTER = express.Router()
|
FFTCGROUTER = express.Router()
|
||||||
# FFTCGROUTER.use express.static path.resolve(__dirname, '../public_html')
|
|
||||||
|
# request logging
|
||||||
|
FFTCGROUTER.use (req, res, next) ->
|
||||||
|
if req.session.user
|
||||||
|
FFTCGLOG.log "user '#{req.session.user.login}' requested '#{req.url}'"
|
||||||
|
else
|
||||||
|
FFTCGLOG.log "requested '#{req.url}'"
|
||||||
|
|
||||||
|
next()
|
||||||
|
|
||||||
|
# static content
|
||||||
|
FFTCGROUTER.use express.static path.resolve(__dirname, '../public_html')
|
||||||
|
|
||||||
# register user
|
# register user
|
||||||
FFTCGROUTER.post '/register', (req, res) ->
|
FFTCGROUTER.post '/register', (req, res) ->
|
||||||
|
@ -19,7 +31,7 @@ FFTCGROUTER.post '/register', (req, res) ->
|
||||||
# registration successful, return JSON status
|
# registration successful, return JSON status
|
||||||
res.json
|
res.json
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
uid: user.id
|
user: user.user
|
||||||
login: user.login
|
login: user.login
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
|
@ -39,7 +51,7 @@ FFTCGROUTER.post '/login', (req, res) ->
|
||||||
# return JSON status
|
# return JSON status
|
||||||
res.json
|
res.json
|
||||||
status: 'ok'
|
status: 'ok'
|
||||||
uid: user.uid
|
user: user.user
|
||||||
login: user.login
|
login: user.login
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
|
@ -48,4 +60,23 @@ FFTCGROUTER.post '/login', (req, res) ->
|
||||||
status: 'fail'
|
status: 'fail'
|
||||||
text: err
|
text: err
|
||||||
|
|
||||||
|
# Templates
|
||||||
|
FFTCGROUTER.get '/:template.html', (req, res) ->
|
||||||
|
# redirect logged-in users to user cp
|
||||||
|
if req.session.user and req.params.template == 'index'
|
||||||
|
return res.redirect '/usercp.html'
|
||||||
|
|
||||||
|
# render requested template
|
||||||
|
res.render (req.params.template + '.pug'), (err, html) ->
|
||||||
|
# redirect invalid requests to index
|
||||||
|
if err
|
||||||
|
return res.redirect '/index.html'
|
||||||
|
|
||||||
|
# actual response
|
||||||
|
res.send html
|
||||||
|
|
||||||
|
# default route
|
||||||
|
FFTCGROUTER.use (req, res) ->
|
||||||
|
return res.redirect '/index.html'
|
||||||
|
|
||||||
module.exports = FFTCGROUTER
|
module.exports = FFTCGROUTER
|
|
@ -1,6 +1,7 @@
|
||||||
# node libraries
|
# node libraries
|
||||||
socketio = (require 'socket.io')
|
socketio = (require 'socket.io')
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGSOCKET')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
|
|
||||||
|
@ -14,17 +15,17 @@ FFTCGSOCKET = (http, session) ->
|
||||||
# on new connection
|
# on new connection
|
||||||
@io.on 'connection', (socket) ->
|
@io.on 'connection', (socket) ->
|
||||||
@session = socket.handshake.session
|
@session = socket.handshake.session
|
||||||
console.log "session '#{@session.id}' connected"
|
FFTCGLOG.log "session '#{@session.id}' connected"
|
||||||
console.log "is user '#{@session.userID}'" if @session.userID
|
FFTCGLOG.log "is user '#{@session.userID}'" if @session.userID
|
||||||
|
|
||||||
socket.on 'disconnect', ->
|
socket.on 'disconnect', ->
|
||||||
console.log "session '#{that.session.id}' disconnected"
|
FFTCGLOG.log "session '#{that.session.id}' disconnected"
|
||||||
console.log "is user '#{that.session.userID}'" if that.session.userID
|
FFTCGLOG.log "is user '#{that.session.userID}'" if that.session.userID
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
FFTCGSOCKET::close = ->
|
FFTCGSOCKET::close = ->
|
||||||
console.log '[FFTCGSOCKET] shutting down'
|
FFTCGLOG.log 'shutting down'
|
||||||
if @db
|
if @db
|
||||||
@db.close()
|
@db.close()
|
||||||
.then (msg) ->
|
.then (msg) ->
|
57
package.json
Normal file
57
package.json
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
{
|
||||||
|
"name": "node-fftcg",
|
||||||
|
"version": "0.0.3",
|
||||||
|
|
||||||
|
"description": "FFTCG online using Socket.IO and CraftyJS on Node.js on Docker",
|
||||||
|
"author": "JMM <jmm@yavook.de>",
|
||||||
|
|
||||||
|
"main": "server.coffee",
|
||||||
|
"private": true,
|
||||||
|
"license": "UNLICENSED",
|
||||||
|
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"watch": "webpack --watch",
|
||||||
|
"start": "webpack && coffee server.coffee",
|
||||||
|
"debug": "webpack --watch & nodemon server.coffee",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "^7.1.6",
|
||||||
|
"@babel/preset-env": "^7.1.6",
|
||||||
|
"coffee-loader": "^0.9.0",
|
||||||
|
|
||||||
|
"autoprefixer": "^9.3.1",
|
||||||
|
"css-loader": "^1.0.1",
|
||||||
|
"postcss-loader": "^3.0.0",
|
||||||
|
"node-sass": "^4.10.0",
|
||||||
|
"precss": "^3.1.2",
|
||||||
|
"sass-loader": "^7.1.0",
|
||||||
|
"style-loader": "^0.23.1",
|
||||||
|
|
||||||
|
"nodemon": "^1.18.9",
|
||||||
|
"webpack": "^4.25.1",
|
||||||
|
"webpack-cli": "^3.1.2",
|
||||||
|
|
||||||
|
"bootstrap": "^4.1.3",
|
||||||
|
"craftyjs": "^0.9.0",
|
||||||
|
"jquery": "^3.3.1",
|
||||||
|
"popper.js": "^1.14.5"
|
||||||
|
},
|
||||||
|
|
||||||
|
"dependencies": {
|
||||||
|
"bcrypt": "^3.0.2",
|
||||||
|
"body-parser": "^1.18.3",
|
||||||
|
"coffeescript": "^2.3.2",
|
||||||
|
"connect-redis": "^3.4.0",
|
||||||
|
"express": "^4.16.4",
|
||||||
|
"express-session": "^1.15.6",
|
||||||
|
"express-socket.io-session": "^1.3.5",
|
||||||
|
"helmet": "^3.15.0",
|
||||||
|
"pug": "^2.0.3",
|
||||||
|
"socket.io": "^2.2.0",
|
||||||
|
"socket.io-client": "^2.2.0",
|
||||||
|
"sqlite3": "^4.0.4"
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 610 KiB After Width: | Height: | Size: 610 KiB |
Before Width: | Height: | Size: 615 KiB After Width: | Height: | Size: 615 KiB |
|
@ -7,9 +7,10 @@ http = (require 'http')
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
FFTCGSOCKET = (require './inc/fftcgsocket')
|
FFTCGSOCKET = (require './inc/socket')
|
||||||
FFTCGSESSION = (require './inc/fftcgsession')
|
FFTCGSESSION = (require './inc/session')
|
||||||
FFTCGROUTER = (require './inc/fftcgrouter')
|
FFTCGROUTER = (require './inc/router')
|
||||||
|
FFTCGLOG = new (require './inc/console')('FFTCG')
|
||||||
|
|
||||||
# express framework
|
# express framework
|
||||||
app = express()
|
app = express()
|
||||||
|
@ -24,23 +25,16 @@ app.use sessionMiddleware
|
||||||
# routes
|
# routes
|
||||||
app.use FFTCGROUTER
|
app.use FFTCGROUTER
|
||||||
|
|
||||||
# Templates
|
|
||||||
app.set 'view engine', 'pug'
|
|
||||||
app.get '/:template.html', (req, res) ->
|
|
||||||
if req.session.user
|
|
||||||
console.log "[FFTCG] user is '#{req.session.user.login}'"
|
|
||||||
res.render (req.params.template + '.pug')
|
|
||||||
|
|
||||||
# socket.io
|
# socket.io
|
||||||
web = http.Server app
|
web = http.Server app
|
||||||
socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
|
socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
|
||||||
|
|
||||||
# Create server
|
# Create server
|
||||||
web.listen 3000, ->
|
web.listen 3000, ->
|
||||||
console.log '[FFTCG] Listening on port 3000 ...'
|
FFTCGLOG.log 'Listening on port 3000 ...'
|
||||||
|
|
||||||
# Handle termination
|
# Handle termination
|
||||||
process.on 'SIGINT', ->
|
process.on 'SIGINT', ->
|
||||||
socket.close()
|
socket.close()
|
||||||
console.log '[FFTCG] shutting down after SIGINT'
|
FFTCGLOG.log 'shutting down after SIGINT'
|
||||||
process.exit()
|
process.exit()
|
|
@ -49,6 +49,7 @@ $ ->
|
||||||
if data.status == 'ok'
|
if data.status == 'ok'
|
||||||
that.fullReset()
|
that.fullReset()
|
||||||
showAlert 'success', "successfully logged in '#{data.login}'"
|
showAlert 'success', "successfully logged in '#{data.login}'"
|
||||||
|
location.reload()
|
||||||
|
|
||||||
else
|
else
|
||||||
switch data.text
|
switch data.text
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
html, body {
|
html, body {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
overflow: hidden;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/usercp.coffee
Normal file
11
src/usercp.coffee
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# libs
|
||||||
|
window.$ = require('jquery')
|
||||||
|
|
||||||
|
# import bootstrap
|
||||||
|
require './style/custom.scss'
|
||||||
|
require 'bootstrap/js/dist/alert'
|
||||||
|
require 'bootstrap/js/dist/collapse'
|
||||||
|
|
||||||
|
# on load
|
||||||
|
$ ->
|
||||||
|
return
|
45
views/usercp.pug
Normal file
45
views/usercp.pug
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
doctype html
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Crafty Things
|
||||||
|
script(src='/usercp.bundle.js')
|
||||||
|
body
|
||||||
|
|
||||||
|
header.jumbotron.jumbotron-fluid.py-4.bg-primary.text-light.text-center
|
||||||
|
div.container
|
||||||
|
h1 Hello World!
|
||||||
|
h2 App under development, please don't submit any valuable data!
|
||||||
|
|
||||||
|
div.container.bg-light
|
||||||
|
h3 Yavook!FFTCG
|
||||||
|
div#alert-area
|
||||||
|
div.row
|
||||||
|
div.col-md-8
|
||||||
|
h4 My Decks
|
||||||
|
ul.list-group#my-decks
|
||||||
|
li.list-group-item
|
||||||
|
div.row
|
||||||
|
div.col-3.btn-group(role="group")
|
||||||
|
button.btn.btn-secondary(data-toggle="collapse" data-target="#modDeck0") Edit
|
||||||
|
button.btn.btn-secondary#delDeck0 Delete
|
||||||
|
div.col-9.align-self-center item1
|
||||||
|
|
||||||
|
form.collapse.pt-2#modDeck0(name="modDeck0")
|
||||||
|
div.form-group
|
||||||
|
label(for="deck") Decklist:
|
||||||
|
textarea.form-control.form-control-sm.text-monospace(name="deck" rows="20" required)
|
||||||
|
|
||||||
|
div.form-group
|
||||||
|
button.btn.btn-primary.w-100(type="submit") Confirm
|
||||||
|
|
||||||
|
p Hello
|
||||||
|
|
||||||
|
div.col-md-4
|
||||||
|
h4 New Deck
|
||||||
|
form(name="addDeck")
|
||||||
|
div.form-group
|
||||||
|
label(for="deck") Paste decklist:
|
||||||
|
textarea.form-control.form-control-sm.text-monospace(name="deck" rows="20" required)
|
||||||
|
|
||||||
|
div.form-group
|
||||||
|
button.btn.btn-primary.w-100(type="submit") Add
|
|
@ -5,7 +5,8 @@ module.exports = {
|
||||||
|
|
||||||
entry: {
|
entry: {
|
||||||
index: './src/index.coffee',
|
index: './src/index.coffee',
|
||||||
game: './src/game.coffee'
|
game: './src/game.coffee',
|
||||||
|
usercp: './src/usercp.coffee'
|
||||||
},
|
},
|
||||||
|
|
||||||
devtool: 'inline-source-map',
|
devtool: 'inline-source-map',
|
Reference in a new issue