function handleStr(str,isHtml,callback)
{
if(!isHtml)
{
callback(str);
return;
}
str= str.replace(/<script(([\s\S])*?)<\/script>/g,"");
str= str.replace(new RegExp(dstHost,"gm"),srcHost);
callback(str);
}
//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 iconv = require('iconv-lite');
//创建目录
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(host,url,callback)
{
var file=__dirname+'/tmp/'+host.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);
}
});
});
}
//获取远程内容
function getRemote(hostset,url,callback)
{
var body="";
var options = {
hostname: hostset.host,
port: 80,
path:url,
method: 'GET'
};
var req = http.request(options, function (remoteRes) {
remoteRes.setEncoding(hostset.charset);
remoteRes.on('data', function (chunk) {
body+=chunk;
});
remoteRes.on("end",function(){
callback(true,body,remoteRes.headers['content-type'].indexOf("text/html") != -1);
});
});
req.on('error', function (e) {
callback(false,e.message,false);
console.log('problem with request: ' + e.message);
});
req.end();
}
//获取域名
function getDstHost(host,callback)
{
fs.readFile('./hostlist.txt', 'utf8', function(err, hostlist){
hostlist=JSON.parse(hostlist);
if(host in hostlist)
{
callback(hostlist[host]);
}else{
var hostset={"title":"baidu","host":"www.baidu.com"};
callback(hostset);
}
});
}
//获得内容
function getContent(hostset,url,callback)
{
if(!hostset.nocatch&&url!=""&&url!="/")
{
getCatch(hostset.host,url,function(rs,file,data){
if(rs)
{
callback(data);
}else{
//获取远程内容
getRemote(hostset,url,function(rs,body,ishtml){
if(rs&&body!="")
{
handleStr(body,ishtml,function(rs){
if(file!=''){
fs.writeFile(file, rs, {flag: 'a'}, function (err) {
if(err) {
console.error(err);
}
});
}
callback(rs);
});
}else{
callback(body);
}
});
}
});
}else{
//获取远程内容
getRemote(hostset,url,function(rs,body){
handleStr(body,true,function(rs){
callback(rs);
});
});
}
}
//需要保证模板文件存在,这里不判断
var tplview=function(tpl,varData,callback){
fs.readFile(tpl,'utf-8',function(err,data){
if(err)
{
callback("");
return ;
}
for(key in varData)
{
re = new RegExp('{'+key+'}','g');
data = data.replace(re,varData[key]);
}
callback(data.toString());
});
};
var mainObj={};
mainObj.main=function(res,data){
var tpl=__dirname+"/index.tpl";
var varData={};
fs.readFile(__dirname+"/hostlist.txt",'utf-8',function(err,data){
data=JSON.parse(data);
varData.list="";
for(var key in data)
{
varData.list+="<div class='col-md-2'><a href='http://"+key+"'>"+data[key].title+"</a></div>";
}
tplview(tpl,varData,function(str){
res.write(str);
res.end();
});
});
};
mainObj.edit=function(res,data){
var file=__dirname+"/edit.html";
backfile(res,file);
};
mainObj.editsave=function(res,data){
var file=__dirname+"/hostlist.txt";
fs.writeFile(file, data, {}, function (err) {
if(err) {
res.write("保存文件失败");
}else{
res.write("保存成功");
}
res.end();
});
};
mainObj.getlist=function(res,data){
//设置返回值类型
res.setHeader("Content-Type", "application/json;charset=utf-8");
var file=__dirname+"/hostlist.txt";
backfile(res,file);
};
function backfile(res,file)
{
fs.exists(file,function(exists){
if(exists)
{
fs.readFile(file,'utf-8',function(err,data){
if(err){
res.statusCode=404;
res.end(err);
}
else{
res.write(data);
res.end();
}
});
}else{
res.statusCode=404;
res.end();
}
});
}
var httpHandler=function(req,res){
var post="";
//接收post数据
req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
post += chunk;
});
req.on('end', function(){
//只支持定义好的方法,不支持未定义的方法
//处理请求
url=req.url;
//去掉第一个斜杠
url=url.substr(1);
if(url==""||url=="/")url="main";
if(!mainObj.hasOwnProperty(url))
{
res.statusCode=404;
res.end();
return;
}
mainObj[url](res,post);
});
}
//创建服务
var server = http.createServer(function(req,res){
srcHost=req.headers.host;
//如果是指定域名,则特别处理
if(srcHost=="www.steel-pot.com")
{
httpHandler(req,res);
return ;
}
//第一步获取域名
getDstHost(srcHost,function(hostset){
dstHost=hostset.host;
//第二步获取内容
getContent(hostset,req.url,function(body){
if(hostset.charset)body=iconv.encode(body, hostset.charset);
res.end(body);
});
});
});
server.listen(port,hostName,function(){
console.log('run');
});