-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
264 lines (247 loc) · 8.51 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require('dotenv').config()
const { CID } = require('multiformats/cid')
const raw = require('multiformats/codecs/raw')
const { sha256 } = require('multiformats/hashes/sha2')
const { TextEncoder } = require('util')
const express = require('express')
const serveStatic = require('serve-static')
const compression = require('compression')
const fileUpload = require('express-fileupload')
const cors = require('cors')
const bodyParser = require('body-parser')
const { curry, map, join, mapObjIndexed, pipe, flatten, values, filter, path } = require('ramda')
const { ethers } = require('ethers')
const BasicFS = require('./basic-fs')
const S3FileSystem = require('./s3')
const IC = require('ic-js')
const jwt = require('jsonwebtoken')
const { expressjwt } = require("express-jwt")
const ServerIcs = require('./server-ics')
const { ADMIN, ADMIN_HOME, PARTY_MODE } = process.env
const fileSystem = S3FileSystem.factory() || new BasicFS()
const serverIcs = new ServerIcs(fileSystem)
const app = express()
app.use(compression())
app.use(fileUpload({
createParentPath: true,
useTempFiles: true,
tempFileDir: 'tmp/'
}))
app.use(cors())
app.use(bodyParser.json({ type: '*/json' }))
app.use(bodyParser.text({ type: 'text/*' }))
app.use(bodyParser.urlencoded({ extended: true }))
let JWT_SECRET = process.env.JWT_SECRET
if (!JWT_SECRET) {
console.log('🔒⚠️ You did not provide a JWT_SECRET. One is being generated for you. That means you will have to reauthenticate every time you restart the server.')
JWT_SECRET = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
}
app.use(expressjwt({
secret: JWT_SECRET,
algorithms: ['HS256'],
credentialsRequired: false
}))
app.use(async (req, res, next) => {
const host = req.headers.host || 'no-host'
const adminIc = await serverIcs.getServerAdminIc()
if (adminIc) {
const domains = adminIc.findTagged(['domains', 'icfs'])
if (domains.length > 0 && !domains.includes(host) && host !== ADMIN_HOME) {
return res.status(401).send('Unauthorized')
}
}
req.filePrefix = `/${host}`
next()
})
const serverIndex = async (req, res) => {
const host = req.headers.host
const { params, query } = req
if (query && query.findTagged) {
const ic = await serverIcs.serverIc(req.filePrefix)
const tagged = ic.findTagged(query.findTagged.split("\n").map(s => s.trim()), { ic: true })
return res.send(tagged.export())
} else if (query && query.seed) {
const ic = await serverIcs.serverIc(req.filePrefix)
const opts = {}
if (query.depth) {
opts.depth = parseInt(query.depth)
}
return res.send(ic.seed(query.seed.split("\n").map(s => s.trim()), opts).export())
} else {
const files = await fileSystem.readDir(req.filePrefix + '/')
const icLines = []
icLines.push(host)
files.forEach(file => {
icLines.push(`+https://${host}/${file}/index.ic`)
})
res.setHeader('Content-Type', 'text/ic')
let ret = ''
const serverAdmin = await serverIcs.getDomainAdmin(host)
if (serverAdmin) {
ret += `${host} admin\n+${serverAdmin}\n`
// serverAdmin has a file
if (files.includes(serverAdmin)) {
const adminIc = await fileSystem.readFile(req.filePrefix + `/${serverAdmin}/index.ic`)
if (adminIc) {
const ic = new IC
await ic.import(adminIc)
const newIc = ic.seed(['icfs'])
ret += `\n${newIc.export().replace(/^_.*\n/, `_${serverAdmin}\n`)}\n_\n`
}
}
}
ret += pipe(
join("\n")
)(icLines)
res.send(ret)
}
}
const userIndex = async (req, res) => {
const { params, query } = req
if (/[^A-Za-z0-9]+/.test(params.username)) return res.sendStatus(404)
const fileName = req.filePrefix + `/${params.username}/index.ic`
if (await fileSystem.exists(fileName)) {
if (query && query.seed) {
const ic = new IC
const icFile = await fileSystem.readFile(fileName)
await ic.import(icFile || '')
const opts = {}
if (query.depth) {
opts.depth = parseInt(query.depth)
}
const seedIc = ic.seed(query.seed.split("\n").map(s => s.trim()), opts)
return res.send(seedIc.export())
} else if (query && query.findTagged) {
const ic = new IC
const icFile = await fileSystem.readFile(fileName)
await ic.import(icFile || '')
return res.send(ic.findTagged(query.findTagged.split("\n").map(s => s.trim())).join("\n"))
} else {
return fileSystem.createReadStream(fileName).pipe(res)
}
} else {
res.sendStatus(404)
}
}
const writeUserFiles = async (req, str) => {
const { params } = req
// const bytes = new TextEncoder('utf8').encode(str)
// const hash = await sha256.digest(bytes)
// const cid = CID.create(1, raw.code, hash)
// const filePath = `/${params.username}/${cid.toString()}.ic`
const indexPath = `/${params.username}/index.ic`
serverIcs.clearServerIc(req.filePrefix)
// await fileSystem.writeFile(req.filePrefix + filePath, str)
await fileSystem.writeFile(req.filePrefix + indexPath, str)
return [{
// cid: cid.toString(),
// static: filePath,
dynamic: indexPath
}]
}
const NONCE_PREFIX = 'Your random nonce: '
const verifyNonce = async (req, signedNonce) => {
const { username } = req.params
if (!signedNonce) return
const storedNonce = await fileSystem.readFile(req.filePrefix + `/${username}/_nonce`)
if (!storedNonce) return
const message = NONCE_PREFIX + storedNonce
const verified = await ethers.utils.verifyMessage(message, signedNonce)
if (verified !== username) return
await fileSystem.unlink(req.filePrefix + `/${username}/_nonce`)
return username
}
app.use('/:username', async (req, res, next) => {
if (!['POST', 'PATCH'].includes(req.method)) {
return next()
}
const fail = () => {
res.sendStatus(401)
}
const { username } = req.params
if (req.path === '/_jwt' || path(['auth', 'username'], req) === username || (await verifyNonce(req, req.headers['x-ic-nonce']))) {
if (PARTY_MODE === 'true' || username === ADMIN) {
return next()
}
const ic = await serverIcs.serverIc(req.filePrefix, { importDepth: 1 })
const invites = ic.findTagged(['.ic invites'])
const admin = await serverIcs.getDomainAdmin(req.filePrefix.replace('/', ''))
if (invites.length === 0
|| invites.find(i => i === `${req.headers.host}/${username}`)
|| (admin && admin === username)
) {
return next()
}
}
fail()
})
app.get('/:username/_nonce', async (req, res) => {
const { username } = req.params
let userNonce = await fileSystem.readFile(req.filePrefix + `/${username}/_nonce`)
if (!userNonce) {
userNonce = Math.floor(Math.random() * 1000000)
await fileSystem.writeFile(req.filePrefix + `/${username}/_nonce`, userNonce.toString())
}
res.send(NONCE_PREFIX + userNonce)
})
app.post('/:username/_jwt', async (req, res) => {
const nonce = path(['body', 'nonce'], req) || req.body
if (await verifyNonce(req, nonce)) {
const { username } = req.params
const token = jwt.sign(Object.assign({ username }, req.body.data), JWT_SECRET)
res.send(token)
} else {
res.sendStatus(401)
}
})
app.get('/', serverIndex)
app.get('/index.ic', serverIndex)
app.get('/:username', userIndex)
app.get('/:username/index.ic', userIndex)
app.post('/:username', async (req, res) => {
const { params } = req
try {
if (req.body) {
const files = await writeUserFiles(req, req.body)
res.send({
ok: true,
files
})
}
} catch (err) {
res.status(500).send(err)
}
})
app.patch('/:username', async (req, res) => {
const { params, auth = {} } = req
const { tags } = auth
try {
if (req.body) {
// verify that they can write to this tag
if (tags) {
const ic = new IC({ importDepth: 1 })
await ic.import(req.body)
const allowedTags = tags.split('\n').map(IC.clean)
const badTags = ic.all().filter(t => !allowedTags.includes(t.to))
if (badTags.length) return res.status(401).send('Unauthorized')
}
const existingFile = await fileSystem.readFile(req.filePrefix + `/${params.username}/index.ic`)
const str = `${existingFile ? existingFile + '\n' : ''}${req.body}`
const files = await writeUserFiles(req, str)
res.send({
ok: true,
files
})
}
} catch (err) {
res.status(500).send(err)
}
})
// start app
const port = process.env.PORT || 3002
app.listen(port, () => {
console.log(`App is listening on port ${port}.`)
if (process.env.PARTY_MODE === 'true') {
console.log('🍾 IT IS PARTY!1! 🕺🏻💃🏽');
}
})