basics might work

This commit is contained in:
Colin 2024-05-21 11:48:38 -04:00
commit 95646d8b31
19 changed files with 175 additions and 0 deletions

70
build.sh Executable file
View File

@ -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."

View File

View File

View File

View File

View File

View File

View File

View File

BIN
dist/ssh-timeout vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_darwin_amd64 vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_darwin_arm64 vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_linux_arm vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_linux_arm64 vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_linux_arm64_static vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_linux_arm_static vendored Executable file

Binary file not shown.

BIN
dist/ssh-timeout_static vendored Executable file

Binary file not shown.

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module yourmodule
go 1.21.1

102
main.go Normal file
View File

@ -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] <URL>")
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)
}
}