Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
antSword
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
HuangJunbo
antSword
Commits
97e29a30
Commit
97e29a30
authored
Apr 25, 2016
by
Antoor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reconstruction of the back-end HTTP request module
重构后端HTTP请求模块
parent
c9aaf2a3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
175 additions
and
132 deletions
+175
-132
CHANGELOG.md
CHANGELOG.md
+2
-0
request.js
modules/request.js
+170
-129
about.jsx
source/modules/settings/about.jsx
+3
-3
No files found.
CHANGELOG.md
View file @
97e29a30
...
@@ -6,6 +6,8 @@
...
@@ -6,6 +6,8 @@
### /25
### /25
1.
移除
`webpack`
以及其他不必要的依赖,直接无需编译即可执行ES6代码(有新模块
`babel`
的加入,请使用
`npm install`
初始化
1.
移除
`webpack`
以及其他不必要的依赖,直接无需编译即可执行ES6代码(有新模块
`babel`
的加入,请使用
`npm install`
初始化
2.
更新美化关于页面
3.
重构
`modules/request.js`
后端数据请求模块
### /24 `(v1.2.1)`
### /24 `(v1.2.1)`
1.
重写前端资源加载方案
1.
重写前端资源加载方案
...
...
modules/request.js
View file @
97e29a30
//
/**
// Superagent发包模块
* HTTP后端数据发送处理函数
//
* 更新: 2016/04/25
*/
'use strict'
;
'use strict'
;
const
fs
=
require
(
'fs'
);
const
fs
=
require
(
'fs'
),
const
through
=
require
(
'through'
);
iconv
=
require
(
'iconv-lite'
),
const
log4js
=
require
(
'log4js'
);
logger
=
require
(
'log4js'
).
getLogger
(
'Request'
),
const
iconv
=
require
(
'iconv-lite'
);
through
=
require
(
'through'
),
const
superagent
=
require
(
'superagent'
);
superagent
=
require
(
'superagent'
),
superagentProxy
=
require
(
'superagent-proxy'
);
// 请求UA
const
USER_AGENT
=
'antSword/v1.3'
;
const
logger
=
log4js
.
getLogger
(
'Request'
);
// 请求超时
const
REQ_TIMEOUT
=
5000
;
var
aproxymode
=
"noproxy"
;
// 代理配置
var
aproxyuri
=
""
;
const
APROXY_CONF
=
{
mode
:
'noproxy'
,
uri
:
''
}
class
Request
{
class
Request
{
constructor
(
electron
)
{
constructor
(
electron
)
{
// 监听请求
const
userAgent
=
'antSword/1.1'
;
const
timeout
=
5000
;
const
ipcMain
=
electron
.
ipcMain
;
const
ipcMain
=
electron
.
ipcMain
;
// 代理测试
ipcMain
.
on
(
'aproxy'
,
this
.
onAproxy
.
bind
(
this
));
ipcMain
.
on
(
'aproxytest'
,
(
event
,
opts
)
=>
{
ipcMain
.
on
(
'aproxytest'
,
this
.
onAproxyTest
.
bind
(
this
));
var
_superagent
=
require
(
'superagent'
);
ipcMain
.
on
(
'request'
,
this
.
onRequest
.
bind
(
this
));
var
_aproxyuri
=
opts
[
'aproxyuri'
];
ipcMain
.
on
(
'download'
,
this
.
onDownlaod
.
bind
(
this
));
logger
.
debug
(
"[aProxy] Test Proxy - "
+
_aproxyuri
+
" - Connect to "
+
opts
[
'url'
]);
}
require
(
'superagent-proxy'
)(
superagent
);
_superagent
/**
* 加载代理配置
* @param {Object} event ipcMain事件
* @param {Object} opts 代理配置
* @return {[type]} [description]
*/
onAproxy
(
event
,
opts
)
{
logger
.
debug
(
'aProxy::Set Proxy Mode -'
,
APROXY_CONF
[
'mode'
]
===
'manualproxy'
?
APROXY_CONF
[
'uri'
]
:
'noproxy'
);
APROXY_CONF
[
'mode'
]
=
opts
[
'aproxymode'
];
APROXY_CONF
[
'uri'
]
=
opts
[
'aproxyuri'
];
if
(
APROXY_CONF
[
'mode'
]
===
'noproxy'
)
{
return
superagent
.
Request
.
prototype
.
proxy
=
function
()
{
return
this
};
}
superagentProxy
(
superagent
);
}
/**
* 监听代理连接测试
* @param {Object} event ipcMain事件
* @param {Object} opts 测试配置
* @return {[type]} [description]
*/
onAproxyTest
(
event
,
opts
)
{
logger
.
debug
(
'aProxy::Test Proxy -'
,
opts
[
'aproxyuri'
],
'- Connect to '
,
opts
[
'url'
]);
superagentProxy
(
superagent
);
superagent
.
get
(
opts
[
'url'
])
.
get
(
opts
[
'url'
])
.
set
(
'User-Agent'
,
userAgent
)
.
set
(
'User-Agent'
,
USER_AGENT
)
.
proxy
(
_aproxyuri
)
.
proxy
(
opts
[
'aproxyuri'
]
)
.
timeout
(
timeout
)
.
timeout
(
REQ_TIMEOUT
)
.
end
((
err
,
ret
)
=>
{
.
end
((
err
,
ret
)
=>
{
if
(
err
)
{
if
(
err
)
{
logger
.
debug
(
"[aProxy] Test Error"
);
logger
.
error
(
"aProxy::Test Error"
,
err
);
return
event
.
sender
.
send
(
'aproxytest-error-'
+
opts
[
'hash'
],
err
);
return
event
.
sender
.
send
(
'aproxytest-error-'
+
opts
[
'hash'
],
err
);
}
else
{
}
else
{
logger
.
debug
(
"[aProxy]
Test Success"
);
logger
.
info
(
"aProxy::
Test Success"
);
return
event
.
sender
.
send
(
'aproxytest-success-'
+
opts
[
'hash'
],
ret
);
return
event
.
sender
.
send
(
'aproxytest-success-'
+
opts
[
'hash'
],
ret
);
}
}
});
});
});
}
// 加载代理设置
ipcMain
.
on
(
'aproxy'
,
(
event
,
opts
)
=>
{
aproxymode
=
opts
[
'aproxymode'
];
/**
aproxyuri
=
opts
[
'aproxyuri'
];
* 监听HTTP请求
logger
.
debug
(
"[aProxy] Set Proxy Mode - "
+
(
aproxymode
==
"manualproxy"
?
aproxyuri
:
" noproxy"
));
* @param {Object} event ipcMain事件对象
if
(
aproxymode
==
"noproxy"
)
{
* @param {Object} opts 请求配置
superagent
.
Request
.
prototype
.
proxy
=
function
(
arg
)
{
* @return {[type]} [description]
return
this
;
*/
};
onRequest
(
event
,
opts
)
{
}
else
{
require
(
'superagent-proxy'
)(
superagent
);
logger
.
debug
(
'onRequest::url'
,
opts
[
'url'
]);
};
logger
.
debug
(
'onRequest::data'
,
opts
[
'data'
]);
});
// 监听请求
ipcMain
.
on
(
'request'
,
(
event
,
opts
)
=>
{
logger
.
debug
(
"[aProxy] Connect mode - "
+
(
aproxymode
==
"manualproxy"
?
aproxyuri
:
" noproxy"
));
logger
.
debug
(
opts
[
'url'
]
+
'
\
n'
,
opts
[
'data'
]);
superagent
superagent
.
post
(
opts
[
'url'
])
.
post
(
opts
[
'url'
])
.
set
(
'User-Agent'
,
userAgent
)
.
set
(
'User-Agent'
,
USER_AGENT
)
.
proxy
(
aproxyuri
)
.
proxy
(
APROXY_CONF
[
'uri'
]
)
.
type
(
'form'
)
.
type
(
'form'
)
.
timeout
(
timeout
)
.
timeout
(
REQ_TIMEOUT
)
.
send
(
opts
[
'data'
])
.
send
(
opts
[
'data'
])
.
parse
((
res
,
callback
)
=>
{
.
parse
((
res
,
callback
)
=>
{
this
.
parse
(
opts
[
'tag_s'
],
opts
[
'tag_e'
],
(
chunk
)
=>
{
this
.
parse
(
opts
[
'tag_s'
],
opts
[
'tag_e'
],
(
chunk
)
=>
{
...
@@ -77,23 +109,25 @@ class Request {
...
@@ -77,23 +109,25 @@ class Request {
if
(
err
)
{
if
(
err
)
{
return
event
.
sender
.
send
(
'request-error-'
+
opts
[
'hash'
],
err
);
return
event
.
sender
.
send
(
'request-error-'
+
opts
[
'hash'
],
err
);
};
};
cons
t
buff
=
ret
.
body
;
le
t
buff
=
ret
.
body
;
// 解码
// 解码
cons
t
text
=
iconv
.
decode
(
buff
,
opts
[
'encode'
]);
le
t
text
=
iconv
.
decode
(
buff
,
opts
[
'encode'
]);
// 回调数据
// 回调数据
event
.
sender
.
send
(
'request-'
+
opts
[
'hash'
],
{
event
.
sender
.
send
(
'request-'
+
opts
[
'hash'
],
{
text
:
text
,
text
:
text
,
buff
:
buff
buff
:
buff
});
});
});
});
});
}
/**
/**
* 监听文件下载请求
* 监听下载请求
* - 技术实现:通过pipe进行数据流逐步解析,当遇到截断符,则标记,最后数据传输完成,利用标记点进行数据截取,最后保存。
* @param {Object} event ipcMain事件对象
* @opts {Object:path,url,data,tag_s,tag_e}
* @param {Object} opts 下载配置
* @return {[type]} [description]
*/
*/
ipcMain
.
on
(
'download'
,
(
event
,
opts
)
=>
{
onDownlaod
(
event
,
opts
)
{
logger
.
debug
(
'DOWNLOAD
'
,
opts
);
logger
.
debug
(
'onDownlaod
'
,
opts
);
// 创建文件流
// 创建文件流
const
rs
=
fs
.
createWriteStream
(
opts
[
'path'
]);
const
rs
=
fs
.
createWriteStream
(
opts
[
'path'
]);
...
@@ -105,8 +139,8 @@ class Request {
...
@@ -105,8 +139,8 @@ class Request {
// 开始HTTP请求
// 开始HTTP请求
superagent
superagent
.
post
(
opts
[
'url'
])
.
post
(
opts
[
'url'
])
.
set
(
'User-Agent'
,
userAgent
)
.
set
(
'User-Agent'
,
USER_AGENT
)
.
proxy
(
aproxyuri
)
.
proxy
(
APROXY_CONF
[
'uri'
]
)
.
type
(
'form'
)
.
type
(
'form'
)
// 设置超时会导致文件过大时写入出错
// 设置超时会导致文件过大时写入出错
// .timeout(timeout)
// .timeout(timeout)
...
@@ -138,10 +172,17 @@ class Request {
...
@@ -138,10 +172,17 @@ class Request {
finalData
=
tempDataBuffer
=
tempData
=
null
;
finalData
=
tempDataBuffer
=
tempData
=
null
;
}
}
));
));
});
}
}
// 二进制数据流解析
/**
* 二进制数据流解析
* @param {String} tag_s 数据截断符号(前)
* @param {String} tag_e 数据截断符号(后)
* @param {Function} chunkCallBack 数据流回调函数
* @param {Object} res Superagent::res对象
* @param {Function} callback 数据获取完毕回调事件
* @return {[type]} [description]
*/
parse
(
tag_s
,
tag_e
,
chunkCallBack
,
res
,
callback
)
{
parse
(
tag_s
,
tag_e
,
chunkCallBack
,
res
,
callback
)
{
// 数据转换二进制处理
// 数据转换二进制处理
res
.
setEncoding
(
'binary'
);
res
.
setEncoding
(
'binary'
);
...
...
source/modules/settings/about.jsx
View file @
97e29a30
...
@@ -19,9 +19,9 @@ class About {
...
@@ -19,9 +19,9 @@ class About {
<hr/>
<hr/>
<h2>中国蚁剑</h2>
<h2>中国蚁剑</h2>
<p>
<p>
<a href="https://github.com/antoor/antSword"><i class="fa fa-github-alt"></i> GitHub</a>
|
<a href="https://github.com/antoor/antSword"><i class="fa fa-github-alt"></i> GitHub</a>
/
<a href="http://uyu.us"><i class="fa fa-home"></i> 主页</a>
|
<a href="http://uyu.us"><i class="fa fa-home"></i> 主页</a>
/
<a href="http://doc.uyu.us"><i class="fa fa-book"></i> 文档</a>
|
<a href="http://doc.uyu.us"><i class="fa fa-book"></i> 文档</a>
/
<a href="http://shang.qq.com/wpa/qunwpa?idkey=51997458a52d534454fd15e901648bf1f2ed799fde954822a595d6794eadc521"><i class="fa fa-qq"></i> Q群</a>
<a href="http://shang.qq.com/wpa/qunwpa?idkey=51997458a52d534454fd15e901648bf1f2ed799fde954822a595d6794eadc521"><i class="fa fa-qq"></i> Q群</a>
</p>
</p>
</div>
</div>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment