commit 95646d8b31049d08c4aa09801750d40d3b090e89 Author: Colin Date: Tue May 21 11:48:38 2024 -0400 basics might work diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8026aa7 --- /dev/null +++ b/build.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Default architecture +DEFAULT_ARCH="linux/amd64" + +# Supported architectures (adjust as needed) +ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7" "darwin/amd64" "darwin/arm64") + +# Ensure all necessary directories exist and go modules are ready +prepare_build() { + # Create necessary directories if they don't exist + mkdir -p dist + mkdir -p build_logs + + # Initialize go modules if go.mod does not exist + if [ ! -f go.mod ]; then + echo "Initializing Go modules" + go mod init yourmodule # Replace 'yourmodule' with your actual module name or path + fi + + # Fetch and ensure all dependencies are up to date + echo "Checking dependencies..." + go mod tidy +} + +# Build function +build_binary() { + os=$1 + arch=$2 + output_name="ssh-timeout" + + if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then + output_name="${output_name}_${os}_${arch}" + fi + + output_name="dist/${output_name}" + + # Dynamic Linking + echo "Building dynamically linked for ${os}/${arch} -> ${output_name}" + GOOS=${os} GOARCH=${arch} go build -o ${output_name} main.go 2>build_logs/${os}_${arch}_build.log + if [ $? -eq 0 ]; then + echo "Successfully built ${output_name}" + else + echo "Failed to build ${output_name}. Check build_logs/${os}_${arch}_build.log for errors." + fi + + # Static Linking + if [[ "$os" == "linux" ]]; then # Typically, static linking is most relevant for Linux environments + static_output_name="${output_name}_static" + echo "Building statically linked for ${os}/${arch} -> ${static_output_name}" + CGO_ENABLED=0 GOOS=${os} GOARCH=${arch} go build -a -ldflags '-extldflags "-static"' -o ${static_output_name} main.go 2>build_logs/${os}_${arch}_static_build.log + if [ $? -eq 0 ]; then + echo "Successfully built ${static_output_name}" + else + echo "Failed to build ${static_output_name}. Check build_logs/${os}_${arch}_static_build.log for errors." + fi + fi +} + +# Main Build Process +prepare_build +for arch in "${ARCHITECTURES[@]}"; do + IFS='/' read -r -a parts <<< "$arch" # Split architecture string + os=${parts[0]} + arch=${parts[1]} + build_binary $os $arch +done + +echo "Build process completed." + diff --git a/build_logs/darwin_amd64_build.log b/build_logs/darwin_amd64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/darwin_arm64_build.log b/build_logs/darwin_arm64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_amd64_build.log b/build_logs/linux_amd64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_amd64_static_build.log b/build_logs/linux_amd64_static_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm64_build.log b/build_logs/linux_arm64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm64_static_build.log b/build_logs/linux_arm64_static_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm_build.log b/build_logs/linux_arm_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm_static_build.log b/build_logs/linux_arm_static_build.log new file mode 100644 index 0000000..e69de29 diff --git a/dist/ssh-timeout b/dist/ssh-timeout new file mode 100755 index 0000000..2ab9c34 Binary files /dev/null and b/dist/ssh-timeout differ diff --git a/dist/ssh-timeout_darwin_amd64 b/dist/ssh-timeout_darwin_amd64 new file mode 100755 index 0000000..98f7ee6 Binary files /dev/null and b/dist/ssh-timeout_darwin_amd64 differ diff --git a/dist/ssh-timeout_darwin_arm64 b/dist/ssh-timeout_darwin_arm64 new file mode 100755 index 0000000..aa90134 Binary files /dev/null and b/dist/ssh-timeout_darwin_arm64 differ diff --git a/dist/ssh-timeout_linux_arm b/dist/ssh-timeout_linux_arm new file mode 100755 index 0000000..aceb603 Binary files /dev/null and b/dist/ssh-timeout_linux_arm differ diff --git a/dist/ssh-timeout_linux_arm64 b/dist/ssh-timeout_linux_arm64 new file mode 100755 index 0000000..1947080 Binary files /dev/null and b/dist/ssh-timeout_linux_arm64 differ diff --git a/dist/ssh-timeout_linux_arm64_static b/dist/ssh-timeout_linux_arm64_static new file mode 100755 index 0000000..37c48c6 Binary files /dev/null and b/dist/ssh-timeout_linux_arm64_static differ diff --git a/dist/ssh-timeout_linux_arm_static b/dist/ssh-timeout_linux_arm_static new file mode 100755 index 0000000..2cc85d7 Binary files /dev/null and b/dist/ssh-timeout_linux_arm_static differ diff --git a/dist/ssh-timeout_static b/dist/ssh-timeout_static new file mode 100755 index 0000000..360dc11 Binary files /dev/null and b/dist/ssh-timeout_static differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e9a94d3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module yourmodule + +go 1.21.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..dc2819e --- /dev/null +++ b/main.go @@ -0,0 +1,102 @@ +package main + +import ( + "crypto/tls" + "flag" + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" + "time" +) + +// MeasureTTFB measures the Time to First Byte for the given URL. +func MeasureTTFB(url string, cookie string, verbose bool) (int64, error) { + // Create a custom HTTP transport to allow measuring the TTFB. + transport := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + + client := &http.Client{ + Transport: transport, + } + + // Create a new request. + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return 0, err + } + + // If a cookie is provided, set it in the request header. + if cookie != "" { + req.Header.Set("Cookie", cookie) + if verbose { + fmt.Println("Using cookie:", cookie) + } + } + + if verbose { + fmt.Println("Sending request to:", url) + } + + // Record the start time. + start := time.Now() + + // Perform the request. + resp, err := client.Do(req) + if err != nil { + return 0, err + } + defer resp.Body.Close() + + // Measure the time taken to receive the first byte. + ttfb := time.Since(start).Milliseconds() + + if verbose { + fmt.Println("Received response status:", resp.Status) + } + + return ttfb, nil +} + +func main() { + // Define and parse command-line flags. + verbose := flag.Bool("v", false, "Enable verbose output") + cookieFile := flag.String("c", "", "Path to file containing the authentication cookie") + flag.Parse() + + // Check if the URL is provided as an argument. + if flag.NArg() != 1 { + fmt.Println("Usage: go run main.go [-v] [-c cookieFile] ") + os.Exit(1) + } + + url := flag.Arg(0) + + // Read the cookie from the specified file if provided. + var cookie 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)) + } + + // Measure the TTFB. + ttfb, err := MeasureTTFB(url, cookie, *verbose) + if err != nil { + fmt.Printf("Error measuring TTFB: %v\n", err) + os.Exit(1) + } + + // Only print the TTFB in milliseconds by default. + if *verbose { + fmt.Printf("Time to First Byte for %s: %d ms\n", url, ttfb) + } else { + fmt.Printf("%d\n", ttfb) + } +} +