Skip to content

Commit

Permalink
导入远程host (youzan#10)
Browse files Browse the repository at this point in the history
* 4.0.5

* save readme

* improve host

* update version

* empty file

* empty file

* mobile doc

* fix not forward

* update version

* save merge change

* 4.0.10

* sync hosts

* fix traffic service init

* CHANGELOG
  • Loading branch information
mickeyinfoshan authored May 16, 2018
1 parent e41fb16 commit 4270bb5
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 79 deletions.
23 changes: 17 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Github Change Log

## [email protected].5 (2018-05-02)
## [email protected].10 (2018-05-16)

**Implemented enhancements:**

- 支持规则集的复制
- 支持Host文件的导出与导入
- 增加导入远程Host文件功能
- 优化mock数据交互

**Fixed bugs:**

- 修复导出中文名字的规则集的bug
- 修复windows平台下导入规则集的[bug](https://github.com/youzan/zan-proxy/issues/2)
- 删除缓存数据导致启动失败的 [issue] (https://github.com/youzan/zan-proxy/issues/9)

## [email protected] ~ 4.0.9 (2018-05-07)

Expand All @@ -24,4 +23,16 @@
**Fixed bugs:**

- 修复请求监控中请求内容被遮挡的问题
- 自更新兼容npm的[issue]https://github.com/npm/npm/issues/17444)
- 自更新兼容npm的[issue]https://github.com/npm/npm/issues/17444)

## [email protected] (2018-05-02)

**Implemented enhancements:**

- 支持规则集的复制
- 支持Host文件的导出与导入

**Fixed bugs:**

- 修复导出中文名字的规则集的bug
- 修复windows平台下导入规则集的[bug](https://github.com/youzan/zan-proxy/issues/2)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zan-proxy",
"version": "4.0.9",
"version": "4.0.10",
"description": "前端代理",
"scripts": {
"build": "tsc",
Expand Down
20 changes: 20 additions & 0 deletions src/App/manager/controller/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,25 @@ export class HostController {
);
ctx.body = content;
});

router.get('/host/import', async ctx => {
const { userId, query } = ctx;
const hostFileUrl = query.url;
try {
const hostFile = await this.hostService.importRemoteHostFile(
userId,
hostFileUrl,
);
ctx.body = {
code: 0,
data: hostFile,
};
} catch (e) {
ctx.body = {
code: 1,
msg: e,
};
}
});
}
}
33 changes: 31 additions & 2 deletions src/App/manager/controller/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import axios from 'axios';
import fs from 'fs';
import path from 'path';
import { Inject, Service } from 'typedi';
import { AppInfoService, RuleFile, RuleService } from '../../services';
import {
AppInfoService,
HostService,
RuleFile,
RuleService,
} from '../../services';

@Service()
export class UtilsController {
@Inject() private appInfoService: AppInfoService;
@Inject() private hostService: HostService;
@Inject() private ruleService: RuleService;

public regist(router) {
Expand Down Expand Up @@ -38,10 +44,33 @@ export class UtilsController {
`if (url.indexOf("${rule.match}") > -1) { return zProxy; }`,
);
matchScripts.push(
`if ((new RegExp("${rule.match}")).test(url)) { return zProxy; }`,
`try {
if ((new RegExp("${rule.match}")).test(url)) { return zProxy; }
} catch(e){}`,
);
}
}

const hostFileList = this.hostService.getHostFileList(userID);
for (const hostFile of hostFileList) {
const hf = this.hostService.getHostFile(userID, hostFile.name);
if (!hf) {
continue;
}
const hostFileContent = hf.content;
const hosts = Object.keys(hostFileContent);
for (const host of hosts) {
matchScripts.push(`if ( host == "${host}" ) { return zProxy; }`);
if (host.startsWith('*')) {
matchScripts.push(
`if ( host.indexOf("${host.substr(
1,
host.length,
)}") > -1 ) { return zProxy; } `,
);
}
}
}
}
const pac = `\n\
var direct = 'DIRECT;';\n\
Expand Down
27 changes: 26 additions & 1 deletion src/App/services/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import EventEmitter from 'events';
import fs from 'fs';
import jsonfile from 'jsonfile';
import { find, forEach } from 'lodash';
import fetch from 'node-fetch';
import path from 'path';
import { Service } from 'typedi';
import { AppInfoService } from './appInfo';
Expand Down Expand Up @@ -88,7 +89,7 @@ export class HostService extends EventEmitter {
name: string;
checked: boolean;
description: string;
meta: object;
meta: any;
}> = [];
forEach(this.userHostFilesMap[userId], content => {
fileList.push({
Expand Down Expand Up @@ -174,6 +175,9 @@ export class HostService extends EventEmitter {
}

public async saveHostFile(userId, name, content) {
if (!this.userHostFilesMap[userId]) {
this.userHostFilesMap[userId] = {};
}
this.userHostFilesMap[userId][name] = content;

// 如果正在使用,则删除
Expand All @@ -187,6 +191,27 @@ export class HostService extends EventEmitter {
this.emit('data-change', userId, this.getHostFileList(userId));
}

public async importRemoteHostFile(userId, url): Promise<any> {
const resp = await fetch(url);
const f = await resp.json();
f.meta = {
local: false,
url,
};
if (!f.content) {
f.content = {};
}
if (!f.name) {
f.name = url.split('/').slice(-1)[0] || url;
}
if (this.getHostFile(userId, f.name).checked) {
f.checked = true;
} else {
f.checked = false;
}
return await this.saveHostFile(userId, f.name, f);
}

private _getHostFilePath(userId, hostName) {
const fileName = `${userId}_${hostName}.json`;
const filePath = path.join(this.hostSaveDir, fileName);
Expand Down
9 changes: 6 additions & 3 deletions src/App/services/httpTraffic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ export class HttpTrafficService extends EventEmitter {
setInterval(() => {
this.sendCachedData();
}, 2000);

rimraf.sync(this.trafficDir);
fs.mkdirSync(this.trafficDir);
try {
rimraf.sync(this.trafficDir);
fs.mkdirSync(this.trafficDir);
} catch (e) {
console.error(e);
}
}

public getFilter(userId) {
Expand Down
2 changes: 2 additions & 0 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ip from 'ip';
import open from 'open';
import selfUpdate from './selfUpdate';
import start from './start';
import syncHost from './syncHost';
import syncRule from './syncRule';

const packageInfo = require('../../package');
Expand All @@ -26,6 +27,7 @@ async function run() {
}
if (program.sync) {
await syncRule();
await syncHost();
}
const managerPort = program.manager_port || 40001;
const url = `http://${ip.address()}:${managerPort}`;
Expand Down
27 changes: 27 additions & 0 deletions src/bin/syncHost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ora from 'ora';
import { AppInfoService, HostService } from './../App/services';

const syncRemoteHosts = async () => {
console.log('开始同步远程Host文件');
const appInfoService = new AppInfoService(false);
const hostService = new HostService(appInfoService);
const hostFileList = hostService.getHostFileList('root');
for (const hostFile of hostFileList) {
if (!hostFile.meta) {
continue;
}
if (!hostFile.meta.url || hostFile.meta.local) {
continue;
}
const spinner = ora(`同步远程Host${hostFile.name}`).start();
try {
await hostService.importRemoteHostFile('root', hostFile.meta.url);
spinner.succeed();
} catch (e) {
spinner.fail();
}
}
console.log('同步远程Host文件结束');
};

export default syncRemoteHosts;
5 changes: 4 additions & 1 deletion webui/src/api/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ var api = {
},
saveFile(name,content){
return axios.post(`/host/savefile?name=${name}`,content);
}
},
importRemote(url) {
return axios.get(`/host/import?url=${encodeURIComponent(url)}`)
},
};
api.debouncedUseFile = _.debounce(function (name, callback) {
api.useFile(name).then((response) => {
Expand Down
Loading

0 comments on commit 4270bb5

Please sign in to comment.