braidfs 0.0.19 → 0.0.21

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 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -9,16 +9,19 @@ process.on("unhandledRejection", (x) => console.log(`unhandledRejection: ${x.sta
9
9
  process.on("uncaughtException", (x) => console.log(`uncaughtException: ${x.stack}`))
10
10
 
11
11
  let braidfs_config_dir = require('path').join(require('os').homedir(), '.braidfs')
12
+ require('fs').mkdirSync(braidfs_config_dir, { recursive: true })
12
13
 
13
14
  let braidfs_config_file = require('path').join(braidfs_config_dir, 'config.json')
14
15
  if (!require('fs').existsSync(braidfs_config_file)) {
15
16
  require('fs').writeFileSync(braidfs_config_file, JSON.stringify({
16
17
  port: 10000,
18
+ allow_remote_access: false,
17
19
  sync_urls: [],
18
20
  sync_index_urls: [],
19
21
  proxy_base: require('path').join(require('os').homedir(), 'http'),
20
22
  proxy_base_last_versions: require('path').join(braidfs_config_dir, 'proxy_base_last_versions'),
21
23
  braid_text_db: require('path').join(braidfs_config_dir, 'braid-text-db'),
24
+ domains: { 'example.com': { auth_headers: { Cookie: "secret_pass" } } }
22
25
  }, null, 4))
23
26
  }
24
27
 
@@ -26,6 +29,7 @@ let config = JSON.parse(require('fs').readFileSync(braidfs_config_file, 'utf8'))
26
29
 
27
30
  // process command line args (override config)
28
31
  let argv = process.argv.slice(2)
32
+ let save_config = false
29
33
  while (argv.length) {
30
34
  let a = argv.shift()
31
35
  if (a.match(/^\d+$/)) {
@@ -37,8 +41,15 @@ while (argv.length) {
37
41
  } else {
38
42
  config.sync_urls.push(b)
39
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
40
50
  }
41
51
  }
52
+ if (save_config) require('fs').writeFileSync(braidfs_config_file, JSON.stringify(config, null, 4))
42
53
 
43
54
  braid_text.db_folder = config.braid_text_db
44
55
 
@@ -65,14 +76,17 @@ const server = http.createServer(async (req, res) => {
65
76
 
66
77
  if (req.url === '/favicon.ico') return;
67
78
 
68
- // Security check: Allow only localhost access
69
- const clientIp = req.socket.remoteAddress;
70
- if (clientIp !== '127.0.0.1' && clientIp !== '::1') {
71
- res.writeHead(403, { 'Content-Type': 'text/plain' });
72
- res.end('Access denied: This proxy is only accessible from localhost');
73
- 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
+ }
74
86
  }
75
87
 
88
+ if (!config.allow_remote_access) only_allow_local_host()
89
+
76
90
  // Free the CORS
77
91
  free_the_cors(req, res);
78
92
  if (req.method === 'OPTIONS') return;
@@ -94,6 +108,10 @@ const server = http.createServer(async (req, res) => {
94
108
  }
95
109
 
96
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()
97
115
 
98
116
  proxy_url(url)
99
117
 
@@ -102,8 +120,8 @@ const server = http.createServer(async (req, res) => {
102
120
  });
103
121
 
104
122
  server.listen(config.port, () => {
105
- console.log(`Proxy server started on port ${config.port}`);
106
- 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 !!');
107
125
  });
108
126
 
109
127
  ////////////////////////////////
@@ -153,7 +171,8 @@ async function proxy_url(url) {
153
171
 
154
172
  console.log(`proxy_url: ${url}`)
155
173
 
156
- let path = url.replace(/^https?:\/\//, '')
174
+ let is_external_link = url.match(/^https?:\/\//)
175
+ let path = is_external_link ? url.replace(/^https?:\/\//, '') : `localhost/${url}`
157
176
  let fullpath = require("path").join(config.proxy_base, path)
158
177
 
159
178
  // if we're accessing /blah/index, it will be normalized to /blah,
@@ -187,7 +206,7 @@ async function proxy_url(url) {
187
206
  }
188
207
 
189
208
  async function send_out(stuff) {
190
- await braid_fetch_wrapper(url, {
209
+ if (is_external_link) await braid_fetch_wrapper(url, {
191
210
  headers: {
192
211
  "Merge-Type": "dt",
193
212
  "Content-Type": 'text/plain',
@@ -243,11 +262,11 @@ async function proxy_url(url) {
243
262
  }
244
263
  if (file_needs_writing) {
245
264
  file_needs_writing = false
246
-
247
- console.log(`writing file ${await get_fullpath()}`)
248
-
249
265
  let { version, body } = await braid_text.get(url, {})
250
266
  if (!v_eq(version, file_last_version)) {
267
+
268
+ console.log(`writing file ${await get_fullpath()}`)
269
+
251
270
  file_last_version = version
252
271
  file_last_text = body
253
272
  await require('fs').promises.writeFile(await get_fullpath(), file_last_text)
@@ -258,7 +277,7 @@ async function proxy_url(url) {
258
277
  file_loop_pump_lock--
259
278
  }
260
279
 
261
- braid_fetch_wrapper(url, {
280
+ if (is_external_link) braid_fetch_wrapper(url, {
262
281
  headers: {
263
282
  "Merge-Type": "dt",
264
283
  Accept: 'text/plain'
@@ -299,17 +318,19 @@ async function proxy_url(url) {
299
318
 
300
319
  // try a HEAD without subscribe to get the version
301
320
  let parents = null
302
- try {
303
- let head_res = await braid_fetch_wrapper(url, {
304
- method: 'HEAD',
305
- headers: { Accept: 'text/plain' },
306
- retry: true,
307
- })
308
- parents = head_res.headers.get('version') ?
309
- JSON.parse(`[${head_res.headers.get('version')}]`) :
310
- null
311
- } catch (e) {
312
- 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
+ }
313
334
  }
314
335
 
315
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.19",
3
+ "version": "0.0.21",
4
4
  "description": "braid technology synchronizing files and webpages",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braidfs",