pinglet-cli 0.2.3 โ 0.2.4
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/dist/commands/login.js +72 -13
- package/package.json +1 -1
package/dist/commands/login.js
CHANGED
|
@@ -35,9 +35,11 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.runLogin = runLogin;
|
|
37
37
|
const child_process_1 = require("child_process");
|
|
38
|
+
const http = __importStar(require("http"));
|
|
38
39
|
const readline = __importStar(require("readline"));
|
|
39
40
|
const config_1 = require("../config");
|
|
40
41
|
const api_1 = require("../api");
|
|
42
|
+
const LOGIN_TIMEOUT_MS = 180_000;
|
|
41
43
|
function prompt(question) {
|
|
42
44
|
const rl = readline.createInterface({
|
|
43
45
|
input: process.stdin,
|
|
@@ -48,10 +50,72 @@ function prompt(question) {
|
|
|
48
50
|
resolve(answer);
|
|
49
51
|
}));
|
|
50
52
|
}
|
|
53
|
+
function openBrowser(url) {
|
|
54
|
+
const cmd = process.platform === "darwin"
|
|
55
|
+
? ["open", url]
|
|
56
|
+
: process.platform === "win32"
|
|
57
|
+
? ["cmd", "/c", "start", "", url]
|
|
58
|
+
: ["xdg-open", url];
|
|
59
|
+
try {
|
|
60
|
+
(0, child_process_1.spawn)(cmd[0], cmd.slice(1), { stdio: "ignore" }).unref();
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const RESULT_HTML = (ok) => `<!doctype html>
|
|
68
|
+
<html lang="ko"><head><meta charset="utf-8"><title>Pinglet</title>
|
|
69
|
+
<style>body{font-family:-apple-system,sans-serif;display:flex;align-items:center;
|
|
70
|
+
justify-content:center;height:100vh;margin:0;background:#0f1115;color:#e6e9ef}
|
|
71
|
+
div{text-align:center}p{color:#8b93a3}</style></head><body><div>
|
|
72
|
+
<h1>${ok ? "๐ ๋ก๊ทธ์ธ ์๋ฃ" : "โ ๋ก๊ทธ์ธ ์คํจ"}</h1>
|
|
73
|
+
<p>${ok ? "์ด ์ฐฝ์ ๋ซ๊ณ ํฐ๋ฏธ๋๋ก ๋์๊ฐ์ธ์." : "ํฐ๋ฏธ๋์์ ๋ค์ ์๋ํด ์ฃผ์ธ์."}</p>
|
|
74
|
+
</div></body></html>`;
|
|
51
75
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
76
|
+
* ๋ฃจํ๋ฐฑ ๋ก๊ทธ์ธ: ์ผํ์ฉ ๋ก์ปฌ ์๋ฒ๋ฅผ ๋์ฐ๊ณ ๋ธ๋ผ์ฐ์ ์์ OAuth๋ฅผ ๋ง์น๋ฉด
|
|
77
|
+
* ์๋ฒ๊ฐ http://127.0.0.1:{port}/callback?token=... ์ผ๋ก ํ ํฐ์ ๋๊ฒจ์ค๋ค.
|
|
78
|
+
* ํ์์์/์คํจ ์ null โ ํธ์ถ๋ถ๊ฐ ์๋ ๋ถ์ฌ๋ฃ๊ธฐ๋ก ํด๋ฐฑํ๋ค.
|
|
54
79
|
*/
|
|
80
|
+
function loginViaLoopback(apiBaseUrl) {
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
let finished = false;
|
|
83
|
+
const done = (token) => {
|
|
84
|
+
if (finished)
|
|
85
|
+
return;
|
|
86
|
+
finished = true;
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
// ๋ง์ง๋ง ์๋ต์ด flush๋ ์๊ฐ์ ์ฃผ๊ณ ๋ซ๋๋ค.
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
server.closeAllConnections?.();
|
|
91
|
+
server.close();
|
|
92
|
+
}, 200).unref();
|
|
93
|
+
resolve(token);
|
|
94
|
+
};
|
|
95
|
+
const timer = setTimeout(() => done(null), LOGIN_TIMEOUT_MS);
|
|
96
|
+
const server = http.createServer((req, res) => {
|
|
97
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
98
|
+
if (url.pathname !== "/callback") {
|
|
99
|
+
res.statusCode = 404;
|
|
100
|
+
res.end();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const token = url.searchParams.get("token");
|
|
104
|
+
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
105
|
+
res.end(RESULT_HTML(Boolean(token)));
|
|
106
|
+
done(token);
|
|
107
|
+
});
|
|
108
|
+
server.on("error", () => done(null));
|
|
109
|
+
server.listen(0, "127.0.0.1", () => {
|
|
110
|
+
const port = server.address().port;
|
|
111
|
+
const url = `${apiBaseUrl}/auth/github?cli_port=${port}`;
|
|
112
|
+
console.log("๋ธ๋ผ์ฐ์ ์์ GitHub ๋ก๊ทธ์ธ์ ์๋ฃํด ์ฃผ์ธ์โฆ");
|
|
113
|
+
console.log(` ${url}\n`);
|
|
114
|
+
openBrowser(url);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/** GitHub OAuth ๋ก๊ทธ์ธ โ ๋ฃจํ๋ฐฑ ์๋ ์์ , ์คํจ ์ ์๋ ๋ถ์ฌ๋ฃ๊ธฐ ํด๋ฐฑ. */
|
|
55
119
|
async function runLogin(args) {
|
|
56
120
|
const config = (0, config_1.loadConfig)();
|
|
57
121
|
let token;
|
|
@@ -60,19 +124,14 @@ async function runLogin(args) {
|
|
|
60
124
|
token = args[tokenIndex + 1];
|
|
61
125
|
}
|
|
62
126
|
if (!token) {
|
|
63
|
-
const url = `${config.apiBaseUrl}/auth/github`;
|
|
64
127
|
console.log(`\n๋ก๊ทธ์ธํ๋ฉด ์ด์ฉ์ฝ๊ด(${config.apiBaseUrl}/terms)๊ณผ ๊ฐ์ธ์ ๋ณด์ฒ๋ฆฌ๋ฐฉ์นจ(${config.apiBaseUrl}/privacy)์ ๋์ํ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.`);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
catch {
|
|
72
|
-
// ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ชป ์ด์ด๋ URL์ ์ถ๋ ฅํ์ผ๋ฏ๋ก ์งํ
|
|
73
|
-
}
|
|
128
|
+
token = (await loginViaLoopback(config.apiBaseUrl)) ?? undefined;
|
|
129
|
+
if (!token) {
|
|
130
|
+
// SSH ๋ฑ ๋ธ๋ผ์ฐ์ ์ ํฐ๋ฏธ๋์ด ๋ค๋ฅธ ๊ธฐ๊ณ์ธ ๊ฒฝ์ฐ๋ฅผ ์ํ ํด๋ฐฑ.
|
|
131
|
+
console.log("์๋ ๋ก๊ทธ์ธ์ ์คํจํ์ต๋๋ค. ์๋ URL์์ ๋ก๊ทธ์ธ ํ ์๋ต JSON์ token ๊ฐ์ ๋ถ์ฌ๋ฃ์ด ์ฃผ์ธ์.");
|
|
132
|
+
console.log(` ${config.apiBaseUrl}/auth/github\n`);
|
|
133
|
+
token = (await prompt("token: ")).trim();
|
|
74
134
|
}
|
|
75
|
-
token = (await prompt("token: ")).trim();
|
|
76
135
|
}
|
|
77
136
|
if (!token) {
|
|
78
137
|
console.log("โ token์ด ๋น์ด ์์ต๋๋ค.");
|
package/package.json
CHANGED