Commit 6aac029b authored by Antoor's avatar Antoor Committed by GitHub

Merge pull request #71 from antoor/v2.0-beta-login-auth

v2.0-beta::Login authentication
parents c32a6334 651813b4
......@@ -40,7 +40,8 @@ class Database {
.on('shell-editDataConf', this.editDataConf.bind(this))
.on('shell-delDataConf', this.delDataConf.bind(this))
.on('shell-getDataConf', this.getDataConf.bind(this))
.on('shell-renameCategory', this.renameShellCategory.bind(this));
.on('shell-renameCategory', this.renameShellCategory.bind(this))
.on('shell-updateHttpConf', this.updateHttpConf.bind(this));
}
/**
......@@ -176,6 +177,27 @@ class Database {
});
}
/**
* 更新httpConf配置信息(包含body&&headers
* @param {[type]} event [description]
* @param {[type]} opt = {} [description]
* @return {[type]} [description]
*/
updateHttpConf(event, opt = {}) {
logger.warn('updateHttpConf', opt);
this.cursor.update({
_id: opt._id
}, {
$set: {
httpConf: opt.conf,
utime: +new Date
}
}, (_err, _ret) => {
event.returnValue = _err || _ret;
});
}
/**
* 删除shell数据
* @param {Object} event ipcMain对象
......
......@@ -55,7 +55,8 @@ module.exports = {
pluginDefault: 'Default',
pluginStore: 'Plugin Store',
clearCache: 'Clear cache',
clearAllCache: 'Clear all cache'
clearAllCache: 'Clear all cache',
viewsite: 'View Site'
},
category: {
title: 'Category',
......@@ -513,5 +514,13 @@ module.exports = {
update: {
title: 'Found updates',
body: (ver) => `New version: ${ver}`
},
viewsite: {
toolbar: {
save: 'Save',
view: 'View'
},
saveSuccess: 'Save cookie configuration is successful!',
saveFailed: (err) => `Save cookie configuration failed!\n${err}`
}
}
......@@ -56,7 +56,8 @@ module.exports = {
pluginDefault: '默认分类',
pluginStore: '插件市场',
clearCache: '清空缓存',
clearAllCache: '清空所有缓存'
clearAllCache: '清空所有缓存',
viewsite: '浏览网站'
},
category: {
title: '分类目录',
......@@ -481,5 +482,13 @@ module.exports = {
update: {
title: '发现更新',
body: (ver) => `新的版本:${ver}`
},
viewsite: {
toolbar: {
save: '保存',
view: '浏览'
},
saveSuccess: '保存Cookie成功!',
saveFailed: (err) => `保存Cookie失败!\n${err}`
}
}
......@@ -4,6 +4,7 @@
const Data = require('../data');
const Form = require('./form');
const ViewSite = require('../../viewsite/');
const Terminal = require('../../terminal/');
const Database = require('../../database/');
const FileManager = require('../../filemanager/');
......@@ -34,6 +35,9 @@ class ContextMenu {
['database', 'database', selectedData, () => {
new Database(data[0]);
}],
['viewsite', 'chrome', selectedData, () => {
new ViewSite(data[0]);
}],
false,
['plugin', 'folder-o', selectedMultiData, null, this.parsePlugContextMenu(data)],
[
......
## 浏览网站模块
> 用于浏览目标站点,从而进行一些登陆操作(自动设置cookie等登陆信息
## 实现思路
1. 打开一个浏览窗口,访问站点域名
2. 创建一个tabbar,用于管理cookie
3. 关闭浏览窗口,tabbar会随时刷新cookie的改动,然后显示到UI中
4. 保存cookie,关闭tabbar(将会把Cookie内容设置为请求的Header.Cookie
const CM = {
/**
* Cookie操作对象
* @type {object}
*/
cookies: antSword.remote.session.defaultSession.cookies,
/**
* 获取Cookie
* @param {Object} opt = {} 查询条件{url, name, domain, path, secure, session}
* @docLink http://electron.atom.io/docs/api/session/#cookiesgetfilter-callback
* @return {Promise} [description]
*/
get: (opt = {}) => {
return new Promise((res, rej) => {
CM.cookies.get(opt, (err, _cookies) => {
if (err) { return rej(err) }
return res(_cookies);
})
})
},
/**
* 获取Cookie字符串
* @param {object} opt = {}
* @return {[type]} [description]
*/
getStr: (opt = {}) => {
return new Promise((res, rej) => {
CM.cookies.get(opt, (err, _cookies) => {
if (err) { return rej(err) }
let _cs = [];
_cookies.map((_) => {
_cs.push(
_['name'] + '=' + _['value']
)
});
return res(_cs.join('; '));
})
})
}
}
module.exports = CM;
/**
* 网站浏览模块
* 开写:2016/07/01
*/
const CookieMgr = require('./cookiemgr');
const LANG = antSword.language['viewsite'];
const LANG_T = antSword.language['toastr'];
class ViewSite {
constructor(opts) {
const hash = String(Math.random()).substr(2, 10);
// 初始化UI::tabbar
const tabbar = antSword['tabbar'];
tabbar.addTab(
`tab_viewsite_${hash}`,
`<i class="fa fa-chrome"></i> ${opts['ip']}`,
null, null, true, true
);
tabbar.attachEvent('onTabClick', (id,lid) => {
if (id !== `tab_viewsite_${hash}`) { return };
});
this.opts = opts;
this.cell = tabbar.cells(`tab_viewsite_${hash}`);
// 初始化工具栏
this.toolbar = this._initToolbar();
this.grid = this._initGrid();
// 定时刷新Cookie
this._refreshCookie();
const inter = setInterval(() => {
if (this.grid.clearAll instanceof Function) {
this._refreshCookie();
} else {
clearInterval(inter);
}
}, 1000);
// 打开浏览窗口
this._loadURL(opts.url);
}
/**
* 初始化工具栏
* @return {[type]} [description]
*/
_initToolbar() {
const toolbar = this.cell.attachToolbar();
toolbar.loadStruct([
{ id: 'save', type: 'button', icon: 'save', text: LANG['toolbar'].save },
{ type: 'separator' },
{ id: 'view', type: 'button', icon: 'chrome', text: LANG['toolbar'].view },
]);
toolbar.attachEvent('onClick', (id) => {
switch(id) {
case 'save':
this._saveCookie();
break;
case 'view':
this._loadURL(this.opts.url);
}
})
return toolbar;
}
/**
* 初始化grid
* @return {[type]} [description]
*/
_initGrid() {
const grid = this.cell.attachGrid();
// 设置grid头
grid.setHeader('Name,Value,Domain,Path,Expires / Max-Age,Size,HTTP,Secure');
grid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro");
grid.setColSorting('str,str,str,str,str,str,str,str');
grid.setInitWidths("120,*,120,50,150,50,50,60");
grid.setColAlign("left,left,left,left,left,right,center,left");
grid.enableMultiselect(true);
grid.init();
return grid;
}
/**
* 刷新Cookie
* @return {[type]} [description]
*/
_refreshCookie() {
CookieMgr.get({
url: this.opts['url']
}).then((cookie) => {
let data = [];
cookie.map((c, i) => {
data.push({
id: i + 1,
data: [
c.name, c.value, c.domain,
c.path, c.session ? 'Session' : new Date(c.expirationDate).toUTCString(),
c.name.length + c.value.length, c.httpOnly ? 'httpOnly': '', c.secure ? 'Secure': ''
]
});
});
// 刷新UI
this.grid.clearAll();
this.grid.parse({
'rows': data
}, 'json');
})
}
/**
* 保存Cookie到配置
* @return {[type]} [description]
*/
_saveCookie() {
CookieMgr.getStr({
url: this.opts.url
}).then((cookie) => {
// 1. 获取旧数据
const oldHttpConf = (antSword.ipcRenderer.sendSync('shell-findOne', this.opts._id).httpConf || {});
// 2. 添加新数据(cookie)
const httpConf = Object.assign({}, oldHttpConf, {
headers: Object.assign({}, oldHttpConf.headers || {}, {
Cookie: cookie
})
})
// 3. 更新数据
const ret = antSword.ipcRenderer.sendSync('shell-updateHttpConf', {
_id: this.opts._id,
conf: httpConf
});
if (ret === 1) {
toastr.success(LANG['saveSuccess'], LANG_T['success']);
} else {
toastr.error(LANG['saveFailed'](ret), LANG_T['error']);
}
})
}
/**
* 初始化浏览窗口
* @param {[type]} url [description]
* @return {[type]} [description]
*/
_loadURL(url) {
let win = new antSword['remote'].BrowserWindow({
width: 930,
height: 666,
minWidth: 888,
minHeight: 555,
show: false,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
},
title: this.opts.url
});
win.loadURL(url);
win.show();
win.openDevTools();
}
}
module.exports = ViewSite;
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