Skip to content

Commit e07c2ce

Browse files
committed
Add p2p dms
1 parent f5d9c21 commit e07c2ce

File tree

9 files changed

+107
-101
lines changed

9 files changed

+107
-101
lines changed

src/backend/beam.cjs

+27-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { saveMsg } = require('./database.cjs')
55
const sanitizeHtml = require('sanitize-html')
66
const progress = require("progress-stream");
77
const {createWriteStream, createReadStream} = require("fs");
8-
const { sleep, sanitize_pm_message, randomKey, hash } = require('./utils.cjs');
8+
const { sleep, sanitize_pm_message, randomKey, hash, parse_call } = require('./utils.cjs');
99
const { ipcMain } = require('electron')
1010
const {Hugin} = require('./account.cjs');
1111
const { keychain, get_new_peer_keys } = require('./crypto.cjs');
@@ -18,7 +18,7 @@ let downloadDirectory
1818

1919
ipcMain.on("end-beam", async (e, chat) => {
2020
console.log("end beam");
21-
end_beam(chat);
21+
end_beam(chat, true);
2222
})
2323

2424
//FILES
@@ -113,7 +113,7 @@ const beam_event = (beam, chat, key) => {
113113
const addr = chat.substring(0,99)
114114
const msgKey = chat.substring(99,163)
115115
active_beams.push({key, chat: addr, beam})
116-
Hugin.send('new-beam', {key, chat: addr})
116+
Hugin.send('new-beam', {key, chat: addr, hugin: addr + msgKey})
117117
beam.on('remote-address', function ({ host, port }) {
118118
if (!host) console.log('Could not find the host')
119119
else console.log('Connected to DHT with' + host + ':' + port)
@@ -138,6 +138,7 @@ const beam_event = (beam, chat, key) => {
138138

139139
beam.on('end', () => {
140140
console.log('Chat beam ended on event')
141+
end_beam(addr, true)
141142
})
142143

143144
beam.on('error', function (e) {
@@ -167,11 +168,13 @@ const decrpyt_beam_message = async (str, msgKey) => {
167168
decrypted_message.k = msgKey
168169
decrypted_message.sent = false
169170
const [message, address, key, timestamp] = sanitize_pm_message(decrypted_message)
170-
171+
171172
if (!message) return
172-
173+
174+
const [text, data, call] = is_call(decrypted_message.msg, address, false, timestamp)
175+
173176
const newMsg = {
174-
msg: message,
177+
msg: call ? text : message,
175178
chat: address,
176179
sent: false,
177180
timestamp: timestamp,
@@ -184,6 +187,22 @@ const decrpyt_beam_message = async (str, msgKey) => {
184187
saveMsg(message, address, false, timestamp)
185188
}
186189

190+
const is_call = (message, address, sent, timestamp) => {
191+
//Checking if private msg is a call
192+
const [text, data, is_call, if_sent] = parse_call(message, address, sent, true, timestamp)
193+
194+
if (text === "Audio call started" || text === "Video call started" && is_call && !if_sent) {
195+
//Incoming calll
196+
Hugin.send('call-incoming', data)
197+
return [text, true]
198+
} else if (text === "Call answered" && is_call && !if_sent) {
199+
//Callback
200+
Hugin.send('got-callback', data)
201+
return [text, true]
202+
}
203+
return ['',false]
204+
}
205+
187206
const send_beam_message = (message, to) => {
188207
const active = active_beams.find(a => a.chat === to)
189208
active.beam.write(message)
@@ -201,10 +220,10 @@ const end_file_beam = async (chat, key) => {
201220
}
202221

203222

204-
const end_beam = async (chat, file = false) => {
223+
const end_beam = async (chat, close) => {
205224
const active = active_beams.find(a => a.chat === chat)
206225
if (!active) return
207-
Hugin.send('stop-beam', chat)
226+
Hugin.send('stop-beam', chat, close)
208227
active.beam.end()
209228
await sleep(2000)
210229
active.beam.destroy()

src/backend/messages.cjs

+32-75
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@ const {
1212
removeMessages,
1313
removeContact,
1414
addGroup,
15-
removeGroup,
1615
unBlockContact,
1716
loadBlockList,
1817
blockContact,
1918
getGroupReply,
20-
printGroup,
21-
getGroups,
2219
loadGroups,
2320
deleteMessage,
2421
addRoom,
25-
loadRooms,
2622
addRoomKeys,
27-
getRooms,
2823
removeRoom} = require("./database.cjs")
2924
const {
3025
trimExtra,
31-
sanitize_pm_message,
32-
parse_call,
26+
sanitize_pm_message,
3327
sleep,
3428
hexToUint,
3529
randomKey,
@@ -159,6 +153,8 @@ ipcMain.on('send-msg', (e, msg, receiver, off_chain, grp, beam) => {
159153
//Listens for event from frontend and saves contact and nickname.
160154
ipcMain.on('add-chat', async (e, hugin_address, nickname, first) => {
161155
save_contact(hugin_address, nickname, first)
156+
const key = await key_derivation_hash(hugin_address.substring(0,99))
157+
await new_beam(key, hugin_address, false);
162158
})
163159

164160

@@ -190,11 +186,12 @@ ipcMain.on('expand-sdp', (e, data, address) => {
190186

191187
//BEAM
192188

193-
ipcMain.on("beam", async (e, link, chat, send = false, offchain = false) => {
194-
let beamMessage = await new_beam(link, chat, send);
189+
ipcMain.on("beam", async (e, chat, send = false, beam = false) => {
190+
const key = await key_derivation_hash(chat.substring(0,99))
191+
let beamMessage = await new_beam(key, chat, send);
195192
if (beamMessage === "Error") return
196193
if (!beamMessage) return
197-
send_message(beamMessage.msg, beamMessage.chat, offchain)
194+
if (beam) send_message(beamMessage.msg, beamMessage.chat, offchain)
198195
});
199196

200197
//SWARM
@@ -207,62 +204,28 @@ ipcMain.on('end-swarm', async (e, key) => {
207204
end_swarm(key)
208205
})
209206

210-
211-
//TORRENT
212-
213-
ipcMain.on('download-torrent', (e, file) => {
214-
download_torrent(file)
215-
})
216-
217-
const download_torrent = (file) => {
218-
return
219-
// const client = new WebTorrent({utp: true, dht: false})
220-
// const path = Hugin.downloadDir
221-
// Hugin.send('downloading', file)
222-
// client.add(file.message, { path: path, utp: true }, torrent => {
223-
// console.log("torrent added!")
224-
// torrent.on('download', function (bytes) {
225-
// console.log("Downloading!!!! --------->")
226-
// Hugin.send('download-file-progress', {
227-
// fileName: file.fileName,
228-
// progress: torrent.progress,
229-
// group: file.group,
230-
// chat: file.group,
231-
// path
232-
// })
233-
// } )
234-
// torrent.on('done', () => {
235-
// console.log("Downloaded torrent!")
236-
// Hugin.send('downloading-torrent')
237-
// console.log('torrent download finished')
238-
// })
239-
// })
207+
const peer_dms = async () => {
208+
const contacts = await getConversations()
209+
for (const c of contacts) {
210+
const hashDerivation = await key_derivation_hash(c.chat)
211+
const beam = await new_beam(hashDerivation, c.chat + c.key, false)
212+
if (beam === "Error") continue
213+
if (!beam) continue
214+
}
215+
240216
}
241217

242-
ipcMain.on('upload-torrent', (e, [fileName, path, size, time, group, hash]) => {
243-
return
244-
// console.log("Upload this!", path, fileName, size, time)
245-
// const client = new WebTorrent()
246-
// Hugin.send('uploading', {fileName, progress: 0, size, chat: group, time, hash})
247-
// client.seed(path, {}, (torrent) => {
248-
// console.log('Client is seeding ' + torrent.magnetURI)
249-
// torrent.on('wire', (wire, addr) => {
250-
// Hugin.send('torrent-connection')
251-
// Hugin.send('uploading-torrent')
252-
// })
253-
// torrent.on('upload', function (uploaded) {
254-
// console.log("Uploaded", uploaded)
255-
// Hugin.send('upload-file-progress', {fileName, progress: (uploaded / size) * 100, chat: group, time})
256-
// })
257-
// const message = {m: 'TORRENT://' + torrent.magnetURI, g: group, t: time}
258-
259-
// send_group_message(message, false, false)
260-
// })
261-
262-
})
218+
async function key_derivation_hash(chat) {
219+
const [privateSpendKey, privateViewKey] = keychain.getPrivKeys()
220+
const recvAddr = await Address.fromAddress(chat)
221+
const recvPubKey = recvAddr.m_keys.m_viewKeys.m_publicKey
222+
const derivation = await crypto.generateKeyDerivation(recvPubKey, privateViewKey);
223+
return await crypto.cn_fast_hash(derivation)
224+
}
263225

264226
const start_message_syncer = async () => {
265227
//Load knownTxsIds to backgroundSyncMessages on startup
228+
peer_dms()
266229
known_keys = Hugin.known_keys
267230
block_list = Hugin.block_list
268231
await background_sync_messages(await load_checked_txs())
@@ -598,6 +561,9 @@ async function fetch_hugin_messages() {
598561

599562
async function send_message(message, receiver, off_chain = false, group = false, beam_this = false) {
600563
//Assert address length
564+
console.log("Send message!", message)
565+
console.log("offchain", off_chain)
566+
console.log("beam_this", beam_this)
601567
if (receiver.length !== 163) {
602568
return
603569
}
@@ -1170,17 +1136,6 @@ async function save_message(msg, offchain = false) {
11701136
if (!message) return
11711137

11721138
if (await messageExists(timestamp)) return
1173-
1174-
//Checking if private msg is a call
1175-
let [text, data, is_call, if_sent] = parse_call(msg.msg, addr, sent, offchain, timestamp)
1176-
1177-
if (text === "Audio call started" || text === "Video call started" && is_call && !if_sent) {
1178-
//Incoming calll
1179-
Hugin.send('call-incoming', data)
1180-
} else if (text === "Call answered" && is_call && !if_sent) {
1181-
//Callback
1182-
Hugin.send('got-callback', data)
1183-
}
11841139

11851140
//If sent set addr to chat instead of from
11861141
if (msg.chat && sent) {
@@ -1193,7 +1148,6 @@ async function save_message(msg, offchain = false) {
11931148
await save_contact(hugin)
11941149
}
11951150

1196-
message = sanitizeHtml(text)
11971151
let newMsg = await saveMsg(message, addr, sent, timestamp, offchain)
11981152
if (sent) {
11991153
//If sent, update conversation list
@@ -1234,6 +1188,9 @@ async function save_contact(hugin_address, nickname = false, first = false) {
12341188
t: Date.now(),
12351189
})
12361190
known_keys.pop(key)
1191+
} else {
1192+
const key = await key_derivation_hash(addr)
1193+
await new_beam(key, hugin_address, false);
12371194
}
12381195
}
12391196

@@ -1256,12 +1213,12 @@ function get_sdp(data)
12561213
if (data.type == 'offer')
12571214
{
12581215
let parsed_data = `${data.video ? 'Δ' : 'Λ'}` + parse_sdp(data.data, false)
1259-
send_message(parsed_data, data.contact, data.offchain, data.group)
1216+
send_message(parsed_data, data.contact, true, data.group, true)
12601217
}
12611218
else if (data.type == 'answer')
12621219
{
12631220
let parsed_data = `${data.video ? 'δ' : 'λ'}` + parse_sdp(data.data, true)
1264-
send_message(parsed_data, data.contact, data.offchain, data.group)
1221+
send_message(parsed_data, data.contact, true, data.group, true)
12651222
}
12661223
}
12671224

src/backend/storage.cjs

+13-6
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,14 @@ async beam_started(beam, upload, key, topic, file, room) {
151151
console.log("----:::::::::::::::::::----")
152152
console.log("------BEAM CONNECTED------")
153153
console.log("----:::::::::::::::::::----")
154-
if (upload) {
155-
//upload
156-
this.upload(beam, file, topic)
157-
} else {
158-
this.download(beam, file, topic, room)
154+
try {
155+
if (upload) {
156+
this.upload(beam, file, topic)
157+
} else {
158+
this.download(beam, file, topic, room)
159+
}
160+
} catch(e) {
161+
159162
}
160163
})
161164

@@ -184,7 +187,11 @@ async upload(beam, file, topic) {
184187
const stream = Readable.from(send)
185188
stream.on('data', data => {
186189
console.log("Sending data ------>", data)
187-
beam.write(data)
190+
try {
191+
beam.write(data)
192+
} catch(e) {
193+
console.log("Error writing data.")
194+
}
188195
})
189196
}
190197

src/backend/swarm.cjs

-2
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,10 @@ const request_file = async (address, topic, file, room) => {
581581

582582
const process_files = async (data, active, con, topic) => {
583583
//Check if the latest 10 files are in sync
584-
console.log("PROCESS FILES")
585584
if (Hugin.syncImages.some(a => a === topic)) {
586585
if (!Array.isArray(data.files)) return 'Ban'
587586
if (data.files.length > 10) return 'Ban'
588587
for (const file of data.files) {
589-
console.log("File", file.fileName)
590588
if (!check_hash(file.hash)) continue
591589
if (downloading.some(a => a === file.hash)) continue
592590
if (Hugin.get_files().some(a => a.time === file.time)) continue

src/lib/components/chat/ChatBubble.svelte

+1-3
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@
129129
}
130130
131131
const joinBeam = () => {
132-
let key = beam_key
133-
if (key === "new") return
134132
clicked = true
135-
window.api.createBeam(key, $user.activeChat.chat + $user.activeChat.key)
133+
window.api.createBeam($user.activeChat.chat + $user.activeChat.key)
136134
$beam.active.push({
137135
chat: $user.activeChat.chat,
138136
connected: false,

src/lib/components/chat/Contact.svelte

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import {createEventDispatcher} from 'svelte'
33
import {fade} from 'svelte/transition'
44
import {get_avatar, getColorFromHash} from '$lib/utils/hugin-utils.js'
5-
import {notify, user, webRTC} from '$lib/stores/user.js'
5+
import {notify, user, webRTC, beam} from '$lib/stores/user.js'
66
import { isLatin } from '$lib/utils/utils'
77
88
export let contact
99
let thisCall = false
1010
let beamInvite = false
1111
let asian = false
12+
let online = false
1213
1314
$: counter = $notify.unread.filter(a => a.type === 'message' && contact.chat === a.chat).length
1415
$: if (contact.msg.substring(0,7) === "BEAM://") {
@@ -43,6 +44,14 @@ const rename = () => {
4344
})
4445
dispatch('openRename')
4546
}
47+
48+
$: {
49+
if ($beam.active.length) {
50+
online = $beam.active.some(a => a.chat == contact.chat && a.connected);
51+
} else {
52+
online = false
53+
}
54+
}
4655
</script>
4756

4857
<div
@@ -51,6 +60,7 @@ const rename = () => {
5160
out:fade
5261
class:rgb="{thisCall}"
5362
class:active="{contact.chat === $user.activeChat.chat}"
63+
class:online={online}
5464
on:click="{() => printThis(contact)}"
5565
>
5666

@@ -162,4 +172,8 @@ p {
162172
.big {
163173
font-size: 17px;
164174
}
175+
176+
.online {
177+
box-shadow: inset 10px 0 7px -7px var(--success-color);
178+
}
165179
</style>

0 commit comments

Comments
 (0)