Commit 40fc904e authored by nanahira's avatar nanahira

add htpasswd

parent 949133ec
Pipeline #22640 passed with stages
in 24 minutes and 43 seconds
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0" "mustache": "^4.2.0",
"nano-md5": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^28.1.6", "@types/jest": "^28.1.6",
...@@ -3657,6 +3658,11 @@ ...@@ -3657,6 +3658,11 @@
"mustache": "bin/mustache" "mustache": "bin/mustache"
} }
}, },
"node_modules/nano-md5": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nano-md5/-/nano-md5-1.0.5.tgz",
"integrity": "sha512-1VAOX0EiuwAdCMGpnglxp9r6ylm+gXwQi+UPAnc/Oj1tLLJ8D1I8rLZeiO4MWsUAqH8tuBAHweT1LYSrDfJlPg=="
},
"node_modules/natural-compare": { "node_modules/natural-compare": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
...@@ -7537,6 +7543,11 @@ ...@@ -7537,6 +7543,11 @@
"resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz", "resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz",
"integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==" "integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ=="
}, },
"nano-md5": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nano-md5/-/nano-md5-1.0.5.tgz",
"integrity": "sha512-1VAOX0EiuwAdCMGpnglxp9r6ylm+gXwQi+UPAnc/Oj1tLLJ8D1I8rLZeiO4MWsUAqH8tuBAHweT1LYSrDfJlPg=="
},
"natural-compare": { "natural-compare": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
......
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
}, },
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mustache": "^4.2.0" "mustache": "^4.2.0",
"nano-md5": "^1.0.5"
} }
} }
import { pickCert } from './check-cert'; import { pickCert } from './check-cert';
import { Parser } from './parser'; import { Parser } from './parser';
import { getSiteNames } from './utility'; import { getSiteNames } from './utility';
import md5 from 'nano-md5';
import fs from 'fs';
export interface SiteHttps { export interface SiteHttps {
ports: number[]; ports: number[];
...@@ -23,6 +25,7 @@ export interface SiteRenderData { ...@@ -23,6 +25,7 @@ export interface SiteRenderData {
disableTop?: boolean; disableTop?: boolean;
serverExtra?: string[]; serverExtra?: string[];
locationExtra?: string[]; locationExtra?: string[];
htpasswd?: string;
} }
export interface ProxyRenderData extends SiteRenderData { export interface ProxyRenderData extends SiteRenderData {
...@@ -145,6 +148,17 @@ async function getSiteData( ...@@ -145,6 +148,17 @@ async function getSiteData(
} as ProxyRenderData; } as ProxyRenderData;
} }
const basicPasswords = Object.entries(parser.getDict('HTPASSWD'));
if (basicPasswords.length) {
const htpasswd = basicPasswords
.map(([user, pass]) => `${user}:${md5.crypt(pass)}`)
.join('\n');
await fs.promises.writeFile(
`/etc/nginx/generated/htpasswd-${domain}`,
htpasswd,
);
}
return { return {
domains: domains, domains: domains,
ports: parser.getArrayNumber('PORT') || [80], ports: parser.getArrayNumber('PORT') || [80],
...@@ -157,6 +171,9 @@ async function getSiteData( ...@@ -157,6 +171,9 @@ async function getSiteData(
disableTop: parser.getBoolean('DISABLE_TOP'), disableTop: parser.getBoolean('DISABLE_TOP'),
serverExtra: parser.getArray('SERVER_EXTRA'), serverExtra: parser.getArray('SERVER_EXTRA'),
locationExtra: parser.getArray('LOCATION_EXTRA'), locationExtra: parser.getArray('LOCATION_EXTRA'),
htpasswd: basicPasswords.length
? `/etc/nginx/generated/htpasswd-${domain}`
: undefined,
...specificRenderData, ...specificRenderData,
}; };
} }
......
...@@ -195,6 +195,11 @@ http { ...@@ -195,6 +195,11 @@ http {
} }
{{/disableTop}} {{/disableTop}}
{{#htpasswd}}
auth_basic "Please enter your username and password";
auth_basic_user_file {{htpasswd}};
{{/htpasswd}}
{{#serverExtra}} {{#serverExtra}}
{{.}} {{.}}
{{/serverExtra}} {{/serverExtra}}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment