-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
154 lines (130 loc) · 3.93 KB
/
proxy.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
const https = require('https');
const http = require('http');
const APP_PORT = process.env.PORT || 8080
const DEBUG = true
const requestListener = function (request, response) {
var proxyData = ''
request.on('data', function(chunk) {
proxyData += chunk
});
request.on('end', function() {
if(request.url.endsWith("/date")) {
makeProxyResponse(200, Math.floor(Date.now() / 1000).toString())
return
}
if(!proxyData) {
makeProxyResponse(500, JSON.stringify({
status: "fail",
message: "No required data"
}))
return
}
if(DEBUG) console.log("Before parse")
if(DEBUG) console.log(proxyData)
proxyData = JSON.parse(proxyData)
if(DEBUG) console.log("Parsed")
if(DEBUG) console.log(proxyData)
if(!valid(proxyData)) {
makeProxyResponse(500, JSON.stringify({
status: "fail",
message: "Incorrect data"
}))
} else {
let httpType = checkHttpType(proxyData['url']);
let connector = httpType === 'http' ? http : https
let [hostname, port, path] = getUrlData(proxyData['url'], httpType)
let lowerMethod = proxyData['method'].toLowerCase()
if(lowerMethod === 'get') {
let separator = path.indexOf('?') == -1 ? '?' : ''
if(proxyData['data'] !== '') {
path += separator + parseGetUrl(proxyData['data'])
}
}
proxyData['headers']['Content-Type'] = 'application/json'
let options = {
hostname: hostname,
port: port,
path: path,
method: proxyData['method'],
headers: proxyData['headers'],
}
if(DEBUG) console.log("Options")
if(DEBUG) console.log(options)
if(DEBUG) console.log("Data")
if(DEBUG) console.log(JSON.stringify(proxyData['data']))
const proxyRequest = connector.request(options, destResponse => {
let receivedData = ''
destResponse.on('data', receivedChunk => {
receivedData += receivedChunk
})
destResponse.on('end', function () {
if(DEBUG) console.log("Airella")
if(DEBUG) console.log(receivedData)
makeProxyResponse(destResponse.statusCode, receivedData)
});
})
.on('error', console.error)
.end(JSON.stringify(proxyData['data']))
proxyRequest.on('error', error => {
console.error(error)
})
}
})
response.setHeader('Content-Type', 'application/json')
function makeProxyResponse(code, data) {
response.writeHead(code)
response.end(data)
}
}
const server = http.createServer(requestListener)
server.listen(APP_PORT)
function valid(data) {
let requireProperty = ['method', 'url']
let optionalProperty = ['headers', 'data']
let params = Object.keys(data)
for(let item of requireProperty) {
if(!params.includes(item)) return true;
}
for(let item of optionalProperty) {
if(!params.includes(item)) data[item] = ""
}
return true
}
function parseGetUrl(args) {
let getParams = ''
let paramsCount = Object.keys(args).length
let index = 1
for(let key in args) {
getParams += key + '=' + args[key]
if(index != paramsCount) getParams += '&'
index++
}
return getParams
}
function checkHttpType(url) {
if(url.indexOf('http://') === 0) return 'http'
if(url.indexOf('https://') === 0) return 'https'
return ""
}
function getUrlData(url, httpType) {
let chunk
if(url.indexOf('http') === 0) {
chunk = url.substring(url.indexOf('/') + 2)
}
let hostname = ''
let path = ''
let port = httpType === 'http' ? 80 : 443
let slashPosition = chunk.indexOf('/')
if(slashPosition !== -1) {
hostname = chunk.substring(0, slashPosition)
path = chunk.substring(slashPosition)
let portPosition = hostname.indexOf(':')
if(portPosition !== -1) {
port = hostname.substring(portPosition)
hostname = hostname.substring(0, portPosition)
}
} else {
hostname = chunk
}
return [hostname, port, path]
}