Skip to content

Commit d9dded0

Browse files
authored
Merge pull request #396 from RookieZoe/private-key-support
add private key support for sftp deploy
2 parents 61b19c4 + ee4c601 commit d9dded0

6 files changed

Lines changed: 52 additions & 9 deletions

File tree

src/assets/locales.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ const message = {
7373
change: '更 换',
7474
editorTip: '你可以插入单独行的 <!-- more --> 为摘要分隔标识(此行之前内容为摘要)',
7575
saveError: '保存失败',
76+
privateKeyTip: '请填写绝对路径,例如:/home/username/.ssh/id_rsa',
77+
remotePathTip: '请填写绝对路径,例如:/home/username/www/',
7678
testConnection: '检测远程连接',
7779
connectSuccess: '远程连接成功',
7880
connectFailed: '远程连接失败,请检查仓库、用户名和 Token 设置',
@@ -182,6 +184,8 @@ const message = {
182184
change: '更 換',
183185
editorTip: '你可以插入單獨行的 <!-- more --> 為摘要分隔標識(此行之前內容為摘要)',
184186
saveError: '保存失敗',
187+
privateKeyTip: '請填寫絕對路徑,例如:/home/username/.ssh/id_rsa',
188+
remotePathTip: '請填寫絕對路徑,例如:/home/username/www/',
185189
testConnection: '檢測遠程連接',
186190
connectSuccess: '遠程連接成功',
187191
connectFailed: '遠程連接失敗,請檢查倉庫、用戶名和 Token 設置',
@@ -290,6 +294,8 @@ const message = {
290294
change: 'Change',
291295
editorTip: 'You can insert a separate line <!-- more --> is the abstract separator identifier ( the content before this line is the abstract)',
292296
saveError: 'Save failed',
297+
privateKeyTip: 'Please fill in the absolute path, for example: /home/username/.ssh/id_rsa',
298+
remotePathTip: 'Please fill in the absolute path, for example::/home/username/www/',
293299
testConnection: 'Test Connection',
294300
connectSuccess: 'Remote connection succeeded',
295301
connectFailed: 'Remote connection failed, please check repository, username and token settings',

src/interfaces/setting.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ISetting {
1010
port: string
1111
server: string
1212
password: string
13+
privateKey: string
1314
remotePath: string
1415
[index: string]: string
1516
}

src/server/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default class App {
7575
port: '22',
7676
server: '',
7777
password: '',
78+
privateKey: '',
7879
remotePath: '',
7980
},
8081
commentSetting: {

src/server/plugins/deploys/sftp.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import * as fse from 'fs-extra'
2+
// import * as fs from 'fs'
23
import path from 'path'
34
import SftpClient from 'ssh2-sftp-client'
45
import NodeSsh from 'node-ssh'
56
import normalizePath from 'normalize-path'
67
import Model from '../../model'
78

9+
type sftpConnectConfig = {
10+
host: string;
11+
port: number;
12+
type?: string;
13+
username: string;
14+
password?: string;
15+
privateKey?: string | Buffer;
16+
}
17+
818
export default class SftpDeploy extends Model {
919
// connect: SftpClient
1020
constructor(appInstance: any) {
@@ -22,11 +32,24 @@ export default class SftpDeploy extends Model {
2232
const client = new SftpClient()
2333

2434
const { setting } = this.db
25-
const connectConfig = {
35+
36+
const connectConfig: sftpConnectConfig = {
2637
host: setting.server,
2738
port: Number(setting.port),
2839
username: setting.username,
29-
password: setting.password,
40+
}
41+
42+
if (setting.privateKey) {
43+
try {
44+
connectConfig.privateKey = fse.readFileSync(setting.privateKey)
45+
} catch (e) {
46+
console.error('SFTP Test Remote Error: ', e.message)
47+
result.success = false
48+
result.message = e.message
49+
return result
50+
}
51+
} else {
52+
connectConfig.password = setting.password
3053
}
3154

3255
const testFilename = 'gridea.txt'
@@ -58,7 +81,7 @@ export default class SftpDeploy extends Model {
5881
} finally {
5982
await client.end()
6083
}
61-
84+
6285
return result
6386
}
6487

@@ -71,12 +94,19 @@ export default class SftpDeploy extends Model {
7194
const client = new NodeSsh()
7295

7396
const { setting } = this.db
74-
const connectConfig = {
97+
98+
const connectConfig: sftpConnectConfig = {
7599
host: setting.server,
76100
port: Number(setting.port),
77-
username: setting.username,
78-
password: setting.password,
79101
type: 'sftp',
102+
username: setting.username,
103+
}
104+
105+
// node-ssh: privateKey is path string.
106+
if (setting.privateKey) {
107+
connectConfig.privateKey = setting.privateKey
108+
} else {
109+
connectConfig.password = setting.password
80110
}
81111

82112
const localPath = normalizePath(path.join(this.appDir, 'output'))

src/store/modules/site.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const siteState: Site = {
5959
port: '22',
6060
server: '',
6161
password: '',
62+
privateKey: '',
6263
remotePath: '',
6364
},
6465
commentSetting: {

src/views/setting/includes/BasicSetting.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
<a-icon class="icon" slot="addonAfter" :type="passVisible ? 'eye-invisible' : 'eye'" @click="passVisible = !passVisible" />
4949
</a-input>
5050
</a-form-item>
51-
<a-form-item label="Remote Path" :labelCol="formLayout.label" :wrapperCol="formLayout.wrapper" :colon="false" help="请填写绝对路径,例如:/home/username/www/">
51+
<a-form-item label="Private Key" :labelCol="formLayout.label" :wrapperCol="formLayout.wrapper" :colon="false" :help="$t('privateKeyTip')">
52+
<a-input v-model="form.privateKey" />
53+
</a-form-item>
54+
<a-form-item label="Remote Path" :labelCol="formLayout.label" :wrapperCol="formLayout.wrapper" :colon="false" :help="$t('remotePathTip')">
5255
<a-input v-model="form.remotePath" />
5356
</a-form-item>
5457
</template>
@@ -99,6 +102,7 @@ export default class BasicSetting extends Vue {
99102
port: '22',
100103
server: '',
101104
password: '',
105+
privateKey: '',
102106
remotePath: '',
103107
}
104108
@@ -110,13 +114,13 @@ export default class BasicSetting extends Vue {
110114
&& form.branch
111115
&& form.username
112116
&& form.token
113-
117+
114118
const sftpPlatformValid = ['sftp'].includes(form.platform)
115119
&& form.port
116120
&& form.server
117121
&& form.username
118-
&& form.password
119122
&& form.remotePath
123+
&& (form.password || form.privateKey)
120124
121125
return pagesPlatfomValid || sftpPlatformValid
122126
}

0 commit comments

Comments
 (0)