2022-11-25 14:43:14 +00:00
|
|
|
package checkers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-11-25 15:26:59 +00:00
|
|
|
logger "github.com/CompeyDev/wsl-archlinux-manager/util"
|
2022-11-25 14:43:14 +00:00
|
|
|
"github.com/briandowns/spinner"
|
|
|
|
"github.com/cavaliergopher/grab/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func VerifySignature(mirrorUrl string) {
|
|
|
|
bar := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
|
|
|
bar.Prefix = " "
|
|
|
|
bar.Suffix = " Verifying signature of RootFS..."
|
|
|
|
bar.Start()
|
|
|
|
|
2022-11-26 13:21:09 +00:00
|
|
|
success, version, _ := pullSig(mirrorUrl)
|
2022-11-25 14:43:14 +00:00
|
|
|
|
|
|
|
if !success {
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Error("Failed to download signature of RootFS. Refusing to continue.")
|
2022-11-25 14:43:14 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
userHomeDir, homeDirErr := os.UserHomeDir()
|
|
|
|
|
|
|
|
if homeDirErr != nil {
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Error("Failed to fetch installation directory, cannot verify authenticity of RootFS.")
|
2022-11-25 14:43:14 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cwd, dirErr := os.Getwd()
|
|
|
|
|
|
|
|
if dirErr != nil {
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Error("Failed to fetch installation directory, cannot verify authenticity of RootFS.")
|
2022-11-25 14:43:14 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
unixWd := fmt.Sprintf("/mnt/c/%s", strings.ReplaceAll(strings.Split(userHomeDir, `C:\`)[1], `\`, "/")) + strings.ReplaceAll(strings.Split(cwd, userHomeDir)[1], `\`, "/")
|
2022-11-26 11:51:52 +00:00
|
|
|
logger.Info(fmt.Sprintf("Looking for verification signature in Unix Directory %s", unixWd))
|
2022-11-26 13:21:09 +00:00
|
|
|
cmd := exec.Command("wsl.exe", `bash`, `-c`, fmt.Sprintf(`gpg --keyserver-options auto-key-retrieve --verify archlinux-bootstrap-%s-x86_64.tar.gz.sig`, version))
|
2022-11-26 11:51:52 +00:00
|
|
|
getAuthenticity, authenticityErr := cmd.CombinedOutput()
|
2022-11-25 14:43:14 +00:00
|
|
|
if authenticityErr != nil {
|
2022-11-26 11:51:52 +00:00
|
|
|
bar.Stop()
|
|
|
|
logger.Error("Failed to verify authenticity of RootFS. Refusing to continue.")
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.Trim(string(getAuthenticity), "\n\r"), "Good signature") {
|
|
|
|
logger.Info("Matching signature: 4AA4 767B BC9C 4B1D 18AE 28B7 7F2D 434B 9741 E8AC")
|
|
|
|
logger.Info("Successfully matched checksums and verified authenticity!")
|
|
|
|
bar.Stop()
|
|
|
|
} else {
|
|
|
|
bar.Stop()
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Error("Failed to verify authenticity of RootFS. Refusing to continue.")
|
2022-11-25 14:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:21:09 +00:00
|
|
|
func pullSig(url string) (isSuccessful bool, ver string, error error) {
|
2022-11-25 14:43:14 +00:00
|
|
|
bar := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
|
|
|
bar.Prefix = " "
|
|
|
|
bar.Suffix = " Downloading Signature..."
|
|
|
|
|
|
|
|
var version = strings.Split(strings.Split(url, "iso/")[1], "/")[0]
|
|
|
|
var structuredUrl = fmt.Sprintf("%s/archlinux-bootstrap-%s-x86_64.tar.gz.sig", url, version)
|
|
|
|
res, err := grab.Get(".", structuredUrl)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Error("Failed to download Signature.")
|
2022-11-26 13:21:09 +00:00
|
|
|
return false, version, err
|
2022-11-25 14:43:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 15:26:59 +00:00
|
|
|
logger.Info(fmt.Sprintf("Downloaded Signature %s", res.Filename))
|
2022-11-25 14:43:14 +00:00
|
|
|
|
2022-11-26 13:21:09 +00:00
|
|
|
return true, version, nil
|
2022-11-25 14:43:14 +00:00
|
|
|
}
|