Archived
1
0

feat: cli arg for file permission of socket (#4923)

This commit is contained in:
Ciel 2022-03-04 00:54:35 +08:00 committed by GitHub
parent 78658f1cf4
commit c4d87580ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 4 deletions

View File

@ -11,7 +11,7 @@ import { disposer } from "./http"
import { isNodeJSErrnoException } from "./util" import { isNodeJSErrnoException } from "./util"
import { handleUpgrade } from "./wsRouter" import { handleUpgrade } from "./wsRouter"
type ListenOptions = Pick<DefaultedArgs, "socket" | "port" | "host"> type ListenOptions = Pick<DefaultedArgs, "socket-mode" | "socket" | "port" | "host">
export interface App extends Disposable { export interface App extends Disposable {
/** Handles regular HTTP requests. */ /** Handles regular HTTP requests. */
@ -22,7 +22,7 @@ export interface App extends Disposable {
server: http.Server server: http.Server
} }
const listen = (server: http.Server, { host, port, socket }: ListenOptions) => { const listen = (server: http.Server, { host, port, socket, "socket-mode": mode }: ListenOptions) => {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
server.on("error", reject) server.on("error", reject)
@ -31,7 +31,16 @@ const listen = (server: http.Server, { host, port, socket }: ListenOptions) => {
server.off("error", reject) server.off("error", reject)
server.on("error", (err) => util.logError(logger, "http server error", err)) server.on("error", (err) => util.logError(logger, "http server error", err))
resolve() if (socket && mode) {
fs.chmod(socket, mode)
.then(resolve)
.catch((err) => {
util.logError(logger, "socket chmod", err)
reject(err)
})
} else {
resolve()
}
} }
if (socket) { if (socket) {

View File

@ -56,6 +56,7 @@ export interface UserProvidedArgs {
open?: boolean open?: boolean
"bind-addr"?: string "bind-addr"?: string
socket?: string socket?: string
"socket-mode"?: string
version?: boolean version?: boolean
"proxy-domain"?: string[] "proxy-domain"?: string[]
"reuse-window"?: boolean "reuse-window"?: boolean
@ -175,6 +176,7 @@ const options: Options<Required<UserProvidedArgs>> = {
port: { type: "number", description: "" }, port: { type: "number", description: "" },
socket: { type: "string", path: true, description: "Path to a socket (bind-addr will be ignored)." }, socket: { type: "string", path: true, description: "Path to a socket (bind-addr will be ignored)." },
"socket-mode": { type: "string", description: "File mode of the socket." },
version: { type: "boolean", short: "v", description: "Display version information." }, version: { type: "boolean", short: "v", description: "Display version information." },
_: { type: "string[]" }, _: { type: "string[]" },
@ -513,6 +515,7 @@ export async function setDefaults(cliArgs: UserProvidedArgs, configArgs?: Config
args.host = "localhost" args.host = "localhost"
args.port = 0 args.port = 0
args.socket = undefined args.socket = undefined
args["socket-mode"] = undefined
args.cert = undefined args.cert = undefined
args.auth = AuthType.None args.auth = AuthType.None
} }

View File

@ -107,6 +107,18 @@ describe("createApp", () => {
app.dispose() app.dispose()
}) })
it("should change the file mode of a socket", async () => {
const defaultArgs = await setDefaults({
socket: tmpFilePath,
"socket-mode": "777",
})
const app = await createApp(defaultArgs)
expect((await promises.stat(tmpFilePath)).mode & 0o777).toBe(0o777)
app.dispose()
})
it("should create an https server if args.cert exists", async () => { it("should create an https server if args.cert exists", async () => {
const testCertificate = await generateCertificate("localhost") const testCertificate = await generateCertificate("localhost")
const cert = new OptionalString(testCertificate.cert) const cert = new OptionalString(testCertificate.cert)

View File

@ -73,6 +73,8 @@ describe("parser", () => {
"--socket=mumble", "--socket=mumble",
"--socket-mode=777",
"3", "3",
["--user-data-dir", "path/to/user/dir"], ["--user-data-dir", "path/to/user/dir"],
@ -110,6 +112,7 @@ describe("parser", () => {
open: true, open: true,
port: 8081, port: 8081,
socket: path.resolve("mumble"), socket: path.resolve("mumble"),
"socket-mode": "777",
verbose: true, verbose: true,
version: true, version: true,
"bind-addr": "192.169.0.1:8080", "bind-addr": "192.169.0.1:8080",
@ -269,7 +272,9 @@ describe("parser", () => {
}) })
it("should override with --link", async () => { it("should override with --link", async () => {
const args = parse("--cert test --cert-key test --socket test --host 0.0.0.0 --port 8888 --link test".split(" ")) const args = parse(
"--cert test --cert-key test --socket test --socket-mode 777 --host 0.0.0.0 --port 8888 --link test".split(" "),
)
const defaultArgs = await setDefaults(args) const defaultArgs = await setDefaults(args)
expect(defaultArgs).toEqual({ expect(defaultArgs).toEqual({
...defaults, ...defaults,
@ -282,6 +287,7 @@ describe("parser", () => {
cert: undefined, cert: undefined,
"cert-key": path.resolve("test"), "cert-key": path.resolve("test"),
socket: undefined, socket: undefined,
"socket-mode": undefined,
}) })
}) })