Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
[wip] #59
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Apr 21, 2018
1 parent a483127 commit 9549022
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .configs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Description
Please create and place necessary config files by copying `*.default.config`, and edit it.

# List of config files
- client-config.json
- server-config.json
4 changes: 4 additions & 0 deletions .configs/client-config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$version": 1,
"apiHost": "localhost:8000"
}
19 changes: 19 additions & 0 deletions .configs/server-config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$version": 1,
"port": 8001,
"apiHost": "localhost:8000",
"session": {
"name": "sid",
"SecretToken": "paste_your_session_secret_token"
},
"applicationId": "paste_your_application_id",
"hostAccessToken": "paste_your_host_access_token",
"accessTokenScopes": {
"clientSide": ["user.read", "user.write", "post.read", "post.write", "storage.read", "storage.write"],
"session": ["app.create"]
},
"reCAPTCHA": {
"siteKey": "paste_your_reCAPTCHA_site_key",
"secretKey": "paste_your_reCAPTCHA_secret_key"
}
}
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package-lock.json
node_modules
built
.vscode
*.log
*.old
/config*.json
package-lock.json

/.configs/*
!/.configs/README.md
!/.configs/*.default.json
8 changes: 4 additions & 4 deletions src/server/helpers/create-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = async (req, streamingRest, config) => {
const getToken = async (scopes) => {
let tokenResult = await streamingRest.request('get', '/auth/tokens', {
query: {
applicationId: config.web.applicationId,
applicationId: config.applicationId,
userId: user.id,
scopes: scopes
}
Expand All @@ -35,7 +35,7 @@ module.exports = async (req, streamingRest, config) => {
if (tokenResult.statusCode == 404) {
tokenResult = await streamingRest.request('post', '/auth/tokens', {
body: {
applicationId: config.web.applicationId,
applicationId: config.applicationId,
userId: user.id,
scopes: scopes
}
Expand All @@ -48,10 +48,10 @@ module.exports = async (req, streamingRest, config) => {
};

// get session accessToken
const sessionToken = await getToken(config.web.accessTokenScopes.session);
const sessionToken = await getToken(config.accessTokenScopes.session);

// get client-side sccessToken
const clientSideToken = await getToken(config.web.accessTokenScopes.clientSide);
const clientSideToken = await getToken(config.accessTokenScopes.clientSide);

req.session.token = sessionToken;
req.session.clientSideToken = clientSideToken;
Expand Down
14 changes: 7 additions & 7 deletions src/server/httpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module.exports = async (hostApiConnection, debug, config) => {

app.use(expressSession({
store: sessionStore,
name: config.web.session.name,
secret: config.web.session.SecretToken,
name: config.session.name,
secret: config.session.SecretToken,
cookie: {
httpOnly: false,
maxAge: 7 * 24 * 60 * 60 * 1000 // 7days
Expand All @@ -72,7 +72,7 @@ module.exports = async (hostApiConnection, debug, config) => {
pageParams = Object.assign(pageParams || {}, {
csrf: req.csrfToken(),
isSmartPhone: req.isSmartPhone,
siteKey: config.web.reCAPTCHA.siteKey
siteKey: config.reCAPTCHA.siteKey
});

// memo: クライアントサイドでは、パラメータ中にuserIdが存在するかどうかでWebSocketへの接続が必要かどうかを判断します。このコードはそのために必要です。
Expand Down Expand Up @@ -113,7 +113,7 @@ module.exports = async (hostApiConnection, debug, config) => {
message: 'ok',
accessToken: req.session.clientSideToken.accessToken,
userId: req.session.clientSideToken.userId,
scopes: config.web.accessTokenScopes.clientSide
scopes: config.accessTokenScopes.clientSide
});
}
catch(err) {
Expand Down Expand Up @@ -155,7 +155,7 @@ module.exports = async (hostApiConnection, debug, config) => {
method: 'POST',
json: true,
form: {
secret: config.web.reCAPTCHA.secretKey,
secret: config.reCAPTCHA.secretKey,
response: req.body.recaptchaToken
}
});
Expand All @@ -180,7 +180,7 @@ module.exports = async (hostApiConnection, debug, config) => {
message: 'ok',
accessToken: req.session.clientSideToken.accessToken,
userId: req.session.clientSideToken.userId,
scopes: config.web.accessTokenScopes.clientSide
scopes: config.accessTokenScopes.clientSide
});
}
catch (err) {
Expand Down Expand Up @@ -252,7 +252,7 @@ module.exports = async (hostApiConnection, debug, config) => {
resolve(http);
});
});
const http = await listen(config.web.port);
const http = await listen(config.port);

console.log('[http server]', 'initialized');

Expand Down
24 changes: 4 additions & 20 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const httpServer = require('./httpServer');
const i = require('./helpers/input-async');
const loadConfig = require('./helpers/load-config');
const request = require('request-promise');
const streamingServer = require('./streamingServer');
const ReconnectingWebSocket = require('./helpers/reconnecting-websocket-node');
const events = require('websocket-events');

const urlConfigFile = 'https://raw.githubusercontent.com/Frost-Dev/Frost/master/config.json';

const q = async str => (await i(str)).toLowerCase().indexOf('y') === 0;
const writeFile = promisify(fs.writeFile);

const isDebug = false;

/**
Expand All @@ -25,21 +15,15 @@ module.exports = async () => {
console.log('| Frost Web Server |');
console.log('+------------------+');

console.log('loading config...');
let config = loadConfig();
console.log('loading config file...');
let config = require(path.resolve('.configs/server-config.json'));
if (config == null) {
if (await q('config file is not found. generate now? (y/n) > ')) {
const parent = await q('generate config.json in the parent directory of repository? (y/n) > ');
const configPath = path.resolve(parent ? '../config.json' : 'config.json');
const configJson = await request(urlConfigFile);
await writeFile(configPath, configJson);
console.log('generated. please edit config.json and restart frost-web.');
}
console.log('config file is not found. please refer to .configs/README.md');
return;
}

console.log('connecting to streaming api as host ...');
const hostApiConnection = await ReconnectingWebSocket.connect(`${config.web.apiBaseUrl}?access_token=${config.web.hostAccessToken}`);
const hostApiConnection = await ReconnectingWebSocket.connect(`ws://${config.apiHost}?access_token=${config.hostAccessToken}`);
hostApiConnection.on('error', err => {
if (err.message.indexOf('ECONNRESET') != -1) {
return;
Expand Down
6 changes: 3 additions & 3 deletions src/server/streamingServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (http, sessionStore, debugDetail, config) => {
let frontConnection;
try {
// セッションを取得
const session = await getSessionFromCookie(wsRequest.httpRequest.headers['cookie'], config.web.session.name, config.web.session.SecretToken, sessionStore);
const session = await getSessionFromCookie(wsRequest.httpRequest.headers['cookie'], config.session.name, config.session.SecretToken, sessionStore);

if (session == null || session.token == null) {
return wsRequest.reject(401, 'Unauthorized');
Expand All @@ -27,7 +27,7 @@ module.exports = (http, sessionStore, debugDetail, config) => {
if (debugDetail) {
console.log('[streaming server]', 'connecting streaming api server...');
}
apiConnection = await WebSocketUtility.connect(`${config.web.apiBaseUrl}?access_token=${session.token.accessToken}`);
apiConnection = await WebSocketUtility.connect(`ws://${config.apiHost}?access_token=${session.token.accessToken}`);
events(apiConnection);
}
catch (err) {
Expand Down Expand Up @@ -98,7 +98,7 @@ module.exports = (http, sessionStore, debugDetail, config) => {
const verifyResult = await request('https://www.google.com/recaptcha/api/siteverify', {
method: 'POST',
json: true,
form: { secret: config.web.reCAPTCHA.secretKey, response: data.recaptchaToken }
form: { secret: config.reCAPTCHA.secretKey, response: data.recaptchaToken }
});
if (!verifyResult.success) {
frontConnection.error('app-create', 'invalid recaptcha');
Expand Down

0 comments on commit 9549022

Please sign in to comment.