Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing refactor switch merge #1

Merged
merged 667 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
667 commits
Select commit Hold shift + click to select a range
ac37652
chore: update contributors
daviddias Mar 31, 2017
cbd2a32
chore: release version v0.29.0
daviddias Mar 31, 2017
a5b1b63
fix: move emitters to last thing in the method (#218)
daviddias Apr 6, 2017
9e6a5ff
chore: update contributors
daviddias Apr 6, 2017
5274e87
chore: release version v0.29.1
daviddias Apr 6, 2017
8b86473
fix: move 'pull-stream' from devDependencies to dependencies (#220)
ArnaudValensi Jul 7, 2017
a1eeb27
chore: update deps
daviddias Jul 8, 2017
3bbb4fd
chore: update contributors
daviddias Jul 8, 2017
e306440
chore: release version v0.29.2
daviddias Jul 8, 2017
80c3853
feat: dial to PeerId and/or Multiaddr in addition to PeerInfo (#222)
daviddias Jul 17, 2017
c5b39d5
chore: update contributors
daviddias Jul 17, 2017
8c8a5ed
chore: release version v0.30.0
daviddias Jul 17, 2017
200c213
chore: no sauce
daviddias Jul 20, 2017
8e5f8df
chore: update deps
daviddias Jul 22, 2017
8b5f162
chore: update contributors
daviddias Jul 22, 2017
7144f0b
chore: release version v0.31.0
daviddias Jul 22, 2017
4a9ecf2
fix: use the right callback
mkg20001 Aug 24, 2017
24ae382
chore: update deps
daviddias Aug 24, 2017
ef1308b
chore: update contributors
daviddias Aug 24, 2017
5c9556e
chore: release version v0.31.1
daviddias Aug 24, 2017
77bf1ab
feat: increase maxListeners to Infinity (#226)
daviddias Aug 25, 2017
d0e33f6
chore: update deps
daviddias Aug 25, 2017
0196ab6
chore: update contributors
daviddias Aug 25, 2017
db9def3
chore: release version v0.31.2
daviddias Aug 25, 2017
6163bed
feat: p2p addrs situation (#229)
daviddias Sep 3, 2017
f266280
chore: update contributors
daviddias Sep 3, 2017
c1090e3
chore: release version v0.32.0
daviddias Sep 3, 2017
85f4a14
chore: update deps
daviddias Sep 7, 2017
0ad057b
chore: update contributors
daviddias Sep 7, 2017
53cda74
chore: release version v0.32.1
daviddias Sep 7, 2017
619c8e9
fix: remove unused protocol-buffers dep (#230)
dignifiedquire Sep 7, 2017
d08c919
chore: update contributors
daviddias Sep 7, 2017
251bdd5
chore: release version v0.32.2
daviddias Sep 7, 2017
dce497a
chore: update deps
daviddias Sep 7, 2017
1487cfe
chore: update contributors
daviddias Sep 7, 2017
114fd54
chore: release version v0.32.3
daviddias Sep 7, 2017
292e922
chore: update deps
daviddias Sep 10, 2017
6bd287c
fix: increase dial timeout
daviddias Sep 10, 2017
a2d1329
chore: update contributors
daviddias Sep 10, 2017
4208f24
chore: release version v0.32.4
daviddias Sep 10, 2017
30fb36f
feat: Circuit Relay (#224)
dryajov Oct 23, 2017
8578c82
chore: update deps
daviddias Oct 23, 2017
21ed197
chore: update contributors
daviddias Oct 23, 2017
3183025
chore: release version v0.33.0
daviddias Oct 23, 2017
94b00dd
fix: don't dial on relay if not enabled (#234)
dryajov Oct 27, 2017
bb4015f
chore: update deps
daviddias Oct 27, 2017
de56586
chore: fix package.json
daviddias Oct 27, 2017
89095ad
chore: update contributors
daviddias Oct 27, 2017
d257c61
chore: release version v0.33.1
daviddias Oct 27, 2017
661e75a
chore: update deps
daviddias Nov 12, 2017
f22ee2a
fix: don't dial circuit if no transports available (#236)
dryajov Nov 18, 2017
f8219e4
chore: update contributors
daviddias Nov 18, 2017
232f61f
chore: release version v0.33.2
daviddias Nov 18, 2017
77a4f61
fix: circuit dialing
dryajov Dec 14, 2017
3845085
chore: updating CI files (#238)
victorb Dec 14, 2017
8ea4ac0
chore: update contributors
daviddias Dec 14, 2017
541c4ba
chore: release version v0.34.0
daviddias Dec 14, 2017
75ba1c1
chore: use latest SECIO API
daviddias Jan 7, 2018
60b43b8
chore: update contributors
daviddias Jan 7, 2018
33c6db7
chore: release version v0.35.0
daviddias Jan 7, 2018
94d8be2
chore: update deps
daviddias Jan 28, 2018
3fb3c0e
chore: update contributors
daviddias Jan 28, 2018
5c0a1d4
chore: release version v0.35.1
daviddias Jan 28, 2018
e483e33
docs: update name references and API touches
daviddias Feb 6, 2018
0ecafd7
chore: update name references
daviddias Feb 6, 2018
43de7aa
refactor: update name to switch, make it a class and rename start and…
daviddias Feb 6, 2018
d559fa5
test: refactor tcp transport tests to avoid code duplication
daviddias Feb 6, 2018
a11b277
test: reuse same test code for Websockets, remove code duplication
daviddias Feb 6, 2018
a283588
test: update aegir pre and post hooks
daviddias Feb 6, 2018
19c3bce
chore: use pre-push instead
daviddias Feb 6, 2018
2c1184b
test: update and deduplicate code on stream muxing tests
daviddias Feb 6, 2018
e825f04
test: restructure test suits
daviddias Feb 6, 2018
9cde7c9
test: refactor swarm-no-muxing tests
daviddias Feb 6, 2018
d522f3a
test: refactor circuit-relay tests
daviddias Feb 6, 2018
5af5660
test: refactor browser tests too
daviddias Feb 6, 2018
e672e1a
style: fix linting
daviddias Feb 6, 2018
69ea434
fix: enableCircuitRelay is async and therefore needs a callback
daviddias Feb 6, 2018
ca610a5
fix: transports.add does not need to be async at all
daviddias Feb 6, 2018
ea84d97
docs: fix badges
daviddias Feb 6, 2018
9121dca
test: Linux does not like that we use multiple sockets with port 0
daviddias Feb 6, 2018
5573289
test: fix test
daviddias Feb 6, 2018
d416b3f
chore: update contributors
daviddias Feb 7, 2018
0b75bcd
chore: release version v0.36.0
daviddias Feb 7, 2018
fd232b8
chore: update deps
daviddias Feb 12, 2018
9d4bfd6
chore: update contributors
daviddias Feb 12, 2018
52967cd
chore: release version v0.36.1
daviddias Feb 12, 2018
0f0f314
feat: use mplex, update CI
daviddias Feb 19, 2018
613e30d
docs: typo
daviddias Feb 19, 2018
a4521dd
feat: observe traffic and expose statistics (#243)
pgte Feb 27, 2018
cf56511
chore: update deps
daviddias Feb 27, 2018
846c3c2
chore: update contributors
daviddias Feb 27, 2018
8b326a7
chore: release version v0.37.0
daviddias Feb 27, 2018
ae6b52e
fix: for when handler func is not defined
pgte Mar 9, 2018
6f98b0c
fix: for when peerinfo resolves to undefined
pgte Mar 12, 2018
439e6a2
chore: update contributors
daviddias Mar 12, 2018
e40d351
chore: release version v0.37.1
daviddias Mar 12, 2018
f45b4a7
chore: update deps
daviddias Mar 14, 2018
e57624d
chore: update contributors
daviddias Mar 14, 2018
93c558f
chore: release version v0.37.2
daviddias Mar 14, 2018
6265ec6
fix: one more observer edge case
pgte Mar 15, 2018
fc5a0c8
chore: update deps
daviddias Mar 15, 2018
a0c877e
chore: fix linting
daviddias Mar 15, 2018
085661f
test: fix transport tests before all step by increasing the timeout
daviddias Mar 15, 2018
9f5efce
chore: update contributors
daviddias Mar 15, 2018
b40ce97
chore: release version v0.37.3
daviddias Mar 15, 2018
7c60361
chore: update deps
harrshasri Apr 3, 2018
132d5df
fix: revert version back to the current release
daviddias Apr 3, 2018
c0eae1e
chore: update deps
daviddias Apr 4, 2018
b126249
chore: update deps
daviddias Apr 5, 2018
895e433
chore: update contributors
daviddias Apr 5, 2018
26c94f0
chore: update deps
daviddias Apr 5, 2018
55560c7
test: timeout
daviddias Apr 5, 2018
17285c4
chore: update contributors
daviddias Apr 5, 2018
97bb973
chore: release version v0.39.0
daviddias Apr 5, 2018
f505ee8
chore: update deps
daviddias Apr 30, 2018
1b7725e
chore: update contributors
daviddias Apr 30, 2018
4b739a2
chore: update deps
daviddias Apr 30, 2018
83a08b8
chore: update contributors
daviddias Apr 30, 2018
1f6b8f7
chore: release version v0.39.2
daviddias Apr 30, 2018
63ad87a
feat: improve circuit err messages (#250)
dryajov May 1, 2018
1166fb9
refactor: update files and add jsdocs to improve readability
jacobheun Apr 23, 2018
b5a2d38
test: add tests for get-peer-info
jacobheun Apr 27, 2018
30ed081
docs: remove answered todo comment
jacobheun Apr 30, 2018
71b6316
fix: dont create base conn when muxed exists
jacobheun Apr 30, 2018
350695c
fix: tests and conflicts
jacobheun May 1, 2018
aba7d1b
chore: update deps
daviddias May 1, 2018
3826d00
chore: update contributors
daviddias May 1, 2018
4c82bb5
chore: release version v0.40.0
daviddias May 1, 2018
36bc50b
test: fix require of multiplex
dryajov May 12, 2018
5cdec34
fix: libp2p/js-libp2p#189 Prevent self-dial
mkg20001 May 22, 2018
1f3a4eb
test: add selfdial test
mkg20001 May 22, 2018
ce8c43d
chore: add lead maintainer
jacobheun May 7, 2018
8749f2a
Merge tag 'v0.40.0' of https://github.com/libp2p/js-libp2p-switch
jacobheun May 23, 2018
d9f5738
chore: update contributors
jacobheun May 23, 2018
f126ad0
chore: update contributors
jacobheun May 23, 2018
4b60484
chore: release version v0.40.1
jacobheun May 23, 2018
fef2d11
fix: return on call to nextMuxer
alanshaw May 30, 2018
cb5245a
fix: drop connection when stream ends unexpectedly
olizilla May 30, 2018
1eb28ca
fix: add utility methods to prevent already piped error
jacobheun May 31, 2018
704e316
chore: update contributors
jacobheun May 31, 2018
bd24b23
chore: release version v0.40.2
jacobheun May 31, 2018
81d4394
fix: prevent undefined error during a mutual hangup
jacobheun May 29, 2018
4fce859
chore: update contributors
jacobheun Jun 1, 2018
6562463
chore: release version v0.40.3
jacobheun Jun 1, 2018
888e973
feat: swap quick-lru by hashlru
hugomrdias May 29, 2018
4963a67
chore: update contributors
jacobheun Jun 6, 2018
79344d2
chore: release version v0.40.4
jacobheun Jun 6, 2018
41210cf
fix: stats - observer expects protocolTag
kumavis Jul 9, 2018
c4ed449
fix: re-enable stats tests in node
dryajov Jul 9, 2018
43fe7d8
chore: Upgrade big.js to 5.1.2
antimatter15 Jul 9, 2018
fb90b68
chore: Change require('big.js') to require('big.js').Big
antimatter15 Jul 8, 2018
bdb598c
chore: update contributors
jacobheun Jul 10, 2018
e9b158d
chore: release version v0.40.5
jacobheun Jul 10, 2018
bff0a8b
fix: no stats on multistream proto dial
dryajov Jul 12, 2018
19d99d3
fix: adjust test values
dryajov Jul 12, 2018
c18e0bd
fix: handle error in protocol handshake
dryajov Jul 13, 2018
0fb1b8b
chore: update contributors
jacobheun Jul 17, 2018
cd794d7
chore: release version v0.40.6
jacobheun Jul 17, 2018
5ee5ac2
chore: remove travis and circleci
jacobheun Jul 17, 2018
b6600da
Add private network support (#266)
Jul 19, 2018
a62a72b
fix: improve erroring around invalid peers
jacobheun Jul 18, 2018
c469745
chore: update contributors
jacobheun Jul 23, 2018
aa7f2e8
chore: release version v0.40.7
jacobheun Jul 23, 2018
30310f8
test: add sample network circuit relay tests (#275)
jacobheun Aug 15, 2018
caf1958
chore: update deps
jacobheun Sep 7, 2018
b92fb35
chore: update contributors
jacobheun Sep 10, 2018
02145a6
chore: release version v0.40.8
jacobheun Sep 10, 2018
e3b4b42
chore: update mplex and stats test numbers
jacobheun Oct 2, 2018
7dcabdd
feat: make switch a state machine (#278)
jacobheun Oct 19, 2018
da942cb
chore: update contributors
jacobheun Oct 19, 2018
06fd7fc
chore: release version v0.41.0
jacobheun Oct 19, 2018
280d54f
fix: ignore dial request when one is in progress (#283)
jacobheun Oct 23, 2018
128d8e9
chore: update contributors
jacobheun Oct 24, 2018
fe84058
chore: release version v0.41.1
jacobheun Oct 24, 2018
e757cf6
fix: improve connection closing and error handling (#285)
jacobheun Nov 15, 2018
f406dd5
fix: dont dial an address that we have
jacobheun Nov 12, 2018
7697281
fix: ensure circuit listens last on start
jacobheun Nov 15, 2018
7a9e924
chore: update npm publish files
jacobheun Nov 15, 2018
62c2414
chore: update contributors
jacobheun Nov 15, 2018
823c776
chore: release version v0.41.2
jacobheun Nov 15, 2018
f43084b
fix: use retimer to avoid creating so many timers (#289)
mcollina Dec 13, 2018
aa86307
fix: improve connection tracking and closing (#291)
jacobheun Dec 14, 2018
78ab5ae
chore: update contributors
jacobheun Dec 14, 2018
1b445cf
chore: release version v0.41.3
jacobheun Dec 14, 2018
b29679d
refactor: stat use for over forEach (#295)
mcollina Dec 19, 2018
089835e
fix: avoid sync callback in async functions (#297)
jacobheun Dec 20, 2018
f65df95
chore: update contributors
jacobheun Dec 20, 2018
5b498a0
chore: release version v0.41.4
jacobheun Dec 20, 2018
b3a6a74
fix: peerBook undefined #299
jacobheun Jan 2, 2019
76e5d1d
fix: reduce bundle size (#292)
hugomrdias Jan 11, 2019
8525bee
chore: update contributors
jacobheun Jan 11, 2019
6fade87
chore: release version v0.41.5
jacobheun Jan 11, 2019
86e0a7a
fix: import async/setImmediate to avoid webpack errors (#303)
PiusNyakoojo Jan 28, 2019
d504040
test: add pull-mplex to test suite (#305)
jacobheun Feb 15, 2019
ddf622b
fix: dial in series until we have proper abort support (#306)
jacobheun Mar 5, 2019
ca78e3b
chore: update contributors
jacobheun Mar 6, 2019
33d49e9
chore: release version v0.41.6
jacobheun Mar 6, 2019
f731cdc
fix: peer disconnect event and improve logging performance (#309)
jacobheun Mar 12, 2019
c0c1009
chore: update contributors
jacobheun Mar 13, 2019
4216144
chore: release version v0.41.7
jacobheun Mar 13, 2019
6a94d9a
feat: add basic dial queue to avoid many connections to peer (#310)
jacobheun Mar 20, 2019
46e2509
feat: update identify to include supported protocols (#311)
jacobheun Mar 21, 2019
485ff7f
chore: update contributors
jacobheun Mar 21, 2019
45a86af
chore: release version v0.42.0
jacobheun Mar 21, 2019
16f2bc3
fix: ensure dials always use the latest PeerInfo from the PeerBook (#…
jacobheun Mar 21, 2019
ce0c76b
chore: update contributors
jacobheun Mar 21, 2019
b318e3f
chore: release version v0.42.1
jacobheun Mar 21, 2019
6437139
fix: identify on dial (#313)
jacobheun Mar 25, 2019
ad9a061
chore: update contributors
jacobheun Mar 25, 2019
1e23eb2
chore: release version v0.42.2
jacobheun Mar 25, 2019
20175dd
feat: global dial queue (#314)
jacobheun Mar 28, 2019
d4351db
chore: update contributors
jacobheun Mar 28, 2019
57146a0
chore: release version v0.42.3
jacobheun Mar 28, 2019
fcbcccc
fix: improve dial queue and parallel dials (#315)
jacobheun Mar 28, 2019
f7a8243
chore: update contributors
jacobheun Mar 28, 2019
d550b48
chore: release version v0.42.4
jacobheun Mar 28, 2019
4a543cb
feat: limit the number of cold calls we can do (#316)
jacobheun Apr 2, 2019
828e685
fix: improve connection tracking (#318)
jacobheun Apr 3, 2019
3e8114e
chore: update contributors
jacobheun Apr 3, 2019
0f05b9f
chore: release version v0.42.5
jacobheun Apr 3, 2019
f31663f
fix: dont blacklist good peers (#319)
jacobheun Apr 3, 2019
805d1ad
fix: revert to try each (#320)
alanshaw Apr 3, 2019
014f4d0
chore: update contributors
jacobheun Apr 3, 2019
9592c22
chore: release version v0.42.6
jacobheun Apr 3, 2019
aeadc1b
fix: missing queue (#323)
alanshaw Apr 3, 2019
5f9a4f6
fix: improve stopping logic (#324)
jacobheun Apr 4, 2019
eb910f6
chore: update contributors
jacobheun Apr 4, 2019
c7b1d05
chore: release version v0.42.7
jacobheun Apr 4, 2019
9584a4b
chore: add discourse badge (#327)
vasco-santos Apr 10, 2019
4862c48
fix: dial self (#329)
alanshaw Apr 10, 2019
0357bf2
feat: support a priority queue for dials (#325)
jacobheun Apr 10, 2019
909d0d2
chore: update contributors
jacobheun Apr 10, 2019
866e5a6
chore: release version v0.42.8
jacobheun Apr 10, 2019
bb1a235
fix: dont compare empty strings (#330)
jacobheun Apr 11, 2019
b0a5054
chore: update contributors
jacobheun Apr 11, 2019
bf4d926
chore: release version v0.42.9
jacobheun Apr 11, 2019
951e0c9
fix: resolve transport sort order in browsers (#333)
jacobheun Apr 16, 2019
9af0e3a
fix: dont use peerinfo distinct (#334)
jacobheun Apr 16, 2019
15f1b26
chore: update contributors
jacobheun Apr 16, 2019
c73efba
chore: release version v0.42.10
jacobheun Apr 16, 2019
7aebb9c
fix(stats): prevent 0ms timeDiff breaking movingAverage (#336)
kumavis Apr 25, 2019
d02b808
chore: update contributors
jacobheun Apr 25, 2019
3ca691f
chore: release version v0.42.11
jacobheun Apr 25, 2019
f879cfc
fix: dont blindly add observed addresses to our list (#337)
jacobheun Apr 26, 2019
f306cba
fix: clear blacklist for peer when connection is established (#340)
jacobheun Jun 5, 2019
44a8690
chore: update contributors
jacobheun Jun 5, 2019
a386bb7
chore: release version v0.42.12
jacobheun Jun 5, 2019
2e71946
refactor: move switch into src/switch
jacobheun Jul 29, 2019
5670c42
chore: merge in switch to libp2p
jacobheun Jul 29, 2019
f5e6f74
refactor: cleanup switch and move tests into test dir
jacobheun Jul 29, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"libp2p-connection-manager": "^0.1.0",
"libp2p-floodsub": "^0.16.1",
"libp2p-ping": "^0.8.5",
"libp2p-switch": "^0.42.12",
"libp2p-switch": "./src/switch",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
"multiaddr": "^6.1.0",
Expand Down
448 changes: 448 additions & 0 deletions src/switch/README.md

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions src/switch/connection/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict'

const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const withIs = require('class-is')

class BaseConnection extends EventEmitter {
constructor ({ _switch, name }) {
super()

this.switch = _switch
this.ourPeerInfo = this.switch._peerInfo
this.log = debug(`libp2p:conn:${name}`)
this.log.error = debug(`libp2p:conn:${name}:error`)
}

/**
* Puts the state into its disconnecting flow
*
* @param {Error} err Will be emitted if provided
* @returns {void}
*/
close (err) {
if (this._state._state === 'DISCONNECTING') return
this.log('closing connection to %s', this.theirB58Id)
if (err && this._events.error) {
this.emit('error', err)
}
this._state('disconnect')
}

emit (eventName, ...args) {
if (eventName === 'error' && !this._events.error) {
this.log.error(...args)
} else {
super.emit(eventName, ...args)
}
}

/**
* Gets the current state of the connection
*
* @returns {string} The current state of the connection
*/
getState () {
return this._state._state
}

/**
* Puts the state into encrypting mode
*
* @returns {void}
*/
encrypt () {
this._state('encrypt')
}

/**
* Puts the state into privatizing mode
*
* @returns {void}
*/
protect () {
this._state('privatize')
}

/**
* Puts the state into muxing mode
*
* @returns {void}
*/
upgrade () {
this._state('upgrade')
}

/**
* Event handler for disconnected.
*
* @fires BaseConnection#close
* @returns {void}
*/
_onDisconnected () {
this.switch.connection.remove(this)
this.log('disconnected from %s', this.theirB58Id)
this.emit('close')
this.removeAllListeners()
}

/**
* Event handler for privatized
*
* @fires BaseConnection#private
* @returns {void}
*/
_onPrivatized () {
this.emit('private', this.conn)
}

/**
* Wraps this.conn with the Switch.protector for private connections
*
* @private
* @fires ConnectionFSM#error
* @returns {void}
*/
_onPrivatizing () {
if (!this.switch.protector) {
return this._state('done')
}

this.conn = this.switch.protector.protect(this.conn, (err) => {
if (err) {
return this.close(err)
}

this.log('successfully privatized conn to %s', this.theirB58Id)
this.conn.setPeerInfo(this.theirPeerInfo)
this._state('done')
})
}
}

module.exports = withIs(BaseConnection, {
className: 'BaseConnection',
symbolName: 'libp2p-switch/BaseConnection'
})
47 changes: 47 additions & 0 deletions src/switch/connection/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const debug = require('debug')
const IncomingConnection = require('./incoming')
const observeConn = require('../observe-connection')

function listener (_switch) {
const log = debug(`libp2p:switch:listener`)

/**
* Takes a transport key and returns a connection handler function
*
* @param {string} transportKey The key of the transport to handle connections for
* @param {function} handler A custom handler to use
* @returns {function(Connection)} A connection handler function
*/
return function (transportKey, handler) {
/**
* Takes a base connection and manages listening behavior
*
* @param {Connection} conn The connection to manage
* @returns {void}
*/
return function (conn) {
log('received incoming connection for transport %s', transportKey)
conn.getPeerInfo((_, peerInfo) => {
// Add a transport level observer, if needed
const connection = transportKey ? observeConn(transportKey, null, conn, _switch.observer) : conn
const connFSM = new IncomingConnection({ connection, _switch, transportKey, peerInfo })

connFSM.once('error', (err) => log(err))
connFSM.once('private', (_conn) => {
// Use the custom handler, if it was provided
if (handler) {
return handler(_conn)
}
connFSM.encrypt()
})
connFSM.once('encrypted', () => connFSM.upgrade())

connFSM.protect()
})
}
}
}

module.exports = listener
115 changes: 115 additions & 0 deletions src/switch/connection/incoming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
'use strict'

const FSM = require('fsm-event')
const multistream = require('multistream-select')
const withIs = require('class-is')

const BaseConnection = require('./base')

class IncomingConnectionFSM extends BaseConnection {
constructor ({ connection, _switch, transportKey, peerInfo }) {
super({
_switch,
name: `inc:${_switch._peerInfo.id.toB58String().slice(0, 8)}`
})
this.conn = connection
this.theirPeerInfo = peerInfo || null
this.theirB58Id = this.theirPeerInfo ? this.theirPeerInfo.id.toB58String() : null
this.ourPeerInfo = this.switch._peerInfo
this.transportKey = transportKey
this.protocolMuxer = this.switch.protocolMuxer(this.transportKey)
this.msListener = new multistream.Listener()

this._state = FSM('DIALED', {
DISCONNECTED: {
disconnect: 'DISCONNECTED'
},
DIALED: { // Base connection to peer established
privatize: 'PRIVATIZING',
encrypt: 'ENCRYPTING'
},
PRIVATIZING: { // Protecting the base connection
done: 'PRIVATIZED',
disconnect: 'DISCONNECTING'
},
PRIVATIZED: { // Base connection is protected
encrypt: 'ENCRYPTING'
},
ENCRYPTING: { // Encrypting the base connection
done: 'ENCRYPTED',
disconnect: 'DISCONNECTING'
},
ENCRYPTED: { // Upgrading could not happen, the connection is encrypted and waiting
upgrade: 'UPGRADING',
disconnect: 'DISCONNECTING'
},
UPGRADING: { // Attempting to upgrade the connection with muxers
done: 'MUXED'
},
MUXED: {
disconnect: 'DISCONNECTING'
},
DISCONNECTING: { // Shutting down the connection
done: 'DISCONNECTED'
}
})

this._state.on('DISCONNECTED', () => this._onDisconnected())
this._state.on('PRIVATIZING', () => this._onPrivatizing())
this._state.on('PRIVATIZED', () => this._onPrivatized())
this._state.on('ENCRYPTING', () => this._onEncrypting())
this._state.on('ENCRYPTED', () => {
this.log('successfully encrypted connection to %s', this.theirB58Id || 'unknown peer')
this.emit('encrypted', this.conn)
})
this._state.on('UPGRADING', () => this._onUpgrading())
this._state.on('MUXED', () => {
this.log('successfully muxed connection to %s', this.theirB58Id || 'unknown peer')
this.emit('muxed', this.conn)
})
this._state.on('DISCONNECTING', () => {
this._state('done')
})
}

/**
* Attempts to encrypt `this.conn` with the Switch's crypto.
*
* @private
* @fires IncomingConnectionFSM#error
* @returns {void}
*/
_onEncrypting () {
this.log('encrypting connection via %s', this.switch.crypto.tag)

this.msListener.addHandler(this.switch.crypto.tag, (protocol, _conn) => {
this.conn = this.switch.crypto.encrypt(this.ourPeerInfo.id, _conn, undefined, (err) => {
if (err) {
return this.close(err)
}
this.conn.getPeerInfo((_, peerInfo) => {
this.theirPeerInfo = peerInfo
this._state('done')
})
})
}, null)

// Start handling the connection
this.msListener.handle(this.conn, (err) => {
if (err) {
this.emit('crypto handshaking failed', err)
}
})
}

_onUpgrading () {
this.log('adding the protocol muxer to the connection')
this.protocolMuxer(this.conn, this.msListener)
this._state('done')
}
}

module.exports = withIs(IncomingConnectionFSM, {
className: 'IncomingConnectionFSM',
symbolName: 'libp2p-switch/IncomingConnectionFSM'
})
Loading