Skip to content

Commit 39799e5

Browse files
committed
i2p socket comms for client or server usage
1 parent 5846e27 commit 39799e5

3 files changed

Lines changed: 132 additions & 10 deletions

File tree

src/comms/i2p-socket-comms.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const debug = require('debug')('dataparty.comms.i2psocket')
2+
3+
const SAM = require('@diva.exchange/i2p-sam')
4+
const EventEmitter = require('eventemitter3')
5+
6+
7+
const PeerComms = require('./peer-comms')
8+
9+
10+
class I2pStreamShim extends EventEmitter {
11+
constructor(conn){
12+
super()
13+
this.conn = conn
14+
15+
this.conn.onmessage = (evt) => {
16+
this.emit('data', evt.data)
17+
}
18+
19+
this.conn.onopen = () => {
20+
debug('shim open')
21+
setTimeout(()=>{this.emit('connect')}, 1)
22+
}
23+
24+
this.conn.onclose = () => {
25+
this.emit('close')
26+
}
27+
28+
this.conn.onerror = (err) => {
29+
this.emit('error', err)
30+
}
31+
32+
if(this.conn.readyState == WebSocket.OPEN){
33+
setTimeout(()=>{this.emit('connect')}, 1)
34+
}
35+
36+
debug('connection shim', this.conn.readyState)
37+
}
38+
39+
close(){
40+
this.conn.close()
41+
}
42+
43+
destroy(){
44+
this.conn.terminate()
45+
}
46+
47+
send(val){ this.conn.send(val) }
48+
49+
}
50+
51+
class I2pSocketComms extends PeerComms {
52+
constructor({destination, stream, samHost, remoteIdentity, host, party, ...options}){
53+
super({remoteIdentity, host, party, ...options})
54+
55+
this.stream = stream
56+
this.destination = destination
57+
this.samHost = samHost || {
58+
host: '127.0.0.1',
59+
portTCP: 7656
60+
}
61+
62+
if(this.host && !this.stream){
63+
throw new Error('existing connection expected')
64+
}
65+
66+
if(!this.host && (!this.destination && !this.stream)){
67+
throw new Error('destination or existing stream expected')
68+
}
69+
}
70+
71+
72+
async socketInit(){
73+
debug('init')
74+
75+
if(!this.host && !this.stream){
76+
debug('opening client connection to -',this.destination, ' via SAM', JSON.stringify({this.samHost,null,2}))
77+
78+
this.stream = await SAM.createStream({
79+
sam: this.samHost,
80+
stream: { destination: this.destination }
81+
})
82+
83+
} else if(this.stream){
84+
85+
debug('using existing stream', this.stream.getPublicKey())
86+
87+
}
88+
89+
this.socket = new I2pStreamShim(this.stream)
90+
}
91+
}
92+
93+
94+
module.exports = I2pSocketComms

src/comms/index-browser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const RestComms = require('./rest-comms')
2+
const SocketComms = require('./socket-comms')
3+
const PeerComms = require('./peer-comms')
4+
const WebsocketComms = require('./websocket-comms')
5+
const SocketOp = require('./socket-comms')
6+
const WebsocketOp = require('./websocket-op')
7+
const BLEMessage = require('./ble/BLEMessage')
8+
const BLEOp = require('./ble/BLEOp')
9+
const AuthOp = require('./op/auth-op')
10+
const AuthError = require('../errors/auth-error')
11+
12+
const LoopbackComms = require('./loopback-comms')
13+
const RTCSocketComms = require('./rtc-socket-comms')
14+
const LoopbackChannel = require('./loopback-channel')
15+
16+
module.exports = {
17+
RestComms, SocketComms,
18+
PeerComms, LoopbackComms, LoopbackChannel, RTCSocketComms,
19+
WebsocketComms,
20+
SocketOp, WebsocketOp, BLEMessage, BLEOp,
21+
AuthOp, AuthError
22+
}

src/comms/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
const RestComms = require('./rest-comms')
2-
const SocketComms = require('./socket-comms')
3-
const PeerComms = require('./peer-comms')
4-
const WebsocketComms = require('./websocket-comms')
5-
const SocketOp = require('./socket-comms')
6-
const WebsocketOp = require('./websocket-op')
71
const BLEMessage = require('./ble/BLEMessage')
82
const BLEOp = require('./ble/BLEOp')
93
const AuthOp = require('./op/auth-op')
104
const AuthError = require('../errors/auth-error')
115

6+
const RestComms = require('./rest-comms')
7+
const SocketComms = require('./socket-comms')
8+
129
const LoopbackComms = require('./loopback-comms')
13-
const RTCSocketComms = require('./rtc-socket-comms')
1410
const LoopbackChannel = require('./loopback-channel')
1511

12+
const PeerComms = require('./peer-comms')
13+
const WebsocketComms = require('./websocket-comms')
14+
const RTCSocketComms = require('./rtc-socket-comms')
15+
const I2pSocketComms = require('./i2p-socket-comms')
16+
17+
const SocketOp = require('./socket-comms')
18+
const WebsocketOp = require('./websocket-op')
19+
20+
21+
1622
module.exports = {
17-
RestComms, SocketComms,
18-
PeerComms, LoopbackComms, LoopbackChannel, RTCSocketComms,
19-
WebsocketComms,
23+
RestComms, SocketComms, PeerComms,
24+
LoopbackComms, LoopbackChannel,
25+
WebsocketComms, RTCSocketComms, I2pSocketComms,
2026
SocketOp, WebsocketOp, BLEMessage, BLEOp,
2127
AuthOp, AuthError
2228
}

0 commit comments

Comments
 (0)