braidfs 0.0.20 → 0.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +46 -27
  2. package/package.json +7 -2
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  let http = require('http');
4
- let { diff_main } = require('./diff.js')
4
+ let { diff_main } = require(require('path').join(__dirname, "diff.js"))
5
5
  let braid_text = require("braid-text");
6
6
  let braid_fetch = require('braid-http').fetch
7
7
 
@@ -15,6 +15,7 @@ let braidfs_config_file = require('path').join(braidfs_config_dir, 'config.json'
15
15
  if (!require('fs').existsSync(braidfs_config_file)) {
16
16
  require('fs').writeFileSync(braidfs_config_file, JSON.stringify({
17
17
  port: 10000,
18
+ allow_remote_access: false,
18
19
  sync_urls: [],
19
20
  sync_index_urls: [],
20
21
  proxy_base: require('path').join(require('os').homedir(), 'http'),
@@ -28,6 +29,7 @@ let config = JSON.parse(require('fs').readFileSync(braidfs_config_file, 'utf8'))
28
29
 
29
30
  // process command line args (override config)
30
31
  let argv = process.argv.slice(2)
32
+ let save_config = false
31
33
  while (argv.length) {
32
34
  let a = argv.shift()
33
35
  if (a.match(/^\d+$/)) {
@@ -39,8 +41,15 @@ while (argv.length) {
39
41
  } else {
40
42
  config.sync_urls.push(b)
41
43
  }
44
+ } else if (a === 'save') {
45
+ save_config = true
46
+ } else if (a === 'expose') {
47
+ config.allow_remote_access = true
48
+ } else if (a === 'unexpose') {
49
+ config.allow_remote_access = false
42
50
  }
43
51
  }
52
+ if (save_config) require('fs').writeFileSync(braidfs_config_file, JSON.stringify(config, null, 4))
44
53
 
45
54
  braid_text.db_folder = config.braid_text_db
46
55
 
@@ -67,21 +76,24 @@ const server = http.createServer(async (req, res) => {
67
76
 
68
77
  if (req.url === '/favicon.ico') return;
69
78
 
70
- // Security check: Allow only localhost access
71
- const clientIp = req.socket.remoteAddress;
72
- if (clientIp !== '127.0.0.1' && clientIp !== '::1') {
73
- res.writeHead(403, { 'Content-Type': 'text/plain' });
74
- res.end('Access denied: This proxy is only accessible from localhost');
75
- return;
79
+ function only_allow_local_host() {
80
+ const clientIp = req.socket.remoteAddress;
81
+ if (clientIp !== '127.0.0.1' && clientIp !== '::1') {
82
+ res.writeHead(403, { 'Content-Type': 'text/plain' });
83
+ res.end('Access denied: only accessible from localhost');
84
+ throw 'done'
85
+ }
76
86
  }
77
87
 
88
+ if (!config.allow_remote_access) only_allow_local_host()
89
+
78
90
  // Free the CORS
79
91
  free_the_cors(req, res);
80
92
  if (req.method === 'OPTIONS') return;
81
93
 
82
94
  if (req.url.endsWith("?editor")) {
83
95
  res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
84
- require("fs").createReadStream("./editor.html").pipe(res)
96
+ require("fs").createReadStream(require('path').join(__dirname, "editor.html")).pipe(res)
85
97
  return
86
98
  }
87
99
 
@@ -96,6 +108,10 @@ const server = http.createServer(async (req, res) => {
96
108
  }
97
109
 
98
110
  let url = req.url.slice(1)
111
+ let is_external_link = url.match(/^https?:\/\//)
112
+
113
+ // we don't want to let remote people access external links for now
114
+ if (config.allow_remote_access && is_external_link) only_allow_local_host()
99
115
 
100
116
  proxy_url(url)
101
117
 
@@ -104,8 +120,8 @@ const server = http.createServer(async (req, res) => {
104
120
  });
105
121
 
106
122
  server.listen(config.port, () => {
107
- console.log(`Proxy server started on port ${config.port}`);
108
- console.log('This proxy is only accessible from localhost');
123
+ console.log(`server started on port ${config.port}`);
124
+ if (!config.allow_remote_access) console.log('!! only accessible from localhost !!');
109
125
  });
110
126
 
111
127
  ////////////////////////////////
@@ -155,7 +171,8 @@ async function proxy_url(url) {
155
171
 
156
172
  console.log(`proxy_url: ${url}`)
157
173
 
158
- let path = url.replace(/^https?:\/\//, '')
174
+ let is_external_link = url.match(/^https?:\/\//)
175
+ let path = is_external_link ? url.replace(/^https?:\/\//, '') : `localhost/${url}`
159
176
  let fullpath = require("path").join(config.proxy_base, path)
160
177
 
161
178
  // if we're accessing /blah/index, it will be normalized to /blah,
@@ -189,7 +206,7 @@ async function proxy_url(url) {
189
206
  }
190
207
 
191
208
  async function send_out(stuff) {
192
- await braid_fetch_wrapper(url, {
209
+ if (is_external_link) await braid_fetch_wrapper(url, {
193
210
  headers: {
194
211
  "Merge-Type": "dt",
195
212
  "Content-Type": 'text/plain',
@@ -245,11 +262,11 @@ async function proxy_url(url) {
245
262
  }
246
263
  if (file_needs_writing) {
247
264
  file_needs_writing = false
248
-
249
- console.log(`writing file ${await get_fullpath()}`)
250
-
251
265
  let { version, body } = await braid_text.get(url, {})
252
266
  if (!v_eq(version, file_last_version)) {
267
+
268
+ console.log(`writing file ${await get_fullpath()}`)
269
+
253
270
  file_last_version = version
254
271
  file_last_text = body
255
272
  await require('fs').promises.writeFile(await get_fullpath(), file_last_text)
@@ -260,7 +277,7 @@ async function proxy_url(url) {
260
277
  file_loop_pump_lock--
261
278
  }
262
279
 
263
- braid_fetch_wrapper(url, {
280
+ if (is_external_link) braid_fetch_wrapper(url, {
264
281
  headers: {
265
282
  "Merge-Type": "dt",
266
283
  Accept: 'text/plain'
@@ -301,17 +318,19 @@ async function proxy_url(url) {
301
318
 
302
319
  // try a HEAD without subscribe to get the version
303
320
  let parents = null
304
- try {
305
- let head_res = await braid_fetch_wrapper(url, {
306
- method: 'HEAD',
307
- headers: { Accept: 'text/plain' },
308
- retry: true,
309
- })
310
- parents = head_res.headers.get('version') ?
311
- JSON.parse(`[${head_res.headers.get('version')}]`) :
312
- null
313
- } catch (e) {
314
- console.log('HEAD failed: ', e)
321
+ if (is_external_link) {
322
+ try {
323
+ let head_res = await braid_fetch_wrapper(url, {
324
+ method: 'HEAD',
325
+ headers: { Accept: 'text/plain' },
326
+ retry: true,
327
+ })
328
+ parents = head_res.headers.get('version') ?
329
+ JSON.parse(`[${head_res.headers.get('version')}]`) :
330
+ null
331
+ } catch (e) {
332
+ console.log('HEAD failed: ', e)
333
+ }
315
334
  }
316
335
 
317
336
  // now get everything since then, and send it back..
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braidfs",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",
@@ -12,5 +12,10 @@
12
12
  },
13
13
  "bin": {
14
14
  "braidfs": "./index.js"
15
- }
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "editor.html",
19
+ "diff.js"
20
+ ]
16
21
  }