forked from AmnesiaLabs/zen-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (103 loc) · 2.9 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
const iMessage = require("./osa");
const fs = require("fs");
const yargs = require("yargs");
function getUserHome() {
let envlet = process.platform == "win32" ? "USERPROFILE" : "HOME";
return process.env[envlet];
}
function wsRelay(msg) {
socket.emit("message", msg);
return;
}
function notifRelay(s) {
socket.emit("raw", { type: "notif", data: s });
}
function joinRoom() {
console.log(process.argv[2], process.argv[3]);
socket.emit(
"client:authenticate",
{
relay: process.argv[2],
uuid: process.argv[3],
},
(ack, err) => {
if (ack) {
socket.emit("client:setName", process.argv[3], (ack, err) => {
if (ack) {
console.log("Successfully connected into UUID room.");
return;
} else {
console.error("Could not connect to relay and/or UUID. Exiting...");
process.exit();
}
});
} else {
console.error("Could not connect to relay and/or UUID. Exiting...");
process.exit();
}
}
);
}
function checkForParams() {
if (process.argv[3] && process.argv[2]) return;
console.error("Missing parameters! Exiting...");
process.exit();
}
checkForParams();
let socket = require("socket.io-client")(process.argv[2]);
async function auth() {
notifRelay(process.argv[3].substring(0, 8));
joinRoom();
return true;
}
auth().then(() => {
console.info("Connected to relay with secret UUID: " + process.argv[3]);
let self = this;
socket.on("raw", (req) => {
console.log(req);
if (req.type === "getAttachmentsFromId") {
let ret = iMessage.getAttachmentsFromId(id).then((c) => {
c.forEach((attachment) => {
fs.readFile(
attachment.filename.replace("~", getUserHome()),
function (err, buf) {
stats.fileSent++;
socket.emit("raw", {
type: "fileTranser",
details: attachment,
handle: handle_id,
buffer: buf.toString("base64"),
});
}
);
});
});
}
if (req.type === "recentContacts") {
iMessage.getRecentChats(50).then((c) => {
socket.emit("raw", { type: "recentContacts", data: c });
});
}
if (req.type === "send") {
iMessage.send(req.id, req.text);
}
if (req.type === "join") {
notifRelay(req.id);
}
if (req.type === "ping") {
notifRelay(process.argv[3]);
}
if (req.type === "recentChats") {
iMessage.getRecentChatsFromId(req.id).then((c) => {
socket.emit("raw", { type: "recentChats", id: req.id, data: c });
});
}
});
socket.on("join", (s) => {
notifRelay(s);
});
});
iMessage.listen().on("message", (msg) => {
if (msg.fromMe) return;
wsRelay(msg);
});