Skip to content
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

fix(protal-web): sshConnect会因为sftp消息过长报500错误 #957

Merged
merged 13 commits into from
Nov 9, 2023
6 changes: 6 additions & 0 deletions .changeset/khaki-rockets-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@scow/portal-web": patch
"@scow/lib-ssh": patch
---

sshConnect 时,提示语过长会使得连接失败,现在捕获了这个错误并提示用户
2 changes: 1 addition & 1 deletion apps/portal-server/src/utils/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export async function sshConnect<T>(
code: status.INTERNAL,
details: e.message,
message: e.message,
metadata: scowErrorMetadata(SSH_ERROR_CODE),
metadata: scowErrorMetadata(SSH_ERROR_CODE, typeof e.cause === "string" ? { cause:e.cause } : undefined),
});
}

Expand Down
2 changes: 2 additions & 0 deletions apps/portal-web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ export default {
_app: {
sshError: "Unable to connect as a user to the login node. Please make sure the permissions "
+ "of your home directory are 700, 750, or 755.",
textExceedsLength:"There are too many welcome messages for terminal login."
+ "Please reduce unnecessary information output!",
sftpError: "SFTP operation failed. Please confirm if you have the necessary permissions.",
otherError: "Server encountered an error!",
},
Expand Down
2 changes: 1 addition & 1 deletion apps/portal-web/src/i18n/zh_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ export default {
},
},
_app: {
textExceedsLength:"终端登录欢迎提示信息过多,请减少不必要的信息输出!",
sshError:"无法以用户身份连接到登录节点。请确认您的家目录的权限为700、750或者755",

sftpError:"SFTP操作失败,请确认您是否有操作的权限",
otherError:"服务器出错啦!",
},
Expand Down
9 changes: 8 additions & 1 deletion apps/portal-web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ const FailEventHandler: React.FC = () => {
return;
}

const regex = /exceeds max length/;
// 如果终端登录欢迎语过长会报错:Packet length xxxx exceeds max length of 262144
if (regex.test(e.data?.message)) {
message.error(t("pages._app.textExceedsLength"));
return;
}

if (e.data?.code === "SSH_ERROR") {
message.error(t("pages._app.sshError"));
return;
}

if (e.data?.code === "SFTP_ERROR") {
message.error(e.data?.details.length > 150 ? e.data?.details.substring(0, 150) + "..." :
e.data?.details || t("pages._app.sftpError"));
Expand Down
4 changes: 3 additions & 1 deletion apps/portal-web/src/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const route: typeof typeboxRoute = (schema, handler) => {
if (!(e.metadata instanceof Metadata)) { throw e; }

const SCOW_ERROR = (e.metadata as Metadata).get("IS_SCOW_ERROR");
const SCOW_CAUSE = (e.metadata as Metadata).get("cause");
if (SCOW_ERROR.length === 0) { throw e; }

const code = e.metadata.get("SCOW_ERROR_CODE")[0].toString();
const details = e.details;
return { 500: { code, details } } as any;
const message = SCOW_CAUSE[0];
return { 500: { code, details, message } } as any;
});
}
});
Expand Down
13 changes: 12 additions & 1 deletion libs/ssh/src/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@
) {
const ssh = await sshRawConnect(address, username, rootKeyPair, logger);

return run(ssh).finally(() => { ssh.dispose(); });
return run(ssh)
.catch((e) => {
// 若在run回调函数中有具体抛错,直接抛出
if (e.code !== undefined) {
throw e;

Check warning on line 100 in libs/ssh/src/ssh.ts

View check run for this annotation

Codecov / codecov/patch

libs/ssh/src/ssh.ts#L100

Added line #L100 was not covered by tests
}
else {
logger.info("Running ssh failed.");
throw new SshConnectError({ cause: e.message });

Check warning on line 104 in libs/ssh/src/ssh.ts

View check run for this annotation

Codecov / codecov/patch

libs/ssh/src/ssh.ts#L103-L104

Added lines #L103 - L104 were not covered by tests
}
})
.finally(() => { ssh.dispose(); });
}

export async function sshConnectByPassword<T>(
Expand Down
Loading