This commit is contained in:
Colin 2024-05-21 12:35:28 -04:00
parent 26522adf6e
commit da62d3ed44
9 changed files with 13 additions and 13 deletions

BIN
dist/ttfb-go vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
dist/ttfb-go_linux_arm vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
dist/ttfb-go_static vendored

Binary file not shown.

22
main.go
View File

@ -12,7 +12,7 @@ import (
)
// MeasureTTFB measures the Time to First Byte for the given URL.
func MeasureTTFB(url string, cookie string) (int64, int, error) {
func MeasureTTFB(url string, cookies []string) (int64, int, error) {
// Create a custom HTTP transport to allow measuring the TTFB.
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@ -28,14 +28,14 @@ func MeasureTTFB(url string, cookie string) (int64, int, error) {
return 0, 0, err
}
// If a cookie is provided, set it in the request header.
if cookie != "" {
// Clean up and format the cookie properly
cookie = strings.TrimSpace(cookie)
// If cookies are provided, set them in the request header.
if len(cookies) > 0 {
for _, cookie := range cookies {
if !strings.Contains(cookie, "=") {
return 0, 0, fmt.Errorf("invalid cookie format")
}
req.Header.Set("Cookie", cookie)
req.Header.Add("Cookie", strings.TrimSpace(cookie))
}
}
// Record the start time.
@ -56,7 +56,7 @@ func MeasureTTFB(url string, cookie string) (int64, int, error) {
func main() {
// Define and parse command-line flags.
cookieFile := flag.String("c", "", "Path to file containing the authentication cookie")
cookieFile := flag.String("c", "", "Path to file containing the authentication cookies")
flag.Parse()
// Check if the URL is provided as an argument.
@ -67,19 +67,19 @@ func main() {
url := flag.Arg(0)
// Read the cookie from the specified file if provided.
var cookie string
// Read the cookies from the specified file if provided.
var cookies []string
if *cookieFile != "" {
data, err := ioutil.ReadFile(*cookieFile)
if err != nil {
fmt.Printf("Error reading cookie file: %v\n", err)
os.Exit(1)
}
cookie = strings.TrimSpace(string(data))
cookies = strings.Split(strings.TrimSpace(string(data)), ";")
}
// Measure the TTFB.
ttfb, statusCode, err := MeasureTTFB(url, cookie)
ttfb, statusCode, err := MeasureTTFB(url, cookies)
if err != nil {
fmt.Printf("Error measuring TTFB: %v\n", err)
os.Exit(1)