-
Notifications
You must be signed in to change notification settings - Fork 16
/
pbsapi.go
456 lines (382 loc) · 12.4 KB
/
pbsapi.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"bytes"
"context"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"hash/crc32"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/klauspost/compress/zstd"
"golang.org/x/net/http2"
)
type IndexCreateResp struct {
WriterID int `json:"data"`
}
type IndexPutReq struct {
DigestList []string `json:"digest-list"`
OffsetList []uint64 `json:"offset-list"`
WriterID uint64 `json:"wid"`
}
type DynamicCloseReq struct {
ChunkCount uint64 `json:"chunk-count"`
CheckSum string `json:"csum"`
Size uint64 `json:"size"`
WriterID uint64 `json:"wid"`
}
type File struct {
CryptMode string `json:"crypt-mode"`
Csum string `json:"csum"`
Filename string `json:"filename"`
Size int64 `json:"size"`
}
type ChunkUploadStats struct {
CompressedSize int64 `json:"compressed_size"`
Count int `json:"count"`
Duplicates int `json:"duplicates"`
Size int64 `json:"size"`
}
type Unprotected struct {
ChunkUploadStats ChunkUploadStats `json:"chunk_upload_stats"`
}
type BackupManifest struct {
BackupID string `json:"backup-id"`
BackupTime int64 `json:"backup-time"`
BackupType string `json:"backup-type"`
Files []File `json:"files"`
Signature interface{} `json:"signature"`
Unprotected Unprotected `json:"unprotected"`
}
type AuthErr struct {
}
func (e *AuthErr) Error() string {
return "Authentication error"
}
type PBSClient struct {
baseurl string
certfingerprint string
apitoken string
secret string
authid string
datastore string
namespace string
manifest BackupManifest
insecure bool
client http.Client
tlsConfig tls.Config
writersManifest map[uint64]int
}
var blobCompressedMagic = []byte{49, 185, 88, 66, 111, 182, 163, 127}
var blobUncompressedMagic = []byte{66, 171, 56, 7, 190, 131, 112, 161}
func (pbs *PBSClient) CreateDynamicIndex(name string) (uint64, error) {
req, err := http.NewRequest("POST", pbs.baseurl+"/dynamic_index", bytes.NewBuffer([]byte(fmt.Sprintf("{\"archive-name\": \"%s\"}", name))))
if err != nil {
return 0, err
}
req.Header.Add("Authorization", fmt.Sprintf("PBSAPIToken=%s:%s", pbs.authid, pbs.secret))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return 0, err
}
if resp2.StatusCode != http.StatusOK {
resp1, err := io.ReadAll(resp2.Body)
fmt.Println("Error making request:", string(resp1), string(resp2.Proto))
return 0, err
}
resp1, err := io.ReadAll(resp2.Body)
var R IndexCreateResp
err = json.Unmarshal(resp1, &R)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return 0, err
}
fmt.Println("Writer id: ", R.WriterID)
defer resp2.Body.Close()
f := File{
CryptMode: "none",
Csum: "",
Filename: name,
Size: 0,
}
pbs.manifest.Files = append(pbs.manifest.Files, f)
pbs.writersManifest[uint64(R.WriterID)] = len(pbs.manifest.Files) - 1
return uint64(R.WriterID), nil
}
func (pbs *PBSClient) UploadUncompressedChunk(writerid uint64, digest string, chunkdata []byte) error {
outBuffer := make([]byte, 0)
outBuffer = append(outBuffer, blobUncompressedMagic...)
checksum := crc32.Checksum(chunkdata, crc32.IEEETable)
outBuffer = binary.LittleEndian.AppendUint32(outBuffer, checksum)
outBuffer = append(outBuffer, chunkdata...)
q := &url.Values{}
q.Add("digest", digest)
q.Add("encoded-size", fmt.Sprintf("%d", len(outBuffer)))
q.Add("size", fmt.Sprintf("%d", len(chunkdata)))
q.Add("wid", fmt.Sprintf("%d", writerid))
req, err := http.NewRequest("POST", pbs.baseurl+"/dynamic_chunk?"+q.Encode(), bytes.NewBuffer(outBuffer))
if err != nil {
return err
}
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return err
}
if resp2.StatusCode != http.StatusOK {
resp1, err := io.ReadAll(resp2.Body)
fmt.Println("Error making request:", string(resp1), string(resp2.Proto))
return err
}
return nil
}
func (pbs *PBSClient) UploadCompressedChunk(writerid uint64, digest string, chunkdata []byte) error {
outBuffer := make([]byte, 0)
outBuffer = append(outBuffer, blobCompressedMagic...)
compressedData := make([]byte, 0)
//opt := zstd.WithEncoderLevel(zstd.SpeedFastest)
w, _ := zstd.NewWriter(nil)
compressedData = w.EncodeAll(chunkdata, compressedData)
checksum := crc32.Checksum(compressedData, crc32.IEEETable)
//binary.Write(outBuffer, binary.LittleEndian, checksum)
outBuffer = binary.LittleEndian.AppendUint32(outBuffer, checksum)
//fmt.Printf("Appended checksum %08x , len: %d\n", checksum, len(outBuffer))
outBuffer = append(outBuffer, compressedData...)
if len(compressedData) > len(chunkdata) {
pbs.UploadUncompressedChunk(writerid, digest, chunkdata)
return nil
}
//fmt.Printf("Compressed: %d , Orig: %d\n", len(compressedData), len(chunkdata))
q := &url.Values{}
q.Add("digest", digest)
q.Add("encoded-size", fmt.Sprintf("%d", len(outBuffer)))
q.Add("size", fmt.Sprintf("%d", len(chunkdata)))
q.Add("wid", fmt.Sprintf("%d", writerid))
req, err := http.NewRequest("POST", pbs.baseurl+"/dynamic_chunk?"+q.Encode(), bytes.NewBuffer(outBuffer))
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return err
}
if resp2.StatusCode != http.StatusOK {
resp1, err := io.ReadAll(resp2.Body)
fmt.Println("Error making request:", string(resp1), string(resp2.Proto))
return err
}
return nil
}
func (pbs *PBSClient) AssignChunks(writerid uint64, digests []string, offsets []uint64) error {
indexput := &IndexPutReq{
WriterID: writerid,
DigestList: digests,
OffsetList: offsets,
}
jsondata, err := json.Marshal(indexput)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", pbs.baseurl+"/dynamic_index", bytes.NewBuffer(jsondata))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return err
}
defer resp2.Body.Close()
return nil
}
func (pbs *PBSClient) CloseDynamicIndex(writerid uint64, checksum string, totalsize uint64, chunkcount uint64) error {
finishreq := &DynamicCloseReq{
WriterID: writerid,
CheckSum: checksum,
Size: totalsize,
ChunkCount: chunkcount,
}
jsonpayload, err := json.Marshal(finishreq)
if err != nil {
return err
}
req, err := http.NewRequest("POST", pbs.baseurl+"/dynamic_close", bytes.NewBuffer(jsonpayload))
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("PBSAPIToken=%s:%s", pbs.authid, pbs.secret))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return err
}
f := &pbs.manifest.Files[pbs.writersManifest[writerid]]
f.Csum = checksum
f.Size = int64(totalsize)
defer resp2.Body.Close()
return nil
}
func (pbs *PBSClient) UploadBlob(name string, data []byte) error {
out := make([]byte, 0)
out = append(out, blobUncompressedMagic...)
checksum := crc32.ChecksumIEEE(data)
out = binary.LittleEndian.AppendUint32(out, checksum)
out = append(out, data...)
q := &url.Values{}
q.Add("encoded-size", fmt.Sprintf("%d", len(out)))
q.Add("file-name", name)
req, _ := http.NewRequest("POST", pbs.baseurl+"/blob?"+q.Encode(), bytes.NewBuffer(out))
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return err
}
if resp2.StatusCode != http.StatusOK {
resp1, err := io.ReadAll(resp2.Body)
fmt.Println("Error making request:", string(resp1), string(resp2.Proto))
return err
}
return nil
}
func (pbs *PBSClient) UploadManifest() error {
manifestBin, err := json.Marshal(pbs.manifest)
if err != nil {
return err
}
return pbs.UploadBlob("index.json.blob", manifestBin)
}
func (pbs *PBSClient) Finish() error {
req, err := http.NewRequest("POST", pbs.baseurl+"/finish", nil)
req.Header.Add("Authorization", fmt.Sprintf("PBSAPIToken=%s:%s", pbs.authid, pbs.secret))
if err != nil {
return err
}
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
if err != nil {
return err
}
}
defer resp2.Body.Close()
return nil
}
func (pbs *PBSClient) Connect(reader bool) {
pbs.writersManifest = make(map[uint64]int)
pbs.tlsConfig = tls.Config{
InsecureSkipVerify: pbs.insecure,
}
if pbs.insecure {
pbs.tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
// Extract the peer certificate
if len(rawCerts) == 0 {
return fmt.Errorf("no certificates presented by the peer")
}
peerCert, err := x509.ParseCertificate(rawCerts[0])
if err != nil {
return fmt.Errorf("failed to parse certificate: %v", err)
}
// Calculate the SHA-256 fingerprint of the certificate
expectedFingerprint := strings.ReplaceAll(pbs.certfingerprint, ":", "")
calculatedFingerprint := sha256.Sum256(peerCert.Raw)
// Compare the calculated fingerprint with the expected one
if hex.EncodeToString(calculatedFingerprint[:]) != expectedFingerprint {
return fmt.Errorf("certificate fingerprint does not match (%s,%s)", expectedFingerprint, hex.EncodeToString(calculatedFingerprint[:]))
}
// If the fingerprint matches, the certificate is considered valid
return nil
}
}
pbs.manifest.BackupTime = time.Now().Unix()
pbs.manifest.BackupType = "host"
if pbs.manifest.BackupID == "" {
hostname, _ := os.Hostname()
pbs.manifest.BackupID = hostname
}
pbs.client = http.Client{
Transport: &http2.Transport{
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
//This is one of the trickiest parts, GO http2 library does not support starting with http1 and upgrading to 2 after
//So to achieve that the function to create SSL socket has been hijacked here
//Here an http 1.1 request to authenticate, start the backup and require upgrade to HTTP2 is done then the socket is passed to
// http2.Transport handler
conn, err := tls.Dial(network, addr, &pbs.tlsConfig)
if err != nil {
return nil, err
}
q := &url.Values{}
q.Add("backup-time", fmt.Sprintf("%d", pbs.manifest.BackupTime))
q.Add("backup-type", pbs.manifest.BackupType)
q.Add("store", pbs.datastore)
if pbs.namespace != "" {
q.Add("ns", pbs.namespace)
}
q.Add("backup-id", pbs.manifest.BackupID)
q.Add("debug", "1")
conn.Write([]byte("GET /api2/json/backup?" + q.Encode() + " HTTP/1.1\r\n"))
conn.Write([]byte("Authorization: " + fmt.Sprintf("PBSAPIToken=%s:%s", pbs.authid, pbs.secret) + "\r\n"))
if !reader {
conn.Write([]byte("Upgrade: proxmox-backup-protocol-v1\r\n"))
} else {
conn.Write([]byte("Upgrade: proxmox-backup-reader-protocol-v1\r\n"))
}
conn.Write([]byte("Connection: Upgrade\r\n\r\n"))
fmt.Printf("Reading response to upgrade...\n")
buf := make([]byte, 0)
for !strings.HasSuffix(string(buf), "\r\n\r\n") && !strings.HasSuffix(string(buf), "\n\n") {
//fmt.Println(buf)
b2 := make([]byte, 1)
nbytes, err := conn.Read(b2)
if err != nil || nbytes == 0 {
fmt.Println("Connection unexpectedly closed")
return nil, err
}
buf = append(buf, b2[:nbytes]...)
//fmt.Println(string(b2))
}
lines := strings.Split(string(buf), "\n")
if len(lines) > 0 {
toks := strings.Split(lines[0], " ")
if len(toks) > 1 && toks[1] != "101" {
fmt.Println("Unexpected response code: " + strings.Join(toks[1:], " "))
return nil, &AuthErr{}
}
}
fmt.Printf("Upgraderesp: %s\n", string(buf))
fmt.Println("Successfully upgraded to HTTP/2.")
return conn, nil
},
},
}
}
func (pbs *PBSClient) DownloadPreviousToBytes(archivename string) ([]byte, error) { //In the future also download to tmp if index is extremely big...
q := &url.Values{}
q.Add("archive-name", archivename)
req, err := http.NewRequest("GET", pbs.baseurl+"/previous?"+q.Encode(), nil)
req.Header.Add("Authorization", fmt.Sprintf("PBSAPIToken=%s:%s", pbs.authid, pbs.secret))
if err != nil {
return nil, err
}
resp2, err := pbs.client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return nil, err
}
defer resp2.Body.Close()
ret, err := io.ReadAll(resp2.Body)
if err != nil {
return nil, err
}
return ret, nil
}