Skip to content

Commit 030ef02

Browse files
author
sevenbitbyte
committed
i2p example, secure config examle tweak, downgrade ncc
1 parent e21f55f commit 030ef02

8 files changed

Lines changed: 144 additions & 20 deletions

File tree

examples/i2p-host.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const Path = require('path')
2+
const debug = require('debug')('test.server-db')
3+
const Dataparty = require('../src')
4+
const dataparty_crypto = require('@dataparty/crypto')
5+
6+
class ExampleService extends Dataparty.IService {
7+
constructor(opts){
8+
super(opts)
9+
10+
this.addMiddleware(Dataparty.middleware_paths.pre.decrypt)
11+
this.addMiddleware(Dataparty.middleware_paths.pre.validate)
12+
13+
this.addMiddleware(Dataparty.middleware_paths.post.validate)
14+
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)
15+
16+
this.addEndpoint(Dataparty.endpoint_paths.echo)
17+
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
18+
this.addEndpoint(Dataparty.endpoint_paths.identity)
19+
this.addEndpoint(Dataparty.endpoint_paths.version)
20+
21+
this.addSchema(Path.join(__dirname, './party/schema/basic_types.js'))
22+
}
23+
24+
}
25+
26+
async function main(){
27+
28+
29+
//const uri = 'mongodb://localhost:27017/server-party-test'
30+
//debug('db location', uri)
31+
32+
33+
const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })
34+
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
35+
36+
const serviceName = build.package.name
37+
const basePath = '/data/dataparty/'
38+
const servicePath = Path.join(basePath, serviceName.replace('/','-'))
39+
40+
let config = new Dataparty.Config.JsonFileConfig({ basePath: servicePath })
41+
config.touchDir('/tingo')
42+
43+
const dbPath = Path.join(servicePath, '/tingo')
44+
45+
let party = new Dataparty.TingoParty({
46+
config,
47+
path: dbPath,
48+
model: build
49+
})
50+
51+
party.topics = new Dataparty.LocalTopicHost()
52+
53+
const live = new Dataparty.IService(build.package, build)
54+
55+
56+
const runner = new Dataparty.ServiceRunnerNode({
57+
party,
58+
//prefix: 'foo',
59+
service: live,
60+
sendFullErrors: false,
61+
useNative: false
62+
})
63+
64+
65+
66+
67+
const runnerRouter = new Dataparty.RunnerRouter(runner)
68+
69+
70+
const host = new Dataparty.ServiceHost({
71+
runner: runnerRouter,
72+
trust_proxy: true,
73+
wsEnabled: true,
74+
i2pEnabled: true
75+
})
76+
77+
debug(runner.party.identity)
78+
await party.start()
79+
await runnerRouter.start()
80+
await host.start()
81+
82+
console.log('started')
83+
}
84+
85+
86+
87+
main().catch(err=>{
88+
console.error(err)
89+
})

examples/secure-config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async function main(){
1515

1616

1717
secureConfig = new Dataparty.Config.SecureConfig({
18-
config: jsonConfig
18+
config: jsonConfig,
19+
//timeoutMs: 15*1000
1920
})
2021

2122

@@ -124,7 +125,7 @@ async function main(){
124125

125126
console.log('timer config', await secureConfig.readAll())
126127

127-
}, 30000)
128+
}, 1000*60*6)
128129

129130
}
130131

