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
|
// static server (8089)
var fs = require('fs')
var http = require('http')
var serve = require('ecstatic')
var open = require('open')
http.createServer(
serve({ root: __dirname})
).listen(8089)
open('http://localhost:8089')
// ws server (8080)
var bog = require('./bog')
var WS = require('ws')
var nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
var homedir = require('os').homedir()
var bogdir = homedir + '/.bogbook/bogs/'
if (!fs.existsSync(homedir + '/.bogbook/')) {fs.mkdirSync(homedir + '/.bogbook/')}
if (!fs.existsSync(bogdir)){fs.mkdirSync(bogdir)}
var wserve = new WS.Server({ port: 8080 })
var adContents = JSON.parse(fs.readFileSync(__dirname + '/ads.json'))
bog.keys().then(key => {
wserve.on('connection', function (ws) {
ws.on('message', function (message) {
var req = JSON.parse(message)
if (req.sendpub) {
ws.send(key.publicKey)
} else {
bog.unbox(req.box, req.requester, key).then(unboxed => {
var unboxedreq = JSON.parse(nacl.util.encodeUTF8(unboxed))
if (unboxedreq.seq >= 0) {
console.log(req.requester + ' asked for feed ' + unboxedreq.author + ' after sequence ' + unboxedreq.seq)
fs.readFile(bogdir + unboxedreq.author, 'UTF-8', function (err, data) {
if (data) {
var feed = JSON.parse(data)
bog.open(feed[0]).then(msg => {
if (unboxedreq.seq === msg.seq) {
//console.log(unboxedreq.author + '\'s feed is identical, sending nothing to client')
//commment this section out to disable ads
console.log(unboxedreq.author + '\'s feed is identical')
if (Math.floor(Math.random() * 4) == 2) {
console.log('sending an ad to ' + req.requester)
var ad = JSON.stringify({
author: key.publicKey,
name: 'http://bogbook.com/',
content: adContents[Math.floor(Math.random() * adContents.length)],
timestamp: Date.now()
})
bog.box(ad, req.requester, key).then(boxed => {
obj = {
requester: key.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
}
}
if (unboxedreq.seq > msg.seq) {
// right now the client is still sending the entire log, which works just fine but isn't optimal
console.log('client feed is longer, requesting diff from client')
var reqdiff = JSON.stringify({author: unboxedreq.author, seq: msg.seq})
bog.box(reqdiff, req.requester, key).then(boxed => {
var obj = {
requester: key.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
}
if (unboxedreq.seq < msg.seq) {
var endrange = feed.length - unboxedreq.seq - 25
if (endrange < 0) {
endrange = feed.length - unboxedreq.seq - 1
}
var baserange = feed.length - unboxedreq.seq
console.log('client feed is shorter, sending from ' + baserange + ' to ' + endrange + ' to client')
var diff = JSON.stringify(
feed.slice(
endrange,
baserange)
)
bog.box(diff, req.requester, key).then(boxed => {
var obj = {
requester: key.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
}
})
} else {
console.log('We don\'t have the log on the server, requesting log from ' + req.requester )
var reqwhole = JSON.stringify({author: unboxedreq.author, seq: 0})
bog.box(reqwhole, req.requester, key).then(boxed => {
var obj = {
requester: key.publicKey,
box: boxed
}
ws.send(JSON.stringify(obj))
})
}
})
} else if (Array.isArray(unboxedreq)) {
bog.open(unboxedreq[0]).then(msg => {
if (msg.seq === unboxedreq.length) {
fs.writeFile(bogdir + msg.author, JSON.stringify(unboxedreq), 'UTF-8', function (err, success) {
console.log('Saved full log of ' + msg.author + ' sent by ' + req.requester)
})
} if (msg.seq > unboxedreq.length) {
fs.readFile(bogdir + msg.author, 'UTF-8', function (err, data) {
var feed = JSON.parse(data)
bog.open(feed[0]).then(lastmsg => {
if (unboxedreq.length + lastmsg.seq === msg.seq) {
var newlog = unboxedreq.concat(feed)
fs.writeFile(bogdir + msg.author, JSON.stringify(newlog), 'UTF-8', function (err, success) {
console.log('combined existing feed of ' + msg.author + ' with diff and saved to server')
})
}
})
})
}
})
}
})
}
})
})
})
|