Skip to content

Commit

Permalink
Merge pull request #978 from wherelse/GitProxy
Browse files Browse the repository at this point in the history
add http proxy for git
  • Loading branch information
EryouHao authored May 10, 2022
2 parents de67f7e + 851ed4c commit 2e3584d
Show file tree
Hide file tree
Showing 11 changed files with 383 additions and 247 deletions.
46 changes: 28 additions & 18 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ module.exports = {
env: {
node: true,
},
extends: [
'plugin:vue/essential',
'@vue/airbnb',
'@vue/typescript',
],
extends: ['plugin:vue/essential', '@vue/airbnb', '@vue/typescript'],
rules: {
// js 和 ts 不需要检查 import 的文件后缀
'import/extensions': ['error', 'always', {
'js': 'never',
'ts': 'never',
}],
'no-restricted-syntax': ['error', 'WithStatement', 'BinaryExpression[operator=\'in\']'],
'import/extensions': [
'error',
'always',
{
js: 'never',
ts: 'never',
},
],
'no-restricted-syntax': [
'error',
'WithStatement',
'BinaryExpression[operator=\'in\']',
],
// 可以 debugger
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
// 不要分号
'semi': [2, 'never'],
semi: [2, 'never'],
// 全部单引号
'quotes': [2, 'single'],
quotes: [2, 'single'],
// 对象缩写
'object-shorthand': 0,
// 可以使用 console
Expand All @@ -38,14 +42,20 @@ module.exports = {
// 不禁止箭头函数直接return对象
'arrow-body-style': 0,
// 允许空行
'no-trailing-spaces': ['error', { 'skipBlankLines': true }],
'no-trailing-spaces': ['error', { skipBlankLines: true }],
// 允许short circuit evaluations
'no-unused-expressions': ['error', { 'allowShortCircuit': true, 'allowTernary': true }],
'no-unused-expressions': [
'error',
{ allowShortCircuit: true, allowTernary: true },
],
// 最长字符
'max-len': ['error', { 'code': 1500 }],
'vue/no-parsing-error': [2, {
'invalid-first-character-of-tag-name': false,
}],
'max-len': ['error', { code: 1500 }],
'vue/no-parsing-error': [
2,
{
'invalid-first-character-of-tag-name': false,
},
],
// no-plusplus
'no-plusplus': 0,
'class-methods-use-this': 0,
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
"feed": "^2.0.4",
"fs-extra": "^7.0.1",
"gray-matter": "^4.0.1",
"isomorphic-git": "^0.78.3",
"hpagent": "^1.0.0",
"isomorphic-git": "^1.17.1",
"junk": "^3.1.0",
"less": "^3.9.0",
"lowdb": "^1.0.0",
"macaddress": "^0.2.9",
"markdown-it": "^8.4.2",
"markdown-it": "^13.0.1",
"markdown-it-abbr": "^1.0.4",
"markdown-it-emoji": "^1.4.0",
"markdown-it-footnote": "^3.0.1",
"markdown-it-image-lazy-loading": "^1.0.2",
"markdown-it-emoji": "^2.0.2",
"markdown-it-footnote": "^3.0.3",
"markdown-it-image-lazy-loading": "^1.2.0",
"markdown-it-implicit-figures": "^0.9.0",
"markdown-it-imsize": "^2.0.1",
"markdown-it-mark": "^2.0.0",
Expand All @@ -55,6 +56,7 @@
"normalize-path": "^3.0.0",
"prismjs": "^1.16.0",
"shortid": "^2.2.14",
"simple-get": "^4.0.1",
"slug": "^0.9.3",
"ssh2-sftp-client": "^4.2.4",
"striptags": "^3.1.1",
Expand Down Expand Up @@ -98,7 +100,7 @@
"@vue/eslint-config-typescript": "^4.0.0",
"babel-eslint": "^10.0.1",
"babel-plugin-prismjs": "^1.0.2",
"electron": "^7.2.4",
"electron": "^7.3.3",
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.2.2",
"husky": "^1.3.1",
Expand Down
15 changes: 15 additions & 0 deletions src/assets/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ const message = {
showFullText: '显示全文',
showAbstractOnly: '仅显示摘要',
numberArticlesRSS: 'RSS/Feed 文章数量',
Proxy: 'HTTP代理',
ProxyAddress: '地址',
ProxyPort: '端口',
},
zh_TW: {
preview: '預 覽',
Expand Down Expand Up @@ -249,6 +252,9 @@ const message = {
showFullText: '显示全文',
showAbstractOnly: '仅显示摘要',
numberArticlesRSS: 'RSS/Feed 文章数量',
Proxy: 'HTTP代理',
ProxyAddress: '地址',
ProxyPort: '端口',
},
en: {
preview: 'Preview',
Expand Down Expand Up @@ -374,6 +380,9 @@ const message = {
showFullText: 'Show full text',
showAbstractOnly: 'Show abstract only',
numberArticlesRSS: 'Number articles RSS/Feed',
Proxy: 'HTTP Proxy',
ProxyAddress: 'Proxy Address',
ProxyPort: 'Proxy Port',
},
fr_FR: {
preview: 'Aperçu',
Expand Down Expand Up @@ -499,6 +508,9 @@ const message = {
showFullText: 'Tout afficher',
showAbstractOnly: 'Afficher seulement le résumé',
numberArticlesRSS: 'Nombre d\'articles RSS/Feed',
Proxy: 'HTTP Proxy',
ProxyAddress: 'Proxy Address',
ProxyPort: 'Proxy Port',
},
ja_JP: {
preview: 'プレビュー',
Expand Down Expand Up @@ -624,6 +636,9 @@ const message = {
showFullText: '全文を表示します',
showAbstractOnly: '摘要のみを表示します',
numberArticlesRSS: 'RSS/Feed 文書数量',
Proxy: 'HTTPプロキシ',
ProxyAddress: 'プロキシアドレス',
ProxyPort: 'プロキシポート',
},
}

Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface ISetting {
password: string
privateKey: string
remotePath: string
proxyPath: string
proxyPort: string
enabledProxy: 'direct' | 'proxy'
[index: string]: string
}

Expand Down
3 changes: 3 additions & 0 deletions src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export default class App {
password: '',
privateKey: '',
remotePath: '',
proxyPath: '',
proxyPort: '',
enabledProxy: 'direct',
},
commentSetting: {
showComment: false,
Expand Down
27 changes: 16 additions & 11 deletions src/server/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs'
import moment from 'moment'
// @ts-ignore
import * as git from 'isomorphic-git/dist/for-node/isomorphic-git'
import Model from './model'
import GitProxy from './plugins/deploys/gitproxy'

const git = require('isomorphic-git')

export default class Deploy extends Model {
outputDir: string = `${this.appDir}/output`
Expand All @@ -11,9 +13,10 @@ export default class Deploy extends Model {

platformAddress = ''

http = new GitProxy(this)

constructor(appInstance: any) {
super(appInstance)

const { setting } = this.db
this.platformAddress = ({
github: 'github.com',
Expand All @@ -33,7 +36,7 @@ export default class Deploy extends Model {
async remoteDetect() {
const result = {
success: true,
message: '',
message: [''],
}
try {
const { setting } = this.db
Expand All @@ -53,13 +56,13 @@ export default class Deploy extends Model {
}
if (!isRepo) {
await git.init({ fs, dir: this.outputDir })
await git.config({
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.name',
value: setting.username,
})
await git.config({
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.email',
Expand All @@ -71,13 +74,13 @@ export default class Deploy extends Model {
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
})
const info = await git.getRemoteInfo({
core: 'default',
http: this.http,
url: this.remoteUrl,
})
console.log('info', info)
result.message = info
result.message = info.capabilities
} catch (e) {
console.log('Test Remote Error: ', e.message)
console.log('Test Remote Error: ', e)
result.success = false
result.message = e.message
}
Expand Down Expand Up @@ -113,13 +116,13 @@ export default class Deploy extends Model {

try {
await git.init({ fs, dir: this.outputDir })
await git.config({
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.name',
value: setting.username,
})
await git.config({
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.email',
Expand All @@ -138,6 +141,7 @@ export default class Deploy extends Model {
await this.checkCurrentBranch()
const pushRes = await git.push({
fs,
http: this.http,
dir: this.outputDir,
remote: 'origin',
ref: setting.branch,
Expand Down Expand Up @@ -184,6 +188,7 @@ export default class Deploy extends Model {

const pushRes = await git.push({
fs,
http: this.http,
dir: this.outputDir,
remote: 'origin',
ref: setting.branch,
Expand Down Expand Up @@ -221,7 +226,7 @@ export default class Deploy extends Model {
await git.branch({ fs, dir: this.outputDir, ref: setting.branch })
}

await git.fastCheckout({ fs, dir: this.outputDir, ref: setting.branch })
await git.checkout({ fs, dir: this.outputDir, ref: setting.branch })
}
}
}
85 changes: 85 additions & 0 deletions src/server/plugins/deploys/gitproxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { IApplicationDb } from '../../interfaces/application'

const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent')
const get = require('simple-get')

export default class GitProxy {
db: IApplicationDb

constructor(appInstance: any) {
this.db = appInstance.db
// console.log('instance git proxy',this.db.setting)
}

public async request({
url,
method,
headers,
body,
}: {
url: any;
method: any;
headers: any;
body: any;
}) {
const { setting } = this.db
body = await this.mergeBuffers(body)
const proxy = url.startsWith('https:')
? { Agent: HttpsProxyAgent }
: { Agent: HttpProxyAgent }
const agent = setting.enabledProxy === 'proxy'
? new proxy.Agent({
proxy: `http://${setting.proxyPath}:${setting.proxyPort}`,
})
: undefined
// const agent = new proxy.Agent({ proxy: 'http://127.0.0.1:1081' })
return new Promise((resolve, reject) => get(
{
url,
method,
agent,
headers,
body,
},
(err: any, res: any) => (err ? reject(err) : resolve(this.transformResponse(res))),
))
}

private async mergeBuffers(data: any[] | Uint8Array) {
if (!Array.isArray(data)) return data
if (data.length === 1 && data[0] instanceof Buffer) return data[0]
const buffers = []
let offset = 0
let size = 0
for await (const chunk of data) {
buffers.push(chunk)
size += chunk.byteLength
}
data = new Uint8Array(size)
for (const buffer of buffers) {
data.set(buffer, offset)
offset += buffer.byteLength
}
return Buffer.from(data.buffer)
}

private async transformResponse(res: {
url: any;
method: any;
statusCode: any;
statusMessage: any;
headers: any;
}) {
const {
url, method, statusCode, statusMessage, headers,
} = res
return {
url,
method,
statusCode,
statusMessage,
headers,
body: res,
}
}
}
2 changes: 1 addition & 1 deletion src/server/plugins/deploys/sftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class SftpDeploy extends Model {
await client.mkdir(remotePath)
const res = await client.putDirectory(localPath, remotePath, {
recursive: true,
concurrency: 10,
concurrency: 1, // 解决同步丢失js、css、图片文件问题
validate: function (itemPath: string) {
const baseName = path.basename(itemPath)
return baseName.substr(0, 1) !== '.' // do not allow dot files
Expand Down
3 changes: 3 additions & 0 deletions src/store/modules/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const siteState: Site = {
password: '',
privateKey: '',
remotePath: '',
proxyPath: '',
proxyPort: '',
enabledProxy: 'direct',
},
commentSetting: {
showComment: false,
Expand Down
Loading

0 comments on commit 2e3584d

Please sign in to comment.