Skip to content

Commit c2d5af6

Browse files
author
dominicriordan
committed
ctrl+c to stop test server, fix bug where not reading jwt count correctly
1 parent 0bdee2d commit c2d5af6

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ Flags:
8787
-H, --headers strings headers to send in request, can have multiple i.e -H 'content-type:application/json' -H' connection:close'
8888
-h, --help help for run
8989
--jwt-aud string JWT audience (aud) claim
90+
--jwt-claims string JWT custom claims
9091
--jwt-header string JWT header field name
9192
--jwt-iss string JWT issuer (iss) claim
9293
--jwt-key string JWT signing private key path
9394
--jwt-kid string JWT KID
9495
--jwt-sub string JWT subject (sub) claim
95-
--jwt-claims string JWT custom claims as a JSON string, ex: {"iat": 1719410063, "browser": "chrome"}
96-
-f, --jwts-filename string File path & name where the JWTs to use are stored
96+
-f, --jwts-filename string File path for pre-generated JWTs, separated by new lines
9797
-m, --method string request method (default "GET")
9898
--mtls-cert string mTLS cert path
9999
--mtls-key string mTLS cert private key path
@@ -222,6 +222,13 @@ https://github.com/domsolutions/gopayloader
222222
+-----------------------+-------------------------------+
223223
```
224224
225+
If you have your own JWTs you want to test, you can supply a file to send the JWTs i.e. `./my-jwts.txt` where each jwt is separated by a new line.
226+
227+
```shell
228+
./gopayloader run http://localhost:8081 -c 1 -r 1000000 --jwt-header "my-jwt" -f ./my-jwts.txt
229+
```
230+
231+
225232
To remove all generated jwts;
226233
227234
```shell

cmd/payloader/test-server.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"log"
1212
"net/http"
1313
"os"
14+
"os/signal"
1415
"path/filepath"
1516
"strconv"
1617
"strings"
18+
"syscall"
1719
"time"
1820
)
1921

@@ -87,9 +89,25 @@ var runServerCmd = &cobra.Command{
8789
},
8890
}
8991

90-
if err := server.ListenAndServe(addr); err != nil {
91-
return err
92+
errs := make(chan error)
93+
go func() {
94+
if err := server.ListenAndServe(addr); err != nil {
95+
log.Println(err)
96+
errs <- err
97+
}
98+
}()
99+
100+
c := make(chan os.Signal, 1)
101+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
102+
103+
select {
104+
case <-c:
105+
log.Println("User cancelled, shutting down")
106+
server.Shutdown()
107+
case err := <-errs:
108+
log.Printf("Got error from server; %v \n", err)
92109
}
110+
93111
return nil
94112
}
95113

pkgs/jwt-generator/cache.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"github.com/pterm/pterm"
78
"os"
89
"strconv"
910
"strings"
@@ -31,9 +32,9 @@ func newCache(f *os.File) (*cache, error) {
3132
return nil, err
3233
}
3334

34-
s := string(bb)
35-
count, err := strconv.ParseInt(s, 10, 64)
35+
count, err := getCount(bb)
3636
if err != nil {
37+
pterm.Error.Printf("Got error reading jwt count from cache; %v", err)
3738
return nil, err
3839
}
3940

@@ -47,6 +48,23 @@ func (c *cache) getJwtCount() int64 {
4748
return c.count
4849
}
4950

51+
func getCount(bb []byte) (int64, error) {
52+
num := make([]byte, 0)
53+
for _, m := range bb {
54+
if m == 0 {
55+
break
56+
}
57+
num = append(num, m)
58+
}
59+
60+
s := string(num)
61+
i, err := strconv.ParseInt(s, 10, 64)
62+
if err != nil {
63+
return 0, err
64+
}
65+
return i, nil
66+
}
67+
5068
func (c *cache) get(count int64) (<-chan string, <-chan error) {
5169
recv := make(chan string, 1000000)
5270
errs := make(chan error, 1)
@@ -78,16 +96,7 @@ func (c *cache) get(count int64) (<-chan string, <-chan error) {
7896
return recv, errs
7997
}
8098

81-
num := make([]byte, 0)
82-
for _, m := range meta {
83-
if m == 0 {
84-
break
85-
}
86-
num = append(num, m)
87-
}
88-
89-
s := string(num)
90-
i, err := strconv.ParseInt(s, 10, 64)
99+
i, err := getCount(meta)
91100
if err != nil {
92101
errs <- fmt.Errorf("failed to get jwt count; %v", err)
93102
close(errs)

pkgs/jwt-generator/jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (j *JWTGenerator) Generate(reqJwtCount int64, dir string, retrying bool) er
141141
return err
142142
}
143143
f.Close()
144-
pterm.Debug.Printf("jwt cache %s file corrupt, attempting to delete and recreate; got error; %v \n", fname, err)
144+
pterm.Error.Printf("jwt cache %s file corrupt, attempting to delete and recreate; got error; %v \n", fname, err)
145145
if err := os.Remove(fname); err != nil {
146146
pterm.Error.Printf("Couldn't remove cache file %s; %v", fname, err)
147147
return err

version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package version
22

3-
const Version = "0.3.2"
3+
const Version = "0.3.3"

0 commit comments

Comments
 (0)