forked from gr2m/CORS-Proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcorsproxy
More file actions
executable file
·120 lines (105 loc) · 2.64 KB
/
corsproxy
File metadata and controls
executable file
·120 lines (105 loc) · 2.64 KB
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
#!/usr/bin/env node
var Hapi = require('hapi')
var plugin = require('../index')
var good = require('good')
var loggerOptions = require('../lib/logger-options')
var server = new Hapi.Server({})
var port = parseInt(process.env.CORSPROXY_PORT || process.env.PORT || 1337, 10)
var host = (process.env.CORSPROXY_HOST || 'localhost');
var maxPayload = parseInt(process.env.CORSPROXY_MAX_PAYLOAD || 1048576, 10)
var proxy = server.connection({ port: port, labels: ['proxy'], host: host})
server.register(require('inert'), function () {})
server.register(require('h2o2'), function () {})
// cors plugin
server.register(plugin, {
select: ['proxy']
}, function (error) {
if (error) server.log('error', error)
})
// logger plugin
server.register({
register: good,
options: loggerOptions
}, function (error) {
if (error) server.log('error', error)
})
// proxy route
proxy.route({
method: '*',
path: '/{host}/{path*}',
handler: {
proxy: {
passThrough: true,
mapUri: function (request, callback) {
request.host = request.params.host
request.path = request.path.substr(request.params.host.length + 1)
request.headers['host'] = request.host
var query = request.url.search ? request.url.search : ''
console.log('proxy to http://' + request.host + request.path)
callback(null, 'http://' + request.host + request.path + query, request.headers)
}
}
},
config: {
payload: {
maxBytes: maxPayload
}
}
})
// default route
proxy.route({
method: 'GET',
path: '/',
handler: {
file: 'public/index.html'
}
})
proxy.route({
method: 'GET',
path: '/favicon.ico',
handler: {
file: 'public/favicon.ico'
}
})
if (process.env.DEBUG) {
var testport = port + 1
var test = server.connection({ port: testport, labels: ['test'], host: host })
server.register(require('vision'), function (error) {
if (error) {
throw error
}
server.views({
engines: { ejs: require('ejs') },
path: 'public/test'
})
})
test.route({
method: 'GET',
path: '/favicon.ico',
handler: {
file: 'public/favicon.ico'
}
})
test.route({
method: 'GET',
path: '/test.json',
handler: {
file: 'public/test/test.json'
}
})
test.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.view('index', {
proxyPort: proxy.info.port,
testPort: test.info.port
})
}
})
server.log('info', 'Debug server starting at: ' + test.info.uri)
}
server.start(function (error) {
if (error) server.log('error', error)
server.log('info', 'CORS Proxy running at: ' + proxy.info.uri)
})