| 1 |
// Copyright 2019 Google LLC. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Hammer tool sends multiple add-pre-chain requests to Submission proxy at the same time. |
| 16 |
package main |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"log" |
| 26 |
"net/http" |
| 27 |
"os" |
| 28 |
"sync" |
| 29 |
"time" |
| 30 |
|
| 31 |
ct "github.com/google/certificate-transparency-go" |
| 32 |
"github.com/google/certificate-transparency-go/schedule" |
| 33 |
"github.com/google/certificate-transparency-go/submission" |
| 34 |
) |
| 35 |
|
| 36 |
// Default number of submissions is intentionally low. |
| 37 |
// After several runs, likely to hit Log rate-limits. |
| 38 |
var ( |
| 39 |
proxyEndpoint = flag.String("proxy_endpoint", "http://localhost:5951/", "Endpoint for HTTP (host:port). Final slash is expected") |
| 40 |
timeout = flag.Duration("duration", 0*time.Minute, "Time to run continuous flow of submissions. "+ |
| 41 |
"When this and --count both have non-zero values, submission ends upon reaching earliest restriction") |
| 42 |
count = flag.Int("count", 10, "Total number of submissions to execute. "+ |
| 43 |
"When this and --duration both have non-zero values, submission ends upon reaching earliest restriction") |
| 44 |
qps = flag.Int("qps", 5, "Number of requests per second") |
| 45 |
) |
| 46 |
|
| 47 |
func main() { |
| 48 |
flag.Parse() |
| 49 |
certData, err := os.ReadFile("submission/hammer/testdata/precert.der") |
| 50 |
if err != nil { |
| 51 |
log.Fatal(err) |
| 52 |
} |
| 53 |
interimData, err := os.ReadFile("submission/hammer/testdata/intermediate.der") |
| 54 |
if err != nil { |
| 55 |
log.Fatal(err) |
| 56 |
} |
| 57 |
|
| 58 |
var req ct.AddChainRequest |
| 59 |
req.Chain = append(req.Chain, certData) |
| 60 |
req.Chain = append(req.Chain, interimData) |
| 61 |
|
| 62 |
postBody, err := json.Marshal(req) |
| 63 |
if err != nil { |
| 64 |
log.Fatalf("%v\n", err) |
| 65 |
} |
| 66 |
|
| 67 |
// Submission runs until timeout or count is reached. If any of those flags |
| 68 |
// is set to 0, only other flag value is used as restriction. |
| 69 |
ctx, cancel := context.WithCancel(context.Background()) |
| 70 |
if *timeout > 0 { |
| 71 |
ctx, cancel = context.WithTimeout(context.Background(), *timeout) |
| 72 |
} else if *count <= 0 { |
| 73 |
log.Print("Both timeout and count flags set to 0. No submissions to be sent.") |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
leftToSend := *count |
| 78 |
// If count flag is not set, send *qps requests each second until *timeout. |
| 79 |
if *count <= 0 { |
| 80 |
leftToSend = int(*timeout/time.Second) * *qps |
| 81 |
} |
| 82 |
var batchSize int |
| 83 |
|
| 84 |
schedule.Every(ctx, time.Second, func(ctx context.Context) { |
| 85 |
batchSize = *qps |
| 86 |
if leftToSend < *qps { |
| 87 |
batchSize = leftToSend |
| 88 |
} |
| 89 |
leftToSend -= batchSize |
| 90 |
if batchSize == 0 { |
| 91 |
cancel() |
| 92 |
} |
| 93 |
var wg sync.WaitGroup |
| 94 |
wg.Add(batchSize) |
| 95 |
for i := 0; i < batchSize; i++ { |
| 96 |
go func() { |
| 97 |
url := *proxyEndpoint + "ct/v1/proxy/add-pre-chain/" |
| 98 |
resp, err := http.Post(url, "application/json", bytes.NewBuffer(postBody)) |
| 99 |
if err != nil { |
| 100 |
log.Fatalf("http.Post(%s)=(_,%q); want (_,nil)", url, err) |
| 101 |
} |
| 102 |
defer func() { |
| 103 |
if err := resp.Body.Close(); err != nil { |
| 104 |
log.Fatalf("Unable to close response body: %v", err) |
| 105 |
} |
| 106 |
}() |
| 107 |
var scts submission.SCTBatch |
| 108 |
err = json.NewDecoder(resp.Body).Decode(&scts) |
| 109 |
if err != nil { |
| 110 |
responseData, err := io.ReadAll(resp.Body) |
| 111 |
if err != nil { |
| 112 |
log.Fatalf("Unable to parse response: %v", err) |
| 113 |
} |
| 114 |
log.Fatalf("Unable to decode response: %v\n%v", err, responseData) |
| 115 |
} |
| 116 |
fmt.Printf("%v\n", scts) |
| 117 |
wg.Done() |
| 118 |
}() |
| 119 |
} |
| 120 |
wg.Wait() |
| 121 |
}) |
| 122 |
} |