shadow-up: Add logging

This commit is contained in:
nemunaire 2022-02-26 12:30:50 +01:00
parent d896911077
commit 0e3bdb4a6b

View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
@ -17,8 +18,8 @@ import (
)
const (
DestHost = "172.23.255.1"
URLShadow = "http://172.23.255.1/passwd"
DestHost = "172.23.255.2"
URLShadow = "https://172.23.255.2/passwd"
PathToWatch = "/etc/shadow"
WatchedNotify = fsnotify.Create
)
@ -28,8 +29,14 @@ var (
)
func sendFile(field, path string) error {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
client := &http.Client{
Timeout: 20 * time.Second,
Timeout: 20 * time.Second,
Transport: tr,
}
body := &bytes.Buffer{}
@ -85,10 +92,14 @@ func main() {
hasModprobe := false
if err := exec.Command("/bin/sh", "-c", "lsmod | grep -q e1000").Run(); err == nil {
log.Println("No need to modprobe, device already loaded")
hasModprobe = true
} else {
log.Println("modprobe some devices")
// modprobe
for _, drv := range NetDrivers {
log.Println("/sbin/modprobe", drv)
err = exec.Command("/sbin/modprobe", drv).Run()
if err != nil {
log.Println("Unable to modprobe", drv, err.Error())
@ -96,16 +107,14 @@ func main() {
}
}
// Search IP
myip, err := os.ReadFile("/root/my_ip")
if err != nil {
log.Println("Unable to read /root/my_ip:", err.Error())
return
// Wait for the eth device
for n := 0; n < 8 && exec.Command("/sbin/ip", "link", "show", "dev", "eth0").Run() != nil; n++ {
log.Println("eth0 not present, waiting for it...")
time.Sleep(250 * time.Millisecond)
}
hasIP := false
if err := exec.Command("/bin/ping", "-c", "1", "-W", "2", DestHost).Run(); err == nil {
hasIP = true
log.Println("The remote host ping, no need to set it up.")
} else {
// ip link set eth0 up
err = exec.Command("/sbin/ip", "link", "set", "eth0", "up").Run()
@ -113,10 +122,10 @@ func main() {
log.Println("Unable to ip link set eth0 up:", err.Error())
}
// ip a add my_ip/17 dev eth0
err = exec.Command("/sbin/ip", "a", "add", string(myip)+"/17", "dev", "eth0").Run()
// ip link set eth0 up
err = exec.Command("/sbin/udhcpc", "-i", "eth0").Run()
if err != nil {
log.Println("Unable to ip a add ... dev eth0:", err.Error())
log.Println("Unable to udhcpc eth0:", err.Error())
}
}
@ -124,9 +133,11 @@ func main() {
err = sendFile("shadow", PathToWatch)
if err != nil {
log.Println("Unable to send file:", err.Error())
continue
}
if !hasModprobe {
log.Println("Unload driver")
// Remove driver
for _, drv := range NetDrivers {
err = exec.Command("/sbin/modprobe", "-r", drv).Run()
@ -134,12 +145,9 @@ func main() {
log.Println("Unable to remove modprobe", drv, err.Error())
}
}
} else if !hasIP {
// Remove IP
exec.Command("/sbin/ip", "a", "del", string(myip)+"/17", "dev", "eth0").Run()
exec.Command("/sbin/ip", "link", "set", "eth0", "down").Run()
}
log.Println("Done, see you later")
} else {
log.Println("Skipped event:", ev, "for", ev.Name)
}