odac 0.9.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/.editorconfig +21 -0
- package/.github/workflows/auto-pr-description.yml +49 -0
- package/.github/workflows/release.yml +32 -0
- package/.github/workflows/test-coverage.yml +58 -0
- package/.husky/pre-commit +2 -0
- package/.kiro/steering/code-style.md +56 -0
- package/.kiro/steering/product.md +20 -0
- package/.kiro/steering/structure.md +77 -0
- package/.kiro/steering/tech.md +87 -0
- package/.prettierrc +10 -0
- package/.releaserc.js +134 -0
- package/AGENTS.md +84 -0
- package/CHANGELOG.md +181 -0
- package/CODE_OF_CONDUCT.md +83 -0
- package/CONTRIBUTING.md +63 -0
- package/LICENSE +661 -0
- package/README.md +57 -0
- package/SECURITY.md +26 -0
- package/bin/candy +10 -0
- package/bin/candypack +10 -0
- package/cli/index.js +3 -0
- package/cli/src/Cli.js +348 -0
- package/cli/src/Connector.js +93 -0
- package/cli/src/Monitor.js +416 -0
- package/core/Candy.js +87 -0
- package/core/Commands.js +239 -0
- package/core/Config.js +1094 -0
- package/core/Lang.js +52 -0
- package/core/Log.js +43 -0
- package/core/Process.js +26 -0
- package/docs/backend/01-overview/01-whats-in-the-candy-box.md +9 -0
- package/docs/backend/01-overview/02-super-handy-helper-functions.md +9 -0
- package/docs/backend/01-overview/03-development-server.md +79 -0
- package/docs/backend/02-structure/01-typical-project-layout.md +39 -0
- package/docs/backend/03-config/00-configuration-overview.md +214 -0
- package/docs/backend/03-config/01-database-connection.md +60 -0
- package/docs/backend/03-config/02-static-route-mapping-optional.md +20 -0
- package/docs/backend/03-config/03-request-timeout.md +11 -0
- package/docs/backend/03-config/04-environment-variables.md +227 -0
- package/docs/backend/03-config/05-early-hints.md +352 -0
- package/docs/backend/04-routing/01-basic-page-routes.md +28 -0
- package/docs/backend/04-routing/02-controller-less-view-routes.md +43 -0
- package/docs/backend/04-routing/03-api-and-data-routes.md +20 -0
- package/docs/backend/04-routing/04-authentication-aware-routes.md +48 -0
- package/docs/backend/04-routing/05-advanced-routing.md +14 -0
- package/docs/backend/04-routing/06-error-pages.md +101 -0
- package/docs/backend/04-routing/07-cron-jobs.md +149 -0
- package/docs/backend/05-controllers/01-how-to-build-a-controller.md +17 -0
- package/docs/backend/05-controllers/02-your-trusty-candy-assistant.md +20 -0
- package/docs/backend/05-controllers/03-controller-classes.md +93 -0
- package/docs/backend/05-forms/01-custom-forms.md +395 -0
- package/docs/backend/05-forms/02-automatic-database-insert.md +297 -0
- package/docs/backend/06-request-and-response/01-the-request-object-what-is-the-user-asking-for.md +96 -0
- package/docs/backend/06-request-and-response/02-sending-a-response-replying-to-the-user.md +40 -0
- package/docs/backend/07-views/01-the-view-directory.md +73 -0
- package/docs/backend/07-views/02-rendering-a-view.md +179 -0
- package/docs/backend/07-views/03-template-syntax.md +181 -0
- package/docs/backend/07-views/03-variables.md +328 -0
- package/docs/backend/07-views/04-request-data.md +231 -0
- package/docs/backend/07-views/05-conditionals.md +290 -0
- package/docs/backend/07-views/06-loops.md +353 -0
- package/docs/backend/07-views/07-translations.md +358 -0
- package/docs/backend/07-views/08-backend-javascript.md +398 -0
- package/docs/backend/07-views/09-comments.md +297 -0
- package/docs/backend/08-database/01-database-connection.md +99 -0
- package/docs/backend/08-database/02-using-mysql.md +322 -0
- package/docs/backend/09-validation/01-the-validator-service.md +424 -0
- package/docs/backend/10-authentication/01-user-logins-with-authjs.md +53 -0
- package/docs/backend/10-authentication/02-foiling-villains-with-csrf-protection.md +55 -0
- package/docs/backend/10-authentication/03-register.md +134 -0
- package/docs/backend/10-authentication/04-candy-register-forms.md +676 -0
- package/docs/backend/10-authentication/05-session-management.md +159 -0
- package/docs/backend/10-authentication/06-candy-login-forms.md +596 -0
- package/docs/backend/11-mail/01-the-mail-service.md +42 -0
- package/docs/backend/12-streaming/01-streaming-overview.md +300 -0
- package/docs/backend/13-utilities/01-candy-var.md +504 -0
- package/docs/frontend/01-overview/01-introduction.md +146 -0
- package/docs/frontend/02-ajax-navigation/01-quick-start.md +608 -0
- package/docs/frontend/02-ajax-navigation/02-configuration.md +370 -0
- package/docs/frontend/02-ajax-navigation/03-advanced-usage.md +519 -0
- package/docs/frontend/03-forms/01-form-handling.md +420 -0
- package/docs/frontend/04-api-requests/01-get-post.md +443 -0
- package/docs/frontend/05-streaming/01-client-streaming.md +163 -0
- package/docs/index.json +452 -0
- package/docs/server/01-installation/01-quick-install.md +19 -0
- package/docs/server/01-installation/02-manual-installation-via-npm.md +9 -0
- package/docs/server/02-get-started/01-core-concepts.md +7 -0
- package/docs/server/02-get-started/02-basic-commands.md +57 -0
- package/docs/server/02-get-started/03-cli-reference.md +276 -0
- package/docs/server/02-get-started/04-cli-quick-reference.md +102 -0
- package/docs/server/03-service/01-start-a-new-service.md +57 -0
- package/docs/server/03-service/02-delete-a-service.md +48 -0
- package/docs/server/04-web/01-create-a-website.md +36 -0
- package/docs/server/04-web/02-list-websites.md +9 -0
- package/docs/server/04-web/03-delete-a-website.md +29 -0
- package/docs/server/05-subdomain/01-create-a-subdomain.md +32 -0
- package/docs/server/05-subdomain/02-list-subdomains.md +33 -0
- package/docs/server/05-subdomain/03-delete-a-subdomain.md +41 -0
- package/docs/server/06-ssl/01-renew-an-ssl-certificate.md +34 -0
- package/docs/server/07-mail/01-create-a-mail-account.md +23 -0
- package/docs/server/07-mail/02-delete-a-mail-account.md +20 -0
- package/docs/server/07-mail/03-list-mail-accounts.md +20 -0
- package/docs/server/07-mail/04-change-account-password.md +23 -0
- package/eslint.config.mjs +120 -0
- package/framework/index.js +4 -0
- package/framework/src/Auth.js +309 -0
- package/framework/src/Candy.js +81 -0
- package/framework/src/Config.js +79 -0
- package/framework/src/Env.js +60 -0
- package/framework/src/Lang.js +57 -0
- package/framework/src/Mail.js +83 -0
- package/framework/src/Mysql.js +575 -0
- package/framework/src/Request.js +301 -0
- package/framework/src/Route/Cron.js +128 -0
- package/framework/src/Route/Internal.js +439 -0
- package/framework/src/Route.js +455 -0
- package/framework/src/Server.js +15 -0
- package/framework/src/Stream.js +163 -0
- package/framework/src/Token.js +37 -0
- package/framework/src/Validator.js +271 -0
- package/framework/src/Var.js +211 -0
- package/framework/src/View/EarlyHints.js +190 -0
- package/framework/src/View/Form.js +600 -0
- package/framework/src/View.js +513 -0
- package/framework/web/candy.js +838 -0
- package/jest.config.js +22 -0
- package/locale/de-DE.json +80 -0
- package/locale/en-US.json +79 -0
- package/locale/es-ES.json +80 -0
- package/locale/fr-FR.json +80 -0
- package/locale/pt-BR.json +80 -0
- package/locale/ru-RU.json +80 -0
- package/locale/tr-TR.json +85 -0
- package/locale/zh-CN.json +80 -0
- package/package.json +86 -0
- package/server/index.js +5 -0
- package/server/src/Api.js +88 -0
- package/server/src/DNS.js +940 -0
- package/server/src/Hub.js +535 -0
- package/server/src/Mail.js +571 -0
- package/server/src/SSL.js +180 -0
- package/server/src/Server.js +27 -0
- package/server/src/Service.js +248 -0
- package/server/src/Subdomain.js +64 -0
- package/server/src/Web/Firewall.js +170 -0
- package/server/src/Web/Proxy.js +134 -0
- package/server/src/Web.js +451 -0
- package/server/src/mail/imap.js +1091 -0
- package/server/src/mail/server.js +32 -0
- package/server/src/mail/smtp.js +786 -0
- package/test/cli/Cli.test.js +36 -0
- package/test/core/Candy.test.js +234 -0
- package/test/core/Commands.test.js +538 -0
- package/test/core/Config.test.js +1435 -0
- package/test/core/Lang.test.js +250 -0
- package/test/core/Process.test.js +156 -0
- package/test/framework/Route.test.js +239 -0
- package/test/framework/View/EarlyHints.test.js +282 -0
- package/test/scripts/check-coverage.js +132 -0
- package/test/server/Api.test.js +647 -0
- package/test/server/Client.test.js +338 -0
- package/test/server/DNS.test.js +2050 -0
- package/test/server/DNS.test.js.bak +2084 -0
- package/test/server/Log.test.js +73 -0
- package/test/server/Mail.account.test_.js +460 -0
- package/test/server/Mail.init.test_.js +411 -0
- package/test/server/Mail.test_.js +1340 -0
- package/test/server/SSL.test_.js +1491 -0
- package/test/server/Server.test.js +765 -0
- package/test/server/Service.test_.js +1127 -0
- package/test/server/Subdomain.test.js +440 -0
- package/test/server/Web/Firewall.test.js +175 -0
- package/test/server/Web.test_.js +1562 -0
- package/test/server/__mocks__/acme-client.js +17 -0
- package/test/server/__mocks__/bcrypt.js +50 -0
- package/test/server/__mocks__/child_process.js +389 -0
- package/test/server/__mocks__/crypto.js +432 -0
- package/test/server/__mocks__/fs.js +450 -0
- package/test/server/__mocks__/globalCandy.js +227 -0
- package/test/server/__mocks__/http-proxy.js +105 -0
- package/test/server/__mocks__/http.js +575 -0
- package/test/server/__mocks__/https.js +272 -0
- package/test/server/__mocks__/index.js +249 -0
- package/test/server/__mocks__/mail/server.js +100 -0
- package/test/server/__mocks__/mail/smtp.js +31 -0
- package/test/server/__mocks__/mailparser.js +81 -0
- package/test/server/__mocks__/net.js +369 -0
- package/test/server/__mocks__/node-forge.js +328 -0
- package/test/server/__mocks__/os.js +320 -0
- package/test/server/__mocks__/path.js +291 -0
- package/test/server/__mocks__/selfsigned.js +8 -0
- package/test/server/__mocks__/server/src/mail/server.js +100 -0
- package/test/server/__mocks__/server/src/mail/smtp.js +31 -0
- package/test/server/__mocks__/smtp-server.js +106 -0
- package/test/server/__mocks__/sqlite3.js +394 -0
- package/test/server/__mocks__/testFactories.js +299 -0
- package/test/server/__mocks__/testHelpers.js +363 -0
- package/test/server/__mocks__/tls.js +229 -0
- package/watchdog/index.js +3 -0
- package/watchdog/src/Watchdog.js +156 -0
- package/web/config.json +5 -0
- package/web/controller/page/about.js +27 -0
- package/web/controller/page/index.js +34 -0
- package/web/package.json +18 -0
- package/web/public/assets/css/style.css +1835 -0
- package/web/public/assets/js/app.js +96 -0
- package/web/route/www.js +19 -0
- package/web/skeleton/main.html +22 -0
- package/web/view/content/about.html +65 -0
- package/web/view/content/home.html +205 -0
- package/web/view/footer/main.html +11 -0
- package/web/view/head/main.html +5 -0
- package/web/view/header/main.html +14 -0
|
@@ -0,0 +1,134 @@
|
|
|
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
|
|
@@ -0,0 +1,451 @@
|
|
|
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()
|