-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
256 lines (213 loc) · 6.29 KB
/
Copy pathmain.go
File metadata and controls
256 lines (213 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"flag"
"gopkg.in/yaml.v3"
)
var appVersion = "0.0.0"
var appBuild = "UNK"
var appBuildDate = "000000.000000"
type Config struct {
DownloadDir string `yaml:"download_directory"`
WorkerLimit int `yaml:"worker_limit"`
RunIntervalSeconds int `yaml:"run_interval_seconds"`
Files []File `yaml:"files"`
}
type File struct {
URL string `yaml:"url"`
Filename string `yaml:"filename"`
}
// loadConfig reads and parses the YAML file
func loadConfig(path string) (Config, error) {
var config Config
yamlFile, err := os.ReadFile(path)
if err != nil {
return config, err
}
err = yaml.Unmarshal(yamlFile, &config)
return config, err
}
func main() {
// Define and parse command-line flags
versionFlag := flag.Bool("V", false, "Print application version and exit")
flag.Parse()
// If the -V flag was provided, print the version and exit immediately
if *versionFlag {
fmt.Printf("gfetch %s (%s)\n", appVersion, appBuild)
return
}
// Define the locations to check, in order of priority
configPaths := []string{
"gfetch.yaml",
"/etc/gfetch.yaml",
}
var config Config
var loadedPath string
var err error
// Print version and build info at startup
printVersion()
// 1. Search for and load the config file
for _, path := range configPaths {
config, err = loadConfig(path)
if err == nil {
loadedPath = path
fmt.Printf("Loaded configuration from: %s\n", loadedPath)
break
}
}
if loadedPath == "" {
log.Fatalf("Failed to load configuration. Searched in: %v", configPaths)
}
// 2. Run the download process immediately
executeDownloads(config)
// 3. Check if we should loop or exit
if config.RunIntervalSeconds <= 0 {
fmt.Println("No run_interval_seconds configured (or set to 0). Running once and exiting.")
return
}
// 4. Set up the Ticker for the loop
ticker := time.NewTicker(time.Duration(config.RunIntervalSeconds) * time.Second)
defer ticker.Stop()
fmt.Printf("Started polling every %d seconds. Press Ctrl+C to stop.\n", config.RunIntervalSeconds)
// 5. The continuous loop
for range ticker.C {
// Hot-Reload: Re-read from the path we successfully found earlier
newConfig, err := loadConfig(loadedPath)
if err != nil {
log.Printf("Failed to reload config from %s (skipping this cycle): %v\n", loadedPath, err)
continue
}
config = newConfig
executeDownloads(config)
}
}
// executeDownloads sets up the worker pool and processes the file queue
func executeDownloads(config Config) {
if err := os.MkdirAll(config.DownloadDir, os.ModePerm); err != nil {
log.Printf("Error creating download directory: %v\n", err)
return
}
numWorkers := config.WorkerLimit
if numWorkers <= 0 {
numWorkers = 3
}
jobs := make(chan File, len(config.Files))
var wg sync.WaitGroup
for w := 1; w <= numWorkers; w++ {
wg.Add(1)
go worker(w, jobs, config.DownloadDir, &wg)
}
for _, file := range config.Files {
jobs <- file
}
close(jobs)
wg.Wait()
fmt.Printf("[%s] Download cycle completed.\n", time.Now().Format("15:04:05"))
fmt.Println(strings.Repeat("-", 40))
}
// worker constantly pulls from the jobs channel until it is closed and empty
func worker(id int, jobs <-chan File, destDir string, wg *sync.WaitGroup) {
defer wg.Done()
for file := range jobs {
destPath := filepath.Join(destDir, file.Filename)
downloadAndVerifyFile(file.URL, destPath, destDir)
}
}
// downloadAndVerifyFile handles fetching, hashing, and replacing the file if necessary
func downloadAndVerifyFile(url, destPath, destDir string) {
tempFile, err := os.CreateTemp(destDir, "temp-dl-*")
if err != nil {
log.Printf("Failed to create temp file for %s: %v\n", url, err)
return
}
tempName := tempFile.Name()
defer os.Remove(tempName)
resp, err := http.Get(url)
if err != nil {
log.Printf("Failed to download %s: %v\n", url, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Bad status: %s for URL: %s\n", resp.Status, url)
return
}
if _, err = io.Copy(tempFile, resp.Body); err != nil {
log.Printf("Failed to write data to temp file for %s: %v\n", url, err)
return
}
tempFile.Close() // Close before we read it for hashing/validation
// CRL Validation
if strings.ToLower(filepath.Ext(destPath)) == ".crl" {
if err := validateCRL(tempName); err != nil {
log.Printf("Validation failed for %s (Not a valid CRL): %v\n", destPath, err)
return // Exit early, temp file is deleted via defer, existing file is safe
}
}
newHash, err := hashFile(tempName)
if err != nil {
log.Printf("Failed to hash temp file for %s: %v\n", url, err)
return
}
if _, err := os.Stat(destPath); err == nil {
existingHash, err := hashFile(destPath)
if err == nil && newHash == existingHash {
return // Silently skip identical files
}
}
if err := os.Rename(tempName, destPath); err != nil {
log.Printf("Failed to move temp file to %s: %v\n", destPath, err)
return
}
fmt.Printf("Successfully updated/downloaded: %s\n", destPath)
}
// validateCRL checks if the downloaded file is a valid Certificate Revocation List
func validateCRL(filePath string) error {
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
// 1. Check if the file is PEM encoded. If so, extract the DER binary block.
block, _ := pem.Decode(data)
if block != nil {
// Replace our data variable with the decoded binary payload
data = block.Bytes
}
// 2. Parse the DER data to verify it is structurally a valid CRL
_, err = x509.ParseRevocationList(data)
if err != nil {
return fmt.Errorf("failed to parse CRL: %w", err)
}
return nil
}
// hashFile generates a SHA-256 hash string for a given file
func hashFile(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func printVersion() {
fmt.Printf("GopherFetch - The Gopher-powered Concurrent File Retrieval Tool\n")
fmt.Printf(" - Version: %s\n", appVersion)
fmt.Printf(" - Build: %s\n", appBuildDate)
fmt.Printf(" - Architecture: %s\n", appBuild)
}