src/index.tsView |
---|
4 | 4 … | import ssbKeys = require('ssb-keys'); |
5 | 5 … | import path = require('path'); |
6 | 6 … | import qr = require('qr-image'); |
7 | 7 … | import net = require('net'); |
| 8 … | +import swarm = require('discovery-swarm'); |
8 | 9 … | import ternaryStream = require('ternary-stream'); |
| 10 … | +import createDebug = require('debug'); |
9 | 11 … | |
| 12 … | +const debug = createDebug('easy-ssb-pub'); |
| 13 … | + |
10 | 14 … | const PUBLIC_PORT = process.env.PUBLIC_PORT || 80; |
11 | 15 … | const INTERNAL_COMMON_PORT = process.env.PORT || process.env.PUBLIC_PORT || 80; |
12 | 16 … | const EXPRESS_PORT = 8009; |
13 | 17 … | let SBOT_PORT = 8008; |
| 18 … | +const DISCOVERY_SWARM_PORT = 8007; |
14 | 19 … | |
15 | | - |
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 | | - |
23 | 20 … | |
24 | 21 … | let argv = process.argv.slice(2); |
25 | 22 … | const i = argv.indexOf('--'); |
26 | 23 … | const conf = argv.slice(i + 1); |
52 | 49 … | id: string; |
53 | 50 … | qr: QRSVG; |
54 | 51 … | } |
55 | 52 … | |
56 | | -let thisBotIdentity: BotIdentity | null = null; |
| 53 … | +const idQR = qr.svgObject(bot.id); |
57 | 54 … | |
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 | | - |
70 | 55 … | bot.address((err, addr) => { |
71 | 56 … | if (err) { |
72 | 57 … | console.error(err); |
73 | 58 … | process.exit(1); |
74 | 59 … | } else { |
75 | | - console.log('Scuttlebot app is running on address', addr); |
| 60 … | + debug('Scuttlebot app is running on address %s', addr); |
76 | 61 … | SBOT_PORT = (/\:(\d+)\~/g.exec(addr) as any)[1]; |
77 | 62 … | } |
78 | 63 … | }); |
79 | 64 … | |
80 | | - |
| 65 … | + |
| 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 … | + |
| 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 … | + |
81 | 102 … | type Route = '/' | '/invited'; |
82 | 103 … | |
83 | 104 … | 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 … | + }); |
97 | 110 … | }); |
98 | 111 … | |
99 | 112 … | app.get('/invited' as Route, (req: express.Request, res: express.Response) => { |
100 | 113 … | bot.invite.create(1, (err, invitation) => { |
113 | 126 … | }); |
114 | 127 … | }); |
115 | 128 … | |
116 | 129 … | 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')); |
118 | 131 … | }); |
119 | 132 … | |
120 | | - |
121 | | -function isHTTP(data) { |
| 133 … | + |
| 134 … | +function isHTTPTraffic(data) { |
122 | 135 … | const str = data.toString('ascii'); |
123 | 136 … | return /^.*HTTP[^\n]*\n/g.exec(str); |
124 | 137 … | }; |
125 | 138 … | |
| 139 … | +function isSwarmTraffic(data) { |
| 140 … | + const str = data.toString('ascii'); |
| 141 … | + return /ssb-discovery-swarm/g.exec(str); |
| 142 … | +} |
| 143 … | + |
126 | 144 … | net.createServer(function onConnect(socket) { |
127 | | - console.log('onConnect internal common socket'); |
| 145 … | + debug('Facade onConnect internal common socket'); |
128 | 146 … | const httpConnection = net.createConnection({port: EXPRESS_PORT}); |
129 | 147 … | const sbotConnection = net.createConnection({port: SBOT_PORT}); |
| 148 … | + const swarmConnection = net.createConnection({port: DISCOVERY_SWARM_PORT}); |
130 | 149 … | |
131 | 150 … | socket |
132 | | - .pipe(ternaryStream(isHTTP, httpConnection, sbotConnection)) |
| 151 … | + .pipe( |
| 152 … | + ternaryStream(isHTTPTraffic, httpConnection, |
| 153 … | + ternaryStream(isSwarmTraffic, swarmConnection, |
| 154 … | + sbotConnection))) |
133 | 155 … | .pipe(socket); |
134 | 156 … | }).listen(INTERNAL_COMMON_PORT); |