-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (94 loc) · 3.02 KB
/
main.go
File metadata and controls
108 lines (94 loc) · 3.02 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
// main.go
// Purpose: a small dev helper CLI that appends a temporary mapping to /etc/hosts
// so that a test domain resolves to a local IP during development.
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
)
const (
hostsPath = "/etc/hosts"
devDomain = "api.dev.local"
devIP = "127.0.0.1"
beginMarker = "# BEGIN my-dev-tool"
endMarker = "# END my-dev-tool"
)
func main() {
// Read current /etc/hosts content. Requires read permission on /etc.
orig, err := os.ReadFile(hostsPath)
if err != nil {
fatal("reading /etc/hosts: %v", err)
}
// Create a timestamped backup next to the original file under /etc.
// Rationale: any accidental corruption can be rolled back quickly.
bak := fmt.Sprintf("%s.bak-%s", hostsPath, time.Now().Format("20060102-150405"))
if err := os.WriteFile(bak, orig, 0644); err != nil {
fatal("creating backup: %v", err)
}
fmt.Println("Backup created at:", bak)
// Prepare new content with our dev block added if not already present.
newContent := ensureDevBlock(orig)
// Write atomically: write to a temp file in /etc and then rename over the target.
// This reduces the chance of partial writes and keeps file permissions stable.
tmpFile, err := os.CreateTemp(filepath.Dir(hostsPath), "hosts.tmp.*")
if err != nil {
fatal("creating temp file: %v", err)
}
tmpName := tmpFile.Name()
defer os.Remove(tmpName)
defer tmpFile.Close()
// Preserve original file mode when replacing the file.
if st, err := os.Stat(hostsPath); err == nil {
_ = os.Chmod(tmpName, st.Mode())
}
// Copy new content into the temp file and flush it.
if _, err := io.Copy(tmpFile, bytes.NewReader(newContent)); err != nil {
fatal("writing temp file: %v", err)
}
if err := tmpFile.Sync(); err != nil {
fatal("sync temp file: %v", err)
}
// Atomic replace: this is where we write below /etc by renaming the temp file to /etc/hosts.
// This operation will trigger Falco's "Write below etc" rule in runtime environments.
if err := os.Rename(tmpName, hostsPath); err != nil {
fatal("renaming to /etc/hosts: %v", err)
}
fmt.Println("Development entry added to /etc/hosts for", devDomain, "->", devIP)
}
// ensureDevBlock appends a clearly delimited block to /etc/hosts only if not present.
// This avoids duplicate lines across multiple executions.
func ensureDevBlock(orig []byte) []byte {
lines := strings.Split(string(orig), "\n")
inBlock := false
for _, l := range lines {
if strings.TrimSpace(l) == beginMarker {
inBlock = true
break
}
}
if inBlock {
fmt.Println("my-dev-tool block already present. No changes made.")
return orig
}
var b bytes.Buffer
b.Write(orig)
if len(orig) > 0 && orig[len(orig)-1] != '\n' {
b.WriteByte('\n')
}
b.WriteString(beginMarker)
b.WriteByte('\n')
b.WriteString(fmt.Sprintf("%s\t%s\n", devIP, devDomain))
b.WriteString(endMarker)
b.WriteByte('\n')
return b.Bytes()
}
// fatal prints an error message and exits non zero.
func fatal(msg string, a ...any) {
fmt.Fprintf(os.Stderr, "Error: "+msg+"\n", a...)
os.Exit(1)
}