Skip to content

Commit

Permalink
feat: https support
Browse files Browse the repository at this point in the history
  • Loading branch information
tyanbiao committed Nov 26, 2020
1 parent c051751 commit 76ed372
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 7 deletions.
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const application = {
password: 'root', // 登录密码
host: '127.0.0.1',
port: 3000,
sslEnabled: true, // 是否启用 https
sslCert: 'ssl/server.crt', // 证书
sslKey: 'ssl/server.key', // 私钥
sslKeyPassphrase: '', // 私钥密码,可选
}

const session = {
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ fava main.bean --host 127.0.0.1 --prefix /fava
}

const application = {
username: 'root', // 设置用户名
password: 'root', // 设置登录密码
username: 'root', // 登录用户名
password: 'root', // 登录密码
host: '127.0.0.1',
port: 3000,
sslEnabled: true, // 是否启用 https
sslCert: 'ssl/server.crt', // 证书
sslKey: 'ssl/server.key', // 私钥
sslKeyPassphrase: '', // 私钥密码,可选
}
```

Expand All @@ -50,6 +54,7 @@ fava main.bean --host 127.0.0.1 --prefix /fava
```sh
npm start
```
启动成功后打印:application is running on https://127.0.0.1:3000

## 扩展
beancount-auth 默认会将 `/src/static/inject.js` 注入到 Fava 的 HTML,如果你需要扩展 Fava 的 UI,只要在这个文件里加入你的逻辑即可。
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const path = require('path')
const fs = require('fs')
const https = require('https')
const http = require('http')
const Koa = require('koa')
const session = require('koa-session')
const views = require('koa-views')
Expand Down Expand Up @@ -54,6 +57,18 @@ app.use(bodyParser())
app.use(router.routes()).use(router.allowedMethods())

const {port, host} = applicationConf
app.listen(port, host, () => {
console.log(`application is running on ${host}:${port}`)
})

if (applicationConf.sslEnabled) {
const options = {
key: fs.readFileSync(path.resolve(process.cwd(), applicationConf.sslKey)),
cert: fs.readFileSync(path.resolve(process.cwd(), applicationConf.sslCert)),
passphrase: applicationConf.sslKeyPassphrase,
}
https.createServer(options, app.callback()).listen(port, host, () => {
console.log(`application is running on https://${host}:${port}`)
})
} else {
http.createServer(app.callback()).listen(port, host, () => {
console.log(`application is running on http://${host}:${port}`)
})
}
16 changes: 16 additions & 0 deletions src/middleware/inject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const http = require('http')
const url = require('url')
const {injectContent} = require('../../config').beancount

module.exports = (request, response, options) => {
Expand All @@ -21,6 +22,13 @@ module.exports = (request, response, options) => {
resolve()
})
} else {
if (pResonse.statusCode === 302 || pResonse.statusCode === 301) {
response.setHeader(
'location',
convertLocation(response.getHeader('location')),
)
}
response.setHeader('X-Forwarded-Proto', 'https')
pResonse.pipe(response)
pResonse.on('end', resolve)
}
Expand All @@ -45,3 +53,11 @@ function contentIsHTML(resonseIncoming) {
contentType.indexOf('text/html') === 0
)
}

// 去除 302 location 中的协议,fava 重定向会默认使用 http 协议
function convertLocation(location) {
if (!location) {
return location
}
return location.replace(/^https?\:/i, '')
}
5 changes: 3 additions & 2 deletions src/view/login.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
.then(res => {
if (res.data && res.data.code === 0) {
console.log('登录成功')
const redirect = new URL(location.href).searchParams.get('redirect') || '/'
location.href = redirect
const url = new URL(location.href)
url.pathname = url.searchParams.get('redirect') || '/'
location.href = url.href
} else {
alert(res.data.message)
}
Expand Down
20 changes: 20 additions & 0 deletions ssl/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDWzCCAkMCFDj/HcB7XNxxMbBt9hq3BtKP/AwgMA0GCSqGSIb3DQEBCwUAMGox
CzAJBgNVBAYTAkNOMRIwEAYDVQQIDAlHdWFuZ0RvbmcxDjAMBgNVBAcMBVNoemVu
MRAwDgYDVQQKDAdUZW5jZW50MQwwCgYDVQQLDANURUQxFzAVBgNVBAMMDjEyNy4w
LjAuMTozMDAwMB4XDTIwMTEyNjA1MTkxNVoXDTIxMTEyNjA1MTkxNVowajELMAkG
A1UEBhMCQ04xEjAQBgNVBAgMCUd1YW5nRG9uZzEOMAwGA1UEBwwFU2h6ZW4xEDAO
BgNVBAoMB1RlbmNlbnQxDDAKBgNVBAsMA1RFRDEXMBUGA1UEAwwOMTI3LjAuMC4x
OjMwMDAwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDG+hJBeXApbXi3
NA6vhvYUF6b+Qz+x5Ydx4cN6bio9u6ms5i7YSg+6CuA204hiusSTA7sfD1vU0XHy
Rpjd4lcNN/st0ZknhCmZHU/UHo6UMWplXwj258Dq+b2iWJbwX4iFBS/cKtWBLMuR
788B5j83zW7SAPYY777u6/51ye3DC6HoeEdKewpJ0yWYGSVI6FG6EUXEmcnxA94p
LyFBMj//B1fCkWmqssXk0BwElY6GbLo3KFUhEDbDoab1MJXKLS2KDzkkV1hUpZGe
s9izpGQx0kCX2zrQ7cbRbWw5yWG0MGMiWhYHFRvT0L9fkZ2L7bA06sNiIZa6Lnjm
Yq4Cu0LFAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAAG+EUKuCh2ivs7m+pRqbnRz
Om+1SDrlKe4iIRksCucMYTrsQt6UOr692UVklR7q4vLNuKWjZlTRqYXnbiR/HIWW
J5cyhFAlOaIauYYLkTjnelMBGMNyn0CM/PuT92O/g+NvxWlJH4K0qXRqFc39lh9A
2QQpOLugKCkMi4D8o+1Bhz8Hnz8HzPhav3e+ut99OWwOaJt0bLmFfDcHWQz4nag2
2GYp6MBMKSg0M/T99AtrI1orf4GRdQQ0Ib8yitfUw0IKMvKU4B3RT5EkF6GcI+51
DuzMQiVW/4I7u8TLZwW+pBZsjJp2cU0FsNgZfAzk4IzFdivv3sPGSSF3Up3WSck=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions ssl/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PR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