/
upstream.py
598 lines (565 loc) · 22.2 KB
/
upstream.py
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/usr/bin/env python2.7
# -*- coding: utf8 -*-
# COPYDOWN (C) You
# All code related to Upstream and code contained in this file
# Is released fully into the public domain
from gevent import monkey
monkey.patch_all()
from gevent import queue
from gevent import event
from gevent import pool
from gevent import socket as gsocket
import gevent
import sys
import os
import logging
import socket
import ssl
import re
import glob
import shelve
import subprocess
import bz2
import time
import fcntl
import binascii
import urllib
import hashlib
import argparse
try:
import simplejson as json
except ImportError:
import json
from StringIO import StringIO
from _yenc import encode_string
from xml.dom.minidom import getDOMImplementation
logging.basicConfig(level=logging.INFO)
TEMPDIR='./temp'
rivermeta = {}
filemeta = {}
nfo = None
filenfo = {}
class UpstreamNNTP:
def __init__(self, server, username=None, password=None, port=-1, ssl=False):
self.server = server
self.username = username
self.password = password
self.port = port
if port == -1:
self.port = 119 if ssl == False else 563
self.ssl = ssl
self.connected = False
def connect(self):
if self.connected:
raise Exception, "Already Connected"
if self.ssl:
self.psock = socket.socket()
self.sock = ssl.wrap_socket(self.psock)
else:
self.sock = socket.socket()
self.sock.connect((self.server, self.port))
result = self.sock.recv(1024)
if result[:3] != '200':
raise Exception, 'Server not accepting connection'
if self.username:
self.sock.send('authinfo user %s\r\n' % self.username)
result = self.sock.recv(1024)
if result[:3] == '381': # PASS required
if not self.password:
raise Exception, "Password required"
self.sock.send('authinfo pass %s\r\n' % self.password)
result = self.sock.recv(1024)
if result[:3] != '281':
raise Exception, 'NNTP Login Incorrect'
self.connected = True
def post(self, data):
global bytes
if not self.connected:
raise Exception, "Not Connected"
self.sock.send("POST\r\n")
self.sock.recv(2048)
while len(data) > 0:
sent = self.sock.send(data)
bytes += sent
data = data[sent:]
self.sock.send("\r\n.\r\n")
result = self.sock.recv(2048)
if result[:3] == '441':
raise Exception, "NNTP Error %s" % repr(result)
return result[:-2]
def article(self, message_id):
if not self.connected:
raise Exception, "Not Connected"
self.sock.send("ARTICLE %s\r\n" % message_id)
output = self.sock.recv(16384)
if output[:3] == '430':
raise Exception, "No such article"
elif output[:3] == '412':
raise Exception, "Not in a newsgroup"
elif output[:1] == '4':
raise Exception, output
if not output[-5:] == '\r\n.\r\n':
while True:
output += self.sock.recv(16384)
if output[-5:] == '\r\n.\r\n': break
output = output[:-5]
rheaders, article = output.split('\r\n\r\n')
headers = rheaders.split('\r\n')
head = dict((k,v) for k,v in (a.split(': ', 1) for a in headers[1:]))
return head, article
def date(self):
if not self.connected:
raise Exception, "Not Connected"
self.sock.send("DATE\r\n")
result = self.sock.recv(1024)
return result[:-2]
def quit(self):
if not self.connected:
raise Exception, "Not Connected"
self.sock.send("QUIT\r\n")
self.sock.recv(1024)
self.sock.close()
self.connected = False
def worker_thread(ready_event):
global conn_workers
logging.info("Starting new NNTP thread")
tries = 0
while True:
try:
n = UpstreamNNTP(server, username, password, port, use_ssl)
n.connect()
logging.info("NNTP thread %s started" % n)
ready_event.set(True)
except Exception, e:
logging.info("NNTP thread failed! %s" % e.args[-1])
if tries == 3:
ready_event.set(False)
return False, e.args[-1]
tries += 1
continue
while True:
try:
messageid, data, name, customheaders, \
yencdata, finish = river_queue.get(True, 60)
ypart, ytotal, ybegin, yend = yencdata
headers = []
ydata, ycrc, ylen = encode_string(data)
mcrc = '%08x' % (binascii.crc32(data) & 2**32L-1)
headers.append('From: %s' % poster)
headers.append('Newsgroups: %s' % group)
headers.append('Subject: %s' % subject.format(name=name))
headers.append('Message-ID: <%s>' % messageid)
headers += customheaders
outdata = '\r\n'.join(headers)+'\r\n\r\n'
outdata += '=ybegin part=%d total=%d line=128 size=%d name=%s\r\n' % (ypart, ytotal, ylen, name)
outdata += '=ypart begin=%d end=%d\r\n' % (ybegin, yend)
outdata += ydata.replace('\r\n.','\r\n..')
outdata += '\r\n=yend size=%d pcrc32=%s' % (ylen, mcrc)
result = n.post(outdata)
finish.set()
except gevent.queue.Empty:
try:
n.date()
except:
logging.info("NNTP thread %s timed out" % n)
break
def init_connections():
global river_queue, river_pool
river_queue = queue.Queue()
river_pool = []
ready_pool = []
for _ in range(connections):
r = event.AsyncResult()
g = gevent.Greenlet(worker_thread, r)
river_pool.append(g)
ready_pool.append(r)
g.start()
failed = 0
for i in ready_pool:
ret = i.wait()
if ret == False:
failed += 1
if failed == connections:
logging.fatal("Upstream could not be started")
exit(1)
pass
def init_metadata():
global meta, TEMPDIR
if not os.path.exists(TEMPDIR):
logging.info("temp directory %s does not exist, using /tmp/upstream" % TEMPDIR)
TEMPDIR = "/tmp/upstream"
if not os.path.exists(TEMPDIR):
os.mkdir(TEMPDIR)
internal_files = []
#meta = shelve.open(TEMPDIR + "/.metadata")
meta = {} # Resume support has been disabled in this release due
for i in files:
if not os.path.exists(i):
logging.fatal("File %s does not exist" % i)
exit(1)
fsize = os.stat(i).st_size
bn = os.path.basename(i)
if not bn in meta:
meta[bn] = fsize
meta[bn+'/segtotal'] = fsize / segbytes + (1 if fsize % segbytes > 0 else 0)
meta[bn+'/arttotal'] = fsize / articlesize + (1 if fsize % articlesize > 0 else 0)
meta[bn+"/segleft"] = range(fsize / segbytes + (1 if fsize % segbytes > 0 else 0))
meta[bn+"/artleft"] = range(fsize / articlesize + (1 if fsize % articlesize > 0 else 0))
def handle_segment(ffile, seg):
global meta, par_pool, article_pool
ifile = os.path.basename(ffile)
seg_articles = range(seg*segsize, seg*segsize+segsize)
gart = []
for i in seg_articles:
if i in meta[ifile+"/artleft"]:
gart.append(article_pool.spawn(handle_article, ffile, seg, i))
par = par_pool.spawn(par_segment, ffile, seg)
gevent.joinall(gart)
def run_par2(args):
p = subprocess.Popen(args, stdout=subprocess.PIPE,\
stderr=subprocess.STDOUT)
fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK)
while True:
gsocket.wait_read(p.stdout.fileno())
d = p.stdout.read(2048)
if d == '':
break
p.stdout.close()
def par_segment(ffile, seg):
global meta, TEMPDIR, article_pool
ifile = os.path.basename(ffile)
fsize = meta[ifile]
byte_start = seg * segbytes
byte_len = min(fsize-byte_start, segbytes)
mainfile = open(ffile)
mainfile.seek(byte_start)
bn = ifile
parfilename = TEMPDIR + '/' + bn + ".%03d" % (seg)
parfile = open(parfilename,'w')
parfile.write(mainfile.read(byte_len))
parfile.close()
if not (os.path.exists(parfilename+'.par2')):
run_par2(['par2', 'c', '-b'+str(parblocks), '-c1',TEMPDIR+'/'+bn+'.%03d' % (seg)])
try:
os.unlink(parfilename)
except:
pass
parfiles = glob.glob(parfilename+'.*par2')
for i in parfiles:
article_pool.spawn(handle_par, ifile, seg, i)
t = meta[ifile+'/segleft']
t.remove(seg)
meta[ifile+'/segleft'] = t
def handle_par(ifile, seg, name):
global meta, TEMPDIR
data = open(name).read()
ypart = 1
ytotal = 1
ybegin = 1
yend = len(data)
ydata = [ypart, ytotal, ybegin, yend]
article, crc32, bytes = upload_article(data, os.path.basename(name), ydata)
meta[ifile+"/par/"+str(seg)+"/"+os.path.basename(name)] = [article, crc32, bytes]
os.unlink(name)
def handle_article(ffile, seg, art):
ifile = os.path.basename(ffile)
fsize = meta[ifile]
byte_start = art * articlesize
byte_len = min(fsize-byte_start, articlesize)
ypart = art - (seg * segsize)
ytotal = fsize / segbytes + (1 if fsize % segbytes > 0 else 0)
ybegin = byte_start
yend = byte_start + byte_len
ydata = [ypart, ytotal, ybegin, yend]
t = meta[ifile+'/artleft']
t.remove(art)
meta[ifile+'/artleft'] = t
#meta.sync() # Resume support has been disabled, uncomment when re-enabled
afile = open(ffile)
afile.seek(byte_start)
fdata = afile.read(byte_len)
afile.close()
article, crc32, bytes = upload_article(fdata, ifile+".%03d" % (seg), ydata)
del fdata
meta[ifile+"/art/"+str(art)] = [article, crc32, bytes]
def upload_article(data, fname, yencdata, headers=[]):
global river_queue
messageid = "%f@%s" % (time.time(), messagecontext)
crc32 = "%08x" % (binascii.crc32(data) & 0xffffffff)
bytes = len(data)
finish = event.Event()
river_queue.put((messageid, data, fname, headers, yencdata, finish))
finish.wait()
return messageid, crc32, bytes
def write_river():
impl = getDOMImplementation()
xmlfile = impl.createDocument(None, 'river', None)
doc = xmlfile.documentElement
if nfo:
outnfo = upload_nfo(nfo)
if outnfo:
rivermeta['nfo'] = outnfo
if len(rivermeta) > 0:
rmnode = xmlfile.createElement('rivermeta')
doc.appendChild(rmnode)
for i in rivermeta:
rminode = xmlfile.createElement(i)
rmnode.appendChild(rminode)
rminode.appendChild(xmlfile.createTextNode(rivermeta[i]))
for i in files:
bn = os.path.basename(i)
fsize = os.stat(i).st_size
fnode = xmlfile.createElement('file')
doc.appendChild(fnode)
fnode.setAttribute('bytes', str(fsize))
fnode.setAttribute('subject', subject.format(name=bn))
fnode.setAttribute('group', group)
fnode.setAttribute('name', bn)
if bn in filenfo:
outnfo = upload_nfo(filenfo[bn])
if outnfo:
filemeta[bn]['nfo'] = outnfo
if bn in filemeta and len(filemeta[bn]) > 0:
fmnode = xmlfile.createElement('filemeta')
fnode.appendChild(fmnode)
for j in filemeta[bn]:
fmjnode = xmlfile.createElement(j)
fmnode.appendChild(fmjnode)
fmjnode.appendChild(xmlfile.createTextNode(filemeta[bn][j]))
segs = range(fsize / segbytes + (1 if fsize % segbytes > 0 else 0))
for seg in segs:
snode = xmlfile.createElement('segment')
fnode.appendChild(snode)
snode.setAttribute('name', bn+".%03d"%(seg))
snode.setAttribute('number',str(seg+1))
arts = range(seg*segsize, seg*segsize+segsize)
sbytes = 0
for art in arts:
if not bn+'/art/'+str(art) in meta: break
anode = xmlfile.createElement('article')
snode.appendChild(anode)
messageid, crc, abytes = meta[bn+'/art/'+str(art)]
sbytes += abytes
anode.setAttribute('bytes', str(abytes))
anode.setAttribute('number', str(art-(seg*segsize)+1))
anode.setAttribute('crc', crc)
anode.appendChild(xmlfile.createTextNode(messageid))
snode.setAttribute('bytes', str(sbytes))
pars = {k:v for k,v in meta.iteritems() if k.startswith(bn+'/par/'+str(seg)+'/')}.items()
for par in pars:
name = par[0].rsplit('/',1)[-1]
messageid, _, pbytes = par[1]
pnode = xmlfile.createElement('par')
snode.appendChild(pnode)
pnode.setAttribute('bytes', str(pbytes))
pnode.setAttribute('name', name)
pnode.setAttribute('number', str(pars.index(par)+1))
pnode.appendChild(xmlfile.createTextNode(messageid))
text_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)
xmlout = text_re.sub('>\g<1></', xmlfile.toprettyxml(indent=' '))
return xmlout
def create_rlink(xmlfile):
if not output:
bn = "output.rlink"
else:
bn = os.path.basename(output)
yencdata = [0,0,0,0]
data = bz2.compress(xmlfile)
crc = "%08x" % (binascii.crc32(data) & 0xffffffff)
open(TEMPDIR+'/'+bn+'.bz2','w').write(data)
run_par2(['par2', 'c', '-b1', '-c1', TEMPDIR+'/'+bn+'.bz2'])
outpars = {}
pars = glob.glob(TEMPDIR+'/'+bn+'.bz2.*par2')
for i in pars:
outpars[os.path.basename(i)] = upload_article(open(i).read(), os.path.basename(i), yencdata)[0]
os.unlink(i)
os.unlink(TEMPDIR+'/'+bn+'.bz2')
articles = range(len(data) / articlesize + (1 if len(data) % articlesize > 0 else 0))
lastarticle = ""
for i in articles[::-1]:
headers = []
if articles.index(i) == 0:
headers.append("X-RLink-Name: %s" % bn)
headers.append("X-RLink-Crc: %s" % crc)
headers.append("X-RLink-Par2: %s" % json.dumps(outpars))
if articles.index(i) == len(articles)-1:
headers.append("X-RLink-Next: END")
lastarticle,_,_ = upload_article(data[i*articlesize:(i+1)*articlesize], bn+".rlink.bz2", yencdata, headers)
else:
headers.append("X-RLink-Next: %s" % lastarticle)
lastarticle,_,_ = upload_article(data[i*articlesize:(i+1)*articlesize], bn+".rlink.bz2", yencdata, headers)
return "river:%s" % lastarticle
def upload_nfo(nfo):
nfofile = open(nfo).read()
if len(nfofile) > articlesize:
logging.error("ERROR: NFO file too large to fit in an article")
return None
yencdata = [0,0,0,0]
article, _, _ = upload_article(nfofile, os.path.basename(nfo), yencdata, {})
return article
last = 0
bytes = 0
bandwidth = 0
def human(bytes):
if bytes > 1000000000:
return "%0.3fGB" % (bytes/1000000000.0)
if bytes > 1000000:
return "%0.3fMB" % (bytes/1000000.0)
if bytes > 1000:
return "%0.3fKB" % (bytes/1000.0)
return "%0.3fB" % bytes
def timer_bandwidth():
global bytes, last, bandwidth
while True:
gevent.sleep(0.5)
now = time.time()
bandwidth = bytes / (now-last) * 0.5
last = now
bytes = 0
arttotal = 0
artdone = 0
for i in files:
bn = os.path.basename(i)
if bn+'/arttotal' in meta:
arttotal += meta[bn+'/arttotal']
if bn+'/artleft' in meta:
artdone += len(meta[bn+'/artleft'])
artdone = arttotal - artdone
logging.info("Uploading [%d/%d articles] [%s/s]" % (artdone, arttotal, human(bandwidth)))
def upload_files():
global meta, TEMPDIR, par_pool, article_pool
par_pool = pool.Pool(parthreads)
article_pool = pool.Pool(connections+10)
segment_pool = pool.Pool()
gsegs = []
logging.info("Uploading articles and par files...")
for i in files:
bn=os.path.basename(i)
for j in meta[bn+'/segleft']:
segment_pool.spawn(handle_segment, i, j)
for i in gsegs:
i.start()
segment_pool.join()
par_pool.join()
article_pool.join()
logging.info("Articles uploaded.")
logging.info("Uploading river link...")
if output == None:
outfile = sys.stdout
else:
outfile = open(output,'w')
xmlfile = write_river()
outfile.write(xmlfile)
outfile.close()
logging.info("River link uploaded.")
if rlink:
if output == None:
frlink = open('output.river.rlink','w')
else:
frlink = open(output+'.rlink', 'w')
outrlink = create_rlink(xmlfile)
frlink.write(outrlink+'\n')
logging.info("The rlink for this river file is %s" % outrlink)
frlink.close()
for i in files:
bn=os.path.basename(i)
del meta[bn+'/segleft']
del meta[bn+'/artleft']
# delete all of the article results in meta
del meta[bn+'/segtotal']
del meta[bn+'/arttotal']
del meta[bn]
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Upload river files to usenet")
parser.add_argument('-s','--server', required=True, help="Usenet server to connect to", dest='server')
parser.add_argument('-o','--output', help="Where to output river file (default is stdout)", dest='output')
parser.add_argument('-u','--username', help="Usenet username to connect with", dest='username')
parser.add_argument('-p','--password', help="Usenet password to connect with", dest='password')
parser.add_argument('-c','--connections', help="Max connections to usenet", dest='connections', default=10, type=int)
parser.add_argument('-r', '--rlink', help="Create river link (currently always true)", dest='rlink', action='store_true', default=False)
parser.add_argument('-m', '--meta', help="Metadata for river file (e.g. -m name=\"river\" description=\"river\")", dest='meta', nargs='+')
parser.add_argument('-f', '--filemeta', help="Metadata for file contained within river file (e.g. -f fileinriverfile.mp3/name=\"river\" fileinriverfile.mp3/description=\"river\")", dest='filemeta', nargs='+')
parser.add_argument('-n','--nfo', help="NFO file to upload with this river", dest='nfo')
parser.add_argument('--filenfo', help="NFO file to upload with a river file (e.g. --filenfo fileinriverfile.mp3=nfofile.nfo)", dest='fnfo', nargs='+')
parser.add_argument('--ssl', help="Connect to usenet with SSL", dest='ssl', action='store_true', default=False)
parser.add_argument('--port', help="Port to connect to usenet with (default 119; 563 with SSL)", dest='port', type=int, default=-1)
parser.add_argument('--subject', help="Subject in usenet articles", dest='subject', default="[RIVER] \"{name}\"")
parser.add_argument('-d', '--domain', help="Domain in message IDs <mid@domain>", dest='domain', default="riverup")
parser.add_argument('-g', '--group', help="Usenet newsgroup to post articles in", dest='group', default="alt.binaries.test")
parser.add_argument('--poster', help="The from header in articles", dest='poster', default="river@riverup")
parser.add_argument('--articlesize', help="The size of uploaded articles", dest='articlesize', default=650000, type=int)
parser.add_argument('--segmentsize', help="Number of articles in a segment", dest='segmentsize', default=4, type=int)
parser.add_argument('--parblocks', help="For 1/parblocks redundancy (4 parblocks is 25%% redundancy)", dest='parblocks', default=4, type=int)
parser.add_argument('--parthreads', help="Max number of par2 instances running at once (should be number of cores plus one)", dest='parthreads', default=5, type=int)
parser.add_argument('files', help="Files to be added to river file", nargs='+')
ns = parser.parse_args(sys.argv[1:])
server = ns.server
username = ns.username
password = ns.password
port = ns.port
use_ssl = ns.ssl
connections = ns.connections
articlesize = ns.articlesize
segsize = ns.segmentsize
segbytes = segsize * articlesize
parblocks = ns.parblocks
parthreads = ns.parthreads
messagecontext = ns.domain
poster = ns.poster
group = ns.group
rlink = ns.rlink
subject = ns.subject
files = ns.files
output = ns.output
if ns.meta:
for i in ns.meta:
k, v = i.split('=', 1)
rivermeta[k] = v
if ns.filemeta:
for i in ns.filemeta:
k, v = i.split('=', 1)
f, k = k.rsplit('/', 1)
if not os.path.basename(f) in filemeta:
filemeta[os.path.basename(f)] = {k: v}
else:
filemeta[os.path.basename(f)][k] = v
if ns.nfo:
if not os.path.exists(ns.nfo):
logging.error("ERROR: File %s does not exist, nfo not added" % ns.nfo)
nfo = None
else:
nfo = ns.nfo
if ns.fnfo:
for i in ns.fnfo:
f, n = i.split('=', 1)
if not os.path.exists(n):
logging.error("ERROR: File %s does not exist, nfo not added" % n)
filenfo[os.path.basename(f)] = n
par = which('par2')
if par == None:
raise Exception, "Par2 not found in path. It is required for Upstream to function"
else:
logging.info("Found par2 at %s" % par)
init_connections()
init_metadata()
logging.info('Starting upload')
gup = gevent.Greenlet(upload_files)
timer = gevent.Greenlet(timer_bandwidth)
gup.start()
timer.start()
gup.join()
#meta.close() # Resume support has been disabled, uncomment when re-enabled
logging.info('Upload completed')