-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtube2tube.coffee
executable file
·64 lines (53 loc) · 1.59 KB
/
tube2tube.coffee
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
#!./node_modules/coffee-script/bin/coffee
config = require './config'
async = require 'async'
_ = require 'lodash'
bs = require 'nodestalker'
handleError = (error)->
console.error error
console.trace()
process.exit(1)
class Forward
constructor: (@rule)->
[@from, @to] = @parseRule(@rule)
console.log "Forwarding #{@rule}"
parseRule: (rule)->
[from, to] = _.map _.map(rule.split('->', 2), _.trim), (dns)->
[server, tube] = dns.split('/', 2)
client: bs.Client(server)
tube: tube
run: ()=>
from_client = @from.client
to_client = @to.client
from_tube = @from.tube
to_tube = @to.tube
from_client.watch(from_tube)
.onSuccess (data)->
to_client.use(to_tube)
.onSuccess (toData)->
resJob = ->
from_client
.reserve()
.onSuccess (job)->
console.log "Reserved #{from_tube}", job.id
to_client.put(job.data)
.onSuccess (fwdData)->
console.log "Forwarded #{from_tube} -> #{to_tube}", fwdData
from_client.deleteJob(job.id)
.onSuccess (delMsg)->
console.log "Deleted #{from_tube}", job.id
resJob()
.onError handleError
.onError handleError
true
.onError handleError
true
resJob()
.onError handleError
.onError handleError
forwarders = _.map config.get('rules'), (ruleLine)-> new Forward(ruleLine)
async.parallel _.map(forwarders, 'run'), (err, results)->
if err?
console.error(err)
process.exit(1)
console.log results