2020-05-10 15:15:29 +07:00
|
|
|
import { field, Level, logger } from "@coder/logger"
|
2020-05-10 12:19:32 +07:00
|
|
|
import * as fs from "fs-extra"
|
|
|
|
import yaml from "js-yaml"
|
2020-05-14 17:08:37 +07:00
|
|
|
import * as os from "os"
|
2020-02-07 02:11:38 +07:00
|
|
|
import * as path from "path"
|
2020-02-07 01:29:19 +07:00
|
|
|
import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
|
2020-10-16 04:17:04 +07:00
|
|
|
import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util"
|
2020-02-07 01:29:19 +07:00
|
|
|
|
2020-10-16 05:00:21 +07:00
|
|
|
export enum AuthType {
|
|
|
|
Password = "password",
|
|
|
|
None = "none",
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
export class Optional<T> {
|
|
|
|
public constructor(public readonly value?: T) {}
|
|
|
|
}
|
|
|
|
|
2020-02-20 00:06:32 +07:00
|
|
|
export enum LogLevel {
|
|
|
|
Trace = "trace",
|
|
|
|
Debug = "debug",
|
|
|
|
Info = "info",
|
|
|
|
Warn = "warn",
|
|
|
|
Error = "error",
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
export class OptionalString extends Optional<string> {}
|
|
|
|
|
2020-02-07 01:29:19 +07:00
|
|
|
export interface Args extends VsArgs {
|
2020-10-16 04:17:04 +07:00
|
|
|
config?: string
|
|
|
|
auth?: AuthType
|
|
|
|
password?: string
|
2020-12-09 03:54:17 +07:00
|
|
|
hashedPassword?: string
|
2020-10-16 04:17:04 +07:00
|
|
|
cert?: OptionalString
|
2020-11-13 00:52:02 +07:00
|
|
|
"cert-host"?: string
|
2020-10-16 04:17:04 +07:00
|
|
|
"cert-key"?: string
|
|
|
|
"disable-telemetry"?: boolean
|
2020-11-25 23:37:40 +07:00
|
|
|
"disable-update-check"?: boolean
|
2020-10-16 04:17:04 +07:00
|
|
|
help?: boolean
|
|
|
|
host?: string
|
|
|
|
json?: boolean
|
2020-02-20 00:06:32 +07:00
|
|
|
log?: LogLevel
|
2020-10-16 04:17:04 +07:00
|
|
|
open?: boolean
|
|
|
|
port?: number
|
|
|
|
"bind-addr"?: string
|
|
|
|
socket?: string
|
|
|
|
version?: boolean
|
|
|
|
force?: boolean
|
|
|
|
"list-extensions"?: boolean
|
|
|
|
"install-extension"?: string[]
|
|
|
|
"show-versions"?: boolean
|
|
|
|
"uninstall-extension"?: string[]
|
|
|
|
"proxy-domain"?: string[]
|
|
|
|
locale?: string
|
|
|
|
_: string[]
|
|
|
|
"reuse-window"?: boolean
|
|
|
|
"new-window"?: boolean
|
|
|
|
|
|
|
|
link?: OptionalString
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Option<T> {
|
|
|
|
type: T
|
|
|
|
/**
|
|
|
|
* Short flag for the option.
|
|
|
|
*/
|
|
|
|
short?: string
|
|
|
|
/**
|
|
|
|
* Whether the option is a path and should be resolved.
|
|
|
|
*/
|
|
|
|
path?: boolean
|
|
|
|
/**
|
|
|
|
* Description of the option. Leave blank to hide the option.
|
|
|
|
*/
|
|
|
|
description?: string
|
2020-10-09 18:38:38 +07:00
|
|
|
|
|
|
|
/**
|
2020-10-09 18:45:20 +07:00
|
|
|
* If marked as beta, the option is not printed unless $CS_BETA is set.
|
2020-10-09 18:38:38 +07:00
|
|
|
*/
|
2020-10-09 18:45:20 +07:00
|
|
|
beta?: boolean
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionType<T> = T extends boolean
|
|
|
|
? "boolean"
|
|
|
|
: T extends OptionalString
|
|
|
|
? typeof OptionalString
|
2020-02-20 00:06:32 +07:00
|
|
|
: T extends LogLevel
|
|
|
|
? typeof LogLevel
|
2020-02-07 07:26:07 +07:00
|
|
|
: T extends AuthType
|
|
|
|
? typeof AuthType
|
|
|
|
: T extends number
|
|
|
|
? "number"
|
|
|
|
: T extends string
|
|
|
|
? "string"
|
|
|
|
: T extends string[]
|
|
|
|
? "string[]"
|
|
|
|
: "unknown"
|
|
|
|
|
|
|
|
type Options<T> = {
|
|
|
|
[P in keyof T]: Option<OptionType<T[P]>>
|
2020-02-07 01:29:19 +07:00
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
const options: Options<Required<Args>> = {
|
|
|
|
auth: { type: AuthType, description: "The type of authentication to use." },
|
2020-05-10 15:15:29 +07:00
|
|
|
password: {
|
|
|
|
type: "string",
|
|
|
|
description: "The password for password authentication (can only be passed in via $PASSWORD or the config file).",
|
|
|
|
},
|
2020-12-09 03:54:17 +07:00
|
|
|
hashedPassword: {
|
|
|
|
type: "string",
|
|
|
|
description:
|
|
|
|
"The password hashed with SHA-256 for password authentication (can only be passed in via $HASHED_PASSWORD or the config file). \n" +
|
|
|
|
"Takes precedence over 'password'.",
|
|
|
|
},
|
2020-02-07 07:26:07 +07:00
|
|
|
cert: {
|
|
|
|
type: OptionalString,
|
|
|
|
path: true,
|
2020-10-30 16:26:40 +07:00
|
|
|
description: "Path to certificate. A self signed certificate is generated if none is provided.",
|
|
|
|
},
|
|
|
|
"cert-host": {
|
|
|
|
type: "string",
|
|
|
|
description: "Hostname to use when generating a self signed certificate.",
|
2020-02-07 07:26:07 +07:00
|
|
|
},
|
|
|
|
"cert-key": { type: "string", path: true, description: "Path to certificate key when using non-generated cert." },
|
2020-02-19 01:24:12 +07:00
|
|
|
"disable-telemetry": { type: "boolean", description: "Disable telemetry." },
|
2020-11-25 23:37:40 +07:00
|
|
|
"disable-update-check": {
|
|
|
|
type: "boolean",
|
|
|
|
description:
|
|
|
|
"Disable update check. Without this flag, code-server checks every 6 hours against the latest github release and \n" +
|
|
|
|
"then notifies you once every week that a new release is available.",
|
|
|
|
},
|
2020-02-07 07:26:07 +07:00
|
|
|
help: { type: "boolean", short: "h", description: "Show this output." },
|
|
|
|
json: { type: "boolean" },
|
2020-02-20 00:06:32 +07:00
|
|
|
open: { type: "boolean", description: "Open in browser on startup. Does not work remotely." },
|
2020-04-27 20:22:52 +07:00
|
|
|
|
2020-05-10 15:15:29 +07:00
|
|
|
"bind-addr": {
|
|
|
|
type: "string",
|
|
|
|
description: "Address to bind to in host:port. You can also use $PORT to override the port.",
|
|
|
|
},
|
2020-04-27 20:22:52 +07:00
|
|
|
|
2020-05-10 13:35:15 +07:00
|
|
|
config: {
|
|
|
|
type: "string",
|
2020-05-10 15:15:29 +07:00
|
|
|
description: "Path to yaml config file. Every flag maps directly to a key in the config file.",
|
2020-05-10 13:35:15 +07:00
|
|
|
},
|
2020-05-10 12:19:32 +07:00
|
|
|
|
2020-04-27 20:22:52 +07:00
|
|
|
// These two have been deprecated by bindAddr.
|
|
|
|
host: { type: "string", description: "" },
|
|
|
|
port: { type: "number", description: "" },
|
|
|
|
|
2020-04-29 05:29:25 +07:00
|
|
|
socket: { type: "string", path: true, description: "Path to a socket (bind-addr will be ignored)." },
|
2020-02-07 07:26:07 +07:00
|
|
|
version: { type: "boolean", short: "v", description: "Display version information." },
|
|
|
|
_: { type: "string[]" },
|
|
|
|
|
|
|
|
"user-data-dir": { type: "string", path: true, description: "Path to the user data directory." },
|
|
|
|
"extensions-dir": { type: "string", path: true, description: "Path to the extensions directory." },
|
|
|
|
"builtin-extensions-dir": { type: "string", path: true },
|
|
|
|
"extra-extensions-dir": { type: "string[]", path: true },
|
|
|
|
"extra-builtin-extensions-dir": { type: "string[]", path: true },
|
2020-03-14 01:21:46 +07:00
|
|
|
"list-extensions": { type: "boolean", description: "List installed VS Code extensions." },
|
|
|
|
force: { type: "boolean", description: "Avoid prompts when installing VS Code extensions." },
|
2020-08-14 06:08:35 +07:00
|
|
|
"install-extension": {
|
|
|
|
type: "string[]",
|
2020-09-09 11:06:28 +07:00
|
|
|
description:
|
|
|
|
"Install or update a VS Code extension by id or vsix. The identifier of an extension is `${publisher}.${name}`.\n" +
|
|
|
|
"To install a specific version provide `@${version}`. For example: 'vscode.csharp@1.2.3'.",
|
2020-08-14 06:08:35 +07:00
|
|
|
},
|
2020-08-27 02:18:40 +07:00
|
|
|
"enable-proposed-api": {
|
|
|
|
type: "string[]",
|
|
|
|
description:
|
|
|
|
"Enable proposed API features for extensions. Can receive one or more extension IDs to enable individually.",
|
|
|
|
},
|
2020-03-14 01:21:46 +07:00
|
|
|
"uninstall-extension": { type: "string[]", description: "Uninstall a VS Code extension by id." },
|
|
|
|
"show-versions": { type: "boolean", description: "Show VS Code extension versions." },
|
2020-03-24 00:08:50 +07:00
|
|
|
"proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
|
2020-02-07 07:26:07 +07:00
|
|
|
|
2020-08-28 01:06:21 +07:00
|
|
|
"new-window": {
|
|
|
|
type: "boolean",
|
|
|
|
short: "n",
|
2020-09-16 00:47:33 +07:00
|
|
|
description: "Force to open a new window.",
|
2020-08-28 01:06:21 +07:00
|
|
|
},
|
|
|
|
"reuse-window": {
|
|
|
|
type: "boolean",
|
|
|
|
short: "r",
|
2020-09-16 00:47:33 +07:00
|
|
|
description: "Force to open a file or folder in an already opened window.",
|
2020-08-28 01:06:21 +07:00
|
|
|
},
|
|
|
|
|
2020-02-21 01:38:46 +07:00
|
|
|
locale: { type: "string" },
|
2020-02-20 00:06:32 +07:00
|
|
|
log: { type: LogLevel },
|
2020-02-07 07:26:07 +07:00
|
|
|
verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
|
2020-09-09 06:39:17 +07:00
|
|
|
|
2020-10-09 23:57:20 +07:00
|
|
|
link: {
|
2020-10-08 02:54:41 +07:00
|
|
|
type: OptionalString,
|
2020-09-09 07:30:31 +07:00
|
|
|
description: `
|
2020-10-07 08:05:32 +07:00
|
|
|
Securely bind code-server via Coder Cloud with the passed name. You'll get a URL like
|
2020-09-09 07:30:31 +07:00
|
|
|
https://myname.coder-cloud.com at which you can easily access your code-server instance.
|
2020-10-07 08:10:46 +07:00
|
|
|
Authorization is done via GitHub.
|
2020-10-09 18:45:20 +07:00
|
|
|
This is presently beta and requires being accepted for testing.
|
|
|
|
See https://github.com/cdr/code-server/discussions/2137
|
2020-10-07 08:10:46 +07:00
|
|
|
`,
|
2020-10-09 18:45:20 +07:00
|
|
|
beta: true,
|
2020-09-09 07:30:31 +07:00
|
|
|
},
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
export const optionDescriptions = (): string[] => {
|
|
|
|
const entries = Object.entries(options).filter(([, v]) => !!v.description)
|
|
|
|
const widths = entries.reduce(
|
|
|
|
(prev, [k, v]) => ({
|
|
|
|
long: k.length > prev.long ? k.length : prev.long,
|
|
|
|
short: v.short && v.short.length > prev.short ? v.short.length : prev.short,
|
|
|
|
}),
|
2020-02-15 07:46:00 +07:00
|
|
|
{ short: 0, long: 0 },
|
2020-02-07 07:26:07 +07:00
|
|
|
)
|
2020-10-09 18:45:20 +07:00
|
|
|
return entries
|
|
|
|
.filter(([, v]) => {
|
|
|
|
// If CS_BETA is set, we show beta options but if not, then we do not want
|
|
|
|
// to show beta options.
|
|
|
|
return process.env.CS_BETA || !v.beta
|
|
|
|
})
|
|
|
|
.map(([k, v]) => {
|
|
|
|
const help = `${" ".repeat(widths.short - (v.short ? v.short.length : 0))}${
|
|
|
|
v.short ? `-${v.short}` : " "
|
|
|
|
} --${k} `
|
|
|
|
return (
|
|
|
|
help +
|
|
|
|
v.description
|
|
|
|
?.trim()
|
|
|
|
.split(/\n/)
|
|
|
|
.map((line, i) => {
|
|
|
|
line = line.trim()
|
|
|
|
if (i === 0) {
|
|
|
|
return " ".repeat(widths.long - k.length) + line
|
|
|
|
}
|
|
|
|
return " ".repeat(widths.long + widths.short + 6) + line
|
|
|
|
})
|
|
|
|
.join("\n") +
|
|
|
|
(typeof v.type === "object" ? ` [${Object.values(v.type).join(", ")}]` : "")
|
|
|
|
)
|
|
|
|
})
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
2020-05-19 11:39:57 +07:00
|
|
|
export const parse = (
|
2020-05-10 15:15:29 +07:00
|
|
|
argv: string[],
|
|
|
|
opts?: {
|
|
|
|
configFile: string
|
|
|
|
},
|
2020-05-19 11:39:57 +07:00
|
|
|
): Args => {
|
2020-05-10 15:15:29 +07:00
|
|
|
const error = (msg: string): Error => {
|
|
|
|
if (opts?.configFile) {
|
|
|
|
msg = `error reading ${opts.configFile}: ${msg}`
|
|
|
|
}
|
|
|
|
return new Error(msg)
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
const args: Args = { _: [] }
|
|
|
|
let ended = false
|
|
|
|
|
|
|
|
for (let i = 0; i < argv.length; ++i) {
|
|
|
|
const arg = argv[i]
|
|
|
|
|
|
|
|
// -- signals the end of option parsing.
|
2020-08-05 03:08:45 +07:00
|
|
|
if (!ended && arg === "--") {
|
2020-02-07 07:26:07 +07:00
|
|
|
ended = true
|
|
|
|
continue
|
|
|
|
}
|
2020-02-07 02:11:38 +07:00
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
// Options start with a dash and require a value if non-boolean.
|
|
|
|
if (!ended && arg.startsWith("-")) {
|
|
|
|
let key: keyof Args | undefined
|
2020-02-19 23:54:23 +07:00
|
|
|
let value: string | undefined
|
2020-02-07 07:26:07 +07:00
|
|
|
if (arg.startsWith("--")) {
|
2020-02-19 23:54:23 +07:00
|
|
|
const split = arg.replace(/^--/, "").split("=", 2)
|
|
|
|
key = split[0] as keyof Args
|
|
|
|
value = split[1]
|
2020-02-07 07:26:07 +07:00
|
|
|
} else {
|
|
|
|
const short = arg.replace(/^-/, "")
|
|
|
|
const pair = Object.entries(options).find(([, v]) => v.short === short)
|
|
|
|
if (pair) {
|
|
|
|
key = pair[0] as keyof Args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !options[key]) {
|
2020-05-10 15:15:29 +07:00
|
|
|
throw error(`Unknown option ${arg}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key === "password" && !opts?.configFile) {
|
|
|
|
throw new Error("--password can only be set in the config file or passed in via $PASSWORD")
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
2020-12-09 03:54:17 +07:00
|
|
|
if (key === "hashedPassword" && !opts?.configFile) {
|
|
|
|
throw new Error("--hashedPassword can only be set in the config file or passed in via $HASHED_PASSWORD")
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
const option = options[key]
|
|
|
|
if (option.type === "boolean") {
|
|
|
|
;(args[key] as boolean) = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-19 23:54:23 +07:00
|
|
|
// Might already have a value if it was the --long=value format.
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
// A value is only valid if it doesn't look like an option.
|
|
|
|
value = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[++i] : undefined
|
|
|
|
}
|
|
|
|
|
2020-02-07 07:26:07 +07:00
|
|
|
if (!value && option.type === OptionalString) {
|
|
|
|
;(args[key] as OptionalString) = new OptionalString(value)
|
|
|
|
continue
|
|
|
|
} else if (!value) {
|
2020-05-10 15:15:29 +07:00
|
|
|
throw error(`--${key} requires a value`)
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:08:45 +07:00
|
|
|
if (option.type === OptionalString && value === "false") {
|
2020-05-10 15:15:29 +07:00
|
|
|
continue
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (option.path) {
|
|
|
|
value = path.resolve(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (option.type) {
|
|
|
|
case "string":
|
|
|
|
;(args[key] as string) = value
|
|
|
|
break
|
|
|
|
case "string[]":
|
|
|
|
if (!args[key]) {
|
|
|
|
;(args[key] as string[]) = []
|
|
|
|
}
|
|
|
|
;(args[key] as string[]).push(value)
|
|
|
|
break
|
|
|
|
case "number":
|
|
|
|
;(args[key] as number) = parseInt(value, 10)
|
|
|
|
if (isNaN(args[key] as number)) {
|
2020-05-10 15:15:29 +07:00
|
|
|
throw error(`--${key} must be a number`)
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case OptionalString:
|
|
|
|
;(args[key] as OptionalString) = new OptionalString(value)
|
|
|
|
break
|
|
|
|
default: {
|
2020-04-29 04:39:01 +07:00
|
|
|
if (!Object.values(option.type).includes(value)) {
|
2020-05-10 15:15:29 +07:00
|
|
|
throw error(`--${key} valid values: [${Object.values(option.type).join(", ")}]`)
|
2020-02-07 07:26:07 +07:00
|
|
|
}
|
|
|
|
;(args[key] as string) = value
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else goes into _.
|
|
|
|
args._.push(arg)
|
2020-02-07 02:11:38 +07:00
|
|
|
}
|
|
|
|
|
2020-10-16 04:17:04 +07:00
|
|
|
// If a cert was provided a key must also be provided.
|
|
|
|
if (args.cert && args.cert.value && !args["cert-key"]) {
|
|
|
|
throw new Error("--cert-key is missing")
|
|
|
|
}
|
|
|
|
|
2020-10-17 00:31:22 +07:00
|
|
|
logger.debug(() => ["parsed command line", field("args", { ...args, password: undefined })])
|
2020-02-07 07:26:07 +07:00
|
|
|
|
2020-09-16 01:43:07 +07:00
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2020-10-16 04:17:04 +07:00
|
|
|
export interface DefaultedArgs extends ConfigArgs {
|
|
|
|
auth: AuthType
|
|
|
|
cert?: {
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
host: string
|
|
|
|
port: number
|
|
|
|
"proxy-domain": string[]
|
|
|
|
verbose: boolean
|
|
|
|
usingEnvPassword: boolean
|
2020-12-09 03:54:17 +07:00
|
|
|
usingEnvHashedPassword: boolean
|
2020-10-16 04:17:04 +07:00
|
|
|
"extensions-dir": string
|
|
|
|
"user-data-dir": string
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take CLI and config arguments (optional) and return a single set of arguments
|
|
|
|
* with the defaults set. Arguments from the CLI are prioritized over config
|
|
|
|
* arguments.
|
|
|
|
*/
|
|
|
|
export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promise<DefaultedArgs> {
|
|
|
|
const args = Object.assign({}, configArgs || {}, cliArgs)
|
2020-09-16 01:43:07 +07:00
|
|
|
|
|
|
|
if (!args["user-data-dir"]) {
|
|
|
|
await copyOldMacOSDataDir()
|
|
|
|
args["user-data-dir"] = paths.data
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args["extensions-dir"]) {
|
|
|
|
args["extensions-dir"] = path.join(args["user-data-dir"], "extensions")
|
|
|
|
}
|
|
|
|
|
2020-04-29 04:39:01 +07:00
|
|
|
// --verbose takes priority over --log and --log takes priority over the
|
|
|
|
// environment variable.
|
|
|
|
if (args.verbose) {
|
|
|
|
args.log = LogLevel.Trace
|
|
|
|
} else if (
|
|
|
|
!args.log &&
|
|
|
|
process.env.LOG_LEVEL &&
|
|
|
|
Object.values(LogLevel).includes(process.env.LOG_LEVEL as LogLevel)
|
|
|
|
) {
|
2020-02-20 00:06:32 +07:00
|
|
|
args.log = process.env.LOG_LEVEL as LogLevel
|
2020-02-07 01:29:19 +07:00
|
|
|
}
|
2020-02-07 07:26:07 +07:00
|
|
|
|
2020-04-29 04:39:01 +07:00
|
|
|
// Sync --log, --verbose, the environment variable, and logger level.
|
|
|
|
if (args.log) {
|
|
|
|
process.env.LOG_LEVEL = args.log
|
|
|
|
}
|
2020-02-07 07:26:07 +07:00
|
|
|
switch (args.log) {
|
2020-02-20 00:06:32 +07:00
|
|
|
case LogLevel.Trace:
|
2020-02-07 07:26:07 +07:00
|
|
|
logger.level = Level.Trace
|
2020-04-29 04:39:01 +07:00
|
|
|
args.verbose = true
|
2020-02-07 07:26:07 +07:00
|
|
|
break
|
2020-02-20 00:06:32 +07:00
|
|
|
case LogLevel.Debug:
|
2020-02-07 07:26:07 +07:00
|
|
|
logger.level = Level.Debug
|
2020-05-19 11:39:57 +07:00
|
|
|
args.verbose = false
|
2020-02-07 07:26:07 +07:00
|
|
|
break
|
2020-02-20 00:06:32 +07:00
|
|
|
case LogLevel.Info:
|
2020-02-07 07:26:07 +07:00
|
|
|
logger.level = Level.Info
|
2020-05-19 11:39:57 +07:00
|
|
|
args.verbose = false
|
2020-02-07 07:26:07 +07:00
|
|
|
break
|
2020-02-20 00:06:32 +07:00
|
|
|
case LogLevel.Warn:
|
2020-02-07 07:26:07 +07:00
|
|
|
logger.level = Level.Warning
|
2020-05-19 11:39:57 +07:00
|
|
|
args.verbose = false
|
2020-02-07 07:26:07 +07:00
|
|
|
break
|
2020-02-20 00:06:32 +07:00
|
|
|
case LogLevel.Error:
|
2020-02-07 07:26:07 +07:00
|
|
|
logger.level = Level.Error
|
2020-05-19 11:39:57 +07:00
|
|
|
args.verbose = false
|
2020-02-07 07:26:07 +07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-10-16 04:17:04 +07:00
|
|
|
// Default to using a password.
|
|
|
|
if (!args.auth) {
|
|
|
|
args.auth = AuthType.Password
|
|
|
|
}
|
|
|
|
|
2020-11-05 06:07:03 +07:00
|
|
|
const addr = bindAddrFromAllSources(configArgs || { _: [] }, cliArgs)
|
2020-11-04 03:26:25 +07:00
|
|
|
args.host = addr.host
|
|
|
|
args.port = addr.port
|
2020-10-16 04:17:04 +07:00
|
|
|
|
|
|
|
// If we're being exposed to the cloud, we listen on a random address and
|
|
|
|
// disable auth.
|
|
|
|
if (args.link) {
|
|
|
|
args.host = "localhost"
|
|
|
|
args.port = 0
|
|
|
|
args.socket = undefined
|
|
|
|
args.cert = undefined
|
2020-11-04 03:30:34 +07:00
|
|
|
args.auth = AuthType.None
|
2020-10-16 04:17:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.cert && !args.cert.value) {
|
2020-11-13 00:52:02 +07:00
|
|
|
const { cert, certKey } = await generateCertificate(args["cert-host"] || "localhost")
|
2020-10-16 04:17:04 +07:00
|
|
|
args.cert = {
|
|
|
|
value: cert,
|
|
|
|
}
|
|
|
|
args["cert-key"] = certKey
|
|
|
|
}
|
|
|
|
|
2020-12-09 03:54:17 +07:00
|
|
|
let usingEnvPassword = !!process.env.PASSWORD
|
2020-10-16 04:17:04 +07:00
|
|
|
if (process.env.PASSWORD) {
|
|
|
|
args.password = process.env.PASSWORD
|
|
|
|
}
|
|
|
|
|
2020-12-09 03:54:17 +07:00
|
|
|
const usingEnvHashedPassword = !!process.env.HASHED_PASSWORD
|
|
|
|
if (process.env.HASHED_PASSWORD) {
|
|
|
|
args.hashedPassword = process.env.HASHED_PASSWORD
|
|
|
|
usingEnvPassword = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure they're not readable by child processes.
|
2020-10-16 04:17:04 +07:00
|
|
|
delete process.env.PASSWORD
|
2020-12-09 03:54:17 +07:00
|
|
|
delete process.env.HASHED_PASSWORD
|
2020-10-16 04:17:04 +07:00
|
|
|
|
|
|
|
// Filter duplicate proxy domains and remove any leading `*.`.
|
|
|
|
const proxyDomains = new Set((args["proxy-domain"] || []).map((d) => d.replace(/^\*\./, "")))
|
|
|
|
args["proxy-domain"] = Array.from(proxyDomains)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...args,
|
|
|
|
usingEnvPassword,
|
2020-12-09 03:54:17 +07:00
|
|
|
usingEnvHashedPassword,
|
2020-10-16 04:17:04 +07:00
|
|
|
} as DefaultedArgs // TODO: Technically no guarantee this is fulfilled.
|
2020-05-19 11:39:57 +07:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:15:29 +07:00
|
|
|
async function defaultConfigFile(): Promise<string> {
|
|
|
|
return `bind-addr: 127.0.0.1:8080
|
2020-05-10 12:35:42 +07:00
|
|
|
auth: password
|
2020-05-10 15:15:29 +07:00
|
|
|
password: ${await generatePassword()}
|
|
|
|
cert: false
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2020-10-16 04:17:04 +07:00
|
|
|
interface ConfigArgs extends Args {
|
|
|
|
config: string
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:15:29 +07:00
|
|
|
/**
|
|
|
|
* Reads the code-server yaml config file and returns it as Args.
|
|
|
|
*
|
|
|
|
* @param configPath Read the config from configPath instead of $CODE_SERVER_CONFIG or the default.
|
|
|
|
*/
|
2020-10-16 04:17:04 +07:00
|
|
|
export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
|
2020-05-10 15:15:29 +07:00
|
|
|
if (!configPath) {
|
|
|
|
configPath = process.env.CODE_SERVER_CONFIG
|
|
|
|
if (!configPath) {
|
|
|
|
configPath = path.join(paths.config, "config.yaml")
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 12:19:32 +07:00
|
|
|
|
|
|
|
if (!(await fs.pathExists(configPath))) {
|
2020-05-10 15:15:29 +07:00
|
|
|
await fs.outputFile(configPath, await defaultConfigFile())
|
|
|
|
logger.info(`Wrote default config file to ${humanPath(configPath)}`)
|
2020-05-10 12:19:32 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
const configFile = await fs.readFile(configPath)
|
|
|
|
const config = yaml.safeLoad(configFile.toString(), {
|
2020-05-10 15:15:29 +07:00
|
|
|
filename: configPath,
|
2020-05-10 12:19:32 +07:00
|
|
|
})
|
2020-08-27 01:21:37 +07:00
|
|
|
if (!config || typeof config === "string") {
|
|
|
|
throw new Error(`invalid config: ${config}`)
|
|
|
|
}
|
2020-05-10 12:19:32 +07:00
|
|
|
|
|
|
|
// We convert the config file into a set of flags.
|
|
|
|
// This is a temporary measure until we add a proper CLI library.
|
2020-05-10 12:35:42 +07:00
|
|
|
const configFileArgv = Object.entries(config).map(([optName, opt]) => {
|
2020-05-10 15:15:29 +07:00
|
|
|
if (opt === true) {
|
2020-05-10 12:35:42 +07:00
|
|
|
return `--${optName}`
|
|
|
|
}
|
|
|
|
return `--${optName}=${opt}`
|
|
|
|
})
|
2020-05-19 11:39:57 +07:00
|
|
|
const args = parse(configFileArgv, {
|
2020-05-10 15:15:29 +07:00
|
|
|
configFile: configPath,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
...args,
|
|
|
|
config: configPath,
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 12:19:32 +07:00
|
|
|
|
2020-11-04 03:26:25 +07:00
|
|
|
function parseBindAddr(bindAddr: string): Addr {
|
2020-05-10 15:15:29 +07:00
|
|
|
const u = new URL(`http://${bindAddr}`)
|
2020-11-04 03:26:25 +07:00
|
|
|
return {
|
|
|
|
host: u.hostname,
|
|
|
|
// With the http scheme 80 will be dropped so assume it's 80 if missing.
|
|
|
|
// This means --bind-addr <addr> without a port will default to 80 as well
|
|
|
|
// and not the code-server default.
|
|
|
|
port: u.port ? parseInt(u.port, 10) : 80,
|
|
|
|
}
|
2020-05-10 12:19:32 +07:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:15:29 +07:00
|
|
|
interface Addr {
|
|
|
|
host: string
|
|
|
|
port: number
|
|
|
|
}
|
|
|
|
|
|
|
|
function bindAddrFromArgs(addr: Addr, args: Args): Addr {
|
|
|
|
addr = { ...addr }
|
|
|
|
if (args["bind-addr"]) {
|
2020-11-04 03:26:25 +07:00
|
|
|
addr = parseBindAddr(args["bind-addr"])
|
2020-05-10 12:19:32 +07:00
|
|
|
}
|
2020-05-10 15:15:29 +07:00
|
|
|
if (args.host) {
|
|
|
|
addr.host = args.host
|
2020-05-10 12:19:32 +07:00
|
|
|
}
|
2020-05-15 08:57:10 +07:00
|
|
|
|
|
|
|
if (process.env.PORT) {
|
|
|
|
addr.port = parseInt(process.env.PORT, 10)
|
|
|
|
}
|
2020-05-10 15:15:29 +07:00
|
|
|
if (args.port !== undefined) {
|
|
|
|
addr.port = args.port
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2020-11-04 03:26:25 +07:00
|
|
|
function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
|
2020-05-10 15:15:29 +07:00
|
|
|
let addr: Addr = {
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
}
|
|
|
|
|
2020-11-04 03:26:25 +07:00
|
|
|
for (const args of argsConfig) {
|
|
|
|
addr = bindAddrFromArgs(addr, args)
|
|
|
|
}
|
2020-05-10 15:15:29 +07:00
|
|
|
|
2020-11-04 03:26:25 +07:00
|
|
|
return addr
|
2020-05-10 12:19:32 +07:00
|
|
|
}
|
2020-05-14 17:08:37 +07:00
|
|
|
|
|
|
|
async function copyOldMacOSDataDir(): Promise<void> {
|
|
|
|
if (os.platform() !== "darwin") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (await fs.pathExists(paths.data)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the old data directory exists, we copy it in.
|
|
|
|
const oldDataDir = path.join(os.homedir(), "Library/Application Support", "code-server")
|
|
|
|
if (await fs.pathExists(oldDataDir)) {
|
|
|
|
await fs.copy(oldDataDir, paths.data)
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 03:57:58 +07:00
|
|
|
|
|
|
|
export const shouldRunVsCodeCli = (args: Args): boolean => {
|
|
|
|
return !!args["list-extensions"] || !!args["install-extension"] || !!args["uninstall-extension"]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if it looks like the user is trying to open a file or folder in an
|
|
|
|
* existing instance. The arguments here should be the arguments the user
|
|
|
|
* explicitly passed on the command line, not defaults or the configuration.
|
|
|
|
*/
|
|
|
|
export const shouldOpenInExistingInstance = async (args: Args): Promise<string | undefined> => {
|
|
|
|
// Always use the existing instance if we're running from VS Code's terminal.
|
|
|
|
if (process.env.VSCODE_IPC_HOOK_CLI) {
|
|
|
|
return process.env.VSCODE_IPC_HOOK_CLI
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:51:43 +07:00
|
|
|
const readSocketPath = async (): Promise<string | undefined> => {
|
|
|
|
try {
|
|
|
|
return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8")
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== "ENOENT") {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// If these flags are set then assume the user is trying to open in an
|
|
|
|
// existing instance since these flags have no effect otherwise.
|
|
|
|
const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
|
|
|
|
return args[cur as keyof Args] ? prev + 1 : prev
|
|
|
|
}, 0)
|
|
|
|
if (openInFlagCount > 0) {
|
|
|
|
return readSocketPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's possible the user is trying to spawn another instance of code-server.
|
2020-10-08 01:03:39 +07:00
|
|
|
// Check if any unrelated flags are set (check against one because `_` always
|
|
|
|
// exists), that a file or directory was passed, and that the socket is
|
|
|
|
// active.
|
|
|
|
if (Object.keys(args).length === 1 && args._.length > 0) {
|
2020-09-16 04:51:43 +07:00
|
|
|
const socketPath = await readSocketPath()
|
|
|
|
if (socketPath && (await canConnect(socketPath))) {
|
|
|
|
return socketPath
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 03:57:58 +07:00
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|