odac 0.9.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/auto-pr-description.yml +0 -2
- package/.github/workflows/codeql.yml +46 -0
- package/.github/workflows/release.yml +13 -6
- package/.github/workflows/test-coverage.yml +10 -9
- package/.releaserc.js +9 -6
- package/CHANGELOG.md +62 -150
- package/CODE_OF_CONDUCT.md +1 -1
- package/CONTRIBUTING.md +8 -8
- package/LICENSE +21 -661
- package/README.md +12 -12
- package/SECURITY.md +4 -4
- package/bin/odac.js +101 -0
- package/{framework/web/candy.js → client/odac.js} +310 -44
- package/docs/backend/01-overview/{01-whats-in-the-candy-box.md → 01-whats-in-the-odac-box.md} +4 -2
- package/docs/backend/01-overview/02-super-handy-helper-functions.md +29 -1
- package/docs/backend/01-overview/03-development-server.md +11 -11
- package/docs/backend/02-structure/01-typical-project-layout.md +4 -4
- package/docs/backend/03-config/00-configuration-overview.md +6 -6
- package/docs/backend/03-config/01-database-connection.md +1 -1
- package/docs/backend/03-config/02-static-route-mapping-optional.md +4 -4
- package/docs/backend/03-config/04-environment-variables.md +20 -20
- package/docs/backend/03-config/05-early-hints.md +4 -4
- package/docs/backend/04-routing/01-basic-page-routes.md +4 -4
- package/docs/backend/04-routing/02-controller-less-view-routes.md +5 -5
- package/docs/backend/04-routing/03-api-and-data-routes.md +3 -3
- package/docs/backend/04-routing/04-authentication-aware-routes.md +5 -5
- package/docs/backend/04-routing/05-advanced-routing.md +3 -3
- package/docs/backend/04-routing/06-error-pages.md +17 -17
- package/docs/backend/04-routing/07-cron-jobs.md +13 -13
- package/docs/backend/04-routing/08-middleware.md +214 -0
- package/docs/backend/04-routing/09-websocket-auth-middleware.md +292 -0
- package/docs/backend/04-routing/09-websocket-examples.md +381 -0
- package/docs/backend/04-routing/09-websocket-quick-reference.md +211 -0
- package/docs/backend/04-routing/09-websocket.md +298 -0
- package/docs/backend/05-controllers/01-how-to-build-a-controller.md +3 -3
- package/docs/backend/05-controllers/02-your-trusty-odac-assistant.md +41 -0
- package/docs/backend/05-controllers/03-controller-classes.md +19 -19
- package/docs/backend/05-forms/01-custom-forms.md +114 -114
- package/docs/backend/05-forms/02-automatic-database-insert.md +82 -82
- package/docs/backend/06-request-and-response/01-the-request-object-what-is-the-user-asking-for.md +26 -26
- package/docs/backend/06-request-and-response/02-sending-a-response-replying-to-the-user.md +10 -10
- package/docs/backend/07-views/01-the-view-directory.md +1 -1
- package/docs/backend/07-views/02-rendering-a-view.md +22 -22
- package/docs/backend/07-views/03-template-syntax.md +52 -52
- package/docs/backend/07-views/03-variables.md +84 -84
- package/docs/backend/07-views/04-request-data.md +57 -57
- package/docs/backend/07-views/05-conditionals.md +78 -78
- package/docs/backend/07-views/06-loops.md +114 -114
- package/docs/backend/07-views/07-translations.md +66 -66
- package/docs/backend/07-views/08-backend-javascript.md +103 -103
- package/docs/backend/07-views/09-comments.md +71 -71
- package/docs/backend/08-database/01-database-connection.md +8 -8
- package/docs/backend/08-database/02-using-mysql.md +49 -49
- package/docs/backend/09-validation/01-the-validator-service.md +38 -38
- package/docs/backend/10-authentication/01-user-logins-with-authjs.md +15 -15
- package/docs/backend/10-authentication/02-foiling-villains-with-csrf-protection.md +10 -10
- package/docs/backend/10-authentication/03-register.md +12 -12
- package/docs/backend/10-authentication/{04-candy-register-forms.md → 04-odac-register-forms.md} +141 -141
- package/docs/backend/10-authentication/05-session-management.md +10 -10
- package/docs/backend/10-authentication/{06-candy-login-forms.md → 06-odac-login-forms.md} +125 -125
- package/docs/backend/11-mail/01-the-mail-service.md +5 -5
- package/docs/backend/12-streaming/01-streaming-overview.md +96 -54
- package/docs/backend/13-utilities/{01-candy-var.md → 01-odac-var.md} +109 -109
- package/docs/frontend/01-overview/01-introduction.md +30 -30
- package/docs/frontend/02-ajax-navigation/01-quick-start.md +45 -45
- package/docs/frontend/02-ajax-navigation/02-configuration.md +14 -14
- package/docs/frontend/02-ajax-navigation/03-advanced-usage.md +36 -36
- package/docs/frontend/03-forms/01-form-handling.md +32 -32
- package/docs/frontend/04-api-requests/01-get-post.md +33 -33
- package/docs/frontend/05-streaming/01-client-streaming.md +15 -15
- package/docs/frontend/06-websocket/00-overview.md +76 -0
- package/docs/frontend/06-websocket/01-websocket-client.md +139 -0
- package/docs/frontend/06-websocket/02-shared-websocket.md +149 -0
- package/docs/index.json +49 -11
- package/eslint.config.mjs +6 -6
- package/{framework/index.js → index.js} +1 -1
- package/package.json +14 -39
- package/{framework/src → src}/Auth.js +59 -59
- package/{framework/src → src}/Config.js +3 -3
- package/{framework/src → src}/Lang.js +7 -7
- package/{framework/src → src}/Mail.js +5 -5
- package/{framework/src → src}/Mysql.js +42 -42
- package/src/Odac.js +112 -0
- package/{framework/src → src}/Request.js +38 -36
- package/{framework/src → src}/Route/Internal.js +116 -116
- package/src/Route/Middleware.js +75 -0
- package/src/Route.js +621 -0
- package/src/Server.js +22 -0
- package/{framework/src → src}/Stream.js +11 -3
- package/{framework/src → src}/Validator.js +21 -21
- package/{framework/src → src}/Var.js +5 -5
- package/{framework/src → src}/View/EarlyHints.js +1 -1
- package/{framework/src → src}/View/Form.js +69 -69
- package/{framework/src → src}/View.js +78 -81
- package/src/WebSocket.js +403 -0
- package/template/config.json +5 -0
- package/{web → template}/controller/page/about.js +6 -6
- package/{web → template}/controller/page/index.js +9 -9
- package/{web → template}/package.json +4 -5
- package/{web → template}/public/assets/css/style.css +4 -4
- package/{web → template}/public/assets/js/app.js +6 -6
- package/{web → template}/route/www.js +6 -6
- package/{web → template}/skeleton/main.html +1 -1
- package/{web → template}/view/content/about.html +5 -5
- package/{web → template}/view/content/home.html +12 -12
- package/template/view/footer/main.html +11 -0
- package/{web → template}/view/head/main.html +1 -1
- package/{web → template}/view/header/main.html +2 -2
- package/test/core/Candy.test.js +58 -58
- package/test/core/Commands.test.js +7 -7
- package/test/core/Config.test.js +82 -85
- package/test/core/Lang.test.js +2 -2
- package/test/core/Process.test.js +6 -6
- package/test/framework/Route.test.js +56 -37
- package/test/framework/View/EarlyHints.test.js +2 -2
- package/test/framework/WebSocket.test.js +100 -0
- package/test/framework/middleware.test.js +85 -0
- package/test/server/Api.test.js +31 -31
- package/test/server/DNS.test.js +11 -11
- package/test/server/Hub.test.js +497 -0
- package/test/server/Mail.account.test_.js +3 -3
- package/test/server/Mail.init.test_.js +10 -10
- package/test/server/Mail.test_.js +20 -20
- package/test/server/SSL.test_.js +54 -54
- package/test/server/Server.test.js +39 -39
- package/test/server/Service.test_.js +7 -7
- package/test/server/Subdomain.test.js +7 -7
- package/test/server/Web/Firewall.test.js +87 -87
- package/test/server/Web/Proxy.test.js +397 -0
- package/test/server/{Web.test_.js → Web.test.js} +137 -205
- package/test/server/__mocks__/fs.js +2 -2
- package/test/server/__mocks__/{globalCandy.js → globalOdac.js} +5 -5
- package/test/server/__mocks__/index.js +6 -6
- package/test/server/__mocks__/testFactories.js +1 -1
- package/test/server/__mocks__/testHelpers.js +7 -7
- package/.husky/pre-commit +0 -2
- package/.kiro/steering/code-style.md +0 -56
- package/.kiro/steering/product.md +0 -20
- package/.kiro/steering/structure.md +0 -77
- package/.kiro/steering/tech.md +0 -87
- package/AGENTS.md +0 -84
- package/bin/candy +0 -10
- package/bin/candypack +0 -10
- package/cli/index.js +0 -3
- package/cli/src/Cli.js +0 -348
- package/cli/src/Connector.js +0 -93
- package/cli/src/Monitor.js +0 -416
- package/core/Candy.js +0 -87
- package/core/Commands.js +0 -239
- package/core/Config.js +0 -1094
- package/core/Lang.js +0 -52
- package/core/Log.js +0 -43
- package/core/Process.js +0 -26
- package/docs/backend/05-controllers/02-your-trusty-candy-assistant.md +0 -20
- package/docs/server/01-installation/01-quick-install.md +0 -19
- package/docs/server/01-installation/02-manual-installation-via-npm.md +0 -9
- package/docs/server/02-get-started/01-core-concepts.md +0 -7
- package/docs/server/02-get-started/02-basic-commands.md +0 -57
- package/docs/server/02-get-started/03-cli-reference.md +0 -276
- package/docs/server/02-get-started/04-cli-quick-reference.md +0 -102
- package/docs/server/03-service/01-start-a-new-service.md +0 -57
- package/docs/server/03-service/02-delete-a-service.md +0 -48
- package/docs/server/04-web/01-create-a-website.md +0 -36
- package/docs/server/04-web/02-list-websites.md +0 -9
- package/docs/server/04-web/03-delete-a-website.md +0 -29
- package/docs/server/05-subdomain/01-create-a-subdomain.md +0 -32
- package/docs/server/05-subdomain/02-list-subdomains.md +0 -33
- package/docs/server/05-subdomain/03-delete-a-subdomain.md +0 -41
- package/docs/server/06-ssl/01-renew-an-ssl-certificate.md +0 -34
- package/docs/server/07-mail/01-create-a-mail-account.md +0 -23
- package/docs/server/07-mail/02-delete-a-mail-account.md +0 -20
- package/docs/server/07-mail/03-list-mail-accounts.md +0 -20
- package/docs/server/07-mail/04-change-account-password.md +0 -23
- package/framework/src/Candy.js +0 -81
- package/framework/src/Route.js +0 -455
- package/framework/src/Server.js +0 -15
- package/locale/de-DE.json +0 -80
- package/locale/en-US.json +0 -79
- package/locale/es-ES.json +0 -80
- package/locale/fr-FR.json +0 -80
- package/locale/pt-BR.json +0 -80
- package/locale/ru-RU.json +0 -80
- package/locale/tr-TR.json +0 -85
- package/locale/zh-CN.json +0 -80
- package/server/index.js +0 -5
- package/server/src/Api.js +0 -88
- package/server/src/DNS.js +0 -940
- package/server/src/Hub.js +0 -535
- package/server/src/Mail.js +0 -571
- package/server/src/SSL.js +0 -180
- package/server/src/Server.js +0 -27
- package/server/src/Service.js +0 -248
- package/server/src/Subdomain.js +0 -64
- package/server/src/Web/Firewall.js +0 -170
- package/server/src/Web/Proxy.js +0 -134
- package/server/src/Web.js +0 -451
- package/server/src/mail/imap.js +0 -1091
- package/server/src/mail/server.js +0 -32
- package/server/src/mail/smtp.js +0 -786
- package/test/server/Client.test.js +0 -338
- package/test/server/__mocks__/http-proxy.js +0 -105
- package/watchdog/index.js +0 -3
- package/watchdog/src/Watchdog.js +0 -156
- package/web/config.json +0 -5
- package/web/view/footer/main.html +0 -11
- /package/{framework/src → src}/Env.js +0 -0
- /package/{framework/src → src}/Route/Cron.js +0 -0
- /package/{framework/src → src}/Token.js +0 -0
package/server/src/Web/Proxy.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const httpProxy = require('http-proxy')
|
|
3
|
-
|
|
4
|
-
class WebProxy {
|
|
5
|
-
#log
|
|
6
|
-
#proxy
|
|
7
|
-
|
|
8
|
-
constructor(log) {
|
|
9
|
-
this.#log = log
|
|
10
|
-
this.#proxy = httpProxy.createProxyServer({
|
|
11
|
-
timeout: 30000,
|
|
12
|
-
proxyTimeout: 30000,
|
|
13
|
-
keepAlive: true
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
#handleEarlyHints(proxyRes, res) {
|
|
18
|
-
if (proxyRes.headers['x-candy-early-hints'] && typeof res.writeEarlyHints === 'function') {
|
|
19
|
-
try {
|
|
20
|
-
const links = JSON.parse(proxyRes.headers['x-candy-early-hints'])
|
|
21
|
-
if (Array.isArray(links) && links.length > 0) {
|
|
22
|
-
res.writeEarlyHints({link: links})
|
|
23
|
-
}
|
|
24
|
-
} catch {
|
|
25
|
-
// Ignore parsing errors
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
http2(req, res, website, host) {
|
|
31
|
-
const options = {
|
|
32
|
-
hostname: '127.0.0.1',
|
|
33
|
-
port: website.port,
|
|
34
|
-
path: req.url,
|
|
35
|
-
method: req.method,
|
|
36
|
-
headers: {},
|
|
37
|
-
timeout: 0
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
41
|
-
if (!key.startsWith(':')) {
|
|
42
|
-
options.headers[key.toLowerCase()] = value
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
options.headers['x-candy-connection-remoteaddress'] = req.socket.remoteAddress ?? ''
|
|
47
|
-
options.headers['x-candy-connection-ssl'] = 'true'
|
|
48
|
-
|
|
49
|
-
const proxyReq = http.request(options, proxyRes => {
|
|
50
|
-
this.#handleEarlyHints(proxyRes, res)
|
|
51
|
-
|
|
52
|
-
const isSSE = proxyRes.headers['content-type']?.includes('text/event-stream')
|
|
53
|
-
if (isSSE) {
|
|
54
|
-
req.setTimeout(0)
|
|
55
|
-
res.setTimeout(0)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const responseHeaders = {}
|
|
59
|
-
const forbiddenHeaders = [
|
|
60
|
-
'connection',
|
|
61
|
-
'keep-alive',
|
|
62
|
-
'transfer-encoding',
|
|
63
|
-
'upgrade',
|
|
64
|
-
'proxy-connection',
|
|
65
|
-
'proxy-authenticate',
|
|
66
|
-
'trailer',
|
|
67
|
-
'x-candy-early-hints'
|
|
68
|
-
]
|
|
69
|
-
|
|
70
|
-
for (const [key, value] of Object.entries(proxyRes.headers)) {
|
|
71
|
-
if (!forbiddenHeaders.includes(key.toLowerCase())) {
|
|
72
|
-
responseHeaders[key] = value
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
res.writeHead(proxyRes.statusCode, responseHeaders)
|
|
77
|
-
proxyRes.pipe(res)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
proxyReq.on('error', err => {
|
|
81
|
-
this.#log(`HTTP/2 proxy error for ${host}: ${err.message}`)
|
|
82
|
-
if (!res.headersSent) {
|
|
83
|
-
res.writeHead(502)
|
|
84
|
-
res.end('Bad Gateway')
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
req.pipe(proxyReq)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
http1(req, res, website, host) {
|
|
92
|
-
const onProxyReq = (proxyReq, req) => {
|
|
93
|
-
proxyReq.setHeader('x-candy-connection-remoteaddress', req.socket.remoteAddress ?? '')
|
|
94
|
-
proxyReq.setHeader('x-candy-connection-ssl', 'true')
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const onProxyRes = (proxyRes, req, res) => {
|
|
98
|
-
this.#handleEarlyHints(proxyRes, res)
|
|
99
|
-
delete proxyRes.headers['x-candy-early-hints']
|
|
100
|
-
|
|
101
|
-
const isSSE = proxyRes.headers['content-type']?.includes('text/event-stream')
|
|
102
|
-
if (isSSE) {
|
|
103
|
-
req.setTimeout(0)
|
|
104
|
-
res.setTimeout(0)
|
|
105
|
-
proxyRes.setTimeout(0)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const onError = (err, req, res) => {
|
|
110
|
-
this.#log(`Proxy error for ${host}: ${err.message}`)
|
|
111
|
-
if (!res.headersSent) {
|
|
112
|
-
res.statusCode = 502
|
|
113
|
-
res.end('Bad Gateway')
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const cleanup = () => {
|
|
118
|
-
this.#proxy.off('proxyReq', onProxyReq)
|
|
119
|
-
this.#proxy.off('proxyRes', onProxyRes)
|
|
120
|
-
this.#proxy.off('error', onError)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
this.#proxy.on('proxyReq', onProxyReq)
|
|
124
|
-
this.#proxy.on('proxyRes', onProxyRes)
|
|
125
|
-
this.#proxy.on('error', onError)
|
|
126
|
-
|
|
127
|
-
res.on('finish', cleanup)
|
|
128
|
-
res.on('close', cleanup)
|
|
129
|
-
|
|
130
|
-
this.#proxy.web(req, res, {target: 'http://127.0.0.1:' + website.port, timeout: 0, proxyTimeout: 0})
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
module.exports = WebProxy
|
package/server/src/Web.js
DELETED
|
@@ -1,451 +0,0 @@
|
|
|
1
|
-
const {log, error} = Candy.core('Log', false).init('Web')
|
|
2
|
-
|
|
3
|
-
const childProcess = require('child_process')
|
|
4
|
-
const fs = require('fs')
|
|
5
|
-
const http = require('http')
|
|
6
|
-
const https = require('https')
|
|
7
|
-
const http2 = require('http2')
|
|
8
|
-
const net = require('net')
|
|
9
|
-
const os = require('os')
|
|
10
|
-
const path = require('path')
|
|
11
|
-
const tls = require('tls')
|
|
12
|
-
|
|
13
|
-
const WebProxy = require('./Web/Proxy.js')
|
|
14
|
-
const WebFirewall = require('./Web/Firewall.js')
|
|
15
|
-
|
|
16
|
-
class Web {
|
|
17
|
-
#active = {}
|
|
18
|
-
#error_counts = {}
|
|
19
|
-
#loaded = false
|
|
20
|
-
#log
|
|
21
|
-
#logs = {log: {}, err: {}}
|
|
22
|
-
#sslCache = new Map()
|
|
23
|
-
#ports = {}
|
|
24
|
-
#proxy
|
|
25
|
-
#firewall
|
|
26
|
-
#server_http
|
|
27
|
-
#server_https
|
|
28
|
-
#started = {}
|
|
29
|
-
#watcher = {}
|
|
30
|
-
|
|
31
|
-
constructor() {
|
|
32
|
-
this.#log = log
|
|
33
|
-
this.#proxy = new WebProxy(this.#log)
|
|
34
|
-
this.#firewall = new WebFirewall()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
clearSSLCache(domain) {
|
|
38
|
-
if (domain) {
|
|
39
|
-
for (const key of this.#sslCache.keys()) {
|
|
40
|
-
if (key === domain || key.endsWith('.' + domain)) {
|
|
41
|
-
this.#sslCache.delete(key)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
this.#sslCache.clear()
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
check() {
|
|
50
|
-
if (!this.#loaded) return
|
|
51
|
-
for (const domain of Object.keys(Candy.core('Config').config.websites ?? {})) {
|
|
52
|
-
if (!Candy.core('Config').config.websites[domain].pid) {
|
|
53
|
-
this.start(domain)
|
|
54
|
-
} else if (!this.#watcher[Candy.core('Config').config.websites[domain].pid]) {
|
|
55
|
-
Candy.core('Process').stop(Candy.core('Config').config.websites[domain].pid)
|
|
56
|
-
Candy.core('Config').config.websites[domain].pid = null
|
|
57
|
-
this.start(domain)
|
|
58
|
-
}
|
|
59
|
-
if (this.#logs.log[domain]) {
|
|
60
|
-
fs.writeFile(os.homedir() + '/.candypack/logs/' + domain + '.log', this.#logs.log[domain], function (err) {
|
|
61
|
-
if (err) log(err)
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
if (this.#logs.err[domain]) {
|
|
65
|
-
fs.writeFile(Candy.core('Config').config.websites[domain].path + '/error.log', this.#logs.err[domain], function (err) {
|
|
66
|
-
if (err) log(err)
|
|
67
|
-
})
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
this.server()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
checkPort(port) {
|
|
74
|
-
return new Promise(resolve => {
|
|
75
|
-
const server = net.createServer()
|
|
76
|
-
server.once('error', () => resolve(false))
|
|
77
|
-
server.once('listening', () => {
|
|
78
|
-
server.close()
|
|
79
|
-
resolve(true)
|
|
80
|
-
})
|
|
81
|
-
server.listen(port, '127.0.0.1')
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
create(domain, progress) {
|
|
86
|
-
let web = {}
|
|
87
|
-
for (const iterator of ['http://', 'https://', 'ftp://', 'www.']) {
|
|
88
|
-
if (domain.startsWith(iterator)) domain = domain.replace(iterator, '')
|
|
89
|
-
}
|
|
90
|
-
if (domain.length < 3 || (!domain.includes('.') && domain != 'localhost'))
|
|
91
|
-
return Candy.server('Api').result(false, __('Invalid domain.'))
|
|
92
|
-
if (Candy.core('Config').config.websites?.[domain]) return Candy.server('Api').result(false, __('Website %s already exists.', domain))
|
|
93
|
-
progress('domain', 'progress', __('Setting up domain %s...', domain))
|
|
94
|
-
web.domain = domain
|
|
95
|
-
web.path = path.join(Candy.core('Config').config.web.path, domain)
|
|
96
|
-
if (!fs.existsSync(web.path)) fs.mkdirSync(web.path, {recursive: true})
|
|
97
|
-
if (!Candy.core('Config').config.websites) Candy.core('Config').config.websites = {}
|
|
98
|
-
web.cert = false
|
|
99
|
-
Candy.core('Config').config.websites[web.domain] = web
|
|
100
|
-
progress('domain', 'success', __('Domain %s set.', domain))
|
|
101
|
-
if (web.domain != 'localhost' && !web.domain.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
|
|
102
|
-
progress('dns', 'progress', __('Setting up DNS records for %s...', domain))
|
|
103
|
-
Candy.core('Config').config.websites[web.domain].subdomain = ['www']
|
|
104
|
-
Candy.server('DNS').record(
|
|
105
|
-
{name: web.domain, type: 'A', value: Candy.server('DNS').ip},
|
|
106
|
-
{name: 'www.' + web.domain, type: 'CNAME', value: web.domain},
|
|
107
|
-
{name: web.domain, type: 'MX', value: web.domain},
|
|
108
|
-
{name: web.domain, type: 'TXT', value: 'v=spf1 a mx ip4:' + Candy.server('DNS').ip + ' ~all'},
|
|
109
|
-
{
|
|
110
|
-
name: '_dmarc.' + web.domain,
|
|
111
|
-
type: 'TXT',
|
|
112
|
-
value: 'v=DMARC1; p=reject; rua=mailto:postmaster@' + web.domain
|
|
113
|
-
}
|
|
114
|
-
)
|
|
115
|
-
progress('dns', 'success', __('DNS records for %s set.', domain))
|
|
116
|
-
Candy.core('Config').config.websites[web.domain].cert = {}
|
|
117
|
-
progress('ssl', 'progress', __('Setting up SSL certificate for %s...', domain))
|
|
118
|
-
}
|
|
119
|
-
progress('directory', 'progress', __('Setting up website files for %s...', domain))
|
|
120
|
-
|
|
121
|
-
childProcess.execSync('npm link candypack', {cwd: web.path})
|
|
122
|
-
if (fs.existsSync(web.path + 'node_modules/.bin')) fs.rmSync(web.path + 'node_modules/.bin', {recursive: true})
|
|
123
|
-
if (!fs.existsSync(web.path + '/node_modules')) fs.mkdirSync(web.path + '/node_modules')
|
|
124
|
-
|
|
125
|
-
// Copy web template files
|
|
126
|
-
fs.cpSync(__dirname + '/../../web/', web.path, {recursive: true})
|
|
127
|
-
|
|
128
|
-
// Process package.json template after copying
|
|
129
|
-
const packageJsonPath = path.join(web.path, 'package.json')
|
|
130
|
-
if (fs.existsSync(packageJsonPath)) {
|
|
131
|
-
let packageTemplate = fs.readFileSync(packageJsonPath, 'utf8')
|
|
132
|
-
|
|
133
|
-
// Replace template variables
|
|
134
|
-
packageTemplate = packageTemplate.replace(/\{\{domain\}\}/g, domain.replace(/\./g, '-')).replace(/\{\{domain_original\}\}/g, domain)
|
|
135
|
-
|
|
136
|
-
fs.writeFileSync(packageJsonPath, packageTemplate)
|
|
137
|
-
}
|
|
138
|
-
progress('directory', 'success', __('Website files for %s set.', domain))
|
|
139
|
-
return Candy.server('Api').result(true, __('Website %s1 created at %s2.', web.domain, web.path))
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async delete(domain) {
|
|
143
|
-
for (const iterator of ['http://', 'https://', 'ftp://', 'www.']) if (domain.startsWith(iterator)) domain = domain.replace(iterator, '')
|
|
144
|
-
if (!Candy.core('Config').config.websites[domain]) return Candy.server('Api').result(false, __('Website %s not found.', domain))
|
|
145
|
-
const website = Candy.core('Config').config.websites[domain]
|
|
146
|
-
delete Candy.core('Config').config.websites[domain]
|
|
147
|
-
|
|
148
|
-
// Stop process if running
|
|
149
|
-
if (website.pid) {
|
|
150
|
-
Candy.core('Process').stop(website.pid)
|
|
151
|
-
delete this.#watcher[website.pid]
|
|
152
|
-
if (website.port) {
|
|
153
|
-
delete this.#ports[website.port]
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Cleanup logs
|
|
158
|
-
delete this.#logs.log[domain]
|
|
159
|
-
delete this.#logs.err[domain]
|
|
160
|
-
delete this.#error_counts[domain]
|
|
161
|
-
delete this.#active[domain]
|
|
162
|
-
delete this.#started[domain]
|
|
163
|
-
|
|
164
|
-
return Candy.server('Api').result(true, __('Website %s deleted.', domain))
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
index(req, res) {
|
|
168
|
-
res.write('CandyPack Server')
|
|
169
|
-
res.end()
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async init() {
|
|
173
|
-
this.#loaded = true
|
|
174
|
-
this.server()
|
|
175
|
-
if (!Candy.core('Config').config.web?.path || !fs.existsSync(Candy.core('Config').config.web.path)) {
|
|
176
|
-
if (!Candy.core('Config').config.web) Candy.core('Config').config.web = {}
|
|
177
|
-
if (os.platform() === 'win32' || os.platform() === 'darwin') {
|
|
178
|
-
Candy.core('Config').config.web.path = os.homedir() + '/Candypack/'
|
|
179
|
-
} else {
|
|
180
|
-
Candy.core('Config').config.web.path = '/var/candypack/'
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async list() {
|
|
186
|
-
let websites = Object.keys(Candy.core('Config').config.websites ?? {})
|
|
187
|
-
if (websites.length == 0) return Candy.server('Api').result(false, __('No websites found.'))
|
|
188
|
-
return Candy.server('Api').result(true, __('Websites:') + '\n ' + websites.join('\n '))
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
request(req, res, secure) {
|
|
192
|
-
const result = this.#firewall.check(req)
|
|
193
|
-
if (!result.allowed) {
|
|
194
|
-
if (result.reason === 'blacklist') {
|
|
195
|
-
res.writeHead(403, {'Content-Type': 'text/plain'})
|
|
196
|
-
res.end('Forbidden')
|
|
197
|
-
} else {
|
|
198
|
-
res.writeHead(429, {'Content-Type': 'text/plain'})
|
|
199
|
-
res.end('Too Many Requests')
|
|
200
|
-
}
|
|
201
|
-
return
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
let host = req.headers.host || req.headers[':authority']
|
|
205
|
-
if (!host) return this.index(req, res)
|
|
206
|
-
|
|
207
|
-
// Remove port from host
|
|
208
|
-
if (host.includes(':')) {
|
|
209
|
-
host = host.split(':')[0]
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Find matching website (check subdomains)
|
|
213
|
-
let matchedHost = host
|
|
214
|
-
while (!Candy.core('Config').config.websites[matchedHost] && matchedHost.includes('.')) {
|
|
215
|
-
matchedHost = matchedHost.split('.').slice(1).join('.')
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const website = Candy.core('Config').config.websites[matchedHost]
|
|
219
|
-
if (!website) return this.index(req, res)
|
|
220
|
-
if (!website.pid || !this.#watcher[website.pid]) return this.index(req, res)
|
|
221
|
-
|
|
222
|
-
try {
|
|
223
|
-
if (!secure) {
|
|
224
|
-
res.writeHead(301, {Location: 'https://' + host + (req.url || req.headers[':path'] || '/')})
|
|
225
|
-
return res.end()
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (req.httpVersion === '2.0') {
|
|
229
|
-
if (!req.url && req.headers[':path']) {
|
|
230
|
-
req.url = req.headers[':path']
|
|
231
|
-
}
|
|
232
|
-
if (!req.method && req.headers[':method']) {
|
|
233
|
-
req.method = req.headers[':method'].toUpperCase()
|
|
234
|
-
}
|
|
235
|
-
if (!req.headers.host) {
|
|
236
|
-
req.headers.host = host
|
|
237
|
-
}
|
|
238
|
-
return this.#proxy.http2(req, res, website, host)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return this.#proxy.http1(req, res, website, host)
|
|
242
|
-
} catch (e) {
|
|
243
|
-
log(e)
|
|
244
|
-
return this.index(req, res)
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
server() {
|
|
249
|
-
if (!this.#loaded) return setTimeout(() => this.server(), 1000)
|
|
250
|
-
if (Object.keys(Candy.core('Config').config.websites ?? {}).length == 0) return
|
|
251
|
-
|
|
252
|
-
if (!this.#server_http) {
|
|
253
|
-
this.#server_http = http.createServer((req, res) => this.request(req, res, false))
|
|
254
|
-
this.#server_http.on('error', err => {
|
|
255
|
-
log(`HTTP server error: ${err.message}`)
|
|
256
|
-
if (err.code === 'EADDRINUSE') {
|
|
257
|
-
log('Port 80 is already in use')
|
|
258
|
-
}
|
|
259
|
-
})
|
|
260
|
-
this.#server_http.listen(80)
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
let ssl = Candy.core('Config').config.ssl ?? {}
|
|
264
|
-
if (!this.#server_https && ssl && ssl.key && ssl.cert && fs.existsSync(ssl.key) && fs.existsSync(ssl.cert)) {
|
|
265
|
-
const useHttp2 = Candy.core('Config').config.http2 !== false
|
|
266
|
-
|
|
267
|
-
const serverOptions = {
|
|
268
|
-
SNICallback: (hostname, callback) => {
|
|
269
|
-
try {
|
|
270
|
-
const cached = this.#sslCache.get(hostname)
|
|
271
|
-
if (cached) return callback(null, cached)
|
|
272
|
-
|
|
273
|
-
let sslOptions
|
|
274
|
-
while (!Candy.core('Config').config.websites[hostname] && hostname.includes('.'))
|
|
275
|
-
hostname = hostname.split('.').slice(1).join('.')
|
|
276
|
-
let website = Candy.core('Config').config.websites[hostname]
|
|
277
|
-
if (
|
|
278
|
-
website &&
|
|
279
|
-
website.cert &&
|
|
280
|
-
website.cert.ssl &&
|
|
281
|
-
website.cert.ssl.key &&
|
|
282
|
-
website.cert.ssl.cert &&
|
|
283
|
-
fs.existsSync(website.cert.ssl.key) &&
|
|
284
|
-
fs.existsSync(website.cert.ssl.cert)
|
|
285
|
-
) {
|
|
286
|
-
sslOptions = {
|
|
287
|
-
key: fs.readFileSync(website.cert.ssl.key),
|
|
288
|
-
cert: fs.readFileSync(website.cert.ssl.cert)
|
|
289
|
-
}
|
|
290
|
-
} else {
|
|
291
|
-
sslOptions = {
|
|
292
|
-
key: fs.readFileSync(ssl.key),
|
|
293
|
-
cert: fs.readFileSync(ssl.cert)
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
const ctx = tls.createSecureContext(sslOptions)
|
|
297
|
-
this.#sslCache.set(hostname, ctx)
|
|
298
|
-
callback(null, ctx)
|
|
299
|
-
} catch (err) {
|
|
300
|
-
log(`SSL certificate error for ${hostname}: ${err.message}`)
|
|
301
|
-
callback(err)
|
|
302
|
-
}
|
|
303
|
-
},
|
|
304
|
-
key: fs.readFileSync(ssl.key),
|
|
305
|
-
cert: fs.readFileSync(ssl.cert),
|
|
306
|
-
sessionTimeout: 300,
|
|
307
|
-
ciphers:
|
|
308
|
-
'TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384:' +
|
|
309
|
-
'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:' +
|
|
310
|
-
'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:' +
|
|
311
|
-
'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305',
|
|
312
|
-
honorCipherOrder: true
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
if (useHttp2) {
|
|
316
|
-
serverOptions.allowHTTP1 = true
|
|
317
|
-
this.#server_https = http2.createSecureServer(serverOptions, (req, res) => {
|
|
318
|
-
this.request(req, res, true)
|
|
319
|
-
})
|
|
320
|
-
log('HTTPS server starting with HTTP/2 support enabled')
|
|
321
|
-
} else {
|
|
322
|
-
this.#server_https = https.createServer(serverOptions, (req, res) => {
|
|
323
|
-
this.request(req, res, true)
|
|
324
|
-
})
|
|
325
|
-
log('HTTPS server starting with HTTP/1.1 only')
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
this.#server_https.on('error', err => {
|
|
329
|
-
log(`HTTPS server error: ${err.message}`)
|
|
330
|
-
if (err.code === 'EADDRINUSE') {
|
|
331
|
-
log('Port 443 is already in use')
|
|
332
|
-
}
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
this.#server_https.listen(443)
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
set(domain, data) {
|
|
340
|
-
Candy.core('Config').config.websites[domain] = data
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
async start(domain) {
|
|
344
|
-
if (this.#active[domain] || !this.#loaded) return
|
|
345
|
-
this.#active[domain] = true
|
|
346
|
-
if (!Candy.core('Config').config.websites[domain]) return (this.#active[domain] = false)
|
|
347
|
-
if (
|
|
348
|
-
Candy.core('Config').config.websites[domain].status == 'errored' &&
|
|
349
|
-
Date.now() - Candy.core('Config').config.websites[domain].updated < this.#error_counts[domain] * 1000
|
|
350
|
-
)
|
|
351
|
-
return (this.#active[domain] = false)
|
|
352
|
-
let port = 60000
|
|
353
|
-
let using = false
|
|
354
|
-
do {
|
|
355
|
-
if (this.#ports[port]) {
|
|
356
|
-
port++
|
|
357
|
-
using = true
|
|
358
|
-
} else {
|
|
359
|
-
if (this.checkPort(port)) {
|
|
360
|
-
using = false
|
|
361
|
-
} else {
|
|
362
|
-
port++
|
|
363
|
-
using = true
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
if (port > 65535) {
|
|
367
|
-
port = 1000
|
|
368
|
-
using = true
|
|
369
|
-
}
|
|
370
|
-
} while (using)
|
|
371
|
-
Candy.core('Config').config.websites[domain].port = port
|
|
372
|
-
this.#ports[port] = true
|
|
373
|
-
var child = childProcess.spawn('candypack', ['framework', 'run', port], {
|
|
374
|
-
cwd: Candy.core('Config').config.websites[domain].path
|
|
375
|
-
})
|
|
376
|
-
let pid = child.pid
|
|
377
|
-
log('Web server started for ' + domain + ' with PID ' + pid)
|
|
378
|
-
child.stdout.on('data', data => {
|
|
379
|
-
if (!this.#logs.log[domain]) this.#logs.log[domain] = ''
|
|
380
|
-
this.#logs.log[domain] +=
|
|
381
|
-
'[LOG][' +
|
|
382
|
-
Date.now() +
|
|
383
|
-
'] ' +
|
|
384
|
-
data
|
|
385
|
-
.toString()
|
|
386
|
-
.trim()
|
|
387
|
-
.split('\n')
|
|
388
|
-
.join('\n[LOG][' + Date.now() + '] ') +
|
|
389
|
-
'\n'
|
|
390
|
-
if (this.#logs.log[domain].length > 100000)
|
|
391
|
-
this.#logs.log[domain] = this.#logs.log[domain].substr(this.#logs.log[domain].length - 1000000)
|
|
392
|
-
if (Candy.core('Config').config.websites[domain] && Candy.core('Config').config.websites[domain].status == 'errored')
|
|
393
|
-
Candy.core('Config').config.websites[domain].status = 'running'
|
|
394
|
-
})
|
|
395
|
-
child.stderr.on('data', data => {
|
|
396
|
-
if (!this.#logs.err[domain]) this.#logs.err[domain] = ''
|
|
397
|
-
this.#logs.log[domain] +=
|
|
398
|
-
'[ERR][' +
|
|
399
|
-
Date.now() +
|
|
400
|
-
'] ' +
|
|
401
|
-
data
|
|
402
|
-
.toString()
|
|
403
|
-
.trim()
|
|
404
|
-
.split('\n')
|
|
405
|
-
.join('\n[ERR][' + Date.now() + '] ') +
|
|
406
|
-
'\n'
|
|
407
|
-
this.#logs.err[domain] += data.toString()
|
|
408
|
-
if (this.#logs.err[domain].length > 100000)
|
|
409
|
-
this.#logs.err[domain] = this.#logs.err[domain].substr(this.#logs.err[domain].length - 1000000)
|
|
410
|
-
if (Candy.core('Config').config.websites[domain]) Candy.core('Config').config.websites[domain].status = 'errored'
|
|
411
|
-
})
|
|
412
|
-
child.on('exit', () => {
|
|
413
|
-
error('Child process exited for ' + domain)
|
|
414
|
-
if (!Candy.core('Config').config.websites[domain]) return
|
|
415
|
-
Candy.core('Config').config.websites[domain].pid = null
|
|
416
|
-
Candy.core('Config').config.websites[domain].updated = Date.now()
|
|
417
|
-
if (Candy.core('Config').config.websites[domain].status == 'errored') {
|
|
418
|
-
Candy.core('Config').config.websites[domain].status = 'errored'
|
|
419
|
-
this.#error_counts[domain] = this.#error_counts[domain] ?? 0
|
|
420
|
-
this.#error_counts[domain]++
|
|
421
|
-
} else Candy.core('Config').config.websites[domain].status = 'stopped'
|
|
422
|
-
this.#watcher[pid] = false
|
|
423
|
-
delete this.#ports[Candy.core('Config').config.websites[domain].port]
|
|
424
|
-
this.#active[domain] = false
|
|
425
|
-
})
|
|
426
|
-
|
|
427
|
-
Candy.core('Config').config.websites[domain].pid = pid
|
|
428
|
-
Candy.core('Config').config.websites[domain].started = Date.now()
|
|
429
|
-
Candy.core('Config').config.websites[domain].status = 'running'
|
|
430
|
-
this.#watcher[pid] = true
|
|
431
|
-
this.#started[domain] = Date.now()
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
async status() {
|
|
435
|
-
this.init()
|
|
436
|
-
return Candy.core('Config').config.websites
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
stopAll() {
|
|
440
|
-
for (const domain of Object.keys(Candy.core('Config').config.websites ?? {})) {
|
|
441
|
-
let website = Candy.core('Config').config.websites[domain]
|
|
442
|
-
if (website.pid) {
|
|
443
|
-
Candy.core('Process').stop(website.pid)
|
|
444
|
-
website.pid = null
|
|
445
|
-
this.set(domain, website)
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
module.exports = new Web()
|