实现代理设置proxy
用户在哪些情况下是需要设置网络代理呢?
1. 内网上不了外网,需要连接能上外网的内网电脑做代理,就能上外网;多个电脑共享上外网,就要用代理;
2.有些网页被封,通过国外的代理就能看到这被封的网站;
3.想隐藏真实IP;
4. 想加快访问网站速度,在网络出现拥挤或故障时,可通过代理服务器访问目的网站。比如A要访问C网站,但A到C网络出现问题,可以通过绕道,假设B是代理服务器,A可通过B, 再由B到C。
我们app的大多数用户情况是第一种.我们参考qq和chrome的插件switchysharp设置代理的方式来设计的界面


我们的项目是基于node-webkit技术进行开发的。
对于浏览器直接发送的请求设置代理可以直接设置chrome.proxy
if (proxy.proxyType == 0) {//不使用代理
chrome.proxy.settings.set({ 'value': { 'mode': 'direct' } }, function (e) { console.log(e) });
} else if (proxy.proxyType == 1) {//使用http代理
chrome.proxy.settings.set({ 'value': { 'mode': 'fixed_servers', rules: { singleProxy: { scheme: 'http', host: proxy.host, port: proxy.port }, bypassList: null } } }, function (e) { console.log(e) });
} else if (proxy.proxyType == 2) {//使用socks代理,可以支持版本4或者5
chrome.proxy.settings.set({ 'value': { 'mode': 'fixed_servers', rules: { singleProxy: { scheme: 'socks' + proxy.ver, host: proxy.host, port: proxy.port }, bypassList: null } } }, function (e) { console.log(e) });
} else if (proxy.proxyType == 3) {//使用系统代理
chrome.proxy.settings.set({ 'value': { 'mode': 'system' } }, function (e) { console.log(e) });
而程序内部使用nodejs发送的请求需要在发送请求时通过option进行配置,才能走代理网络
var options = {
hostname: Common.Config.addrInfo.openApi,
path: '/api/Author/Article/Collected?skip=' + skip + ' & rows=' + this.pageSize,
method : "GET";
headers: {
'Accept' : "application/json",
'Authorization': 'Bearer ' + index.userInfo.token
}
};
var req = require('http').request(HttpUtil.setProxy(options), function (res) {
var err = HttpUtil.resStatus(options, res);
clearTimeout(timeoutEventId);
if (err) {
callback(err);
return;
}
var resData = [];
res.setEncoding('utf8');
res.on('data', function (chunk) {
resData.push(chunk);
}).on("end", function () {
callback(null, resData.join(""));
});
});
req.on('error', function (e) {
clearTimeout(timeoutEventId);
callback(HttpUtil.reqStatus(options, e));
req.end();
});
req.on('timeout', function (e) {
clearTimeout(timeoutEventId);
callback(HttpUtil.reqStatus(options, e));
});
req.end();
}
static setProxy(options) {//设置代理信息
if (this.proxy) {
if (this.proxy.proxyType == 1 || (this.proxy.proxyType == 3 && this.proxy.host && this.proxy.sysProxyType == "PROXY")) {
options.path = "http://" + options.hostname + options.path;
options.hostname = null;
options.host = this.proxy.host;
options.port = this.proxy.port;
} else if (this.proxy.proxyType == 2 || (this.proxy.proxyType == 3 && this.proxy.host && this.proxy.sysProxyType == "SOCKS")) {
try {
if (!this.SocksProxyAgent)
this.SocksProxyAgent = require('socks-proxy-agent');//引用socks代理配置模块
} catch (e) {
this.SocksProxyAgent = null;
}
if (this.SocksProxyAgent) {
var agent = new this.SocksProxyAgent("socks" + this.proxy.ver + "://" + this.proxy.host + ":" + this.proxy.port);
options.agent = agent;
}
}
}
return options;
}
但是经过实践证明这个方法解决不了很多网络代理用户请求我们服务器接口失败的问题。
我们又找到了request模块,经过在有问题的电脑上进行测试这个模块是可以访问成功的。
var options1 = {
url: 'http://openapi.axeslide.com/api/TemplateTypes',
proxy: "http://10.22.138.21:8080",
headers: {
'User-Agent': 'request',
'Authorization': 'Bearer ' + index.userInfo.token,
'Accept' :"application/json"
}
};
this.request.get(options1, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response,body,"ok")
} else {
console.log(response,error, "err")
}
})
但是不管是之前的方式还是现在的request模板,在使用socks代理时,接收的数据量超过一定值时返回就会出现问题,然后把之前的socks-proxy-agent
又换了socks5-http-client做测试解决了该问题。
现在说一下测试工具,fiddler和node搭建的socks代理服务器
fiddler相信对大家来对并不陌生,先打开fiddler options选项,然后配置下代理信息,主要是端口号,这里可以设置是否设置成系统代理,设置之后需要重新启动fiddler,如果浏览器配置成走9999 所有的请求fiddler都会抓取到。


