-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
157 lines (130 loc) · 4.49 KB
/
main.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
"use strict";
var win = require('nw.gui');
$('.open_dev_tools').on('click', function(){
win.Window.get().showDevTools();
});
var opt = require('./lib/options.js');
var thsBuilder = require('ths');
var fs = require('fs');
var DataServer = require('./lib/dataserver');
var crpt = require('crypto');
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');
var bs58 = require('bs58');
var DataBase = require('./lib/dbase.js');
var KeyStore = require('./lib/keystore.js');
var Ui = require('./lib/ui');
var msgcrypt = require('./lib/msgcrpyt.js');
var path = require('path');
/*global $:false */
var app = {'$': $};
app.db = new DataBase();
app.keys = new KeyStore(app);
var ds = false;
var ths = new thsBuilder(opt.datadir, opt.get('socksPortNumber'), opt.get('controlPortNumber'), function(err){console.error('Tor error: ' + err);});
app.ths = ths;
if(!ths.getServices() || ths.getServices().length ===0 || ths.getServices()[0].name != 'ddd_service'){
ths.createHiddenService('ddd_service', '47654 ' + opt.get('listenPort'), true);
console.log('service created');
console.log(ths.getServices());
}
if(!opt.get('disableNetworking')){
ths.start(false, function(){
console.log(ths.getServices());
ths.getOnionAddress('ddd_service', function(err, hostname){
ds = new DataServer(hostname);
$('#ths-address').text(hostname);
app.ds = ds;
app.db.getSendQueue().then(function(val){
function reinseretFile(err, data) {
if (err){
console.log(err);
return false;
}
app.ds.insertFile(data);
}
for (var i = 0; i < val.length; i++) {
fs.readFile(opt.datadir + '/temp_msg/' + val[i].id, reinseretFile);
}
}, function(err){console.log(err);});
ds.dataStore.on('fileadded', function(fname){
var content = ds.dataStore.getFile(fname), k, decMSG;
content = content.slice(10);
var msgHash = crpt.createHash('sha256').update(content).digest('hex');
for(k in app.keys.subs){
var sk = ec.keyPair({priv: bs58.decode(app.keys.subs[k].private)});
console.log(sk);
decMSG = msgcrypt.decodeMessage(content, sk);
if(!decMSG) continue;
console.log(decMSG, msgHash);
break;
}
if(decMSG){
if(decMSG.msg.parent != '0'){
app.db.addPost({
id: decMSG.msg.parent,
parent: 0,
posted_at: 0,
bumped: decMSG.msg.timestamp,
message: '',
sent_to: k,
state: 'placeholder',
file: '',
file_size: 0,
file_ext: 'none'
}, function(){
app.db.bumpThread(decMSG.msg.parent, decMSG.msg.timestamp);
});
}
if(decMSG.hasAttach){
fs.writeFileSync(opt.datadir + '/files/' + msgHash, decMSG.attach);
decMSG.msg.fileIsImage = !!decMSG.msg.filename.match(/(gif|png|jpe?g)$/i);
}
var postData = {
id: msgHash,
parent: decMSG.msg.parent,
posted_at: decMSG.msg.timestamp,
bumped: decMSG.msg.timestamp,
message: decMSG.msg.text,
sent_to: k,
state: 'recieved',
file: decMSG.hasAttach ? decMSG.msg.filename : '',
file_size: decMSG.hasAttach ? decMSG.attach.length : 0,
file_ext: decMSG.hasAttach ? (decMSG.msg.fileIsImage? 'image': 'file') : 'none'
};
app.db.addPost(postData, function(){app.ui.postReceived(msgHash);});
app.db.updatePost(postData, function(){});
fs.unlink(opt.datadir + '/temp_msg/' + msgHash, function(){return true;});
}
});
app.ui = new Ui(app);
setTimeout(app.ui.inject.bind(app.ui), 1000);
});
});
}else{
app.ui = new Ui(app);
setTimeout(app.ui.inject.bind(app.ui), 0);
}
$('.update_bbd').on('click', function(){
$('.update_bbd').replaceWith('<div class="update_bbd">Downloading... <i class="fa fa-refresh fa-spin"></i></div>');
app.ths.stop();
if(ds) ds.server.close();
var Download = require('download');
new Download({mode: '755', extract: true, strip: 1})
.get('https://github.com/desudesutalk/barabaradark/archive/master.zip')
.dest('.')
.run(function(){
var child,
child_process = require("child_process"),
gui = require('nw.gui'),
win = gui.Window.get();
if (process.platform == "darwin") {
child = child_process.spawn("open", ["-n", "-a", process.execPath.match(/^([^\0]+?\.app)\//)[1]], {detached:true});
} else {
child = child_process.spawn(process.execPath, [], {detached: true, cwd: process.cwd()});
}
child.unref();
win.hide();
gui.App.quit();
});
});