diff --git a/src/assets/locales.ts b/src/assets/locales.ts index 8d9bc07c..20bf4acc 100644 --- a/src/assets/locales.ts +++ b/src/assets/locales.ts @@ -73,6 +73,8 @@ const message = { change: '更 换', editorTip: '你可以插入单独行的 为摘要分隔标识(此行之前内容为摘要)', saveError: '保存失败', + privateKeyTip: '请填写绝对路径,例如:/home/username/.ssh/id_rsa', + remotePathTip: '请填写绝对路径,例如:/home/username/www/', testConnection: '检测远程连接', connectSuccess: '远程连接成功', connectFailed: '远程连接失败,请检查仓库、用户名和 Token 设置', @@ -182,6 +184,8 @@ const message = { change: '更 換', editorTip: '你可以插入單獨行的 為摘要分隔標識(此行之前內容為摘要)', saveError: '保存失敗', + privateKeyTip: '請填寫絕對路徑,例如:/home/username/.ssh/id_rsa', + remotePathTip: '請填寫絕對路徑,例如:/home/username/www/', testConnection: '檢測遠程連接', connectSuccess: '遠程連接成功', connectFailed: '遠程連接失敗,請檢查倉庫、用戶名和 Token 設置', @@ -290,6 +294,8 @@ const message = { change: 'Change', editorTip: 'You can insert a separate line is the abstract separator identifier ( the content before this line is the abstract)', saveError: 'Save failed', + privateKeyTip: 'Please fill in the absolute path, for example: /home/username/.ssh/id_rsa', + remotePathTip: 'Please fill in the absolute path, for example::/home/username/www/', testConnection: 'Test Connection', connectSuccess: 'Remote connection succeeded', connectFailed: 'Remote connection failed, please check repository, username and token settings', diff --git a/src/interfaces/setting.ts b/src/interfaces/setting.ts index da123e31..e7179fa8 100644 --- a/src/interfaces/setting.ts +++ b/src/interfaces/setting.ts @@ -10,6 +10,7 @@ export interface ISetting { port: string server: string password: string + privateKey: string remotePath: string [index: string]: string } diff --git a/src/server/app.ts b/src/server/app.ts index 81417c58..3b030f9d 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -75,6 +75,7 @@ export default class App { port: '22', server: '', password: '', + privateKey: '', remotePath: '', }, commentSetting: { diff --git a/src/server/plugins/deploys/sftp.ts b/src/server/plugins/deploys/sftp.ts index 157dab27..515d214a 100644 --- a/src/server/plugins/deploys/sftp.ts +++ b/src/server/plugins/deploys/sftp.ts @@ -1,10 +1,20 @@ import * as fse from 'fs-extra' +// import * as fs from 'fs' import path from 'path' import SftpClient from 'ssh2-sftp-client' import NodeSsh from 'node-ssh' import normalizePath from 'normalize-path' import Model from '../../model' +type sftpConnectConfig = { + host: string; + port: number; + type?: string; + username: string; + password?: string; + privateKey?: string | Buffer; +} + export default class SftpDeploy extends Model { // connect: SftpClient constructor(appInstance: any) { @@ -22,11 +32,24 @@ export default class SftpDeploy extends Model { const client = new SftpClient() const { setting } = this.db - const connectConfig = { + + const connectConfig: sftpConnectConfig = { host: setting.server, port: Number(setting.port), username: setting.username, - password: setting.password, + } + + if (setting.privateKey) { + try { + connectConfig.privateKey = fse.readFileSync(setting.privateKey) + } catch (e) { + console.error('SFTP Test Remote Error: ', e.message) + result.success = false + result.message = e.message + return result + } + } else { + connectConfig.password = setting.password } const testFilename = 'gridea.txt' @@ -58,7 +81,7 @@ export default class SftpDeploy extends Model { } finally { await client.end() } - + return result } @@ -71,12 +94,19 @@ export default class SftpDeploy extends Model { const client = new NodeSsh() const { setting } = this.db - const connectConfig = { + + const connectConfig: sftpConnectConfig = { host: setting.server, port: Number(setting.port), - username: setting.username, - password: setting.password, type: 'sftp', + username: setting.username, + } + + // node-ssh: privateKey is path string. + if (setting.privateKey) { + connectConfig.privateKey = setting.privateKey + } else { + connectConfig.password = setting.password } const localPath = normalizePath(path.join(this.appDir, 'output')) diff --git a/src/store/modules/site.ts b/src/store/modules/site.ts index d82a0fdd..8a828450 100644 --- a/src/store/modules/site.ts +++ b/src/store/modules/site.ts @@ -59,6 +59,7 @@ const siteState: Site = { port: '22', server: '', password: '', + privateKey: '', remotePath: '', }, commentSetting: { diff --git a/src/views/setting/includes/BasicSetting.vue b/src/views/setting/includes/BasicSetting.vue index d4fce3f6..bfa6df1a 100644 --- a/src/views/setting/includes/BasicSetting.vue +++ b/src/views/setting/includes/BasicSetting.vue @@ -48,7 +48,10 @@ - + + + + @@ -99,6 +102,7 @@ export default class BasicSetting extends Vue { port: '22', server: '', password: '', + privateKey: '', remotePath: '', } @@ -110,13 +114,13 @@ export default class BasicSetting extends Vue { && form.branch && form.username && form.token - + const sftpPlatformValid = ['sftp'].includes(form.platform) && form.port && form.server && form.username - && form.password && form.remotePath + && (form.password || form.privateKey) return pagesPlatfomValid || sftpPlatformValid }