asdass1 commited on
Commit
e565f01
·
verified ·
1 Parent(s): df2fc56

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +21 -0
  2. crc32.js +41 -0
  3. index.js +146 -0
  4. package.json +14 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Menggunakan image Node.js yang lebih stabil
2
+ FROM node:16-slim
3
+
4
+
5
+ # Tentukan direktori kerja di dalam container
6
+ WORKDIR /app
7
+
8
+ # Salin file package.json dan package-lock.json terlebih dahulu
9
+ COPY package*.json ./
10
+
11
+ # Install dependensi Node.js
12
+ RUN npm install
13
+
14
+ # Salin seluruh kode aplikasi ke dalam container
15
+ COPY . .
16
+
17
+ # Ekspose port 3000 yang akan digunakan aplikasi Express
18
+ EXPOSE 3000
19
+
20
+ # Tentukan perintah untuk menjalankan aplikasi Express.js
21
+ CMD ["npm", "start"]
crc32.js ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * 计算CRC32
3
+ * @param {ArrayBuffer} buffer - ArrayBuffer
4
+ */
5
+ export default buffer => {
6
+ buffer = new Uint8Array(buffer)
7
+ const module = {
8
+ polynom: 0xEDB88320,
9
+ initValue: -1,
10
+ bytes: 4,
11
+ table: []
12
+ }
13
+ for (let a = 0; a < 256; ++a) {
14
+ let b = a
15
+ for (let c = 0; c < 8; ++c) {
16
+ b = b & 1 ? module.polynom ^ (b >>> 1) : b >>> 1
17
+ }
18
+ module.table[a] = b >>> 0
19
+ }
20
+ let crc = module.initValue
21
+ let length = buffer.length
22
+ let table = module.table
23
+ for (let a = 0; a < 256; ++a) {
24
+ let b = a
25
+ for (let c = 0; c < 8; ++c) {
26
+ b = b & 1 ? module.polynom ^ (b >>> 1) : b >>> 1
27
+ }
28
+ module.table[a] = b >>> 0
29
+ }
30
+ for (let i = 0; i < length; ++i) {
31
+ crc = table[(crc ^ buffer[i]) & 0xFF] ^ (crc >>> 8)
32
+ }
33
+ crc ^= module.initValue
34
+ let HEX_CHARS = '0123456789abcdef'.split('');
35
+ let hex = '';
36
+ if (module.bytes > 2) {
37
+ hex += HEX_CHARS[(crc >> 28) & 0x0F] + HEX_CHARS[(crc >> 24) & 0x0F] + HEX_CHARS[(crc >> 20) & 0x0F] + HEX_CHARS[(crc >> 16) & 0x0F]
38
+ }
39
+ hex += HEX_CHARS[(crc >> 12) & 0x0F] + HEX_CHARS[(crc >> 8) & 0x0F] + HEX_CHARS[(crc >> 4) & 0x0F] + HEX_CHARS[crc & 0x0F]
40
+ return hex
41
+ }
index.js ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import crc32 from './crc32.js'
2
+ import http from 'axios'
3
+ import express from 'express'
4
+
5
+ const app = express()
6
+ const port = 7860
7
+
8
+ app.use(express.json())
9
+ app.use(express.urlencoded({ extended: true }))
10
+
11
+ const upload_buffer = async data => {
12
+ try {
13
+ const file = Buffer.from(data)
14
+ const hash = crc32(file)
15
+ const upload_info = await http({
16
+ method: 'GET',
17
+ url: 'https://api.xs.cx/upload/sg'
18
+ })
19
+ const upload_result = await http({
20
+ method: 'POST',
21
+ url: upload_info.data.url,
22
+ headers: {
23
+ 'content-crc32': hash,
24
+ 'authorization': upload_info.data.authorization
25
+ },
26
+ data: file
27
+ })
28
+ if (upload_result.status == 200) {
29
+ return {
30
+ vid: upload_info.data.vid,
31
+ url: `https://sf16-sg-default.akamaized.net/obj/${upload_info.data.uri}`
32
+ }
33
+ } else {
34
+ throw Error
35
+ }
36
+ } catch (e) {
37
+ console.log(e.toString())
38
+ }
39
+ }
40
+
41
+ const upload_file = async url => {
42
+ try {
43
+ const file_url = url
44
+ const file = await http({
45
+ method: 'GET',
46
+ url: file_url,
47
+ responseType: 'arraybuffer'
48
+ })
49
+ const hash = crc32(file.data)
50
+ const upload_info = await http({
51
+ method: 'GET',
52
+ url: 'https://api.xs.cx/upload/sg'
53
+ })
54
+ const upload_result = await http({
55
+ method: 'POST',
56
+ url: upload_info.data.url,
57
+ headers: {
58
+ 'content-crc32': hash,
59
+ 'authorization': upload_info.data.authorization
60
+ },
61
+ data: file.data
62
+ })
63
+ if (upload_result.status == 200) {
64
+ return {
65
+ vid: upload_info.data.vid,
66
+ url: `https://sf16-sg-default.akamaized.net/obj/${upload_info.data.uri}`
67
+ }
68
+ } else {
69
+ throw Error
70
+ }
71
+ } catch (e) {
72
+ console.log(e.toString())
73
+ }
74
+ }
75
+
76
+ const m3u8_parse = async url => {
77
+ while (true) {
78
+ try {
79
+ const m3u8_file = await http({
80
+ method: 'GET',
81
+ url: url
82
+ })
83
+ if (m3u8_file.status != 200) {
84
+ console.log(`[m3u8加载失败] ${url}`)
85
+ return
86
+ }
87
+ let text = m3u8_file.data
88
+ const is_m3u8 = m3u8_file.data.match(/\n.*?\.m3u8(\n|$)/gim)?.at(0)
89
+ if (is_m3u8) {
90
+ let new_url
91
+ if (/^\nhttp/i.test(url)) {
92
+ new_url = is_m3u8.replace('\n', '')
93
+ } else {
94
+ new_url = url.replace(url.split('/').pop(), is_m3u8.replace('\n', ''))
95
+ }
96
+ text = m3u8_parse(new_url)
97
+ } else {
98
+ const queue = text.match(/\n.*.(ts|png|jpg|jpeg|gif|webp)/gim)
99
+ while (queue.length) {
100
+ await Promise.all(queue.splice(0, 500).map(async item => {
101
+ let ts_url
102
+ if (/^\nhttp/i.test(item)) {
103
+ ts_url = item.replace('\n', '')
104
+ } else {
105
+ ts_url = url.replace(url.split('/').pop(), item.replace('\n', ''))
106
+ }
107
+ let upload_result
108
+ while (!upload_result) {
109
+ try {
110
+ const result = await upload_file(ts_url)
111
+ if (/^http/i.test(result.url)) {
112
+ upload_result = result.url
113
+ // console.log({
114
+ // a: ts_url,
115
+ // b: upload_result
116
+ // })
117
+ } else {
118
+ console.log(`[上传失败] ${url}`)
119
+ }
120
+ } catch (e) {
121
+ console.log(e, ts_url)
122
+ }
123
+ }
124
+ text = text.replace(item, `\n${upload_result}`);
125
+ }))
126
+ }
127
+ }
128
+ return text
129
+ } catch (e) {
130
+ console.log(e)
131
+ }
132
+ }
133
+ }
134
+
135
+ app.get('*', async (req, res) => {
136
+ try {
137
+ if (/favicon.ico/.test(req.path)) return
138
+ res.send(await upload_buffer(await m3u8_parse(req.path.replace(/^\//, ''))))
139
+ } catch (e) {
140
+ res.send(e.toString())
141
+ }
142
+ })
143
+
144
+ app.listen(port, async () => {
145
+ console.log(`http://localhost:${port}`)
146
+ })
package.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "xx",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node index"
8
+ },
9
+ "dependencies": {
10
+ "axios": "^1.3.4",
11
+ "express": "^4.18.2",
12
+ "form-data": "^4.0.1"
13
+ }
14
+ }