搭建socks代理可以从网上找现成的代码,node搭建的。大家也可以下载 http://files.cnblogs.com/files/fangsmile/socks5.zip ,下载后运行node proxy

下面附一段switchysharp的实现代码:
ProxyPlugin.setProxy = function (proxyMode, proxyString, proxyExceptions, proxyConfigUrl) {
if (ProxyPlugin.disabled) return 0;
var config;
ProxyPlugin.proxyMode = Settings.setValue('proxyMode', proxyMode);
ProxyPlugin.proxyServer = Settings.setValue('proxyServer', proxyString);
ProxyPlugin.proxyExceptions = Settings.setValue('proxyExceptions', proxyExceptions);
ProxyPlugin.proxyConfigUrl = Settings.setValue('proxyConfigUrl', proxyConfigUrl);
switch (proxyMode) {
case 'system':
config = {mode:"system"};
break;
case 'direct':
config = {mode:"direct"};
break;
case 'manual':
var tmpbypassList = [];
var proxyExceptionsList = ProxyPlugin.proxyExceptions.split(';');
var proxyExceptionListLength = proxyExceptionsList.length;
for (var i = 0; i < proxyExceptionListLength; i++) {
tmpbypassList.push(proxyExceptionsList[i].trim())
}
proxyExceptionsList = null;
var profile = ProfileManager.parseProxyString(proxyString);
if (profile.useSameProxy) {
config = {
mode:"fixed_servers",
rules:{
singleProxy:ProxyPlugin._parseProxy(profile.proxyHttp),
bypassList:tmpbypassList
}
};
}
else {
var socksProxyString;
if (profile.proxySocks && !profile.proxyHttp && !profile.proxyFtp && !profile.proxyHttps) {
socksProxyString = profile.socksVersion == 4 ? 'socks=' + profile.proxySocks : 'socks5=' + profile.proxySocks;
config = {
mode:"fixed_servers",
rules:{
singleProxy:ProxyPlugin._parseProxy(socksProxyString),
bypassList:tmpbypassList
}
}
}
else {
config = {
mode:"fixed_servers",
rules:{
bypassList:tmpbypassList
}
};
if (profile.proxySocks) {
socksProxyString = profile.socksVersion == 4 ? 'socks=' + profile.proxySocks : 'socks5=' + profile.proxySocks;
config.rules.fallbackProxy = ProxyPlugin._parseProxy(socksProxyString);
}
if (profile.proxyHttp)
config.rules.proxyForHttp = ProxyPlugin._parseProxy(profile.proxyHttp);
if (profile.proxyFtp)
config.rules.proxyForFtp = ProxyPlugin._parseProxy(profile.proxyFtp);
if (profile.proxyHttps)
config.rules.proxyForHttps = ProxyPlugin._parseProxy(profile.proxyHttps);
}
}
tmpbypassList = null;
break;
case 'auto':
if (ProxyPlugin.proxyConfigUrl == memoryPath) {
config = {
mode:"pac_script",
pacScript:{
data:Settings.getValue('pacScriptData', '')
}
};
Settings.setValue('pacScriptData', '');
}
else {
config = {
mode:"pac_script",
pacScript:{
url:ProxyPlugin.proxyConfigUrl
}
}
}
break;
}
ProxyPlugin.mute = true;
ProxyPlugin._proxy.settings.set({'value':config}, function () {
ProxyPlugin.mute = false;
if (ProxyPlugin.setProxyCallback != undefined) {
ProxyPlugin.setProxyCallback();
ProxyPlugin.setProxyCallback = undefined;
}
});
profile = null;
config = null;
return 0;
};
实现代理设置proxy的更多相关文章
- C#程序中设置全局代理(Global Proxy)
1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1 //设置代理 2 WebProxy WP = new Web ...
- [Z] C#程序中设置全局代理(Global Proxy)
https://www.cnblogs.com/Javi/p/7274268.html 1. HttpWebRequest类的Proxy属性,只要设置了该属性就能够使用代理了,如下: 1 ...
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...
- JAVA HTTP请求 常用的代理设置
由于公司上网实行代理机制, 而最近一段时间又在研究Web上的OpenApi. 没办法一定要使用代理,我之前有文章介绍了httpclient的代理使用方式, 这里介绍基本java的代理使用方式. 最常使 ...
- Django如何设置proxy
设置porxy的原因 一般情况下我们代理设置是针对与浏览器而言,通常只需在浏览器设置中进行配置,但它只针对浏览器有效,对我们自己编写的程序并任何效果,这时就需要我们在软件编码中加入代理设置. --- ...
- git 代理设置
git 代理设置: git config --global http.proxy http://proxy.com:8080git config --global https.proxy http:/ ...
- HttpURLConnection中使用代理(Proxy)及其验证(Authentication)
HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...
- 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头
目录 https请求 代理设置 请求头设置 获取状态码 接收响应头 https请求 https协议(Secure Hypertext Transfer Protocol) : 安全超文本传输协议, H ...
- nodejs爬虫笔记(二)---代理设置
node爬虫代理设置 最近想爬取YouTube上面的视频信息,利用nodejs爬虫笔记(一)的方法,代码和错误如下 var request = require('request'); var chee ...
随机推荐
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- 前端框架 EasyUI (1)熟悉一下EasyUI
jQuery EasyUI 官方网站 http://www.jeasyui.com/ .去年新开了个中文网 http://www.jeasyui.net/,不知道是不是官方的,不过看着挺像样.但是,广 ...
- C# 破解 Reflector8.5
一.分析 破解.net .dll,可以使用reflector,但官方提供的reflector是需要购买的,因此,破解reflector势在必行. 二.破解Reflector具体步骤 下面为详细的破解步 ...
- RxJS + Redux + React = Amazing!(译二)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...
- 预览github里面的网页或dome
1.问题所在: 之前把项目提交到github都可以在路径前面加上http://htmlpreview.github.io/?来预览demo,最近发现这种方式预览的时候加载不出来css,js(原因不详) ...
- .NET CoreCLR开发人员指南(上)
1.为什么每一个CLR开发人员都需要读这篇文章 和所有的其他的大型代码库相比,CLR代码库有很多而且比较成熟的代码调试工具去检测BUG.对于程序员来说,理解这些规则和习惯写法非常的重要. 这篇文章让所 ...
- Python爬虫小白入门(四)PhatomJS+Selenium第一篇
一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...
- Android SDK 在线更新镜像服务器资源
本文转自:http://blog.kuoruan.com/24.html.感谢原作者. 什么是Android SDK SDK:(software development kit)软件开发工具包.被软件 ...
- [数据结构]——堆(Heap)、堆排序和TopK
堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...
- 微信小程序开发日记——高仿知乎日报(中)
本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该教 ...