-
Notifications
You must be signed in to change notification settings - Fork 346
エンジンが使うポートが割り当てできなければ、他の空いているポートで起動してユーザーに通知する #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hiroshiba
merged 24 commits into
VOICEVOX:main
from
wappon28dev:fix/handle-port-50021-used
Apr 25, 2023
Merged
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3adb7c1
Add: ポートが割り当て済みかどうかをまず検知
wappon28dev 057e95f
Add: 使っているプロセスIDと名前の取得
wappon28dev 619809b
Fix: dependencies 消し忘れ
wappon28dev f05a87a
Add: 空いているポートを探してエンジンに port を伝える
wappon28dev 2f81e09
Fix: 代替ポートが見つからないときのエラーメッセージを追加
wappon28dev f39ef13
Add: ChangePortInfosの追加
wappon28dev 0060e97
Add: トースト通知と代替ポート用にstateを追加
wappon28dev e445bfd
Add: IPCに代替ポートの情報を生やした
wappon28dev 7b0ac15
Fix: 変なdiff削除
wappon28dev 44a5ffc
Add: "今後この通知をしない" オプション追加
wappon28dev 3f5e18e
Enh: コメントいろいろ追加
wappon28dev 78b9f80
Enh: 良い書き方あった
wappon28dev 824e411
Merge branch 'VOICEVOX:main' into fix/handle-port-50021-used
wappon28dev 220068f
Fix: 古いコメント削除
wappon28dev 9f78aa7
Fix: 色, CSS変数の名前
wappon28dev bc5a40a
コメント追加
wappon28dev 8d711ff
TODO 追加
wappon28dev eed8d24
Enh: getUrlを直接実行へ
wappon28dev dc85487
Enh: AltPortInfo -> AltPortInfos
wappon28dev 21a4d9a
Enh: 早期リターンの削除
wappon28dev 6b71976
Enh: noticeAltPortInfo -> engineStartedOnAltPort
wappon28dev 324dcbc
Add: ループバックアドレスの探索追加
wappon28dev 2857f01
Add: 探索まわりのコメントを追加
wappon28dev a9ca4fa
Typo: loopBack -> loopback
wappon28dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { execFileSync } from "child_process"; | ||
| import log from "electron-log"; | ||
|
|
||
| const isWindows = process.platform === "win32"; | ||
|
|
||
| export class PortManager { | ||
| constructor(private hostname: string, private port: number) {} | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| portLog = (...message: any) => log.info(`PORT ${this.port}: ${message}`); | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| portWarn = (...message: any) => log.warn(`PORT ${this.port}: ${message}`); | ||
|
|
||
| public getUrl(isHttps = false): string { | ||
| return `${isHttps ? "https" : "http"}://${this.hostname}:${this.port}`; | ||
| } | ||
|
|
||
| /** | ||
| * "netstat -ano" の stdout から, 指定したポートを使用しているプロセスの process id を取得する | ||
| * | ||
| * ex) stdout: | ||
| * ``` cmd | ||
| * TCP 127.0.0.1:5173 127.0.0.1:50170 TIME_WAIT 0 | ||
| * TCP 127.0.0.1:6463 0.0.0.0:0 LISTENING 18692 | ||
| * TCP 127.0.0.1:50021 0.0.0.0:0 LISTENING 17320 | ||
| * ``` | ||
| * -> `17320` | ||
| * | ||
| * @param stdout netstat の stdout | ||
| * @returns `process id` or `undefined` (ポートが使用されていないとき) | ||
| */ | ||
| private stdout2processId(stdout: string): number | undefined { | ||
| const lines = stdout.split("\n"); | ||
| for (const line of lines) { | ||
| if (line.includes(`${this.hostname}:${this.port}`)) { | ||
wappon28dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const parts = line.trim().split(/\s+/); | ||
| return parseInt(parts[parts.length - 1], 10); | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| async getProcessIdFromPort(): Promise<number | undefined> { | ||
| this.portLog("Getting process id..."); | ||
| const exec = isWindows | ||
| ? { | ||
| cmd: "netstat", | ||
| args: ["-ano"], | ||
| } | ||
| : { | ||
| cmd: "lsof", | ||
| args: ["-i", `:${this.port}`, "-t", "-sTCP:LISTEN"], | ||
| }; | ||
|
|
||
| this.portLog(`Running command: "${exec.cmd} ${exec.args.join(" ")}"`); | ||
|
|
||
| let stdout = execFileSync(exec.cmd, exec.args, { | ||
| shell: true, | ||
| }).toString(); | ||
|
|
||
| if (isWindows) { | ||
wappon28dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| stdout = this.stdout2processId(stdout)?.toString() ?? ""; | ||
| } | ||
|
|
||
| if (!stdout || !stdout.length) { | ||
| this.portLog("Assignable; Nobody uses this port!"); | ||
| return undefined; | ||
| } | ||
|
|
||
| this.portWarn(`Nonassignable; pid=${stdout} uses this port!`); | ||
| return parseInt(stdout); | ||
| } | ||
|
|
||
| async getProcessNameFromPid(pid: number): Promise<string> { | ||
| this.portLog(`Getting process name from pid=${pid}...`); | ||
| const exec = isWindows | ||
| ? { | ||
| cmd: "wmic", | ||
| args: ["process", "where", `"ProcessID=${pid}"`, "get", "name"], | ||
| } | ||
| : { | ||
| cmd: "ps", | ||
| args: ["-p", pid.toString(), "-o", "comm="], | ||
| }; | ||
|
|
||
| let stdout = execFileSync(exec.cmd, exec.args, { shell: true }).toString(); | ||
|
|
||
| if (isWindows) { | ||
| /* | ||
| * ex) stdout: | ||
| * ``` | ||
| * Name | ||
| * node.exe | ||
| * ``` | ||
| * -> `node.exe` | ||
| */ | ||
| stdout = stdout.split("\r\n")[1]; | ||
| } | ||
|
|
||
| this.portLog(`Found process name: ${stdout}`); | ||
| return stdout.trim(); | ||
| } | ||
|
|
||
| /** | ||
| * 割り当て可能な他のポートを探します | ||
| * | ||
| * @returns 割り当て可能なポート番号 or `undefined` (割り当て可能なポートが見つからなかったとき) | ||
| */ | ||
| async findAltPort(): Promise<number | undefined> { | ||
| this.portLog(`Find another assignable port from ${this.port}...`); | ||
|
|
||
| // エンジン指定のポート + 100番までを探索 エフェメラルポートの範囲の最大は超えないようにする | ||
| const altPortMax = Math.min(this.port + 100, 65535); | ||
|
|
||
| for (let altPort = this.port + 1; altPort <= altPortMax; altPort++) { | ||
| this.portLog(`Trying whether port ${altPort} is assignable...`); | ||
| const altPid = await new PortManager( | ||
wappon28dev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.hostname, | ||
| altPort | ||
| ).getProcessIdFromPort(); | ||
|
|
||
| // ポートを既に割り当てられているプロセスidの取得: undefined → ポートが空いている | ||
| if (altPid === undefined) return altPort; | ||
| } | ||
|
|
||
| this.portWarn(`No alternative port found! ${this.port}...${altPortMax}`); | ||
| return undefined; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.