刚接触nodejs,做个东西练下手,通过nodejs直接转发整站,原本想把内容全翻译成英文,但google对流量行审查,被封IP,所以就没啥用了, 效果像这样

var b = function (a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
} var gettk = function (a,TKK) {
//console.log(a,TKK);
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + "." + (a ^ h)
} function getTransRecursive(text,allrs,callback)
{
console.log(text.length);
var surplus=text.substring(4000);
text=text.substring(0,4000);
getTrans(text,function(rs){
allrs+=rs;
if(surplus.length>0)
{
getTransRecursive(surplus,allrs,callback);
}else{
callback(allrs);
}
});
}
function getTrans(text,callback)
{ var tk=gettk(text, "424997.418814026");
var url='/translate_a/single?client=t&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&pc=1&otf=1&ssel=6&tsel=3&kc=0&tk='+ tk ;
var options = {
hostname: 'translate.google.cn',
port: 80,
path: url,
method: 'POST',
headers: { // 必选信息, 可以抓包工看一下
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
}
};
var tbody="";
var req = http.request(options, function (remoteRes) {
remoteRes.setEncoding('utf8');
remoteRes.on('data', function (chunk) {
tbody+=chunk;
});
remoteRes.on("end",function(){
try
{
tbody=eval(tbody);
tbody=tbody[0];
var rstext="";
for(var i in tbody)
{
rstext+=tbody[i][0];
}
callback(rstext);
}
catch(err)
{
console.log("transErr1:");
console.log(err);
callback(text);
}
});
});
req.on('error', function (e) {
console.log('transErr2:' + e.message);
callback(text);
});
req.write("q="+ encodeURI(text));
req.end();
} function handleStr(newhost,url,str,isHtml,callback)
{
if(!isHtml||url==""||url=="/")
{
callback(str);
return;
}
str= str.replace(/<script (([\s\S])*?)<\/script>/g,"");
//替换掉所有列表中的网址
for(var key in hostList){
str= str.replace(hostList[key],key);
} //callback(str); //根据域名进行翻译
switch(newhost)
{
case "www.guancha.cn":
var $ = cheerio.load(str);
var headtitle=$("head>title").text();
headtitle=unescape(headtitle.replace(/&#x/g,'%u').replace(/;/g,'')); var nav=$(".nav").html();
nav=unescape(nav.replace(/&#x/g,'%u').replace(/;/g,'')); var str = $('.all-txt').text();
var title= $('.left-main').find('h3').text(); getTransRecursive(str,"",function(str){
$('.all-txt').text(str);
getTransRecursive(title,"",function(title){
$('.left-main').find('h3').text(title);
getTransRecursive(headtitle,"",function(head){
$("head>title").text(title);
getTransRecursive(nav,"",function(nav){
$(".nav").html(nav);
callback($.html());
});
});
});
}); break;
default:
callback(str);
break;
}
}
var hostList={"www.thiscnnews.com":"www.guancha.cn","localhost1:8080":"user.guancha.cn"}
var noCatchList=['/?s=dhshouye','/internation?s=dhguoji','/military-affairs?s=dhjunshi','/economy?s=dhcaijing','/industry-science?s=dhkeji','/TMT?s=dhtmt','/car?s=dhqiche','/thinktank?s=dhzhiku','/GuanWangKanPian?s=dhshipin']; function isInArray3(arr,value){
if(arr.indexOf&&typeof(arr.indexOf)=='function'){
var index = arr.indexOf(value);
if(index >= 0){
return true;
}
}
return false;
} function getNewHost(host)
{
if(host in hostList)
{
return hostList[host];
}else{
return "www.guancha.cn";
}
}
function getRemote(newhost,url,res,file)
{ var body="";
var options = {
hostname: newhost,
port: 80,
path:url,
method: 'GET'
};
var req = http.request(options, function (remoteRes) {
remoteRes.setEncoding('utf8');
remoteRes.on('data', function (chunk) {
body+=chunk;
});
remoteRes.on("end",function(){
handleStr(newhost,url,body,remoteRes.headers['content-type'].indexOf("text/html") != -1,function(rs){
if(file!=''){
fs.writeFile(file, rs, {flag: 'a'}, function (err) {
if(err) {
console.error(err);
}
});
}
res.end(rs);
});
});
});
req.on('error', function (e) {
res.end( e.message);
console.log('problem with request: ' + e.message);
});
req.end();
}
var mkdirs = module.exports.mkdirs = function(dirpath, mode, callback) {
fs.exists(dirpath, function(exists) {
if(exists) {
callback(dirpath);
} else {
//尝试创建父目录,然后再创建当前目录
mkdirs(path.dirname(dirpath), mode, function(){
fs.mkdir(dirpath, mode, callback);
});
}
});
}; function getCatch(newhost,url,callback)
{
var file=__dirname+'/tmp/'+newhost.replace(':','');
mkdirs(file,777,function(){
file+='/'+cryptos.md5(url);
fs.exists(file,function(exists){
if(exists)
{
fs.readFile(file,'utf-8',function(err,data){
if(err){
callback(false,file);
}
else{
callback(true,file,data);
}
});
}else{
callback(false,file);
}
});
});
}
var useCatch=true;
//html解析器
var cheerio = require('cheerio');
//文件操作模块
var fs = require('fs'),path = require('path');
//加密模块
var cryptos=require("./cryptos");
//引入http模块
var http = require("http");
//设置主机名
var hostName = '127.0.0.1';
//设置端口
var port = 9000;
//创建服务
var server = http.createServer(function(req,res){
var newhost= getNewHost(req.headers.host);
var url=req.url;
if(useCatch&&!isInArray3(noCatchList,url))
{
getCatch(newhost,url,function(hasData,file,data){
if(hasData)
{
res.end(data);
}else{
getRemote(newhost,url,res,file);
}
});
}else{
getRemote(newhost,url,res,'');
} });
server.listen(port,hostName,function(){
console.log('run');
});

  

Nodejs做整站转发的更多相关文章

  1. nodejs做中间层,转发请求

    简述node中间层的优势 node中间层,可以解决前端的跨域问题,因为服务器端的请求是不涉及跨域的,跨域是浏览器的同源策略导致的,关于跨域可以查看跨域复习使用node坐中间层,方便前后端分离,后端只需 ...

  2. Linux系统下利用wget命令把整站下载做镜像网站

    Linux系统下利用wget命令把整站下载做镜像网站 2011-05-28 18:13:01 | 1次阅读 | 评论:0 条 | itokit  在linux下完整的用wget命令整站采集网站做镜像 ...

  3. 前端资源多个产品整站一键打包&包版本管理(一)

    来新公司工作的第五个月.整站资源打包管理也提上了日程. 问题: 首先.什么是整站的打包管理呢? 我们公司的几个重要产品都在同一个webapp里面,但是,不同的开发部门独立开发不同的产品,长期以来,我们 ...

  4. 301跳转:IIS服务器网站整站301永久重定向设置方法(阿里云)

    欢迎来到重庆SEO俱乐部:搜索引擎优化学习交流QQ群224306761. 承接:seo优化.网站建设.论坛搭建.博客制作.全网营销 博主可接:百度百家.今日头条.一点资讯等软文发布,有需要请联系PE! ...

  5. 前端跨域问题的总结&&nodejs 中间层的路由转发

    前后端交互的时候,跨域是避不开的问题. 总结就是如下: 1.Cors 我在做前后端分离的时候,会采用cors 的方法:便于其他源的调用接口,这个可以设置成任意的源头,也可以允许指定的源头. 下面的是n ...

  6. WordPress整站轻松开启HTTPS

    近两年来HTTPS取代HTTP已经成为大势所趋.早在2014年google Chromium安全团队提议将所有的HTTP协议网站标注为不安全.现在,Chrome浏览器已经开始执行这一标准了.从 Chr ...

  7. wordpress整站无损搬迁的几种方法 最后一种最完美

    网站建设之wordpress整站无损搬迁的几种方法 最后一种最完美 网站搬家,当我们更换php虚拟主机,空间升级或更好空间提供商都会发生,站长们请注意,掌握网站迁移方法,是网站日常维护技术中必须掌握的 ...

  8. 【整站源码分享】分享一个JFinal3.4开发的整站源码,适合新手学习

    分享这个源码是14年开发上线的<威海创业者>站点的全套整站源码,前后端都在一个包里.当时开发使用的是JFinal1.4,最近改成了JFinal3.4.使用的JSP做的页面.有一定的参考价值 ...

  9. 前端到后台ThinkPHP开发整站--php开发案例

    前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...

随机推荐

  1. TOJ 1258 Very Simple Counting

    Description Let f(n) be the number of factors of integer n. Your task is to count the number of i(1 ...

  2. Unity3d Attribute 总结

    举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会在C ...

  3. 02.List泛型集合

    List泛型可以转换成数组 List泛型和数组的相同点: List泛型的数据类型必须是指定的,数组的数据类型也必须是指定的. List泛型和数组的不同点: List泛型的长度是随意的,而数组的长度必须 ...

  4. Spring学习笔记:Spring概述,第一个IoC依赖注入案例

    一.Spring的优点 企业及系统: 1.大规模:用户数量多.数据规模大.功能众多 2.性能和安全要求高 3.业务复杂 4.灵活应变 Java技术:高入侵式依赖EJB技术框架-->Spring框 ...

  5. time&datetime模块

    在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个. UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间, ...

  6. vue-cli脚手架和webpack-simple模板项目

    vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. GitHub地址是:https://github.com/vuejs/vue-cli 一. ...

  7. SQLAlchemy的使用---M2M增删改查

    from sqlalchemy.orm import sessionmaker from sqlalchemy_M2M import engine, Girls, Boys Session = ses ...

  8. react里面stateless函数的默认参数

    function fn({  children,  params,  dispatch,  location}) { }

  9. PopupWindow简单使用

    如图是效果图 当点击 “点我”的按钮是   会弹出 如图的 弹窗 补充为PopupWindow设置一个显示动画和消失的动画 先在anim的文件下分别设置显示和消失的动画 <?xml versio ...

  10. linux 安装和卸载软件

    安装: apt-get install cups-pdf 卸载:apt -get remove cups