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.

26
main.go
View File

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