-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalache2tls.js
155 lines (114 loc) · 5.16 KB
/
malache2tls.js
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
"use strict";
/* This simple http server is made by malpower
* for testing web applications of mobile.
* This porgram is based on Node.js which is
* a platform from joyent, inc. It allows user
* building backend service programs with JS.
* Node.js uses Google V8 engine, it means that
* it will be fast.
* You can also share your files on http with
* this simple http server.Conf.js is a configure
* file for this simple server.Anthor named
* malpower who is a web programmer in Chengdu.
* This is the seconds edition of malache, you
* can visit [https://github.com/malpower/Malache2]
* to get the latest code.
* Email: [email protected]
*
========================================================================================================================
*
# |-------------------------------------------------------------|
### | |
## ##### ## | # # |
########### | ### ##### |
# \ \ # | ####### # # |
#\ \ \# | ############### |
# \ \ # | ########### |
#\ \ \# | #### #### # |
# \ \ # | ## ## ##### |
#\ \ \# | # # # # |
# \ \ # | |
#\ \ \# | # # |
|-------|-----#--|\-#-----|------|---| | ##### ##### |
======================================== | # # # # |
========================================== | |
============================================ | |
=============================================== |-------------------------------------------------------------|
* * * ***** ******
* * * * * * * * *
* * * * * * * * ******
* * * ******* * * *
* * * * * ***** ******
***** * *
* * * *
* * * *
* * **
***** * *
*** * * ***** * * *
* * * * * * * * * *
* ****** * * * * * *
* * * * * * ** *******
*** * * ***** * * * *
=======================================================================================================================
*/
const tls=require("tls");
const net=require("net");
const conf=require("./conf");
const http=require("http");
if (conf.https!=true)
{
console.log("Please set [https] section in conf.js!");
process.exit(0);
}
let cp=require("child_process");
cp.fork("./malache2");
let server=tls.createServer(conf.tls,function(socket)
{
let m=net.connect({host: "127.0.0.1",port: conf.port},function()
{
socket.pipe(m);
m.pipe(socket);
});
m.on("error",function()
{
socket.end();
});
socket.on("error",function()
{
try
{
m.end();
}
catch (e)
{
//
}
});
});
server.on("error",function(e)
{
console.log(e);
});
server.listen(conf.httpsPort);
let redirectServer=http.createServer(function(req,res)
{
let domain=req.headers["host"] || req.headers["Host"] || req.headers["HOST"];
if (domain==undefined)
{
res.end();
return;
}
if (domain.indexOf(":")!=-1)
{
domain=domain.split(":")[0];
}
if (conf.httpsPort!=443)
{
domain+=":"+conf.httpsPort;
}
let url=domain+req.url;
res.setHeader("Location","https://"+url);
res.statusCode=302;
res.end();
});
redirectServer.listen(conf.redirPort);