examples/test-service-node-host.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function main(){
3434
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
3535

3636
const serviceName = build.package.name
37-
const basePath = '/data/datparty/'
37+
const basePath = '/data/dataparty/'
3838
const servicePath = Path.join(basePath, serviceName.replace('/','-'))
3939

4040
let config = new Dataparty.Config.JsonFileConfig({ basePath: servicePath })
@@ -80,7 +80,7 @@ async function main(){
8080

8181
console.log('started')
8282

83-
process.exit()
83+
//process.exit()
8484
}
8585

8686

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@diva.exchange/i2p-sam": "^4.1.8",
6565
"@markwylde/liferaft": "^1.3.4",
6666
"@vercel/ncc": "^0.36.1",
67+
"@zeit/ncc": "^0.22.3",
6768
"ajv": "6.9.1",
6869
"axios": "^0.27.2",
6970
"bleno": "npm:@abandonware/bleno@^0.5.1-4",

src/config/secure-config.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const reach = require('../utils/reach')
88
const IConfig = require('./iconfig')
99

1010
const PASSWORD_HASHING_ROUNDS = 1000000
11+
const DEFAULT_TIMEOUT_MS = 5*60*1000 //! 5min
1112

1213
class SecureConfig extends IConfig {
1314
constructor({
1415
id = 'secure-config',
15-
config, timeoutMs=15*1000, includeActivity=true
16+
config, timeoutMs=DEFAULT_TIMEOUT_MS, includeActivity=true
1617
}){
1718
super()
1819
this.id = id || 'secure-config'
@@ -22,7 +23,7 @@ class SecureConfig extends IConfig {
2223
this.identity = null
2324
this.timer = null
2425
this.lastActivity = null
25-
this.timeoutMs = timeoutMs || 15*1000
26+
this.timeoutMs = timeoutMs || DEFAULT_TIMEOUT_MS
2627
this.includeActivity = includeActivity || true
2728

2829
this.blocked = false
@@ -58,8 +59,19 @@ class SecureConfig extends IConfig {
5859
const salt = await dataparty_crypto.Routines.generateSalt()
5960
const rounds = PASSWORD_HASHING_ROUNDS
6061

62+
63+
await this.config.write('salt', salt.toString('hex'))
64+
await this.config.write('rounds', rounds)
65+
6166
let key = await dataparty_crypto.Routines.createKeyFromPassword(password, salt, rounds)
6267

68+
await this.setIdentity(key)
69+
}
70+
71+
async setIdentity(key){
72+
debug('setIdentity')
73+
if(await this.isInitialized()){ throw new Error('already initialized') }
74+
6375
const pwIdentity = new dataparty_crypto.Identity({
6476
key,
6577
id: this.id,
@@ -69,9 +81,6 @@ class SecureConfig extends IConfig {
6981

7082
await initialContent.encrypt(pwIdentity, pwIdentity.toMini())
7183

72-
await this.config.write('salt', salt.toString('hex'))
73-
await this.config.write('rounds', rounds)
74-
//await this.config.write('identity', pwIdentity.toJSON())
7584
await this.config.write('content', initialContent.toJSON())
7685

7786
debug('\t', 'identity', pwIdentity)
@@ -142,7 +151,9 @@ class SecureConfig extends IConfig {
142151

143152
this.identity = pwIdentity
144153

145-
this.timer = setTimeout(this.onTimeout.bind(this), this.timeoutMs)
154+
if(this.timeoutMs >= 0){
155+
this.timer = setTimeout(this.onTimeout.bind(this), this.timeoutMs)
156+
}
146157

147158
this.emit('unlocked')
148159
}
@@ -176,7 +187,11 @@ class SecureConfig extends IConfig {
176187
}
177188

178189
clearTimeout(this.timer)
179-
this.timer = setTimeout(this.onTimeout.bind(this), this.timeoutMs)
190+
191+
if(this.timeoutMs >= 0){
192+
this.timer = setTimeout(this.onTimeout.bind(this), this.timeoutMs)
193+
}
194+
this.lastActivity = Date.now()
180195
}
181196

182197
async clear(){

src/service/iservice.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const Path = require('path')
3-
const NCC = require('@vercel/ncc')
3+
//const NCC = require('@vercel/ncc')
4+
const NCC = require('@zeit/ncc')
45
const Hoek = require('@hapi/hoek')
56
const {JSONPath} = require('jsonpath-plus')
67
const gitRepoInfo = require('git-repo-info')

src/service/service-host.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const bodyParser = require('body-parser')
88
const expressListRoutes = require('express-list-routes')
99
const debug = require('debug')('dataparty.service.host')
1010

11+
const reach = require('../utils/reach')
12+
1113
const ServiceHostWebsocket = require('./service-host-websocket')
1214

1315
const Pify = async (p)=>{
@@ -24,7 +26,7 @@ class ServiceHost {
2426
* @param {string} options.listenUri The uri of the host interface to tell express to listen on. Defaults to `http://0.0.0.0:4001
2527
* @param {boolean} options.i2pEnabled When true, this server will be available over i2p
2628
* @param {string} options.i2pSamHost The hostname of the i2p SAM control API. Defaults to `127.0.0.1`
27-
* @param {Integer} options.i2pSamPort The port of the i2p SAM control API. Defaults to `4444`
29+
* @param {Integer} options.i2pSamPort The port of the i2p SAM control API. Defaults to `7654`
2830
* @param {string} options.i2pKey When set this i2p key will be used to host the service. If not set a new i2p key will be generated. Defaults to `null`
2931
* @param {boolean} options.wsEnabled When true the server will host a dataparty websocket service. Defaults to `true`
3032
* @param {Integer} options.wsPort Port for the websocket service to listen on. Defaults to `null`, using the same port as the http server.
@@ -38,7 +40,7 @@ class ServiceHost {
3840
listenUri = 'http://0.0.0.0:4001',
3941
i2pEnabled = false,
4042
i2pSamHost = '127.0.0.1',
41-
i2pSamPort = 4444,
43+
i2pSamPort = 7656,
4244
i2pKey = null,
4345
wsEnabled = true,
4446
wsPort = null,
@@ -107,8 +109,8 @@ class ServiceHost {
107109
privateKey: reach(i2pKey, 'privateKey')
108110
},
109111
forward: {
110-
host: this.apiServerUri.host,
111-
port: this.apiServerUri.port
112+
host: '127.0.0.1', //this.apiServerUri.host,
113+
port: 4001 //this.apiServerUri.port
112114
}
113115
}
114116
}
@@ -178,17 +180,30 @@ class ServiceHost {
178180
}
179181

180182
if(this.i2pEnabled && this.i2p == null){
181-
debug('starting i2p forward')
183+
debug('starting i2p forward', this.i2pSettings)
182184
const SAM = require('@diva.exchange/i2p-sam')
183185

184186
this.i2p = await SAM.createForward(this.i2pSettings)
185-
this.i2pUri = this.i2p.getPublicKeys()
187+
this.i2pUri = this.i2p.getB32Address()
186188
this.i2pSettings.privateKey = null // clear no longer needed
187189

190+
191+
188192
this.i2p.on('error', this.reportI2pError.bind(this))
189193

194+
195+
this.i2p.on('close', ()=>{
196+
debug('i2p closed')
197+
})
198+
199+
this.i2p.on('data', (data)=>{
200+
debug('i2p data')
201+
debug(data.toString())
202+
})
203+
190204
debug('i2p started')
191-
debug('\t','address', this.i2pUri)
205+
debug('\t', 'address', this.i2pUri)
206+
debug('\t', 'key', this.i2p.getPublicKey())
192207
}
193208
}
194209

src/service/service-runner-node.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ class ServiceRunnerNode {
151151

152152
if(!this.useNative){
153153
const build = Hoek.reach(this.service, `compiled.endpoints.${name}`)
154+
//debug('build', build)
154155
endpoint = eval(build.code/*, build.map*/)
155156
}
156157
else{
157158
endpoint = this.service.constructors.endpoints[name]
158159
}
159160

161+
debug('endpoint', endpoint)
160162

161163
debug('endpoint info', endpoint.info)
162164

@@ -265,7 +267,7 @@ class ServiceRunnerNode {
265267
* @returns
266268
*/
267269
async onRequest(req, res){
268-
debug('onRequest', req)
270+
debug('onRequest')
269271

270272
debug('req', req.method, req.hostname,'-', req.url, req.ips, req.body)
271273

0 commit comments

Comments
 (0)