Skip to content

Commit

Permalink
Merge pull request #22 from Medicean/master
Browse files Browse the repository at this point in the history
新增代理功能//Add Proxy Configuration
  • Loading branch information
antoor committed Mar 23, 2016
2 parents 9dd036a + 1f8654d commit 60e309b
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### /22
1. 数据分类重命名
2. 新增代理连接配置

### /21
1. 优化UI组建自适应,在调整窗口大小的时候不刷新就能调整UI尺寸
Expand Down Expand Up @@ -43,4 +44,4 @@
* 中文开发文档
* 英文说明+开发文档
* nodejs服务端脚本支持
* python服务端脚本支持
* python服务端脚本支持
47 changes: 44 additions & 3 deletions modules/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
//
// Superagent发包模块
//
//

'use strict';

Expand All @@ -10,16 +10,57 @@ const superagent = require('superagent');

const logger = log4js.getLogger('Request');

var aproxymode = "noproxy";
var aproxyuri = "";

class Request {

constructor(electron) {
// 监听请求
const ipcMain = electron.ipcMain;

// 代理测试
ipcMain.on('aproxytest', (event, opts) => {
var _superagent = require('superagent');
var _aproxyuri = opts['aproxyuri'];
logger.debug("[aProxy] Test Proxy - " + _aproxyuri + " - Connect to " + opts['url']);
require('superagent-proxy')(superagent);
_superagent
.get(opts['url'])
.proxy(_aproxyuri)
.timeout(5000)
.end((err, ret) => {
if (err) {
logger.debug("[aProxy] Test Error");
return event.sender.send('aproxytest-error', err);
}else{
logger.debug("[aProxy] Test Success");
return event.sender.send('aproxytest-success', ret);
}
});

});
// 加载代理设置
ipcMain.on('aproxy', (event, opts) => {
aproxymode = opts['aproxymode'];
aproxyuri = opts['aproxyuri'];
logger.debug("[aProxy] Set Proxy Mode - " + (aproxymode == "manualproxy" ? aproxyuri : " noproxy"));
if (aproxymode == "noproxy") {
superagent.Request.prototype.proxy=function(arg) {
return this;
};
}else{
require('superagent-proxy')(superagent);
};
});
// 监听请求
ipcMain.on('request', (event, opts) => {
logger.debug("[aProxy] Connect mode - " + (aproxymode == "manualproxy" ? aproxyuri : " noproxy"));
logger.debug(opts['url'] + '\n', opts['data']);
superagent
.post(opts['url'])
.set('User-Agent', 'antSword/1.0')
.proxy(aproxyuri)
.type('form')
.timeout(5000)
.send(opts['data'])
Expand Down Expand Up @@ -89,4 +130,4 @@ class Request {

}

module.exports = Request;
module.exports = Request;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"log4js": "^0.6.29",
"nedb": "^1.5.1",
"superagent": "^1.6.1",
"superagent-proxy": "^1.0.0",
"webpack": "^1.12.14"
},
"scripts": {
Expand Down
26 changes: 24 additions & 2 deletions source/app.entry.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// -------
// create: 2015/12/20
// update: 2016/01/20
//
//

'use strict';

Expand Down Expand Up @@ -32,6 +32,28 @@ let _lang = localStorage.getItem('language') || navigator.language;
_lang = ['en', 'zh'].indexOf(_lang) === -1 ? 'en' : _lang;
antSword['language'] = require(`./language/${_lang}`);

// 加载代理
var _aproxymode = localStorage.getItem('aproxymode') || "noproxy";
var _aproxyprotocol = localStorage.getItem('aproxyprotocol');
var _aproxyserver = localStorage.getItem('aproxyserver');
var _aproxyport = localStorage.getItem('aproxyport');
var _aproxyusername = localStorage.getItem('aproxyusername');
var _aproxypassword = localStorage.getItem('aproxypassword');

antSword['aproxymode'] = _aproxymode;

if (_aproxyusername == "" || _aproxyusername == null || _aproxypassword == "" || _aproxypassword == null) {
antSword['aproxyauth'] = "";
}else{
antSword['aproxyauth'] = _aproxyusername + ":" + _aproxypassword;
}
antSword['aproxyuri'] = _aproxyprotocol + "://" + antSword['aproxyauth']+ "@" + _aproxyserver + ":" + _aproxyport;

ipcRenderer.send('aproxy', {
aproxymode: antSword['aproxymode'],
aproxyuri: antSword['aproxyuri']
});

antSword['ipcRenderer'] = ipcRenderer;
antSword['CacheManager'] = CacheManager;
antSword['menubar'] = new Menubar();
Expand All @@ -50,4 +72,4 @@ antSword['tabbar'] = new dhtmlXTabBar(document.body);
});
// 移除加载界面&&设置标题
$('#loading').remove();
document.title = antSword['language']['title'] || 'AntSword';
document.title = antSword['language']['title'] || 'AntSword';
39 changes: 36 additions & 3 deletions source/language/en.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
//
// language::en
//
//
module.exports = {
toastr: {
info: 'Info',
Expand Down Expand Up @@ -407,7 +407,40 @@ module.exports = {
}
}
},
aproxy: {
title: 'Proxy Configuration',
toolbar: {
save: 'Save',
test: 'Test Connection'
},
form: {
label: 'Configuring proxy access to the Internet',
mode:{
noproxy: 'No Proxy',
manualproxy: 'Set the proxy manually'
},
proxy: {
protocol: 'Protocol',
server: 'Server',
port: 'Port',
username: 'Username',
password: 'Password',
authtip: 'Leave blank if no authentication'
}
},
success: 'Successfully save the Proxy Configuration!',
error: 'Failed to save the Proxy Configuration!',
confirm: {
content: 'Restart the application?',
title: 'Proxy Configuration'
},
prompt:{
title: 'Input the target URL',
success: 'Successfully connect to the proxy server',
error: 'Failed to connect to the proxy server'
}
},
plugin: {
error: (err) => antSword.noxss(`Load plugin center failed!<br/>${err}`)
}
}
}
37 changes: 35 additions & 2 deletions source/language/zh.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// language::zh
//
//
module.exports = {
title: '中国蚁剑',
toastr: {
Expand Down Expand Up @@ -406,9 +406,42 @@ module.exports = {
toolbar: {
check: '检查'
}
},
aproxy: {
title: '代理设置',
toolbar: {
save: '保存',
test: '测试连接'
},
form: {
label: '配置访问互联网的代理',
mode:{
noproxy: '不使用代理',
manualproxy: '手动设置代理'
},
proxy: {
protocol: '代理协议',
server: '代理服务器',
port: '端口',
username: '用户名',
password: '密码',
authtip: '如果无认证方式请留空'
}
},
success: '保存代理设置成功!',
error: '保存代理设置失败!',
confirm: {
content: '重启应用生效,是否重启?',
title: '更改代理设置'
},
prompt:{
title: '输入测试的 URL',
success: '连接到代理服务器成功',
error: '连接到代理服务器失败'
}
}
},
plugin: {
error: (err) => antSword.noxss(`加载插件中心失败!<br/>${err}`)
}
}
}
131 changes: 131 additions & 0 deletions source/modules/settings/aproxy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// 代理设置
//

