|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "crypto/hmac" |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/hex" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + callbackURL = "http://localhost:47731/backup" |
| 20 | + signatureHeader = "X-Signature" |
| 21 | +) |
| 22 | + |
| 23 | +var configFilePath = "../../backup.conf" |
| 24 | + |
| 25 | +// RequestBody represents the JSON structure for the request |
| 26 | +type RequestBody struct { |
| 27 | + Args []string `json:"args"` |
| 28 | +} |
| 29 | + |
| 30 | +// readConfig reads the configuration file and extracts the callback secret. |
| 31 | +func readConfig() (string, error) { |
| 32 | + content, err := os.ReadFile(configFilePath) |
| 33 | + if err != nil { |
| 34 | + return "", fmt.Errorf("failed to read config file: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + lines := strings.Split(string(content), "\n") |
| 38 | + for _, line := range lines { |
| 39 | + // Skip comments and empty lines |
| 40 | + trimmedLine := strings.TrimSpace(line) |
| 41 | + if trimmedLine == "" || strings.HasPrefix(trimmedLine, "#") { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + // Check if the line contains the callback_secret |
| 46 | + if strings.HasPrefix(trimmedLine, "callback_secret") { |
| 47 | + parts := strings.SplitN(trimmedLine, "=", 2) |
| 48 | + if len(parts) == 2 { |
| 49 | + // Remove quotes if present |
| 50 | + secret := strings.TrimSpace(parts[1]) |
| 51 | + secret = strings.Trim(secret, "\"'") |
| 52 | + return secret, nil |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return "", fmt.Errorf("callback_secret not found in config file") |
| 57 | +} |
| 58 | + |
| 59 | +// generateSignature generates an HMAC-SHA256 signature for the given body using the provided secret. |
| 60 | +func generateSignature(body []byte, secret string) string { |
| 61 | + h := hmac.New(sha256.New, []byte(secret)) |
| 62 | + h.Write(body) |
| 63 | + signature := h.Sum(nil) |
| 64 | + return "sha256=" + hex.EncodeToString(signature) |
| 65 | +} |
| 66 | + |
| 67 | +func main() { |
| 68 | + if len(os.Args) < 2 { |
| 69 | + fmt.Println("Usage: client_example.go <arg1> [arg2] ...") |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + // Read the callback secret from the config file |
| 74 | + secret, err := readConfig() |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Error reading config: %v\n", err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + // Get command line arguments (excluding program name) |
| 81 | + args := os.Args[1:] |
| 82 | + |
| 83 | + // Create the request body |
| 84 | + requestBody := RequestBody{ |
| 85 | + Args: args, |
| 86 | + } |
| 87 | + |
| 88 | + // Marshal the request body to JSON |
| 89 | + jsonBody, err := json.Marshal(requestBody) |
| 90 | + if err != nil { |
| 91 | + fmt.Printf("Error marshaling JSON: %v\n", err) |
| 92 | + os.Exit(1) |
| 93 | + } |
| 94 | + |
| 95 | + // Generate the signature |
| 96 | + signature := generateSignature(jsonBody, secret) |
| 97 | + |
| 98 | + // Create the HTTP request |
| 99 | + req, err := http.NewRequest("POST", callbackURL, bytes.NewBuffer(jsonBody)) |
| 100 | + if err != nil { |
| 101 | + fmt.Printf("Error creating request: %v\n", err) |
| 102 | + os.Exit(1) |
| 103 | + } |
| 104 | + |
| 105 | + // Set headers |
| 106 | + req.Header.Set("Content-Type", "application/json") |
| 107 | + req.Header.Set(signatureHeader, signature) |
| 108 | + |
| 109 | + // Send the request |
| 110 | + client := &http.Client{} |
| 111 | + resp, err := client.Do(req) |
| 112 | + if err != nil { |
| 113 | + fmt.Printf("Error sending request: %v\n", err) |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + defer resp.Body.Close() |
| 117 | + |
| 118 | + // Read and print the response |
| 119 | + respBody, err := io.ReadAll(resp.Body) |
| 120 | + if err != nil { |
| 121 | + fmt.Printf("Error reading response: %v\n", err) |
| 122 | + os.Exit(1) |
| 123 | + } |
| 124 | + |
| 125 | + fmt.Printf("Response Status: %s\n", resp.Status) |
| 126 | + fmt.Printf("Response Body: %s\n", string(respBody)) |
| 127 | +} |
0 commit comments