This repository was archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
152 lines (142 loc) · 4.58 KB
/
index.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
const got = require('got')
const express = require('express')
const app = express()
app.disable('etag')
app.get('/', (request, response) => {
response.status(200).json({
'message': "Welcome to Twitch API"
})
})
app.get('/hls', (request, response) => {
response.status(404).json({
'message': 'unknown channel name'
})
})
app.get('/hls/:id', async (request, response) => {
try {
let id = request.params.id
let token = await got(`https://gql.twitch.tv/gql`, {
method: 'POST',
responseType: 'json',
retry: {
limit: 4
},
throwHttpErrors: false,
headers: {
'Client-ID': 'kimne78kx3ncx6brgo4mv6wki5h1ko',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
'X-Device-Id': 'twitch-web-wall-mason',
'Device-ID': 'twitch-web-wall-mason'
},
body: JSON.stringify({
"operationName": "PlaybackAccessToken",
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "0828119ded1c13477966434e15800ff57ddacf13ba1911c129dc2200705b0712"
}
},
"variables": {
"isLive": true,
"login": id,
"isVod": false,
"vodID": "",
"playerType": "embed"
}
})
})
switch (token.statusCode) {
default: //Error with connect with Twitch API
response.status(500).json({
'message': 'Error with Twitch API'
})
break
case 200: //Channel founded
if (token.body.data.streamPlaybackAccessToken === null) { //Channel not found
response.status(404).json({
'message': 'Channel not found'
})
} else {
function base64Encode(data) {
return Buffer.from(data).toString('base64')
}
function cleanupAllAdStuff(data) {
return data
.replace(/X-TV-TWITCH-AD-URL="[^"]+"/g, 'X-TV-TWITCH-AD-URL="javascript:alert(\'pogo\')"')
.replace(
/X-TV-TWITCH-AD-CLICK-TRACKING-URL="[^"]+"/g,
'X-TV-TWITCH-AD-CLICK-TRACKING-URL="javascript:alert(\'pogo\')"'
)
.replace(/X-TV-TWITCH-AD-ADVERIFICATIONS="[^"]+"/g, `X-TV-TWITCH-AD-ADVERIFICATIONS="${base64Encode('{}')}"`)
.replace(/#EXT-X-DATERANGE.+CLASS=".*ad.*".+\n/g, '')
.replace(/\n#EXTINF.+(?<!live)\nhttps:.+/g, '');
}
let url = `http://usher.twitch.tv/api/channel/hls/${id}.m3u8?player=twitchweb&&token=${token.body.data.streamPlaybackAccessToken.value}&sig=${token.body.data.streamPlaybackAccessToken.signature}&allow_audio_only=true&allow_source=true&type=any&p=${parseInt(Math.random() * 999999)}`
let hls = await got(url, {
method: 'GET',
responseType: 'text',
retry: {
limit: 4
},
throwHttpErrors: false,
headers: {
'X-Device-Id': 'twitch-web-wall-mason',
'Device-ID': 'twitch-web-wall-mason'
}
})
switch (hls.statusCode) {
default: //m3u8 data doesn't exsit
response.status(404).json({
'message': 'm3u8 data not found'
})
break
case 200: //m3u8 data exist
hls = cleanupAllAdStuff(hls.body)
hls = hls.replace(/.*#.*\n?/gm, '')
response.status(200).json(hls.split('\n'))
break
}
}
}
} catch (error) {
response.status(500).json({
message: 'processing error',
error: error
})
}
})
app.get('/proxy/*', async (request, response) => {
try {
const url = request.params['0'] + '?' + Object.entries(request.query).map(element => element.join('=')).join('&')
const domain = (new URL(url)).hostname
if (domain === 'usher.ttvnw.net') {
const headers = {
'host': domain,
'user-agent': request.headers['user-agent']
}
let hls = await got(url, {
method: 'GET',
responseType: 'text',
retry: {
limit: 4
},
throwHttpErrors: false,
headers: headers
})
response.status(hls.statusCode).send(hls.body)
} else {
response.status(403).json({
message: 'URL not allowed'
})
}
} catch (error) {
response.status(500).json({
message: 'processing error',
error: error
})
}
})
app.listen(8080, () => {
console.log("API started")
})