Skip to content

Commit a8719f4

Browse files
committed
support i2p host forwarding
1 parent 04d5796 commit a8719f4

14 files changed

Lines changed: 187 additions & 22 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@dataparty/bouncer-db": "1.0.1",
6262
"@dataparty/crypto": "datapartyjs/dataparty-crypto#parcel-build",
6363
"@dataparty/tasker": "^0.0.2",
64+
"@diva.exchange/i2p-sam": "^4.1.8",
6465
"@hapi/joi": "^17.1.1",
6566
"@zeit/ncc": "^0.22.3",
6667
"ajv": "6.9.1",

src/service/endpoint-context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class EndpointContext {
4444

4545
setSession(session){
4646
this.session = session
47-
this.debug('session.id' + session.id)
47+
this.debug('session' + session)
4848
}
4949

5050
setOauthCloud(oauth_cloud){

src/service/endpoints/service-identity.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ module.exports = class ServiceIdentity extends IEndpoint {
2626
validate: Joi.object().keys({
2727
id: Joi.string(),
2828
key: {
29-
type: Joi.string().valid('ecdsa'),
29+
type: Joi.alternatives().try(
30+
Joi.string().valid('ecdsa'),
31+
Joi.string().valid('nacl')
32+
),
3033
public: Joi.object().keys({
3134
box: Joi.string(),
3235
sign: Joi.string()

src/service/middleware/pre/decrypt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = class Decrypt extends IMiddleware {
5151

5252

5353
context.setSenderKey({
54-
type: 'ecdsa',
54+
type: 'nacl',
5555
public: publicKeys
5656
})
5757

src/service/service-host-websocket.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ const debug = require('debug')('dataparty.service.host-websocket')
44
const ws = require('ws')
55
const WebSocketServer = ws.WebSocketServer
66

7-
const WATCHDOG_INTERVAL = 30000
7+
const WATCHDOG_INTERVAL = 5*60*1000
88

99
const Comms = require('../comms')
1010
const PeerParty = require('../party/peer/peer-party')
1111

1212
class ServiceHostWebsocket{
1313

14-
constructor({trust_proxy, port, path, runner, wsSettings}){
14+
constructor({trust_proxy, port, upgradePath, runner, wsSettings}){
1515
this.port = port
16-
this.path = path || '/ws'
16+
this.upgradePath = upgradePath
1717
this.runner = runner
1818
this.trust_proxy = trust_proxy
1919
this.wsSettings = wsSettings || {}
@@ -49,7 +49,7 @@ class ServiceHostWebsocket{
4949

5050
debug('handleUpgrade', request.headers.host, request.url)
5151

52-
if(request.url == this.path){
52+
if(request.url == this.upgradePath){
5353
this.doUpgrade(request, socket, head)
5454
} else {
5555
socket.destroy()
@@ -96,6 +96,7 @@ class ServiceHostWebsocket{
9696

9797
conn.on('close',()=>{
9898
debug('connection closed', conn.ip)
99+
conn.isAlive = false
99100
})
100101

101102
debug('creating peer party')

src/service/service-host.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const CORS = require('cors')
22
const {URL} = require('url')
3-
const net = require('net')
43
const http = require('http')
54
const https = require('https')
65
const morgan = require('morgan')
@@ -24,8 +23,13 @@ class ServiceHost {
2423
cors = {},
2524
trust_proxy = false,
2625
listenUri = 'http://0.0.0.0:4001',
26+
i2pEnabled = false,
27+
i2pSamHost = '127.0.0.1',
28+
i2pSamPort = 4444,
29+
i2pKey = null,
2730
wsEnabled = true,
2831
wsPort = null,
32+
wsUpgradePath = '/ws',
2933
runner
3034
}={}){
3135
this.apiApp = express()
@@ -55,12 +59,30 @@ class ServiceHost {
5559
this.wsServer = new ServiceHostWebsocket({
5660
trust_proxy,
5761
port: wsPort,
62+
upgradePath: wsUpgradePath,
5863
runner: this.runner
5964
})
6065
}
6166

62-
this.started = false
67+
if(i2pEnabled){
68+
this.i2pEnabled = true
69+
70+
this.i2p = null
71+
this.i2pSettings = {
72+
sam: {
73+
host: i2pSamHost,
74+
portTCP: i2pSamPort,
75+
publicKey: reach(i2pKey, 'publicKey'),
76+
privateKey: reach(i2pKey, 'privateKey')
77+
},
78+
forward: {
79+
host: this.apiServerUri.host,
80+
port: this.apiServerUri.port
81+
}
82+
}
83+
}
6384

85+
this.started = false
6486
}
6587

6688
async start(){
@@ -118,6 +140,18 @@ class ServiceHost {
118140
debug('starting websocket')
119141
this.wsServer.start(this.apiServer)
120142
}
143+
144+
if(this.i2pEnabled){
145+
debug('starting i2p forward')
146+
const SAM = require('@diva.exchange/i2p-sam')
147+
148+
this.i2p = await i2p.createForward(this.i2pSettings)
149+
this.i2pUri = this.i2p.getPublicKeys()
150+
this.i2pSettings.privateKey = null // clear no longer needed
151+
152+
153+
debug('i2p address - ', this.i2pUri)
154+
}
121155
}
122156

123157
async stop(){

src/venue/endpoints/allocate-nacl-session.js

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('venue.middleware.pre.decrypt')
5+
6+
const IMiddleware = require('../../../service/imiddleware')
7+
8+
module.exports = class DecryptNaCl extends IMiddleware {
9+
10+
static get Name(){
11+
return 'decrypt-nacl'
12+
}
13+
14+
static get Type(){
15+
return 'pre'
16+
}
17+
18+
static get Description(){
19+
return 'Decrypt inbound data'
20+
}
21+
22+
static get ConfigSchema(){
23+
return Joi.boolean()
24+
}
25+
26+
static async start(party){
27+
28+
}
29+
30+
static async run(context, {Config}){
31+
32+
if (!Config){ return }
33+
34+
if(!context.input || !context.input.enc){
35+
throw new Error('insecure message')
36+
}
37+
38+
context.debug('input', context.input, typeof context.input)
39+
40+
41+
const msg = new Message(context.input)
42+
context.debug('privateIdentity', context.party.privateIdentity.id)
43+
44+
const publicKeys = Routines.extractPublicKeys(msg.enc)
45+
46+
context.debug('sender', publicKeys)
47+
context.debug(typeof context.party.privateIdentity.key.private.box)
48+
context.debug(context.input.enc)
49+
50+
const jsonContent = await msg.decrypt(context.party.privateIdentity)
51+
52+
53+
context.setSenderKey({
54+
type: 'nacl',
55+
public: publicKeys
56+
})
57+
58+
//context.setInputSession(Hoek.reach(jsonContent, 'session'))
59+
context.setInput(Hoek.reach(jsonContent, 'data'))
60+
}
61+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('venue.middleware.pre.decrypt')
5+
6+
const IMiddleware = require('../../../service/imiddleware')
7+
8+
module.exports = class DecryptNaCl extends IMiddleware {
9+
10+
static get Name(){
11+
return 'decrypt-nacl'
12+
}
13+
14+
static get Type(){
15+
return 'pre'
16+
}
17+
18+
static get Description(){
19+
return 'Decrypt inbound data'
20+
}
21+
22+
static get ConfigSchema(){
23+
return Joi.boolean()
24+
}
25+
26+
static async start(party){
27+
28+
}
29+
30+
static async run(context, {Config}){
31+
32+
if (!Config){ return }
33+
34+
if(!context.input || !context.input.enc){
35+
throw new Error('insecure message')
36+
}
37+
38+
context.debug('input', context.input, typeof context.input)
39+
40+
41+
const msg = new Message(context.input)
42+
context.debug('privateIdentity', context.party.privateIdentity.id)
43+
44+
const publicKeys = Routines.extractPublicKeys(msg.enc)
45+
46+
context.debug('sender', publicKeys)
47+
context.debug(typeof context.party.privateIdentity.key.private.box)
48+
context.debug(context.input.enc)
49+
50+
//! check if sender is a known session
51+
// if known, context.setSenderKey & context.setActor( User )
52+
// verify (sender>user)+(user>sender) trusts
53+
54+
const jsonContent = await msg.decrypt(context.party.privateIdentity)
55+
56+
57+
context.setSenderKey({
58+
type: 'nacl',
59+
public: publicKeys
60+
})
61+
62+
//context.setInputSession(Hoek.reach(jsonContent, 'session'))
63+
context.setInput(Hoek.reach(jsonContent, 'data'))
64+
}
65+
}

src/venue/schema/admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22
{
33
created: Date,
4-
user: User
4+
identity
55
}

0 commit comments

Comments
 (0)