-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
292 lines (222 loc) · 5.98 KB
/
main.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
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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"path"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/ethersphere/bee/pkg/api"
"github.com/ethersphere/bee/pkg/cac"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/soc"
"github.com/ethersphere/bee/pkg/swarm"
)
const port = 9999
const topicStr = "test radio"
type segmentState struct {
ref swarm.Address
ts time.Time
}
func (s *segmentStore) store(key string, value swarm.Address) {
s.mtx.Lock()
defer s.mtx.Unlock()
s.segments[key] = segmentState{ref: value, ts: time.Now()}
}
func (s *segmentStore) get(key string) swarm.Address {
s.mtx.Lock()
defer s.mtx.Unlock()
return s.segments[key].ref
}
func (s *segmentStore) cleanup() {
s.mtx.Lock()
defer s.mtx.Unlock()
for k, v := range s.segments {
if time.Since(v.ts) > time.Hour {
delete(s.segments, k)
}
}
}
type segmentStore struct {
mtx sync.Mutex
segments map[string]segmentState
}
func main() {
var (
privateKey string
batchID string
since time.Duration
)
flag.StringVar(&privateKey, "private-key", "", "")
flag.StringVar(&batchID, "batch-id", "", "")
flag.DurationVar(&since, "since", time.Hour*24*7, "amount of time to rollback")
flag.Parse()
if privateKey == "" || batchID == "" {
log.Fatal("missing flag")
}
mtx := sync.Mutex{}
store := &segmentStore{segments: map[string]segmentState{}}
var index atomic.Uint64
index.Store(0)
topicRaw, err := crypto.LegacyKeccak256([]byte(topicStr))
if err != nil {
log.Fatal(err)
}
topicStr := hex.EncodeToString(topicRaw[:])
fmt.Println("topic", topicStr)
privKeyRaw, _ := hex.DecodeString(privateKey)
privKey, err := crypto.DecodeSecp256k1PrivateKey(privKeyRaw)
if err != nil {
log.Fatal(err)
}
signer := crypto.NewDefaultSigner(privKey)
signer.EthereumAddress()
ethAddr, err := signer.EthereumAddress()
if err != nil {
log.Fatal(err)
}
ethAddrStr := ethAddr.String()[2:]
fmt.Println("owner", ethAddrStr)
idx, err := getFeed(ethAddrStr, topicStr)
if err != nil {
log.Fatal(err)
}
if idx > 0 {
index.Store(idx + 1)
}
fmt.Println("using index", index.Load())
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mtx.Lock()
defer mtx.Unlock()
defer store.cleanup()
_, file := path.Split(r.URL.Path)
data, _ := io.ReadAll(r.Body)
if strings.Contains(file, ".m3u8") {
fmt.Println(string(data))
manifest := emplaceM3u8Urls(string(data), store)
fmt.Println("manifest", manifest)
_, err := updateFeed(ethAddrStr, batchID, identifier(topicStr, index.Load()), []byte(manifest), signer)
if err != nil {
fmt.Println("error", err)
return
}
index.Add(1)
} else {
ref, err := uploadData(data, batchID)
if err != nil {
fmt.Println("error", err)
return
}
store.store(file, ref)
fmt.Println("uploaded segment", ref)
}
}))
fmt.Printf("Starting server on %v\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
func emplaceM3u8Urls(manifest string, store *segmentStore) string {
lines := strings.Split(manifest, "\n")
for i, l := range lines {
if strings.Contains(l, ".ts") {
lines[i] = fmt.Sprintf("/bytes/%s", store.get(l).String())
}
}
return strings.Join(lines, "\n")
}
type referenceResponse struct {
Reference swarm.Address `json:"reference"`
}
func parseRef(r io.Reader) (swarm.Address, error) {
data, err := io.ReadAll(r)
if err != nil {
return swarm.ZeroAddress, err
}
var ref referenceResponse
err = json.Unmarshal(data, &ref)
if err != nil {
return swarm.ZeroAddress, err
}
return ref.Reference, nil
}
func getFeed(ethAddr, topic string) (uint64, error) {
resp, err := http.Get(fmt.Sprintf("http://localhost:1633/feeds/%s/%s", ethAddr, topic))
if err != nil {
return 0, err
}
if resp.StatusCode == http.StatusNotFound {
fmt.Println("feed not found")
return 0, nil
}
hexIndex := resp.Header.Get(api.SwarmFeedIndexHeader)
indexRaw, err := hex.DecodeString(hexIndex)
if err != nil {
return 0, err
}
binary.BigEndian.Uint64(indexRaw)
return binary.BigEndian.Uint64(indexRaw), nil
}
func updateFeed(owner string, batchID string, id []byte, data []byte, signer crypto.Signer) (swarm.Address, error) {
// manifest data
ref, err := uploadData(data, batchID)
if err != nil {
return swarm.ZeroAddress, err
}
// prepare ref data for soc upload
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(time.Now().Unix()))
data = append(buf, ref.Bytes()...)
ch, err := cac.New(data)
if err != nil {
return swarm.ZeroAddress, err
}
sch, err := soc.New(id, ch).Sign(signer)
if err != nil {
return swarm.ZeroAddress, err
}
fmt.Println("soc", sch.Address())
sigStr := hex.EncodeToString(sch.Data()[swarm.HashSize : swarm.HashSize+swarm.SocSignatureSize])
idStr := hex.EncodeToString(id)
// upload soc with topic and id
r, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:1633/soc/%s/%s?sig=%s", owner, idStr, sigStr), bytes.NewBuffer(ch.Data()))
if err != nil {
return swarm.ZeroAddress, err
}
r.Header.Set(api.SwarmPostageBatchIdHeader, batchID)
r.Header.Set("content-type", "application/octet-stream")
res, err := http.DefaultClient.Do(r)
if err != nil {
return swarm.ZeroAddress, err
}
return parseRef(res.Body)
}
func uploadData(data []byte, batchID string) (swarm.Address, error) {
// upload data and get swarm reference
r, err := http.NewRequest("POST", "http://localhost:1633/bytes", bytes.NewBuffer(data))
if err != nil {
return swarm.ZeroAddress, err
}
r.Header.Set(api.SwarmPostageBatchIdHeader, batchID)
resp, err := http.DefaultClient.Do(r)
if err != nil {
return swarm.ZeroAddress, err
}
return parseRef(resp.Body)
}
func identifier(topic string, index uint64) []byte {
indexB := make([]byte, 8)
binary.BigEndian.PutUint64(indexB, index)
topicHex, _ := hex.DecodeString(topic)
h, err := crypto.LegacyKeccak256(append(topicHex, indexB...))
if err != nil {
log.Fatal(err)
}
return h
}