Nodejs做整站转发
刚接触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做整站转发的更多相关文章
- nodejs做中间层,转发请求
简述node中间层的优势 node中间层,可以解决前端的跨域问题,因为服务器端的请求是不涉及跨域的,跨域是浏览器的同源策略导致的,关于跨域可以查看跨域复习使用node坐中间层,方便前后端分离,后端只需 ...
- Linux系统下利用wget命令把整站下载做镜像网站
Linux系统下利用wget命令把整站下载做镜像网站 2011-05-28 18:13:01 | 1次阅读 | 评论:0 条 | itokit 在linux下完整的用wget命令整站采集网站做镜像 ...
- 前端资源多个产品整站一键打包&包版本管理(一)
来新公司工作的第五个月.整站资源打包管理也提上了日程. 问题: 首先.什么是整站的打包管理呢? 我们公司的几个重要产品都在同一个webapp里面,但是,不同的开发部门独立开发不同的产品,长期以来,我们 ...
- 301跳转:IIS服务器网站整站301永久重定向设置方法(阿里云)
欢迎来到重庆SEO俱乐部:搜索引擎优化学习交流QQ群224306761. 承接:seo优化.网站建设.论坛搭建.博客制作.全网营销 博主可接:百度百家.今日头条.一点资讯等软文发布,有需要请联系PE! ...
- 前端跨域问题的总结&&nodejs 中间层的路由转发
前后端交互的时候,跨域是避不开的问题. 总结就是如下: 1.Cors 我在做前后端分离的时候,会采用cors 的方法:便于其他源的调用接口,这个可以设置成任意的源头,也可以允许指定的源头. 下面的是n ...
- WordPress整站轻松开启HTTPS
近两年来HTTPS取代HTTP已经成为大势所趋.早在2014年google Chromium安全团队提议将所有的HTTP协议网站标注为不安全.现在,Chrome浏览器已经开始执行这一标准了.从 Chr ...
- wordpress整站无损搬迁的几种方法 最后一种最完美
网站建设之wordpress整站无损搬迁的几种方法 最后一种最完美 网站搬家,当我们更换php虚拟主机,空间升级或更好空间提供商都会发生,站长们请注意,掌握网站迁移方法,是网站日常维护技术中必须掌握的 ...
- 【整站源码分享】分享一个JFinal3.4开发的整站源码,适合新手学习
分享这个源码是14年开发上线的<威海创业者>站点的全套整站源码,前后端都在一个包里.当时开发使用的是JFinal1.4,最近改成了JFinal3.4.使用的JSP做的页面.有一定的参考价值 ...
- 前端到后台ThinkPHP开发整站--php开发案例
前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...
随机推荐
- sql server 远程备份 bak 删除
前言: 管理一个公司的一个服务器,最近有一些维护SQLserver数据库活弄,写下防止忘了. 因为公司采用SQL\Redis\MongoDB共用,SQL用来存储基础的结构\权限\等一些杂七杂八的东西. ...
- 【转】CentOS6下安装mysql后,重置root密码方法
本文转自:CentOS6下安装mysql后,重置root密码方法 centos下安装mysql,居然不知道root用户密码,本想重装,不过还是先度娘了一些,发现这篇文章,刚好解决我的燃眉之急,太赞了. ...
- [转] CentOS下添加用户并且让用户获得root权限
http://www.centoscn.com/CentOS/config/2014/0810/3471.html 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser ...
- c#-foreach的秘密
foreach的秘密 class Program { static void Main(string[] args) { //创建Person的对象 Person p1=new Person(); / ...
- Html-知识总结
1.Html概述 1.1什么是Html Html:超文本标记语言(hyperText Markup Language) “超文本”加上指页面内可以包含图片,链接等非文字内容. “标记”就是使用标签的方 ...
- C++中 结构体和类的异同
在C++中,结构体是一种特殊形态的类. 结构体和类的唯一区别就是: 结构体和类具有不同的默认访问控制属性. 类中,对于未指定访问控制属性的成员,其访问控制属性为私有类型(private) 结构体中, ...
- jquery获取transform里面的值
用transform的translateX写了一个侧滑效果,如何获取它改变的值是多少呢? 获取translateX值的方法: $('div').css("transform").r ...
- ArcGIS Geodabase OBJECTID重新初始编号的间接方法
ArcGIS Geodabase OBJECTID为Long型,随着Feature的增加和删除自动编号,均为增加.即删除Feature后,再增加该号码自动只增加不重新编号. 间接方法可以实现重新从1编 ...
- Android性能优化之渲染篇
下面是渲染篇章的学习笔记,部分内容和前面的性能优化典范有重合,欢迎大家一起学习交流! 1)Why Rendering Performance Matters 现在有不少App为了达到很华丽的视觉效果, ...
- git clone 指定的单个目录或文件夹
git clone 指定的单个目录或文件夹 针对自己的项目 方法一 基于sparse clone变通方法 创建一个空仓库 拉取远程仓库信息 开启 sparse clone 设置过滤 更新仓库 创建空仓 ...