This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.js
84 lines (77 loc) · 2.18 KB
/
hook.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
const cache = require('./cache')
const parse = require('url').parse
const format = require('url').format
const request = require('./request')
const hook = {
request: {
before: () => {},
after: () => {},
},
connect: {
before: () => {}
},
negotiate: {
before: () => {}
},
target: {
host: [],
path: []
}
}
hook.target.host = [
'app.jike.ruguoapp.com',
'jike-io.jike.ruguoapp.com',
'sensorsdata.ruguoapp.com',
'activity.jike.ruguoapp.com',
'track.jike.ruguoapp.com'
]
hook.target.path = [
'/1.0/interactiveMessages/list'
]
hook.request.before = ctx => {
const req = ctx.req
req.url = (req.url.startsWith('http://') ? '' : (req.socket.encrypted ? 'https:' : 'http:') + '//' + (hook.target.host.includes(req.headers.host) ? req.headers.host : null)) + req.url
const url = parse(req.url)
if(hook.target.host.includes(url.hostname)){
let host = (url.hostname == 'app.jike.ruguoapp.com') ? 'api.jellow.club' : url.hostname.replace('jike.ruguoapp.com', 'jellow.club')
req.headers['host'] = host
req.url = format(Object.assign(url, {host}))
ctx.jike = {path: url.pathname}
if(url.pathname == '/1.0/topics/tabs/square/feed'){
req.url = parse(url).resolve('/1.0/topicFeed/list')
}
}
}
hook.request.after = ctx => {
const jike = ctx.jike
const proxyRes = ctx.proxyRes
if(jike && hook.target.path.includes(jike.path)){
return request.read(proxyRes)
.then(body => proxyRes.body = body)
.then(body => {
if(proxyRes.statusCode == 503 && jike.path == '/1.0/interactiveMessages/list'){
proxyRes.statusCode = 200
proxyRes.body = JSON.stringify({})
proxyRes.headers['content-type'] = 'application/json'
if('content-length' in proxyRes.headers) delete proxyRes.headers['content-length']
}
})
}
}
hook.connect.before = ctx => {
let url = parse('https://' + ctx.req.url)
if([url.hostname, ctx.req.headers.host].some(host => hook.target.host.includes(host))){
if(url.port == 80){
ctx.req.url = `${global.address || 'localhost'}:${global.port[0]}`
ctx.req.local = true
}
else if(global.port[1]){
ctx.req.url = `${global.address || 'localhost'}:${global.port[1]}`
ctx.req.local = true
}
else{
ctx.decision = 'blank'
}
}
}
module.exports = hook