-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
178 lines (160 loc) · 5.76 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var steem = require('steem')
steem.api.setOptions({ url: 'https://api.steemit.com' });
var mysql = require('mysql')
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
var getJSON = require('get-json')
var streamsFolder = process.env.STREAMS_PATH || '/mnt/streams'
var bodyParser = require('body-parser');
app.use(bodyParser.text({ type: 'text/plain' }))
var sql = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'dtube'
});
sql.connect();
clearUnverifiedKeys()
app.get('/getStreams', function (req, res) {
var command = {
active_streams: [
"clients",
"lastms"
]
}
getJSON('http://localhost:4242/api?command='+JSON.stringify(command), function(err, jRes) {
if (!jRes || !jRes.active_streams) res.send([])
else res.send(jRes.active_streams)
})
})
app.get('/resetKey/:username', function (req, res) {
keyLine = {
username: req.params.username,
streamKey: createKey(16),
verifKey: createKey(16)
}
console.log(keyLine.username+' requested a new key')
var query = mysql.format('INSERT INTO streamKeys SET ?', keyLine)
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
res.send(keyLine)
})
})
app.get('/verify/:block/:tx_num', function (req, res) {
console.log('Trying to find verification in block '+req.params.block)
steem.api.getBlock(req.params.block, function(err, sRes) {
var tx = sRes.transactions[req.params.tx_num]
var op = tx.operations[0]
if (op[0] !== "custom_json") {
res.send('Nope')
return
}
if (op[1].id != "dtubeStreamVerif") {
res.send('Nope')
return
}
try {
var json = JSON.parse(op[1].json)
} catch (error) {
res.send('Nope')
return
}
var query = 'SELECT * FROM streamKeys WHERE verifKey="'
+json.key
+'" AND username="'
+op[1].required_posting_auths[0]
+'" AND verified=0'
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
if (qRes.length < 1) {
res.send('Couldnt find matching stream key')
return
}
console.log('Verified stream key of '+op[1].required_posting_auths[0])
var query = 'UPDATE streamKeys SET verified=1 WHERE verifKey="'+json.key+'"'
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
var query = 'UPDATE streamKeys SET verified=0 WHERE username = "'+op[1].required_posting_auths[0]+'" AND verifKey != "'+json.key+'"'
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
res.send('Ok')
})
})
})
})
})
app.get('/oldStream/:username/:datetime', function (req, res) {
var username = req.params.username
var datetime = req.params.datetime
var query = 'SELECT filePath, timeStart, timeEnd FROM oldStreams WHERE username="'+username+'"'+
' AND timeStart <= '+datetime+' ORDER BY timeStart DESC LIMIT 2'
var results = []
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
results.push(qRes)
var query = 'SELECT filePath, timeStart, timeEnd FROM oldStreams WHERE username="'+username+'"'+
' AND timeStart > '+datetime+' ORDER BY timeStart ASC LIMIT 2'
sql.query(query, function(err, qRes2, fields) {
if (err) throw err;
results.push(qRes2)
res.send(results)
})
})
})
app.post('/rtmpRewrite', function (req, res) {
var params = req.body.split('\n')
var streamKey = params[0].split('/')[params[0].split('/').length-1]
sql.query('SELECT username FROM streamKeys WHERE verified=1 AND streamKey = "'+streamKey+'"', function(err, qRes, fields) {
if (err) throw err;
if (qRes.length < 1) {
res.send('Nope')
} else {
console.log(qRes[0].username+' started streaming.')
res.send('rtmp://'+params[0].split('/')[2]+'/live/normal+'+qRes[0].username)
}
})
})
app.post('/recordingEnd', function (req, res) {
var params = req.body.split('\n')
console.log(params[0].replace('normal+', '')+' stopped streaming.')
line = {
username: params[0].replace('normal+', ''),
filePath: params[1].replace(streamsFolder+'/', ''),
fileSize: params[3],
timeStart: params[5],
timeEnd: params[6],
duration: params[7]
}
var query = mysql.format('INSERT INTO oldStreams SET ?', line)
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
res.send('Ok')
})
})
app.listen(process.env.PORT)
function createKey(length) {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function clearUnverifiedKeys() {
setInterval(function() {
var query = 'DELETE FROM streamKeys WHERE verified = 0 AND generatedOn < (NOW() - INTERVAL 10 MINUTE)'
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
if (qRes.affectedRows > 0)
console.log(qRes.affectedRows+' unverified keys removed')
})
}, 10*60*1000)
setInterval(function() {
var query = 'SELECT 1'
sql.query(query, function(err, qRes, fields) {
if (err) throw err;
// console.log('ping')
})
}, 15*1000)
}