Commit 4b765483 authored by Antoor's avatar Antoor Committed by GitHub

Merge pull request #59 from antoor/v2.0-beta-advanced-configure

v2.0-beta Advanced configure
parents 464c23d6 e87446fc
......@@ -9,12 +9,10 @@
const fs = require('fs'),
path = require('path'),
CONF = require('./config'),
// Logger = require('./logger'),
// logger = null,
// logger = require('log4js').getLogger('Cache'),
Datastore = require('nedb');
let logger
var logger;
class Cache {
/**
......
/**
* Shell数据库管理模块
* 更新:2016/04/28
* 作者:蚁逅 <https://github.com/antoor>
* 更新:2016/06/28
*/
'use strict';
......@@ -10,13 +9,10 @@ const fs = require('fs'),
dns = require('dns'),
path = require('path'),
CONF = require('./config'),
// Logger = require('./logger'),
// logger = null,
// logger = require('log4js').getLogger('Database'),
Datastore = require('nedb'),
qqwry = require("geoips").info();
let logger;
var logger;
class Database {
......@@ -80,6 +76,36 @@ class Database {
});
}
/**
* 根据URL解析出IP&&地理位置
* @param {String} url URL地址
* @return {Promise} ip, addr
*/
_url2ip(url) {
return new Promise((res, rej) => {
// 解析domain
const urlArr = url.match(/(\w+):\/\/([\w\.\-]+)[:]?([\d]*)([\s\S]*)/i);
// 无效url
if (!urlArr || urlArr.length < 3) {
return rej('Unable to resolve domain name from URL');
}
// 获取IP
const domain = urlArr[2];
dns.lookup(domain, (err, ip) => {
if (err) {
return rej(err.toString());
}
// 获取地理位置
const _addr = qqwry.searchIP(ip);
return res({
ip: ip,
addr: `${_addr.Country} ${_addr.Area}`
});
})
})
}
/**
* 添加shell数据
* @param {Object} event ipcMain对象
......@@ -87,68 +113,67 @@ class Database {
*/
addShell(event, opts) {
logger.info('addShell', opts);
// 获取目标IP以及地理位置
// 1. 获取域名
let parse = opts['url'].match(/(\w+):\/\/([\w\.\-]+)[:]?([\d]*)([\s\S]*)/i);
if (!parse || parse.length < 3) { return event.returnValue = 'Unable to resolve domain name from URL' };
// 2. 获取域名IP
dns.lookup(parse[2], (err, ip) => {
if (err) { return event.returnValue = err.toString() };
// 3. 查询IP对应物理位置
const addr = qqwry.searchIP(ip);
// 插入数据库
this.cursor.insert({
category: opts['category'] || 'default',
url: opts['url'],
pwd: opts['pwd'],
type: opts['type'],
ip: ip,
addr: `${addr.Country} ${addr.Area}`,
encode: opts['encode'],
encoder: opts['encoder'],
ctime: +new Date,
utime: +new Date
}, (err, ret) => {
event.returnValue = err || ret;
});
});
this._url2ip(opts.base['url'])
.then((ret) => {
this.cursor.insert({
category: opts.base['category'] || 'default',
url: opts.base['url'],
pwd: opts.base['pwd'],
type: opts.base['type'],
ip: ret['ip'],
addr: ret['addr'],
encode: opts.base['encode'],
encoder: opts.base['encoder'],
httpConf: opts.http,
otherConf: opts.other,
ctime: +new Date,
utime: +new Date
}, (_err, _ret) => {
event.returnValue = _err || _ret;
});
})
.catch((_err) => {
event.returnValue = _err;
})
}
/**
* 编辑shell数据
* @param {Object} event ipcMain对象
* @param {Object} opts 数据(url,_id,pwd,type,encode,encoder
* @param {Object} opts 数据(old,new
* @return {[type]} [description]
*/
editShell(event, opts) {
logger.warn('editShell', opts);
// 获取目标IP以及地理位置
// 1. 获取域名
let parse = opts['url'].match(/(\w+):\/\/([\w\.\-]+)[:]?([\d]*)([\s\S]*)/i);
if (!parse || parse.length < 3) { return event.returnValue = 'Unable to resolve domain name from URL' };
// 2. 获取域名IP
dns.lookup(parse[2], (err, ip) => {
if (err) { return event.returnValue = err.toString() };
// 3. 查询IP对应物理位置
const addr = qqwry.searchIP(ip);
// 更新数据库
this.cursor.update({
_id: opts['_id']
}, {
$set: {
ip: ip,
addr: `${addr.Country} ${addr.Area}`,
url: opts['url'],
pwd: opts['pwd'],
type: opts['type'],
encode: opts['encode'],
encoder: opts['encoder'],
utime: +new Date
}
}, (err, num) => {
event.returnValue = err || num;
const _new = opts.new;
const _old = opts.old;
this._url2ip(_new.base['url'])
.then((ret) => {
this.cursor.update({
_id: _old['_id']
}, {
$set: {
ip: ret['ip'],
addr: ret['addr'],
url: _new.base['url'],
pwd: _new.base['pwd'],
type: _new.base['type'],
encode: _new.base['encode'],
encoder: _new.base['encoder'],
httpConf: _new.http,
otherConf: _new.other,
utime: +new Date
}
}, (_err, _ret) => {
event.returnValue = _err || _ret;
})
})
});
.catch((_err) => {
event.returnValue = _err;
});
}
/**
......
......@@ -68,33 +68,6 @@ class Menubar {
click: this.app.quit.bind(this.app)
},
]
}, {
// 数据管理
label: LANG['shell']['title'],
submenu: [
{
label: LANG['shell']['add'],
accelerator: 'Shift+A',
click: event.sender.send.bind(event.sender, 'menubar', 'shell-add')
}, {
label: LANG['shell']['search'],
accelerator: 'Shift+S',
enabled: false
}, {
type: 'separator'
}, {
label: LANG['shell']['import'],
enabled: false
}, {
label: LANG['shell']['dump'],
enabled: false
}, {
type: 'separator'
}, {
label: LANG['shell']['clear'],
enabled: false
}
]
}, {
// 编辑
label: LANG['edit']['title'],
......
......@@ -19,14 +19,6 @@ module.exports = {
update: 'Check update',
quit: 'Quit'
},
shell: {
title: 'Data',
add: 'Add data',
search: 'Search data',
dump: 'Dump data',
import: 'Import data',
clear: 'Clear all data'
},
edit: {
title: 'Edit',
undo: 'Undo',
......@@ -154,6 +146,15 @@ module.exports = {
confirm: 'Are you sure to clear all the cache?',
success: 'Clear all cache success!',
error: (err) => antSword.noxss(`Clear all cache failed!\n${err}`)
},
accordion: {
base: 'Base',
http: 'HTTP',
other: 'Other'
},
otherConf: {
nohttps: 'Ignore HTTPS certificate',
notermcache: "Don't use the terminal's cache"
}
}
},
......
......@@ -20,14 +20,6 @@ module.exports = {
update: '检查更新',
quit: '退出程序'
},
shell: {
title: '数据',
add: '添加数据',
search: '搜索数据',
dump: '导出数据',
import: '导入数据',
clear: '清空数据'
},
edit: {
title: '编辑',
undo: '撤销',
......@@ -155,6 +147,15 @@ module.exports = {
confirm: '确定清空所有缓存数据吗?',
success: '清空全部缓存完毕!',
error: (err) => antSword.noxss(`清空全部缓存失败!\n${err}`)
},
accordion: {
base: '基础配置',
http: '请求信息',
other: '其他设置'
},
otherConf: {
nohttps: '忽略HTTPS证书',
notermcache: "虚拟终端不使用缓存"
}
}
},
......
......@@ -2,7 +2,8 @@
* 右键菜单
*/
const DATA = require('../data');
const Data = require('../data');
const Form = require('./form');
const Terminal = require('../../terminal/');
const Database = require('../../database/');
const FileManager = require('../../filemanager/');
......@@ -41,13 +42,13 @@ class ContextMenu {
],
false,
['add', 'plus-circle', false, this.addData.bind(this)],
['edit', 'edit', selectedData, this.editData.bind(this, id)],
['edit', 'edit', selectedData, this.editData.bind(this, data[0])],
['delete', 'remove', selectedMultiData, this.delData.bind(this, ids)],
false,
['move', 'share-square', selectedMultiData, null, this.parseMoveCategoryMenu(ids)],
['search', 'search', true],
false,
['clearCache', 'trash-o', selectedMultiData, this.clearCache.bind(this, ids)],
['clearCache', 'trash-o', selectedData, this.clearCache.bind(this, id)],
['clearAllCache', 'trash', false, this.clearAllCache.bind(this)]
].map((menu) => {
// 分隔符号
......@@ -182,16 +183,57 @@ class ContextMenu {
* 添加数据
*/
addData() {
new Form({
title: LANG['list']['add']['title'],
icon: 'plus-circle',
text: LANG['list']['add']['toolbar']['add']
}, {}, (data) => {
return new Promise((res, rej) => {
// 获取当前分类
data['base']['category'] = antSword.modules.shellmanager.category.sidebar.getActiveItem();
// 通知后台插入数据
const ret = antSword.ipcRenderer.sendSync('shell-add', data);
if (ret instanceof Object) {
// 重新加载数据
antSword.modules.shellmanager.reloadData({
category: data['base']['category']
});
return res(LANG['list']['add']['success']);
} else {
return rej(LANG['list']['add']['error'](ret.toString()));
}
});
})
}
/**
* 编辑数据
* @param {number} id [description]
* @param {Object} info 当前选中的数据
* @return {[type]} [description]
*/
editData(id) {
editData(info) {
new Form({
title: LANG['list']['edit']['title'](info.url),
icon: 'save',
text: LANG['list']['edit']['toolbar']['save']
}, info, (data) => {
return new Promise((res, rej) => {
// 通知后台更新数据
const ret = antSword.ipcRenderer.sendSync('shell-edit', {
old: info,
new: data
});
if (ret === 1) {
// 重新加载数据
antSword.modules.shellmanager.reloadData({
category: info['category']
});
return res(LANG['list']['edit']['success']);
} else {
return rej(LANG['list']['edit']['error'](ret.toString()));
}
})
})
}
/**
......@@ -200,7 +242,23 @@ class ContextMenu {
* @return {[type]} [description]
*/
delData(ids) {
layer.confirm(
LANG['list']['del']['confirm'](ids.length), {
icon: 2, shift: 6,
title: `<i class="fa fa-trash"></i> ${LANG['list']['del']['title']}`
}, (_) => {
layer.close(_);
const ret = antSword['ipcRenderer'].sendSync('shell-del', ids);
if (typeof(ret) === 'number') {
toastr.success(LANG['list']['del']['success'](ret), LANG_T['success']);
// 更新UI
antSword.modules.shellmanager.reloadData({
category: antSword.modules.shellmanager.category.sidebar.getActiveItem()
});
}else{
toastr.error(LANG['list']['del']['error'](ret.toString()), LANG_T['error']);
}
});
}
/**
......@@ -213,11 +271,29 @@ class ContextMenu {
/**
* 清空缓存
* @param {array} ids [description]
* @param {number} id ID
* @return {[type]} [description]
*/
clearCache(ids) {
clearCache(id) {
layer.confirm(
LANG['list']['clearCache']['confirm'], {
icon: 2, shift: 6,
title: `<i class="fa fa-trash"></i> ${LANG['list']['clearCache']['title']}`
}, (_) => {
layer.close(_);
const ret = antSword['ipcRenderer'].sendSync('cache-clear', {
id: id
});
if (ret === true) {
toastr.success(LANG['list']['clearCache']['success'], LANG_T['success']);
}else{
toastr.error(
LANG['list']['clearCache']['error'](
ret['errno'] === -2 ? 'Not cache file.' : ret['errno']
), LANG_T['error']
);
}
});
}
/**
......@@ -225,7 +301,19 @@ class ContextMenu {
* @return {[type]} [description]
*/
clearAllCache() {
layer.confirm(
LANG['list']['clearAllCache']['confirm'], {
icon: 2, shift: 6,
title: `<i class="fa fa-trash"></i> ${LANG['list']['clearAllCache']['title']}`
}, (_) => {
layer.close(_);
const ret = antSword['ipcRenderer'].sendSync('cache-clearAll');
if (ret === true) {
toastr.success(LANG['list']['clearAllCache']['success'], LANG_T['success']);
}else{
toastr.error(LANG['list']['clearAllCache']['error'](ret), LANG_T['error']);
}
});
}
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment