Skip to content

Commit

Permalink
4.0.14 (#34)
Browse files Browse the repository at this point in the history
**Implemented enhancements:**

- [支持Host多选](#24)
- [支持转发规则集信息编辑](#30)
- [使用fs.realpathSync判断dataFileRealPath是否包含mockDataDir](#25)
- 优化构建命令
- 增加更新提示

**Fixed bugs:**

- [修复@types/node版本过高导致构建失败的问题](#27)
- [删除特殊字符的规则时报错](#28)
- 修复监控器请求数据展示异常
- 兼容没有action的老数据
  • Loading branch information
mickeyinfoshan committed Jul 5, 2018
1 parent 33741ab commit 8c6ef9b
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 120 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Github Change Log

## zan-proxy@4.0.14 (2018-07-05)

**Implemented enhancements:**

- [支持Host多选](https://github.com/youzan/zan-proxy/issues/24)
- [支持转发规则集信息编辑](https://github.com/youzan/zan-proxy/pull/30)
- [使用fs.realpathSync判断dataFileRealPath是否包含mockDataDir](https://github.com/youzan/zan-proxy/pull/25)
- 优化构建命令
- 增加更新提示

**Fixed bugs:**

- [修复@types/node版本过高导致构建失败的问题](https://github.com/youzan/zan-proxy/issues/27)
- [删除特殊字符的规则时报错](https://github.com/youzan/zan-proxy/pull/28)
- 修复监控器请求数据展示异常
- 兼容没有action的老数据

## zan-proxy@4.0.11 ~ 4.0.13 (2018-06-12)

**Implemented enhancements:**
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "zan-proxy",
"version": "4.0.13",
"version": "4.0.14",
"description": "前端代理",
"scripts": {
"build": "tsc",
"build": "tsc && cd ./webui && npm run build && cd ../",
"test": "HOME=/tmp/proxy-test nyc mocha -r ts-node/register -c --recursive 'test/**/**/*.spec.ts'",
"watch": "tsc --watch",
"postinstall": "node ./scripts/resetDataFile.js",
Expand Down Expand Up @@ -50,7 +50,7 @@
"@types/chai": "^4.1.2",
"@types/lru-cache": "^4.1.0",
"@types/mocha": "^5.0.0",
"@types/node": "^9.4.7",
"@types/node": "<=9.6.13",
"chai": "^4.1.2",
"chai-events": "^0.0.1",
"mocha": "^5.1.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/resetDataFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function convertRuleFile(oldRuleFile) {
newRuleFile.content = newRuleFile
.content
.filter((rule) => {
return rule.action.type === "redirect"; // 其他action暂时不支持
return rule.action && rule.action.type === "redirect"; // 其他action暂时不支持
})
.map(convertRule);
return newRuleFile;
Expand Down
7 changes: 4 additions & 3 deletions src/App/manager/controller/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export class HostController {
code: 0,
};
});
// /host/usefile?name=${name}
router.get('/host/usefile', async ctx => {
// /host/togglefile?name=${name}
router.get('/host/togglefile', async ctx => {
const userId = ctx.userId;
await this.hostService.setUseHost(userId, ctx.query.name);
const { name } = ctx.query;
await this.hostService.toggleUseHost(userId, name);
ctx.body = {
code: 0,
};
Expand Down
56 changes: 43 additions & 13 deletions src/App/manager/controller/rule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Router from 'koa-router';
import { Inject, Service } from 'typedi';
import { ProfileService, RuleService } from '../../services';
import { ProfileService, RuleService, ErrNameExists } from '../../services';
/**
* Created by tsxuehu on 4/11/17.
*/
Expand All @@ -20,15 +20,27 @@ export class RuleController {
});
ruleRouter.post('/create', async ctx => {
const userId = ctx.userId;
const result = await this.ruleService.createRuleFile(
userId,
ctx.request.body.name,
ctx.request.body.description,
);
ctx.body = {
code: result ? 0 : 1,
msg: result ? '' : '文件已存在',
};
try {
const result = await this.ruleService.createRuleFile(
userId,
ctx.request.body.name,
ctx.request.body.description,
);
ctx.body = {
code: 0,
msg: result,
};
return;
} catch (error) {
const msg =
error === ErrNameExists
? '文件已存在'
: `未知错误: ${error.toString()}`;
ctx.body = {
code: 1,
msg,
};
}
});
// 获取规则文件列表
// /rule/filelist
Expand Down Expand Up @@ -99,9 +111,27 @@ export class RuleController {
});

// 重命名规则文件
// /rule/changefilename?newName=${name}, body -> RuleFile
ruleRouter.post('/changefilename', async ctx => {
ctx.body = await this.ruleService.changeRuleFileName(ctx.userId, ctx.request.body, ctx.query.newName, ctx.query.newDescription);
// /rule/changefilename/:origin, body -> { name, description }
ruleRouter.post('/updatefileinfo/:origin', async ctx => {
const { userId, params, request } = ctx;
const { origin } = params;
const { name, description } = request.body;
try {
await this.ruleService.updateFileInfo(userId, origin, {
description,
name,
});
ctx.body = {
code: 0,
};
} catch (e) {
const msg =
e === ErrNameExists ? '有重复名字' : `未知错误: ${e.toString()}`;
ctx.body = {
code: 1,
msg,
};
}
});

// 导出规则文件
Expand Down
25 changes: 10 additions & 15 deletions src/App/services/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ export class HostService extends EventEmitter {
let ip;
const inUsingHosts = this.getInUsingHosts(userId);
ip = inUsingHosts.hostMap[hostname];
if (ip) {
return ip;
}
// 配置 *开头的host 计算属性globHostMap已经将*去除
ip = find(inUsingHosts.globHostMap, (_, host) => {
return hostname.endsWith(host);
Expand Down Expand Up @@ -149,14 +146,11 @@ export class HostService extends EventEmitter {
this.emit('host-deleted', userId, name);
}

public async setUseHost(userId, filename) {
public async toggleUseHost(userId, filename) {
const toSaveFileName: string[] = [];
forEach(this.userHostFilesMap[userId], (content, name) => {
if (content.name === filename && content.checked !== true) {
content.checked = true;
toSaveFileName.push(name);
} else if (content.name !== filename && content.checked !== false) {
content.checked = false;
if (content.name === filename) {
content.checked = !content.checked;
toSaveFileName.push(name);
}
});
Expand Down Expand Up @@ -239,18 +233,19 @@ export class HostService extends EventEmitter {
// 读文件加载host
const hostMap = {};
const globHostMap = {};
const findedUsingHost = find(this.userHostFilesMap[userId], content => {
return content.checked;
});
if (findedUsingHost) {
forEach(findedUsingHost.content, (ip, host) => {
Object.keys(this.userHostFilesMap[userId]).forEach(name => {
const file = this.userHostFilesMap[userId][name];
if (!file.checked) {
return;
}
forEach(file.content, (ip, host) => {
if (host.startsWith('*')) {
globHostMap[host.substr(1, host.length)] = ip;
} else {
hostMap[host] = ip;
}
});
}
});
hosts = {
globHostMap,
hostMap,
Expand Down
9 changes: 5 additions & 4 deletions src/App/services/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ export class MockDataService extends EventEmitter {
* @private
*/
private _getDataFilePath(userId, dataId) {
const dataFileRealPath = fs.realpathSync(path.join(this.mockDataDir, userId + '_' + dataId));
if (dataFileRealPath.includes(this.mockDataDir)){
return path.join(this.mockDataDir, userId + '_' + dataId);
}else {
const p = path.join(this.mockDataDir, userId + '_' + dataId);
const dataFileRealPath = fs.realpathSync(p);
if (dataFileRealPath.includes(this.mockDataDir)) {
return p;
} else {
return '';
}
}
Expand Down
52 changes: 21 additions & 31 deletions src/App/services/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppInfoService } from './appInfo';

const jsonfileWriteFile = promisify(jsonfile.writeFile);
const fsUnlink = promisify(fs.unlink);
export const ErrNameExists = new Error('name exists');

export interface Rule {
name: string;
Expand Down Expand Up @@ -84,7 +85,7 @@ export class RuleService extends EventEmitter {
// 创建规则文件
public async createRuleFile(userId, name, description) {
if (this.rules[userId] && this.rules[userId][name]) {
return false;
return ErrNameExists;
}
const ruleFile = {
checked: false,
Expand Down Expand Up @@ -202,42 +203,31 @@ export class RuleService extends EventEmitter {
}

// 修改规则文件名称
public async changeRuleFileName(userId, ruleFile: RuleFile, newName: string, newDescription: string) {
const oldName = ruleFile.name;
public async updateFileInfo(
userId,
originName: string,
{
name,
description,
}: {
name: string;
description: string;
},
) {
const userRuleMap = this.rules[userId] || {};
if (userRuleMap[newName]) {
return {
code: -1,
msg: `名称为"${newName}"的规则集已存在!`,
};
if (userRuleMap[name]) {
throw ErrNameExists;
}

const ruleFile = userRuleMap[originName];
// 删除旧的rule
delete this.rules[userId][oldName];
const ruleFilePath = this._getRuleFilePath(userId, oldName);
delete this.rules[userId][originName];
const ruleFilePath = this._getRuleFilePath(userId, originName);
await fsUnlink(ruleFilePath);

// 修改rule名称
ruleFile.name = newName;
ruleFile.description = newDescription;
userRuleMap[newName] = ruleFile;
this.rules[userId] = userRuleMap;

// 写文件
const filePath = this._getRuleFilePath(userId, newName);
await jsonfileWriteFile(filePath, userRuleMap[newName], {
encoding: 'utf-8',
});

// 清空缓存
delete this.usingRuleCache[userId];

// 发送消息通知
this.emit('data-change', userId, this.getRuleFileList(userId));

return {
code: 0,
};
ruleFile.name = name;
ruleFile.description = description;
await this.saveRuleFile(userId, ruleFile);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/bin/selfUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export default async () => {
.then(() => exec(`npm install --global --silent ${packageInfo.name}`))
.then(() => {
updateSpinner.stop();
console.log('更新完成,请重新启动!');
console.log(
`更新完成,请重新启动! 如出现命令丢失情况,请手动重新安装:npm install -g ${
packageInfo.name
}`,
);
process.exit(0);
})
.catch(error => {
Expand Down
2 changes: 1 addition & 1 deletion test/App/services/host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("HostService", () => {
},
name: "test2",
});
await hostService.setUseHost("root", "test2");
await hostService.toggleUseHost("root", "test2");
const address = await hostService.resolveHost("root", "test.youzan.com");
address.should.equal("192.168.1.1");
await hostService.deleteHostFile("root", "test2");
Expand Down
1 change: 0 additions & 1 deletion webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"is-json": "^2.0.1",
"jquery": "^3.1.1",
"lodash": "^4.17.4",
"prettysize": "^1.1.0",
"prettytime": "^1.0.0",
"qrcode-js": "0.0.2",
"query-string": "^4.3.4",
Expand Down
6 changes: 3 additions & 3 deletions webui/src/api/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var api = {
deleteFile(name){
return axios.get(`/host/deletefile?name=${name}`);
},
useFile(name){
return axios.get(`/host/usefile?name=${name}`);
toggleFile(name){
return axios.get(`/host/togglefile?name=${name}`);
},
getFileContent(name){
return axios.get(`/host/getfile?name=${name}`);
Expand All @@ -37,7 +37,7 @@ var api = {
},
};
api.debouncedUseFile = _.debounce(function (name, callback) {
api.useFile(name).then((response) => {
api.toggleFile(name).then((response) => {
callback(response)
});
}, 500);
Expand Down
10 changes: 5 additions & 5 deletions webui/src/api/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ var api = {

/**
* 修改规则集文件名
* @param {String} newName 新名称
* @param {String} newDescription 新描述
* @param {Object} content 规则集内容
* @param {String} origin 原名称
* @param {String} name 新名称
* @param {String} description 新描述
*/
changeFileName(newName, newDescription, content) {
return axios.post(`/rule/changefilename?newName=${encodeURIComponent(newName)}&newDescription=${newDescription}`, content);
updateFileInfo(origin, { name, description }) {
return axios.post(`/rule/updatefileinfo/${encodeURIComponent(origin)}`, { name, description });
},

testRule(content){
Expand Down
Loading

0 comments on commit 8c6ef9b

Please sign in to comment.