summaryrefslogtreecommitdiff
path: root/webserver.js
blob: 8335dfd7a40c3b443c745de7e19a3e2d12bca00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
var cmn=require("./modules/$common.js");
var app=require("express")(),
    http=require("http"),
    httpServer=http.Server(app),
    io=require("socket.io")(httpServer),
    url=require("url"),
    fs=require("fs"),
    util=require("util"),
    node_path=require("path"),
    bodyParser=require("body-parser"),
    basicAuth=require("basic-auth"),
    Logger=require("./logger");


if(process.argv.length>3){
	console.log("Pass optional port as first argument");
	process.exit(1);
}
var PORT=process.argv.length==3?+process.argv[2]:80;


// var reqlogger=new Logger("request_log.txt");


var whatpulse={"keys":"<??>","clicks":"<??>"};
function refreshWhatpulse(){
	http.get("http://api.whatpulse.org/user.php?user=tomsmeding&format=json&formatted=yes",function(res){
		var body="";
		res.on("data",function(data){body+=data;});
		res.on("end",function(){
			try{body=JSON.parse(body);}
			catch(e){return;}
			whatpulse.keys=body.Keys/*.replace(/,/g,"&nbsp;")*/;
			whatpulse.clicks=body.Clicks/*.replace(/,/g,"&nbsp;")*/;
		});
	});
}
setInterval(refreshWhatpulse,6*3600*1000); //every 6 hours
refreshWhatpulse();

// function simpleLogEscape(str){
// 	return str
// 			.replace(/\\/g,"\\\\")
// 			.replace(/\n/g,"\\n")
// 			.replace(/ /g,"\\_");
// }

// app.use(function(req,res,next){
// 	var line=simpleLogEscape(req.ip)+" "+
// 	         simpleLogEscape(req.method)+" "+
// 	         simpleLogEscape(req.hostname)+" "+
// 	         simpleLogEscape(req.url);
// 	if(req.headers["dnt"])line+=" dnt="+simpleLogEscape(req.headers["dnt"]);
// 	if(req.headers["referer"])line+=" referer="+simpleLogEscape(req.headers["referer"]);
// 	reqlogger.log(line);
// 	next();
// });

/*app.use(function (req, res, next) {
	console.log(req.subdomains);
	next();
});*/

app.use(bodyParser.text());


var module_list=fs.readdirSync("modules").filter(function(f){
	return ["$common.js",".DS_Store"].indexOf(f)==-1&&f[0]!="$";
});
for(i=0;i<module_list.length;i++){
	require("./modules/"+module_list[i]+"/"+module_list[i]+".js")(app,io,cmn.serverdir+"/modules/"+module_list[i]);
	console.log("Loaded module "+module_list[i]);
}


function requestFile(req,res,path){
	fname=cmn.rootdir+"/web_files"+path;
	console.log("Requesting file "+fname);
	if(!fs.existsSync(fname)){
		res.status(404).send("That file does not exist.");
		return;
	}
	var stats=fs.statSync(fname);
	if(stats.isFile()){
		res.sendFile(fname);
	} else if(req.authuser!=null&&stats.isDirectory()){
		var items=fs
			.readdirSync(fname)
			.filter(function(f){return f[0]!=".";})
			.map(function(f){
				try {
					if(fs.statSync(node_path.join(fname,f)).isDirectory())return f+"/";
					else return f;
				} catch(e){
					return null;
				}
			})
			.filter(function(f){
				return f!==null;
			});
		res.send(
			String(fs.readFileSync(cmn.rootdir+"/web_files/dirlisting.html"))
			.replace("<!--DIRDIR-->",path)
			.replace("[/*LISTINGLISTING*/]",JSON.stringify(items))
		);
	} else {
		res.status(404).send("That file does not exist.");
		return;
	}
}

function makeUrlSafe(req,sliceLength){
	var parsed=url.parse(req.url);
	var part=parsed.pathname.slice(sliceLength);
	if(part=="")return "/";
	return part.replace(/\/\.+[^\/]*\//g,"/");
}



app.get("/",function(req,res){
	res.send(
		String(fs.readFileSync(cmn.serverdir+"/index.html"))
		.replace(/<!--<<PULSE-KEYS>>-->/,whatpulse["keys"])
		.replace(/<!--<<PULSE-CLICKS>>-->/,whatpulse["clicks"])
	);
});

app.get(["/f/univq","/f/univq/*"],cmn.authgen());

app.get(["/f","/f/*"],function(req,res){
	requestFile(req,res,unescape(makeUrlSafe(req,2)));
});

app.get(["/ff","/ff/*"],cmn.authgen());
app.get(["/ff","/ff/*"],function(req,res){
	requestFile(req,res,unescape(makeUrlSafe(req,3)));
});

app.get("/.well-known/*",function(req,res){
	requestFile(req,res,"/well-known"+unescape(makeUrlSafe(req,12)));
});

app.get("/google*.html",function(req,res){
	var url=unescape(makeUrlSafe(req,0));
	if(url.match(/^\/google[0-9a-fA-F]*\.html$/)){
		res.sendFile(__dirname+url);
	} else {
		res.status(404).send("That file does not exist.");
	}
});

["o","k","rip","rip2"].forEach(function(target){
	app.get("/"+target,function(req,res){
		res.sendFile(cmn.rootdir+"/web_files/"+target+".html");
	});
	app.get("/"+target+"/*",function(req,res){
		res.set('Content-Type', 'text/html');
		res.send(
			String(fs.readFileSync(cmn.rootdir+"/web_files/"+target+".html"))
			.replace("<!--MESSAGEMESSAGE-->",cmn.simpleHTMLescape(url.parse(req.url).pathname.slice(target.length+2)))
		);
	});
});

app.get("/dr",function(req,res){
	res.sendFile(cmn.rootdir+"/web_files/duckroll.html");
});

app.get(["/gpg","/pgp","/gpg.asc","/pgp.asc"],function(req,res){
	res.type("text/plain");
	res.sendFile(cmn.rootdir+"/web_files/pgp.asc");
});

app.get("/tomsg",function(req,res){
	res.sendFile(cmn.rootdir+"/web_files/tomsg.html");
});

app.get("/goioi",function(req,res){
	res.sendFile(cmn.rootdir+"/web_files/goioi.html");
});


/*app.get("/chat",function(req,res){
	res.redirect(301,"http://tomsmeding.com:81");
});*/


var server=httpServer.listen(PORT,function(){
	var host=server.address().address;
	var port=server.address().port;
	console.log("Server listening at http://"+host+":"+port);
});