pinokiod 3.15.26 → 3.16.0

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.
@@ -77,6 +77,7 @@ class Bin {
77
77
  "User-Agent": userAgent
78
78
  }
79
79
  }
80
+ console.log("Download", opts)
80
81
  if (process.env.HTTP_PROXY && process.env.HTTP_PROXY.length > 0) {
81
82
  opts.httpRequestOptions = { agent }
82
83
  opts.httpsRequestOptions = { agent }
package/kernel/index.js CHANGED
@@ -36,6 +36,9 @@ const Router = require("./router")
36
36
  const Procs = require('./procs')
37
37
  const Peer = require('./peer')
38
38
  const Connect = require('./connect')
39
+ const { DownloaderHelper } = require('node-downloader-helper');
40
+ const { ProxyAgent } = require('proxy-agent');
41
+ const fakeUa = require('fake-useragent');
39
42
  //const kill = require('./tree-kill');
40
43
  const kill = require('kill-sync')
41
44
  const VARS = {
@@ -285,16 +288,15 @@ class Kernel {
285
288
  }
286
289
  }
287
290
  async refresh(notify_peers) {
288
- console.log("kernel.refresh")
289
291
  const ts = Date.now()
290
292
  let network_active = await this.network_active()
291
- console.log({ network_active })
293
+ // console.log({ network_active })
292
294
  if (!network_active) {
293
295
  return
294
296
  }
295
297
  let network_running = await this.network_running()
296
- console.log({ network_running })
297
- console.log({ refreshing: this.processes.refreshing })
298
+ // console.log({ network_running })
299
+ // console.log({ refreshing: this.processes.refreshing })
298
300
  if (network_running) {
299
301
 
300
302
 
@@ -305,58 +307,58 @@ class Kernel {
305
307
  }
306
308
 
307
309
  // 1. get the process list
308
- console.time("> 1. Proc Refresh"+ts)
310
+ // console.time("> 1. Proc Refresh"+ts)
309
311
  await this.processes.refresh()
310
312
 
311
313
  // diff check
312
314
  let new_config = JSON.stringify(this.processes.info)
313
- if (this.old_config !== new_config) {
314
- console.log("Proc config has changed")
315
- console.log("old", this.old_config)
316
- console.log("new", new_config)
317
- } else {
318
- console.log("Proc config is the same")
319
- }
315
+ // if (this.old_config !== new_config) {
316
+ // console.log("Proc config has changed")
317
+ // console.log("old", this.old_config)
318
+ // console.log("new", new_config)
319
+ // } else {
320
+ // console.log("Proc config is the same")
321
+ // }
320
322
  this.old_config = new_config
321
323
 
322
- console.timeEnd("> 1. Proc Refresh"+ts)
324
+ // console.timeEnd("> 1. Proc Refresh"+ts)
323
325
 
324
326
  // 2. refresh peer info to reflect the proc info
325
- console.time("> 2. Peer Refresh"+ts)
327
+ // console.time("> 2. Peer Refresh"+ts)
326
328
  await this.peer.refresh()
327
- console.timeEnd("> 2. Peer Refresh"+ts)
329
+ // console.timeEnd("> 2. Peer Refresh"+ts)
328
330
 
329
331
  // 3. load custom routers from ~/pinokio/network
330
- console.time("> 3. Router Init"+ts)
332
+ // console.time("> 3. Router Init"+ts)
331
333
  await this.router.init()
332
- console.timeEnd("> 3. Router Init"+ts)
334
+ // console.timeEnd("> 3. Router Init"+ts)
333
335
 
334
336
  // 4. set current local host router info
335
- console.time("> 4. Router Local"+ts)
337
+ // console.time("> 4. Router Local"+ts)
336
338
  await this.router.local()
337
- console.timeEnd("> 4. Router Local"+ts)
339
+ // console.timeEnd("> 4. Router Local"+ts)
338
340
 
339
341
  // 5. refresh peer info to reflect the updated router info
340
- console.time("> 5. Peer Refresh"+ts)
342
+ // console.time("> 5. Peer Refresh"+ts)
341
343
  await this.peer.refresh()
342
- console.timeEnd("> 5. Peer Refresh"+ts)
344
+ // console.timeEnd("> 5. Peer Refresh"+ts)
343
345
 
344
346
  // 6. tell peers to refresh
345
347
  if (notify_peers) {
346
- console.time("> 6. Peer Notify Peers"+ts)
348
+ // console.time("> 6. Peer Notify Peers"+ts)
347
349
  await this.peer.notify_peers()
348
- console.timeEnd("> 6. Peer Notify Peers"+ts)
350
+ // console.timeEnd("> 6. Peer Notify Peers"+ts)
349
351
  }
350
352
 
351
353
  // 7. update remote router
352
- console.time("> 7. Router Remote"+ts)
354
+ // console.time("> 7. Router Remote"+ts)
353
355
  await this.router.remote()
354
- console.timeEnd("> 7. Router Remote"+ts)
356
+ // console.timeEnd("> 7. Router Remote"+ts)
355
357
 
356
358
  // 8. update caddy config
357
- console.time("> 8. Router Update"+ts)
359
+ // console.time("> 8. Router Update"+ts)
358
360
  await this.router.update()
359
- console.timeEnd("> 8. Router Update"+ts)
361
+ // console.timeEnd("> 8. Router Update"+ts)
360
362
 
361
363
  // 9. announce self to the peer network
362
364
  this.peer.announce()
@@ -382,6 +384,62 @@ class Kernel {
382
384
  }
383
385
  }
384
386
  }
387
+ async download(options, ondata) {
388
+ console.log("download", { options })
389
+ const agent = new ProxyAgent();
390
+ const userAgent = fakeUa()
391
+ let url = options.uri
392
+ let cwd = options.path
393
+ const opts = {
394
+ override: true,
395
+ headers: {
396
+ "User-Agent": userAgent
397
+ }
398
+ }
399
+ if (options.filename) {
400
+ opts.fileName = options.filename
401
+ }
402
+ console.log("Download", opts)
403
+ if (process.env.HTTP_PROXY && process.env.HTTP_PROXY.length > 0) {
404
+ opts.httpRequestOptions = { agent }
405
+ opts.httpsRequestOptions = { agent }
406
+ }
407
+ if (process.env.HTTPS_PROXY && process.env.HTTPS_PROXY.length > 0) {
408
+ opts.httpRequestOptions = { agent }
409
+ opts.httpsRequestOptions = { agent }
410
+ }
411
+ const dl = new DownloaderHelper(url, cwd, opts)
412
+ ondata({ raw: `\r\nDownloading ${url} to ${cwd}...\r\n` })
413
+ let res = await new Promise((resolve, reject) => {
414
+ dl.on('end', () => {
415
+ ondata({ raw: `\r\nDownload Complete!\r\n` })
416
+ resolve()
417
+ })
418
+ dl.on('error', (err) => {
419
+ ondata({ raw: `\r\nDownload Failed: ${err.message}!\r\n` })
420
+ reject(err)
421
+ })
422
+ dl.on('progress', (stats) => {
423
+ let p = Math.floor(stats.progress)
424
+ let str = ""
425
+ for(let i=0; i<p; i++) {
426
+ str += "#"
427
+ }
428
+ for(let i=p; i<100; i++) {
429
+ str += "-"
430
+ }
431
+ ondata({ raw: `\r${str}` })
432
+ })
433
+ dl.start().catch((err) => {
434
+ ondata({ raw: `\r\nDownload Failed: ${err.message}!\r\n` })
435
+ reject(err)
436
+ })
437
+ })
438
+
439
+ /*
440
+ await this.exec({ message: `aria2 -o download.zip ${url}` })
441
+ */
442
+ }
385
443
  async _log(data, group, info) {
386
444
  if (group) {
387
445
 
package/kernel/procs.js CHANGED
@@ -7,27 +7,65 @@ const limit = pLimit(10)
7
7
  const platform = os.platform();
8
8
  const isWin = platform === 'win32';
9
9
  const cls = isWin ? 'cls' : 'clear'
10
+ const net = require('net');
10
11
  class Procs {
11
12
  constructor (kernel) {
12
13
  // console.log("Initializing procs")
13
14
  this.kernel = kernel
14
15
  this.cache = {}
15
16
  }
16
- async isHttp(localAddress) {
17
- if (this.cache.hasOwnProperty(localAddress)) {
18
- console.log("Use cached", localAddress)
19
- return this.cache[localAddress]
20
- }
21
- console.log("Not cached", localAddress)
22
- try {
23
- await axios.head(`http://${localAddress}`, { timeout: 3000 });
24
- this.cache[localAddress] = true
25
- return true;
26
- } catch (err) {
27
- // console.log("HEAD ERROR",{ localAddress, err })
28
- this.cache[localAddress] = false
29
- return false;
17
+ async isHttp(port) {
18
+ //async isHttp(localAddress) {
19
+ // if (this.cache.hasOwnProperty(localAddress)) {
20
+ //// console.log("Use cached", localAddress)
21
+ // return this.cache[localAddress]
22
+ // }
23
+ // console.log("Not cached", localAddress)
24
+ //try {
25
+ // //await axios.head(`http://${localAddress}`, { timeout: 3000 });
26
+ // await axios.get(`http://${localAddress}`, { timeout: 3000 });
27
+ // this.cache[localAddress] = true
28
+ // return true;
29
+ //} catch (err) {
30
+ // console.log("HEAD ERROR",{ localAddress, err })
31
+ // this.cache[localAddress] = false
32
+ // return false;
33
+ //}
34
+ if (this.cache.hasOwnProperty("" + port)) {
35
+ return this.cache["" + port]
30
36
  }
37
+ let ip = "127.0.0.1"
38
+ let timeout = 1000
39
+ let response = await new Promise(resolve => {
40
+ const socket = new net.Socket();
41
+ let response = '';
42
+ let resolved = false;
43
+ socket.setTimeout(timeout);
44
+ socket.connect(port, ip, () => {
45
+ // Use a nonsense method to trigger a 400/405/501 from real HTTP servers
46
+ socket.write(`FOO / HTTP/1.1\r\nHost: ${ip}\r\nConnection: close\r\n\r\n`);
47
+ });
48
+ socket.on('data', chunk => {
49
+ response += chunk.toString();
50
+ if (/^HTTP\/\d+\.\d+ \d+/.test(response)) {
51
+ resolved = true;
52
+ socket.destroy();
53
+ resolve(true); // Valid HTTP response detected
54
+ }
55
+ });
56
+ socket.on('error', () => {
57
+ if (!resolved) resolve(false);
58
+ });
59
+ socket.on('timeout', () => {
60
+ if (!resolved) resolve(false);
61
+ socket.destroy();
62
+ });
63
+ socket.on('close', () => {
64
+ if (!resolved) resolve(false);
65
+ });
66
+ });
67
+ this.cache["" + port] = response
68
+ return response
31
69
  }
32
70
  newline(id) {
33
71
  this.kernel.shell.emit({
@@ -124,13 +162,13 @@ class Procs {
124
162
  }
125
163
  }
126
164
  // console.timeEnd("###### Line parsing")
127
- console.time("########## http_check")
128
- const http_check = await Promise.all(results.map(({ ip }) => {
165
+ // console.time("########## http_check")
166
+ const http_check = await Promise.all(results.map(({ port }) => {
129
167
  return limit(() => {
130
- return this.isHttp(ip)
168
+ return this.isHttp(port)
131
169
  })
132
170
  }))
133
- console.timeEnd("########## http_check")
171
+ // console.timeEnd("########## http_check")
134
172
  let filtered = []
135
173
  for(let i=0; i<http_check.length; i++) {
136
174
  if (http_check[i]) {
@@ -292,7 +330,7 @@ class Procs {
292
330
  // console.log("get ports")
293
331
  this.getPortPidList((portPidList) => {
294
332
  // console.log("done: get ports")
295
- console.log({ portPidList })
333
+ // console.log({ portPidList })
296
334
  // console.timeEnd(">>>>>>>>GET PORTS " + ts)
297
335
  // console.time(">>>>>>> GET PIDS " + ts)
298
336
  // if there's any new port, run getPidToNameMap
@@ -208,9 +208,9 @@ class Router {
208
208
  // update caddy config
209
209
  async update() {
210
210
  if (JSON.stringify(this.config) === JSON.stringify(this.old_config)) {
211
- console.log("######### config hasn't updated")
211
+ // console.log("######### config hasn't updated")
212
212
  } else {
213
- console.log("######### caddy config has updated. refresh")
213
+ // console.log("######### caddy config has updated. refresh")
214
214
  // console.log("Old", JSON.stringify(this.old_config, null, 2))
215
215
  // console.log("New", JSON.stringify(this.config, null, 2))
216
216
  try {
package/kernel/shell.js CHANGED
@@ -98,6 +98,7 @@ class Shell {
98
98
  this.env.CMAKE_OBJECT_PATH_MAX = 1024
99
99
  this.env.PYTORCH_ENABLE_MPS_FALLBACK = 1
100
100
  this.env.TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD = 1
101
+ //this.env.NODE_EXTRA_CA_CERTS = this.kernel.path("cache/XDG_DATA_HOME/caddy/pki/authorities/local/root.crt")
101
102
  // this.env.PIP_REQUIRE_VIRTUALENV = "true"
102
103
  // this.env.NPM_CONFIG_USERCONFIG = this.kernel.path("user_npmrc")
103
104
  // this.env.NPM_CONFIG_GLOBALCONFIG = this.kernel.path("global_npmrc")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "3.15.26",
3
+ "version": "3.16.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/server/index.js CHANGED
@@ -4274,7 +4274,6 @@ class Server {
4274
4274
  }))
4275
4275
  this.app.post("/pinokio/peer/refresh", ex(async (req, res) => {
4276
4276
  // refresh and broadcast
4277
- console.log("kernel.refresh /pinokio/peer/refresh")
4278
4277
  await this.kernel.refresh()
4279
4278
  res.json({ success: true })
4280
4279
  }))