-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
231 lines (183 loc) · 5.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// import './shim'
let reqId = null
let requests = []
let payload = null
let cb = null
let net = null
let client = null
exports.hello = function () {
console.log('Hi! This is a sample log message from Peer Zero.')
return 'Hi there! My name is Peer Zero. Would you like to play a game?'
}
exports.helloAgain = function () {
console.log('Hello AGAIN!')
return 'Hi AGAIN!! My name is Peer Zero. Would you like to play a game?'
}
exports.triple = function (val) {
return val * 3
}
exports.download = function (_net=null, _client=null) {
return new Promise((resolve, reject) => {
cb = resolve
// cb('PEER IS STILL TALKING via CB!!')
// return new Promise(function (resolve, reject) {
// setTimeout(resolve, 100, 'foo-foo-so-fancy')
if (_net) {
net = _net
console.log('someone sent you net and this is what we got', net)
}
// OR, if not shimming via package.json "browser" field:
// var net = require('react-native-tcp')
const hostIp = '178.128.8.225'
const hostPort = 13312
// const hostIp = '82.217.119.49'
// const hostPort = 16065
// var server = net.createServer(function(socket) {
// socket.write('excellent!')
// }).listen(12345)
const host = {
host: hostIp,
port: hostPort
}
// setState({ debug: JSON.stringify(host) })
client = net.createConnection(hostPort, hostIp, () => {
// 'connect' listener
console.log('Connected to peer!')
// setState({ debug: 'Connected to peer!' })
const pkg = _encode(_handshakePkg())
client.write(pkg)
})
// client.on('connect', function () {
// console.info('Connection opened.')
// setState({ debug: 'Connection opened.' })
//
// /* Create encoded package. */
// // const pkg = _encode(_handshakePkg)
//
// /* Send the handshake. */
// // client.write(pkg, function () {
// // console.log('Sent handshake.')
// // // console.log('sent handshake', pkg)
// // })
// })
client.on('error', function (error) {
console.log(error)
// setState({ debug: error.toString() })
})
let called = 0
let stop = 0
client.on('data', function(_data) {
try {
if (payload) {
payload = Buffer.concat([payload, _data])
} else {
payload = _data
}
/* Attempt to decode the data. */
const decoded = _decode(payload)
console.log('Message #%d was received [%d bytes]', ++called, _data.length, _data, decoded)
// setState({ debug: 'received:\n' + _data.length + '\n\n' + JSON.stringify(decoded) })
if (decoded.body) {
payload = null
let body = decoded.body.toString()
console.log('check out my HTML body', body)
cb(body)
}
if (decoded.protocol === 'v2' && stop === 0) {
payload = null
stop = 1
console.log('lets go grab a file!')
_getFile()
}
} catch (e) {
console.log('Failed to decoded data', e, _data);
}
})
client.on('close', function () {
console.info('Connection closed.')
// setState({ debug: 'Connection closed.' })
})
})
}
const CONFIG = {
peerId: '-UT3530-FFFFFFFFFFFF',
hostIp: '178.128.8.225', // OUR TEST SERVER
hostPort: 13312
}
const _handshakePkg = () => {
const cmd = 'handshake'
const request = { cmd }
const req_id = _addRequest(request)
const crypt = null
const crypt_supported = []
// const crypt_supported = ['tls-rsa']
const fileserver_port = 15441
const protocol = 'v2'
const port_opened = true
const peer_id = CONFIG.peerId
const rev = 2122
const version = '0.5.6'
const target_ip = CONFIG.hostIp
/* Build parameters. */
const params = {
crypt,
crypt_supported,
fileserver_port,
protocol,
port_opened,
peer_id,
rev,
version,
target_ip
}
return { cmd, req_id, params }
}
const _parseIp = (_buf) => {
const ip = _buf.readUInt8(0) +
'.' + _buf.readUInt8(1) +
'.' + _buf.readUInt8(2) +
'.' + _buf.readUInt8(3)
return ip
}
const _parsePort = (_buf) => {
const port = (_buf.readUInt8(1) * 256) + _buf.readUInt8(0)
return port
}
const _addRequest = (_request) => {
if (!reqId)
reqId = 0
/* Initialize request id (auto-increment). */
reqId++
// const reqId = reqId++
requests[reqId] = _request
/* Return the request id. */
return reqId
}
const _encode = (msg) => {
const msgpack = require('zeronet-msgpack')()
const encode = msgpack.encode
return encode(msg)
}
const _decode = (msg) => {
const msgpack = require('zeronet-msgpack')()
const decode = msgpack.decode
return decode(msg)
}
const _getFile = () => {
const cmd = 'getFile'
// const innerPath = 'index.html'
const innerPath = 'content.json'
// const site = '1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D'
const site = '1Name2NXVi1RDPDgf5617UoW7xA6YrhM9F'
const request = { cmd, innerPath, site }
// const req_id = 1
const req_id = _addRequest(request) // eslint-disable-line camelcase
const inner_path = innerPath // eslint-disable-line camelcase
const location = 0
const params = { site, inner_path, location }
const pkg = { cmd, req_id, params }
/* Send request. */
client.write(_encode(pkg), function () {
console.log('Sent request for [ %s ]', inner_path)
})
}