...
|
...
|
@@ -4,23 +4,69 @@ const Controller = require('egg').Controller; |
4
|
4
|
class ProxyController extends Controller {
|
5
|
5
|
async info() {
|
6
|
6
|
const { ctx } = this;
|
|
7
|
+ const { params } = ctx;
|
|
8
|
+
|
7
|
9
|
ctx.body = {
|
8
|
|
- name: `hello ${ctx.params.id}`,
|
|
10
|
+ name: `hello ${params.id}`,
|
9
|
11
|
};
|
10
|
12
|
}
|
11
|
13
|
|
|
14
|
+ addToken(options) {
|
|
15
|
+ const { ctx } = this;
|
|
16
|
+ const { session, headers, ip } = ctx;
|
|
17
|
+
|
|
18
|
+ if (session.passport && session.passport.user && session.passport.user && session.passport.user.token) {
|
|
19
|
+ options.headers['Authorization'] = "Bearer " + session.passport.user.token;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ if (headers['device-id']) {
|
|
23
|
+ options.headers['Device-Id'] = headers['device-id'];
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ if (ip) {
|
|
27
|
+ options.headers['Client-Ip'] = ip;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ return options;
|
|
31
|
+ }
|
12
|
32
|
|
13
|
|
- getOptions() {
|
14
|
|
- const { ctx, config } = this;
|
15
|
|
- const { url, type } = ctx;
|
|
33
|
+ splitUrl(fullUrl) {
|
|
34
|
+ const { config } = this;
|
|
35
|
+ const { prefix } = config;
|
16
|
36
|
|
17
|
|
- let catalog = url.substring(1, url.indexOf('/', 1));
|
18
|
|
- let pathUrl = url.substring(url.indexOf('/', 1));
|
19
|
|
- let fullPath = '/' + catalog + config.restful.version + pathUrl;
|
|
37
|
+ if (prefix && prefix.length > 1) {
|
|
38
|
+ fullUrl = fullUrl.substring(prefix.length - 1);
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ return fullUrl.substring(4);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ addParams(options) {
|
|
45
|
+ const { ctx } = this;
|
|
46
|
+ const postData = !ctx.body ? '' : JSON.stringify(ctx.body || {});
|
|
47
|
+
|
|
48
|
+ options.params = postData;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ addContentLength(options) {
|
|
52
|
+ const { params, headers } = options;
|
|
53
|
+
|
|
54
|
+ if (params) {
|
|
55
|
+ headers['Content-Length'] = params.length;
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ getOptions(url, type) {
|
|
60
|
+ const { config } = this;
|
|
61
|
+ const { restful = {} } = config;
|
|
62
|
+ const { version, host, port } = restful;
|
|
63
|
+ const catalog = url.substring(1, url.indexOf('/', 1));
|
|
64
|
+ const pathUrl = url.substring(url.indexOf('/', 1));
|
|
65
|
+ const fullPath = '/' + catalog + version + pathUrl;
|
20
|
66
|
|
21
|
67
|
return {
|
22
|
|
- 'host': config.restful.host, //后台请求地址
|
23
|
|
- 'port': config.restful.port,
|
|
68
|
+ 'host': host, //后台请求地址
|
|
69
|
+ 'port': port,
|
24
|
70
|
'path': fullPath,
|
25
|
71
|
'method': type,
|
26
|
72
|
'agent': false,
|
...
|
...
|
@@ -32,69 +78,218 @@ class ProxyController extends Controller { |
32
|
78
|
};
|
33
|
79
|
}
|
34
|
80
|
|
35
|
|
- addToken(options) {
|
36
|
|
- const { ctx } = this;
|
|
81
|
+ async get() {
|
|
82
|
+ const { ctx, config, logger } = this;
|
|
83
|
+ const { originalUrl, session, curl } = ctx;
|
|
84
|
+ const { restful = {} } = config;
|
|
85
|
+ const { host } = restful;
|
|
86
|
+ const { access_token } = session.user_info;
|
|
87
|
+ const url = splitUrl(originalUrl);
|
|
88
|
+ const options = getOptions(url, 'GET');
|
37
|
89
|
|
38
|
|
- if (ctx.session.passport && ctx.session.passport.user && ctx.session.passport.user && ctx.session.passport.user.token) {
|
39
|
|
- options.headers['Authorization'] = "Bearer " + ctx.session.passport.user.token;
|
40
|
|
- }
|
|
90
|
+ addToken(options);
|
41
|
91
|
|
42
|
|
- if (ctx.headers['device-id']) {
|
43
|
|
- options.headers['Device-Id'] = ctx.headers['device-id'];
|
44
|
|
- }
|
|
92
|
+ const result = await curl(
|
|
93
|
+ `${host}${url}`,
|
|
94
|
+ {
|
|
95
|
+ method: 'GET',
|
|
96
|
+ dataType: 'json',
|
|
97
|
+ headers: {
|
|
98
|
+ 'authorization': `Bearer ${access_token}`,
|
|
99
|
+ 'accept': 'application/json',
|
|
100
|
+ 'content-type': 'application/json'
|
|
101
|
+ },
|
|
102
|
+ timeout: [5000, 60000]
|
|
103
|
+ }
|
|
104
|
+ );
|
45
|
105
|
|
46
|
|
- if (ctx.ip) {
|
47
|
|
- options.headers['Client-Ip'] = ctx.ip;
|
48
|
|
- }
|
|
106
|
+ logger.info(
|
|
107
|
+ "get originalUrl:",
|
|
108
|
+ `${splitUrl(originalUrl)}`,
|
|
109
|
+ "get host:",
|
|
110
|
+ `${host}`,
|
|
111
|
+ "get url:",
|
|
112
|
+ `${url}`,
|
|
113
|
+ "result:",
|
|
114
|
+ result
|
|
115
|
+ );
|
49
|
116
|
|
50
|
|
- return options;
|
|
117
|
+ ctx.body = result.data || {};
|
|
118
|
+
|
|
119
|
+ return ctx.body;
|
51
|
120
|
}
|
52
|
121
|
|
53
|
122
|
|
54
|
|
- splitUrl(fullUrl) {
|
55
|
|
- const { config } = this;
|
|
123
|
+ async post() {
|
|
124
|
+ const { ctx, config, logger } = this;
|
|
125
|
+ const { originalUrl, session, curl, request } = ctx;
|
|
126
|
+ const { restful = {} } = config;
|
|
127
|
+ const { host } = restful;
|
|
128
|
+ const { access_token } = session.user_info
|
|
129
|
+ const url = splitUrl(originalUrl);
|
|
130
|
+ const options = getOptions(url, 'POST');
|
56
|
131
|
|
57
|
|
- if (config.prefix && config.prefix.length > 1) {
|
58
|
|
- fullUrl = fullUrl.substring(config.prefix.length - 1);
|
59
|
|
- }
|
|
132
|
+ addToken(options);
|
|
133
|
+ addParams(options);
|
60
|
134
|
|
61
|
|
- return fullUrl.substring(4);
|
|
135
|
+ const result = await curl(
|
|
136
|
+ `${host}${url}`,
|
|
137
|
+ {
|
|
138
|
+ method: 'POST',
|
|
139
|
+ dataType: 'json',
|
|
140
|
+ data: JSON.stringify(request.body),
|
|
141
|
+ headers: {
|
|
142
|
+ 'authorization': `Bearer ${access_token}`,
|
|
143
|
+ 'accept': 'application/json',
|
|
144
|
+ 'content-type': 'application/json'
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ );
|
|
148
|
+
|
|
149
|
+ logger.info(
|
|
150
|
+ "post proxy url:",
|
|
151
|
+ `${host}${url}`,
|
|
152
|
+ 'headers',
|
|
153
|
+ {
|
|
154
|
+ 'authorization': `Bearer ${access_token}`,
|
|
155
|
+ 'accept': 'application/json',
|
|
156
|
+ 'content-type': 'application/json'
|
|
157
|
+ },
|
|
158
|
+ 'data',
|
|
159
|
+ JSON.stringify(request.body)
|
|
160
|
+ );
|
|
161
|
+
|
|
162
|
+ ctx.body = result.data || {};
|
|
163
|
+
|
|
164
|
+ return ctx.body;
|
62
|
165
|
}
|
63
|
166
|
|
|
167
|
+ async put() {
|
|
168
|
+ const { ctx, config, logger } = this;
|
|
169
|
+ const { originalUrl, session, curl, request } = ctx;
|
|
170
|
+ const { hroProxy = {} } = config;
|
|
171
|
+ const { host } = hroProxy;
|
|
172
|
+ const { access_token } = session.user_info;
|
|
173
|
+ const url = splitUrl(originalUrl);
|
|
174
|
+ const options = getOptions(url, 'PUT');
|
64
|
175
|
|
65
|
|
- async get() {
|
|
176
|
+ addToken(options);
|
|
177
|
+ addParams(options);
|
|
178
|
+
|
|
179
|
+ const result = await curl(
|
|
180
|
+ `${host}${url}`,
|
|
181
|
+ {
|
|
182
|
+ method: 'PUT',
|
|
183
|
+ dataType: 'json',
|
|
184
|
+ data: JSON.stringify(request.body),
|
|
185
|
+ headers: {
|
|
186
|
+ 'authorization': `Bearer ${access_token}`,
|
|
187
|
+ 'accept': 'application/json',
|
|
188
|
+ 'content-type': 'application/json'
|
|
189
|
+ }
|
|
190
|
+ }
|
|
191
|
+ );
|
|
192
|
+
|
|
193
|
+ logger.info(
|
|
194
|
+ "put proxy url:",
|
|
195
|
+ `${host}${url}`,
|
|
196
|
+ 'headers',
|
|
197
|
+ {
|
|
198
|
+ 'authorization': `Bearer ${access_token}`,
|
|
199
|
+ 'accept': 'application/json',
|
|
200
|
+ 'content-type': 'application/json'
|
|
201
|
+ },
|
|
202
|
+ 'data',
|
|
203
|
+ JSON.stringify(request.body)
|
|
204
|
+ );
|
|
205
|
+
|
|
206
|
+ ctx.body = result.data || {};
|
|
207
|
+
|
|
208
|
+ return ctx.body;
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ async head() {
|
66
|
212
|
const { ctx, config, logger } = this;
|
67
|
|
- const { originalUrl } = ctx;
|
68
|
|
- const { restful = {} } = config;
|
69
|
|
- const { host } = restful;
|
70
|
|
- const { access_token } = ctx.session.user_info;
|
71
|
|
- const url = originalUrl.substring(4);
|
|
213
|
+ const { originalUrl, session, curl, request } = ctx;
|
|
214
|
+ const { hroProxy = {} } = config;
|
|
215
|
+ const { host } = hroProxy;
|
|
216
|
+ const { access_token } = session.user_info;
|
|
217
|
+ const url = splitUrl(originalUrl);
|
|
218
|
+ const options = getOptions(url, 'HEAD');
|
72
|
219
|
|
73
|
|
- const result = await ctx.curl(`${host}${url}`, {
|
74
|
|
- method: 'GET',
|
75
|
|
- dataType: 'json',
|
76
|
|
- headers: {
|
|
220
|
+ addToken(options);
|
|
221
|
+ addParams(options);
|
|
222
|
+
|
|
223
|
+ const result = await curl(
|
|
224
|
+ `${host}${url}`,
|
|
225
|
+ {
|
|
226
|
+ method: 'HEAD',
|
|
227
|
+ dataType: 'json',
|
|
228
|
+ data: JSON.stringify(request.body),
|
|
229
|
+ headers: {
|
|
230
|
+ 'authorization': `Bearer ${access_token}`,
|
|
231
|
+ 'accept': 'application/json',
|
|
232
|
+ 'content-type': 'application/json'
|
|
233
|
+ }
|
|
234
|
+ }
|
|
235
|
+ );
|
|
236
|
+
|
|
237
|
+ logger.info(
|
|
238
|
+ "head proxy url:",
|
|
239
|
+ `${host}${url}`,
|
|
240
|
+ 'headers',
|
|
241
|
+ {
|
77
|
242
|
'authorization': `Bearer ${access_token}`,
|
78
|
243
|
'accept': 'application/json',
|
79
|
244
|
'content-type': 'application/json'
|
80
|
245
|
},
|
81
|
|
- timeout: [5000, 60000]
|
82
|
|
- });
|
|
246
|
+ 'data',
|
|
247
|
+ JSON.stringify(request.body)
|
|
248
|
+ );
|
|
249
|
+
|
|
250
|
+ ctx.body = result.data || {};
|
|
251
|
+
|
|
252
|
+ return ctx.body;
|
|
253
|
+ }
|
|
254
|
+
|
|
255
|
+ async del() {
|
|
256
|
+ const { ctx, config, logger } = this;
|
|
257
|
+ const { originalUrl, session, curl } = ctx;
|
|
258
|
+ const { hroProxy = {} } = config;
|
|
259
|
+ const { host } = hroProxy;
|
|
260
|
+ const { access_token } = session.user_info
|
|
261
|
+ const url = splitUrl(originalUrl);
|
|
262
|
+ const options = getOptions(url, 'DELETE');
|
|
263
|
+
|
|
264
|
+ addToken(options);
|
|
265
|
+ addParams(options);
|
|
266
|
+ addContentLength(options);
|
|
267
|
+
|
|
268
|
+ const result = await curl(
|
|
269
|
+ `${host}${url}`,
|
|
270
|
+ {
|
|
271
|
+ method: 'DELETE',
|
|
272
|
+ dataType: 'json',
|
|
273
|
+ headers: {
|
|
274
|
+ 'authorization': `Bearer ${access_token}`,
|
|
275
|
+ 'accept': 'application/json',
|
|
276
|
+ 'content-type': 'application/json'
|
|
277
|
+ }
|
|
278
|
+ }
|
|
279
|
+ );
|
83
|
280
|
|
84
|
281
|
logger.info(
|
85
|
|
- "proxy url:",
|
|
282
|
+ "delete proxy url:",
|
86
|
283
|
`${host}${url}`,
|
87
|
284
|
'headers:',
|
88
|
285
|
{
|
89
|
286
|
'authorization': `Bearer ${access_token}`,
|
90
|
287
|
'accept': 'application/json',
|
91
|
288
|
'content-type': 'application/json'
|
92
|
|
- },
|
93
|
|
- "result:",
|
94
|
|
- result
|
|
289
|
+ }
|
95
|
290
|
);
|
96
|
291
|
|
97
|
|
- ctx.body = result.data;
|
|
292
|
+ ctx.body = result.data || {};
|
98
|
293
|
|
99
|
294
|
return ctx.body;
|
100
|
295
|
}
|
...
|
...
|
|