-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_client.js
105 lines (104 loc) · 4.59 KB
/
api_client.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
const request = require('sync-request')
const API_SERVER_URL = 'http://localhost/client/'
const API_SECERT = process.env.SECERET || process.argv[3]
const get = ((url) => request('GET', API_SERVER_URL + url).getBody())
const post = ((url, body) => request('POST', API_SERVER_URL + url, body).getBody())
const fs = {
readFileSync: ((path) => get(API_SECERT + '/' + path)),
writeFileSync: ((path, body) => post(API_SECERT + '/' + path, body)),
readdirSync: ((path) => get(API_SECERT + '/readdir/' + path))
}
const crypto = require('crypto')
const _api = module.exports = {
sha256: ((string) => {
const lines = string.split('\n')
const hash = crypto.createHash('sha256');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim(); // remove leading/trailing whitespace
if (line === '') continue; // skip empty lines
hash.write(line); // write a single line to the buffer
}
return hash.digest('hex');
}),
hash: ((string, loops) => {
let hash = _api.sha256(string)
for (let i = 0; i < loops; i++) {
hash = _api.sha256(hash)
hash += hash[0] + hash[hash.length - 1]
}
return hash
}),
post: {
get: ((id) => {
if (!fs.existsSync('./storage/posts/' + id + '.json')) return { id: '-1' }
const data = JSON.parse(fs.readFileSync('./storage/posts/' + id + '.json', 'utf8'))
return data
}),
set: ((parent = '-1', author = 'anon', body, title = body.split('\n')[0].replace(/^\s+|\s+$/g, '').substring(0, 20)) => {
fs.writeFileSync('./storage/posts/' + fs.readdirSync('./storage/posts').length + '.json', JSON.stringify({
id: fs.readdirSync('./storage/posts').length,
parent: parent.toString(),
author: author,
timestamp: +new Date(),
title: title,
body: body.replaceAll(/&/g, '&')
.replaceAll(/</g, '<').replaceAll(/>/g, '>')
.replaceAll(/"/g, '"').replaceAll(/'/g, ''')
.replaceAll(/\$IMG\[\[/g, '<img src="').replaceAll(/\]\]/g, '">')
}))
}),
all: ((parent = '-1') => {
const posts = fs.readdirSync('./storage/posts')
const data = []
for (let i = 0; i < posts.length; i++) {
let post = JSON.parse(fs.readFileSync('./storage/posts/' + posts[i], 'utf8'))
if (post.parent.toString() === parent.toString()) data.push(post)
}
return data
})
},
response: {
build: ((res, code = -1, message = 'idk server shit') => {
res.status(code).send({
code: code,
message: message
})
})
},
user: {
authorize: ((username, password) => {
if (username === 'anon') return true
if (fs.existsSync('./storage/users/' + username + '.json')) {
const data = JSON.parse(fs.readFileSync('./storage/users/' + username + '.json', 'utf8'))
if (data.password === _api.hash(password)) return true
}
}),
register: ((username, password) => {
// check if username is built only out of letters, numbers and underscores
if (!username.match(/^[a-zA-Z0-9_]+$/)) return { code: 401, message: 'username is not built only out of letters, numbers and underscores' }
if (fs.existsSync('./storage/users/' + username + '.json')) return false
fs.writeFileSync('./storage/users/' + username + '.json', JSON.stringify({
timestamp: +new Date(),
password: _api.hash(password)
}))
return true
}),
last: ((username) => {
// get latest post by user from feed
const posts = fs.readdirSync('./storage/posts')
const data = []
for (let i = 0; i < posts.length; i++) {
let post = JSON.parse(fs.readFileSync('./storage/posts/' + posts[i], 'utf8'))
if (post.author === username) data.push(post)
}
return data[data.length - 1]?.timestamp || 0
}),
ratelimit: ((username) => {
if (username === 'anon' && Math.floor(Math.random() * 20) === 1) return true
if (fs.existsSync('./storage/users/' + username + '.json')) {
if (parseInt(_api.user.last(username)) + 10000 > +new Date()) return true
else return false
} else return false
})
}
}