forked from meehow/privtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
167 lines (153 loc) · 3.81 KB
/
storage.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
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
package main
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"net"
"sync"
"time"
)
type serializedPeer string
type hash [20]byte
type shard struct {
swarms map[hash]swarm
sync.RWMutex
}
type swarm struct {
seeders map[serializedPeer]int64
leechers map[serializedPeer]int64
}
var shards = NewShards(512)
var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
func shardIndex(hash [20]byte) int {
return int(binary.BigEndian.Uint32(hash[:4])) % len(shards)
}
func NewShards(size int) []*shard {
shards := make([]*shard, size)
for i := 0; i < size; i++ {
shards[i] = &shard{
swarms: make(map[hash]swarm),
}
}
return shards
}
func serialize(ip string, port uint16) serializedPeer {
return serializedPeer(append(net.ParseIP(ip), byte(port>>8), byte(port)))
}
func PutPeer(room, infoHash, ip string, port uint16, seeding bool) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]
shard.Lock()
if _, ok := shard.swarms[h]; !ok {
shard.swarms[h] = swarm{
seeders: make(map[serializedPeer]int64),
leechers: make(map[serializedPeer]int64),
}
}
client := serialize(ip, port)
if seeding {
shard.swarms[h].seeders[client] = time.Now().Unix()
} else {
shard.swarms[h].leechers[client] = time.Now().Unix()
}
shard.Unlock()
}
func DeletePeer(room, infoHash, ip string, port uint16) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]
shard.Lock()
if _, ok := shard.swarms[h]; !ok {
return
}
client := serialize(ip, port)
delete(shard.swarms[h].seeders, client)
delete(shard.swarms[h].leechers, client)
shard.Unlock()
}
func GraduateLeecher(room, infoHash, ip string, port uint16) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]
shard.Lock()
if _, ok := shard.swarms[h]; !ok {
shard.swarms[h] = swarm{
seeders: make(map[serializedPeer]int64),
leechers: make(map[serializedPeer]int64),
}
}
client := serialize(ip, port)
shard.swarms[h].seeders[client] = time.Now().Unix()
delete(shard.swarms[h].leechers, client)
shard.Unlock()
}
func GetPeers(room, infoHash, ip string, port uint16, seeding bool, numWant uint) (peersIPv4, peersIPv6 []byte, numSeeders, numLeechers int) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]
shard.RLock()
client := serialize(ip, port)
// seeders don't need other seeders
if !seeding {
for peer := range shard.swarms[h].seeders {
if numWant == 0 {
break
}
if bytes.HasPrefix([]byte(peer), v4InV6Prefix) {
peersIPv4 = append(peersIPv4, peer[12:]...)
} else {
peersIPv6 = append(peersIPv6, peer...)
}
numWant--
}
}
for peer := range shard.swarms[h].leechers {
if peer == client {
continue
}
if numWant == 0 {
break
}
if bytes.HasPrefix([]byte(peer), v4InV6Prefix) {
peersIPv4 = append(peersIPv4, peer[12:]...)
} else {
peersIPv6 = append(peersIPv6, peer...)
}
numWant--
}
numSeeders = len(shard.swarms[h].seeders)
numLeechers = len(shard.swarms[h].leechers)
shard.RUnlock()
return
}
func GetStats(room, infoHash string) (numSeeders, numLeechers int) {
h := sha1.Sum([]byte(room + infoHash))
shard := shards[shardIndex(h)]
shard.RLock()
numSeeders = len(shard.swarms[h].seeders)
numLeechers = len(shard.swarms[h].leechers)
shard.RUnlock()
return
}
func Cleanup() {
for {
expiration := time.Now().Unix() - 600
for _, shard := range shards {
shard.Lock()
for h, swarm := range shard.swarms {
for peer, lastSeen := range swarm.seeders {
if lastSeen < expiration {
delete(swarm.seeders, peer)
}
}
for peer, lastSeen := range swarm.leechers {
if lastSeen < expiration {
delete(swarm.leechers, peer)
}
}
if len(swarm.leechers) == 0 && len(swarm.seeders) == 0 {
delete(shard.swarms, h)
}
}
shard.Unlock()
}
time.Sleep(time.Minute * 3)
}
}