aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Ball <chris@printf.net>2015-05-13 16:37:38 -0400
committerChris Ball <chris@printf.net>2015-05-13 16:37:38 -0400
commit01a7cd79111cf0d8fc67c91454f147d5213634f1 (patch)
tree511273da5c7c72e9c7081e2c616f9f7428c83048
parent3eeaa7bfd8d0a40a175d77203d12edb29d586cd6 (diff)
Use webtorrent's deps instead of webtorrent itself
-rwxr-xr-xgit-remote-gitswarm28
-rwxr-xr-xgitswarmd23
2 files changed, 38 insertions, 13 deletions
diff --git a/git-remote-gitswarm b/git-remote-gitswarm
index 99c97b2..517de84 100755
--- a/git-remote-gitswarm
+++ b/git-remote-gitswarm
@@ -1,7 +1,10 @@
#!/usr/bin/env node
+var DHT = require('bittorrent-dht')
var exec = require('child_process').exec
-var WebTorrent = require('webtorrent')
+var magnet = require('magnet-uri')
+var Swarm = require('bittorrent-swarm')
+var ut_gitswarm = require('ut_gitswarm')
// We use console.warn (stderr) because git ignores our writes to stdout.
url = process.argv[3].replace(/^gitswarm:/i, 'git:')
@@ -29,16 +32,21 @@ exec('git ls-remote ' + url + ' HEAD', function (err, stdout, stderr) {
console.warn("Okay, we want to get: " + ref)
- var client = new WebTorrent()
+ var dht = new DHT()
var magnetUri = 'magnet:?xt=urn:btih:' + ref
+ var parsed = magnet(magnetUri)
+ dht.on('ready', function () {
+ dht.lookup(parsed.infoHash)
+ })
+ dht.on('peer', function (addr, hash, from) {
+ console.error(addr, hash, from)
+ swarm.addPeer(addr)
+ })
- client.add(magnetUri, function (torrent) {
- // Got torrent metadata!
- console.log('Torrent info hash:', torrent.infoHash)
-
- torrent.files.forEach(function (file) {
- // Get a url for each file
- console.log(file)
- })
+ var swarm = new Swarm(parsed.infoHash, 'cafebabecafebabecafecafebabecafebabecafe')
+ swarm.on('wire', function(wire) {
+ console.error('we got a wire')
+ wire.use('ut_gitswarm')
+ //wire.ut_gitswarm.askforsha(parsed.infoHash)
})
})
diff --git a/gitswarmd b/gitswarmd
index 960b4d8..4ec7fb5 100755
--- a/gitswarmd
+++ b/gitswarmd
@@ -1,9 +1,14 @@
#!/usr/bin/env node
-var spawn = require('child_process').spawn
+var DHT = require('bittorrent-dht')
var glob = require('glob')
+var magnet = require('magnet-uri')
+var net = require('net')
+var Protocol = require('bittorrent-protocol')
+var spawn = require('child_process').spawn
+var Swarm = require('bittorrent-swarm')
var WebTorrent = require('webtorrent')
-var DHT = require('bittorrent-dht')
+var ut_gitswarm = require('ut_gitswarm')
function die (error) {
console.error(error)
@@ -30,12 +35,14 @@ repos.forEach(function (repo) {
arr = line.toString().split(' ')
if (arr.length == 2) {
var sha = arr[0].toString()
+ // First four chars are git-upload-pack's length-of-line metadata.
+ sha = sha.substring(4)
var ref = arr[1].toString()
if (ref.search(/^refs\/heads\//) !== -1 || ref.search(/^refs\/remotes\//) !== -1) {
console.log('Announcing ' + sha + ' for ref ' + ref + ' on repo ' + repo)
announcedRefs[sha] = repo
console.log(announcedRefs)
- dht.announce(sha, 20000, function (err) {
+ dht.announce(sha, 30000, function (err) {
if (err !== null)
console.log('Announced ' + sha)
})
@@ -52,4 +59,14 @@ repos.forEach(function (repo) {
})
})
+net.createServer(function (socket) {
+ var wire = new Protocol()
+ socket.pipe(wire).pipe(socket)
+ wire.on('handshake', function (infoHash, peerId) {
+ console.log('Received handshake for ' + infoHash)
+ wire.handshake(new Buffer(infoHash), new Buffer(peerId))
+ wire.use('ut_gitswarm')
+ })
+}).listen(30000)
+