-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcamouflage.go
73 lines (68 loc) · 1.97 KB
/
camouflage.go
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
package main
import (
"fmt"
"sync"
"github.com/anacrolix/torrent"
pp "github.com/anacrolix/torrent/peer_protocol"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
// Provides torrent callbacks that can track peer information that would be useful for identifying
// camouflaging opportunities in the network.
type camouflageCollector struct {
mu sync.Mutex
SqliteConn *sqlite.Conn
peerConnToRowId map[*torrent.PeerConn]int64
}
func (me *camouflageCollector) Init() {
me.peerConnToRowId = make(map[*torrent.PeerConn]int64)
}
func (me *camouflageCollector) TorrentCallbacks() torrent.Callbacks {
return torrent.Callbacks{
PeerConnClosed: func(pc *torrent.PeerConn) {
me.mu.Lock()
defer me.mu.Unlock()
delete(me.peerConnToRowId, pc)
},
CompletedHandshake: func(peerConn *torrent.PeerConn, infoHash torrent.InfoHash) {
me.mu.Lock()
defer me.mu.Unlock()
err := sqlitex.Exec(
me.SqliteConn,
"insert into peers(infohash, extension_bytes, peer_id, remote_addr) values (?, ?, ?, ?)",
nil,
infoHash, peerConn.PeerExtensionBytes, fmt.Sprintf("%q", peerConn.PeerID[:]), peerConn.RemoteAddr.String())
if err != nil {
panic(err)
}
me.peerConnToRowId[peerConn] = me.SqliteConn.LastInsertRowID()
},
ReadMessage: func(conn *torrent.PeerConn, message *pp.Message) {
if message.Type != pp.Extended || message.ExtendedID != pp.HandshakeExtendedID {
return
}
me.mu.Lock()
defer me.mu.Unlock()
err := sqlitex.Exec(
me.SqliteConn,
"update peers set extended_handshake=? where rowid=?",
nil,
message.ExtendedPayload, me.peerConnToRowId[conn])
if err != nil {
panic(err)
}
},
ReadExtendedHandshake: func(conn *torrent.PeerConn, p *pp.ExtendedHandshakeMessage) {
me.mu.Lock()
defer me.mu.Unlock()
err := sqlitex.Exec(
me.SqliteConn,
"update peers set extended_v=? where rowid=?",
nil,
p.V, me.peerConnToRowId[conn])
if err != nil {
panic(err)
}
},
}
}