forked from skx/node-reverse-proxy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wildcard-hosts.js
124 lines (111 loc) · 3.58 KB
/
wildcard-hosts.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
/**
* This configuration file is a little more advanced than the previous
* ones, and relies upon having wildcard DNS-entries present.
*
* The basic intention is that a request to:
*
* http://"project".repository.steve.org.uk/
*
* Will :
*
* a. Succeed.
*
* b. Present the hgwebdir interface to the specific project named in the hostname.
*
* This is a pretty basic operation, but it requires that we modify
* the incoming "Host:" header which was submitted by the client,
* and also allow some files to be passed through unmolested:
*
* /favicon.ico
* /robots.txt
*
* The configuration presented here does precisely that.
*
*/
exports.options = {
/**
* rewrites for dynamic domains.
*
* This rule matches any hostname with a "repository.steve.org.uk"
* suffix - because these keys are regular expressions, not literal
* hostnames.
*
*/
'([^.]*).repository.steve.org.uk': {
/**
* Rewrites for static files - these will be handled via a
* separate virtual host.
*/
'rules': {
'^/robots.txt': 'http://repository.steve.org.uk/robots.txt',
'^/favicon.ico': 'http://repository.steve.org.uk/favicon.ico'
},
/**
* This function will be invoked for each requested file,
* as it matches against "/" - which all requests will be
* prefixed with.
*
* Again the key here "/" is a regular expression; albeit a permissive
* one.
*
*/
'functions': {
'/': (function(orig_host, vhost, req, res)
{
/**
* Get the requested hostname.
*/
var hostRE = new RegExp("^([^.]+)\.repository\.steve.\org\.uk");
var hostMatch = hostRE.exec(orig_host);
if (hostMatch != null)
{
/**
* If that worked and we update the path
*/
req.url = "/cgi-bin/hgwebdir.cgi/" + hostMatch[1] + req.url;
/**
* And rewrite to the static host
*/
res.writeHead(301, {
'Location': "http://repository.steve.org.uk" + req.url
});
res.end();
/**
* We return here because we've handled the result,
* (issuing a redirect) and that means that we don't
* need the server to proxy the request for us.
*/
return true;
}
/**
* If we reach here then we haven't carried out our
* mission, and the request should be proxied.
*
* However that isn't actually possible - we'll
* not be invoked if our hostname doesn't match the
* (simplified) regexp: *.repository.steve.org.uk, and
* similarly every URL request is going to match "/".
*
* So this is a "can't happen" situation.
*
*/
return false;
})
}
},
/**
* The static host: proxy all requests to the localhost:1018 server.
*/
'repository.steve.org.uk': {
'port': '1018',
'host': 'localhost'
}
};
/**
* The port we listen upon.
*/
exports.port = 8080
/**
* The addresses we will listen upon.
*/
exports.bind = new Array("127.0.0.1", "::1")