git ssb

3+

andrestaltz / easy-ssb-pub



Commit 02d03ee0ffb24d6fc6ab8d4efade6101c173609d

Use discovery swarm to connect to other SSB peers

Andre Staltz committed on 2/27/2017, 12:56:17 PM
Parent: 13cfbc0950b2f01cf961530f12a3cc3959a35d98

Files changed

package.jsonchanged
src/index.tschanged
package.jsonView
@@ -7,8 +7,10 @@
77 "main": "dist/index.js",
88 "dependencies": {
99 "@types/express": "^4.0.35",
1010 "body-parser": "^1.16.0",
11 + "debug": "^2.6.1",
12 + "discovery-swarm": "^4.3.0",
1113 "ejs": "^2.5.5",
1214 "express": "^4.14.0",
1315 "minimist": "^1.2.0",
1416 "path": "^0.12.7",
src/index.tsView
@@ -4,23 +4,20 @@
44 import ssbKeys = require('ssb-keys');
55 import path = require('path');
66 import qr = require('qr-image');
77 import net = require('net');
8 +import swarm = require('discovery-swarm');
89 import ternaryStream = require('ternary-stream');
10 +import createDebug = require('debug');
911
12 +const debug = createDebug('easy-ssb-pub');
13 +
1014 const PUBLIC_PORT = process.env.PUBLIC_PORT || 80;
1115 const INTERNAL_COMMON_PORT = process.env.PORT || process.env.PUBLIC_PORT || 80;
1216 const EXPRESS_PORT = 8009;
1317 let SBOT_PORT = 8008;
18 +const DISCOVERY_SWARM_PORT = 8007;
1419
15-// Setup Express app ===========================================================
16-const app = express();
17-app.use(express.static(__dirname + '/public'));
18-app.use(require('body-parser').urlencoded({ extended: true }));
19-app.set('port', EXPRESS_PORT);
20-app.set('views', __dirname + '/../pages');
21-app.set('view engine', 'ejs');
22-
2320 // Setup Scuttlebot ============================================================
2421 let argv = process.argv.slice(2);
2522 const i = argv.indexOf('--');
2623 const conf = argv.slice(i + 1);
@@ -52,49 +49,65 @@
5249 id: string;
5350 qr: QRSVG;
5451 }
5552
56-let thisBotIdentity: BotIdentity | null = null;
53 +const idQR = qr.svgObject(bot.id);
5754
58-bot.whoami((err, identity: {id: string}) => {
59- if (err) {
60- console.error(err);
61- process.exit(1);
62- } else {
63- thisBotIdentity = {
64- id: identity.id,
65- qr: qr.svgObject(identity.id) as QRSVG,
66- };
67- }
68-});
69-
7055 bot.address((err, addr) => {
7156 if (err) {
7257 console.error(err);
7358 process.exit(1);
7459 } else {
75- console.log('Scuttlebot app is running on address', addr);
60 + debug('Scuttlebot app is running on address %s', addr);
7661 SBOT_PORT = (/\:(\d+)\~/g.exec(addr) as any)[1];
7762 }
7863 });
7964
80-// Setup Express routes ========================================================
65 +// Setup Discovery Swarm =======================================================
66 +var peer = swarm({
67 + maxConnections: 1000,
68 + utp: true,
69 + id: 'ssb-discovery-swarm:' + bot.id,
70 +});
71 +
72 +peer.listen(DISCOVERY_SWARM_PORT)
73 +peer.join('ssb-discovery-swarm', {announce: false}, function () {});
74 +
75 +peer.on('connection', function (connection, _info) {
76 + const info = _info;
77 + info.id = info.id.toString('ascii');
78 + info._peername = connection._peername;
79 + if (info.id.indexOf('ssb-discovery-swarm:') === 0) {
80 + debug('Discovery swarm found peer %s:%s', info.host, info.port);
81 + const remotePublicKey = info.id.split('ssb-discovery-swarm:')[1];
82 + const addr = `${info.host}:${info.port}:${remotePublicKey}`;
83 + debug(`Connecting to SSB peer ${addr} found through discovery swarm`);
84 + bot.gossip.connect(`${info.host}:${info.port}:${remotePublicKey}`, function (err) {
85 + if (err) {
86 + console.error(err);
87 + } else {
88 + debug('Successfully connected to remote SSB peer ' + addr);
89 + }
90 + });
91 + }
92 +})
93 +
94 +// Setup Express app ===========================================================
95 +const app = express();
96 +app.use(express.static(__dirname + '/public'));
97 +app.use(require('body-parser').urlencoded({ extended: true }));
98 +app.set('port', EXPRESS_PORT);
99 +app.set('views', __dirname + '/../pages');
100 +app.set('view engine', 'ejs');
101 +
81102 type Route = '/' | '/invited';
82103
83104 app.get('/' as Route, (req: express.Request, res: express.Response) => {
84- function tryToRender() {
85- if (thisBotIdentity) {
86- res.render('index', {
87- id: thisBotIdentity.id,
88- qrSize: thisBotIdentity.qr.size,
89- qrPath: thisBotIdentity.qr.path,
90- });
91- } else {
92- setTimeout(tryToRender, 200);
93- }
94- }
95-
96- tryToRender();
105 + res.render('index', {
106 + id: bot.id,
107 + qrSize: idQR.size,
108 + qrPath: idQR.path,
109 + });
97110 });
98111
99112 app.get('/invited' as Route, (req: express.Request, res: express.Response) => {
100113 bot.invite.create(1, (err, invitation) => {
@@ -113,22 +126,31 @@
113126 });
114127 });
115128
116129 app.listen(app.get('port'), () => {
117- console.log('Express app is running on port', app.get('port'));
130 + debug('Express app is running on port %s', app.get('port'));
118131 });
119132
120-// Y-redirection server ========================================================
121-function isHTTP(data) {
133 +// Facade redirection server ===================================================
134 +function isHTTPTraffic(data) {
122135 const str = data.toString('ascii');
123136 return /^.*HTTP[^\n]*\n/g.exec(str);
124137 };
125138
139 +function isSwarmTraffic(data) {
140 + const str = data.toString('ascii');
141 + return /ssb-discovery-swarm/g.exec(str);
142 +}
143 +
126144 net.createServer(function onConnect(socket) {
127- console.log('onConnect internal common socket');
145 + debug('Facade onConnect internal common socket');
128146 const httpConnection = net.createConnection({port: EXPRESS_PORT});
129147 const sbotConnection = net.createConnection({port: SBOT_PORT});
148 + const swarmConnection = net.createConnection({port: DISCOVERY_SWARM_PORT});
130149
131150 socket
132- .pipe(ternaryStream(isHTTP, httpConnection, sbotConnection))
151 + .pipe(
152 + ternaryStream(isHTTPTraffic, httpConnection,
153 + ternaryStream(isSwarmTraffic, swarmConnection,
154 + sbotConnection)))
133155 .pipe(socket);
134156 }).listen(INTERNAL_COMMON_PORT);

Built with git-ssb-web