-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
160 lines (136 loc) · 4.05 KB
/
test.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
// Test the insertion of multiple records in an LCP License Server.
//
// Source issue from Internet Archive:
// I ran LCP encrypting tool and it errored while notifying the LCP server.
// The error was TLS Handshake Timeout.
// The TLS handshake occurs at the start of establishing the connection with LCP License Server.
// Notify the LCP Server : Put "https://<LCP_License_server>:8989/contents/<content_id>": net/http: TLS handshake timeout
//
// There are several lcpencrypt instances running in parallel, which all send notifications to the unique LCP server.
// lcpencrypt is moving the file and just notifying the server.
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"time"
"github.com/google/uuid"
)
type EncryptionNotification struct {
ContentID string `json:"content-id"`
ContentKey []byte `json:"content-encryption-key"`
StorageMode int `json:"storage-mode"`
Output string `json:"protected-content-location"`
FileName string `json:"protected-content-disposition"`
Size int64 `json:"protected-content-length"`
Checksum string `json:"protected-content-sha256"`
ContentType string `json:"protected-content-type,omitempty"`
}
type Problem struct {
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Status int `json:"status,omitempty"` //if present = http response code
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
}
var Counter int
func main() {
// get parameters
var serverURL = flag.String("url", "", "LCP License Server URL")
var tick = flag.Int("tick", 100, "Tick time in ms, 100 ms default")
var testtime = flag.Int("testtime", 4000, "Test time in ms, 4 sc default")
flag.Parse()
if *serverURL == "" {
fmt.Println("-url param is required")
return
}
start := time.Now()
ticker := time.NewTicker(time.Duration(*tick) * time.Millisecond)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case t := <-ticker.C:
callLicenseServer(t, *serverURL)
}
}
}()
time.Sleep(time.Duration(*testtime) * time.Millisecond)
ticker.Stop()
done <- true
elapsed := time.Since(start)
fmt.Println("Process took", elapsed, "counter hit", Counter)
}
func callLicenseServer(t time.Time, serverURL string) {
// init the file name
filename := fmt.Sprintf("test-%s.epub", t.Format(time.RFC3339Nano))
// init the content key
contentKey, err := generateKey(16)
if err != nil {
fmt.Println("unable to generate a key")
}
// init the encryption notification structure
notif := EncryptionNotification{
ContentKey: contentKey,
StorageMode: 2, // already stored in a file system
Output: "http://edrlab.org/encrypted/" + filename,
FileName: filename,
Size: 65348042,
Checksum: "3d2a8964075bd2064d4234ab03ec9da0f96006d8058142301d58c9c1350b6717",
ContentType: "application/epub+zip",
}
// set access to the license server
username := "laurent"
password := "laurent"
// set content ID
contentID := uuid.New().String()
// prepare the call to service/content/<id>,
url := serverURL + "/contents/" + contentID
jsonBody, err := json.Marshal(notif)
if err != nil {
fmt.Println("json marshal error", err)
return
}
req, err := http.NewRequest("PUT", url, bytes.NewReader(jsonBody))
if err != nil {
fmt.Println("call error", err)
return
}
req.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("call error", err)
return
}
if (resp.StatusCode != 302) && (resp.StatusCode/100) != 2 {
fmt.Printf("lcp server error %d\n", resp.StatusCode)
// details
b, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("unable to read response body")
}
var pb Problem
err = json.Unmarshal(b, &pb)
if err != nil {
fmt.Println("unable to unmarshal response body")
}
fmt.Println("detail: ", pb.Detail)
} else {
Counter++
}
}
func generateKey(size int) ([]byte, error) {
k := make([]byte, size)
_, err := rand.Read(k)
if err != nil {
return nil, err
}
return k, nil
}