const LANG = antSword['language']['settings']['aproxy'];
const LANG_T = antSword['language']['toastr'];

class AProxy {

constructor(sidebar) {
sidebar.addItem({
id: 'aproxy',
text: `<i class="fa fa-paper-plane"></i> ${LANG['title']}`
});
const cell = sidebar.cells('aproxy');
// 代理数据
const aproxymode = localStorage.getItem('aproxymode') || 'noproxy';
const aproxyprotocol = localStorage.getItem('aproxyprotocol') || 'http';
const aproxyserver = localStorage.getItem('aproxyserver');
const aproxyport = localStorage.getItem('aproxyport');
const aproxyusername = localStorage.getItem('aproxyusername');
const aproxypassword = localStorage.getItem('aproxypassword');

// 工具栏
const toolbar = cell.attachToolbar();
toolbar.loadStruct([
{ id: 'save', type: 'button', text: LANG['toolbar']['save'], icon: 'save' },
{ type: 'separator' },
{ id: 'test', name: 'test', type: 'button', text: LANG['toolbar']['test'], icon: 'spinner', disabled: aproxymode === 'noproxy'}
]);

// 表单
const form = cell.attachForm([
{ type: 'settings', position: 'label-left', labelWidth: 150, inputWidth: 100 },
{ type: 'block', inputWidth: 'auto', offsetTop: 12, list: [
{ type: 'label', label: LANG['form']['label'] },

{ type: 'radio', position: 'label-right', label: LANG['form']['mode']['noproxy'], name: 'aproxymode', value: 'noproxy', checked: aproxymode === 'noproxy'},

{ type: 'radio', position: 'label-right', label: LANG['form']['mode']['manualproxy'], name: 'aproxymode', value: 'manualproxy', checked: aproxymode === 'manualproxy' ,list:[
{ type: 'combo',label: LANG['form']['proxy']['protocol'],readonly: true, name: 'protocol', options:[
{ text: 'HTTP', value: 'http', selected: aproxyprotocol === 'http' },
{ text: 'HTTPS', value: 'https', selected: aproxyprotocol === 'https' },
{ text: 'SOCKS5', value: 'socks', selected: aproxyprotocol === 'socks' },
{ text: 'SOCKS4', value: 'socks4', selected: aproxyprotocol === 'socks4' },
]},
{ type: 'input', label: LANG['form']['proxy']['server'], name: 'server', required: true, validate:"NotEmpty", value: aproxyserver},
{ type: 'input', label: LANG['form']['proxy']['port'], name: 'port', required: true, validate:"NotEmpty,ValidInteger", value: aproxyport},
{ type: 'input', label: LANG['form']['proxy']['username'], name: 'username', value: aproxyusername},
{ type: 'password', label: LANG['form']['proxy']['password'], name: 'password', value:aproxypassword }
]}
]}
], true);
form.enableLiveValidation(true);
form.attachEvent("onChange", function(name, value, is_checked){
if (name == "aproxymode") {
if (value == "manualproxy") {
toolbar.enableItem('test');
}else{
toolbar.disableItem('test');
}
}
});
// 工具栏点击事件
toolbar.attachEvent('onClick', (id) => {
switch(id) {
case 'save':
if(form.validate()){
var formvals = form.getValues();
const aproxymode = formvals['aproxymode'];
// 保存设置
localStorage.setItem('aproxymode', aproxymode);
localStorage.setItem('aproxyprotocol', formvals['protocol']);
localStorage.setItem('aproxyserver', formvals['server'].replace(/.+:\/\//, '').replace(/:.+/, ''));
localStorage.setItem('aproxyport', formvals['port']);
localStorage.setItem('aproxyusername', formvals['username']);
localStorage.setItem('aproxypassword', formvals['password']);

toastr.success(LANG['success'], LANG_T['success']);
// 重启应用
layer.confirm(LANG['confirm']['content'], {
icon: 2, shift: 6,
title: LANG['confirm']['title']
}, (_) => {
location.reload();
});
}else{
toastr.error(LANG['error'], LANG_T['error']);
}
break;
case 'test':
if(form.validate()){
layer.prompt({
title: LANG['prompt']['title'],
value: 'http://uyu.us',
formType: 0
}, function(testurl, index){
layer.close(index);
var loadindex = layer.load(2, {time: 6*1000});
var _formvals = form.getValues();
var _server = _formvals['server'].replace(/.+:\/\//, '').replace(/:.+/, '');
var _aproxyauth = "";
if (_formvals['username'] == "" || _formvals['password'] == "" || _formvals['username'] == null || _formvals['password'] == null) {
_aproxyauth = "";
}else{
_aproxyauth = _formvals['username'] + ":" + _formvals['password'];
}
var _aproxyuri = _formvals['protocol'] + '://' + _aproxyauth + '@' +_server + ':' + _formvals['port'];

antSword['ipcRenderer']
.on('aproxytest-error', (event, err) => {
layer.close(loadindex);
toastr.error(LANG['prompt']['error']+ "\n" + err['code'], LANG_T['error']);
})
.on('aproxytest-success', (event, ret) => {
layer.close(loadindex);
toastr.success(LANG['prompt']['success'], LANG_T['success']);
})
.send('aproxytest',{
url: testurl || 'http://uyu.us',
aproxyuri: _aproxyuri
});
});
}
break;
}
});
}
}

export default AProxy;
Loading

0 comments on commit 60e309b

Please sign in to comment.