|
| 1 | +package keycard_link |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "io" |
| 9 | + "runtime" |
| 10 | + "os/exec" |
| 11 | + "strings" |
| 12 | + "net/http" |
| 13 | + "path/filepath" |
| 14 | +) |
| 15 | + |
| 16 | +// GLOBAL PLATFORM PRO VERSION V20.01.23 |
| 17 | + |
| 18 | +const gpJarURL = "https://github.com/martinpaljak/GlobalPlatformPro/releases/download/v20.01.23/gp.jar" |
| 19 | + |
| 20 | +func InstallKeycard() error { |
| 21 | + cmd := exec.Command("./keycard-linux-amd64", "install") |
| 22 | + output, err := cmd.CombinedOutput() |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("error executing keycard-linux-amd64 install: %v, output: %s", err, output) |
| 25 | + } |
| 26 | + fmt.Printf("Output: %s\n", output) |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// GetKeycardPublicKey retrieves the public key from the keycard. |
| 31 | +func GetKeycardPublicKey() (string, error) { |
| 32 | + // Command to execute |
| 33 | + fmt.Print("Scan your card now! :) \n") |
| 34 | + cmd := exec.Command("./keycard-linux-amd64", "info") |
| 35 | + |
| 36 | + // Capture the output of the command |
| 37 | + var out bytes.Buffer |
| 38 | + cmd.Stdout = &out |
| 39 | + |
| 40 | + // Run the command |
| 41 | + err := cmd.Run() |
| 42 | + if err != nil { |
| 43 | + return "", err |
| 44 | + } |
| 45 | + |
| 46 | + // Process the output to find the public key |
| 47 | + scanner := bufio.NewScanner(&out) |
| 48 | + for scanner.Scan() { |
| 49 | + line := scanner.Text() |
| 50 | + if strings.Contains(line, "PublicKey:") { |
| 51 | + // Assuming the public key is the last element in the line, separated by spaces |
| 52 | + parts := strings.Fields(line) |
| 53 | + if len(parts) > 1 { |
| 54 | + return parts[len(parts)-1], nil |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if err := scanner.Err(); err != nil { |
| 60 | + return "", err |
| 61 | + } |
| 62 | + |
| 63 | + return "", fmt.Errorf("public key not found in the output") |
| 64 | +} |
| 65 | + |
| 66 | +// ReadPassphrase prompts the user to enter a passphrase. |
| 67 | +func ReadPassphrase() (string, error) { |
| 68 | + fmt.Print("Enter a unique passphrase for this particular file: ") |
| 69 | + reader := bufio.NewReader(os.Stdin) |
| 70 | + passphrase, err := reader.ReadString('\n') |
| 71 | + if err != nil { |
| 72 | + return "", err |
| 73 | + } |
| 74 | + return strings.TrimSpace(passphrase), nil |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +func JavaDependency() { |
| 81 | + // Check if Java is installed |
| 82 | + if err := exec.Command("java", "-version").Run(); err == nil { |
| 83 | + fmt.Println("Java is already installed! :)") |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Determine the operating system |
| 88 | + switch os := runtime.GOOS; os { |
| 89 | + case "linux": |
| 90 | + // Identify Linux distribution |
| 91 | + out, err := exec.Command("sh", "-c", "cat /etc/os-release").Output() |
| 92 | + if err != nil { |
| 93 | + fmt.Printf("Failed to execute command: %s\n", err) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Parse /etc/os-release |
| 98 | + osRelease := string(out) |
| 99 | + if strings.Contains(osRelease, "ID=arch") || strings.Contains(osRelease, "ID_LIKE=arch") { |
| 100 | + // Install Java for Arch-based systems |
| 101 | + fmt.Println("Installing Java using pacman...") |
| 102 | + exec.Command("sudo", "pacman", "-Sy", "--noconfirm", "jdk-openjdk").Run() |
| 103 | + |
| 104 | + // Install pcscd and pcsc-tools using pacman |
| 105 | + fmt.Println("Installing pcscd and pcsc-tools using pacman...") |
| 106 | + exec.Command("sudo", "pacman", "-Syu", "--noconfirm", "ccid", "libnfc", "acsccid", "pcsclite", "pcsc-tools").Run() |
| 107 | + exec.Command("sudo", "systemctl", "enable", "--now", "pcscd").Run() |
| 108 | + } else if strings.Contains(osRelease, "ID=debian") || strings.Contains(osRelease, "ID=ubuntu") { |
| 109 | + // Install Java for Debian-based systems |
| 110 | + fmt.Println("Installing Java using apt...") |
| 111 | + exec.Command("sudo", "apt", "update").Run() |
| 112 | + exec.Command("sudo", "apt", "install", "-y", "default-jdk").Run() |
| 113 | + |
| 114 | + // Install pcscd and pcsc-tools using apt |
| 115 | + fmt.Println("Installing pcscd and pcsc-tools using apt...") |
| 116 | + exec.Command("sudo", "apt-get", "update").Run() |
| 117 | + exec.Command("sudo", "apt-get", "install", "-y", "pcsc-lite", "pcsc-tools").Run() |
| 118 | + exec.Command("sudo", "systemctl", "enable", "pcscd").Run() |
| 119 | + exec.Command("sudo", "systemctl", "start", "pcscd").Run() |
| 120 | + } else { |
| 121 | + fmt.Println("Unsupported Linux distribution") |
| 122 | + } |
| 123 | + default: |
| 124 | + fmt.Printf("%s is not supported by this script\n", os) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func GlobalPlatformDependency() { |
| 129 | + // Define paths to search |
| 130 | + paths := []string{"../", os.Getenv("HOME") + "/Downloads/"} |
| 131 | + |
| 132 | + found := false |
| 133 | + for _, path := range paths { |
| 134 | + found = searchFile(path, "gp.jar") |
| 135 | + if found { |
| 136 | + fmt.Println("gp.jar found in:", path) |
| 137 | + fmt.Println("Nice, GlobalPlatformDependency is good :) ") |
| 138 | + break |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // If not found, download the file |
| 143 | + if !found { |
| 144 | + fmt.Println("gp.jar not found :/ Downloading...") |
| 145 | + err := downloadFile("gp.jar", gpJarURL) |
| 146 | + if err != nil { |
| 147 | + fmt.Println("Error downloading gp.jar:", err) |
| 148 | + return |
| 149 | + } |
| 150 | + fmt.Println("Downloaded gp.jar successfully.") |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func searchFile(dir, filename string) bool { |
| 155 | + found := false |
| 156 | + filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + if info.Name() == filename { |
| 161 | + found = true |
| 162 | + } |
| 163 | + return nil |
| 164 | + }) |
| 165 | + return found |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +func downloadFile(filepath string, url string) error { |
| 173 | + // Get the data |
| 174 | + resp, err := http.Get(url) |
| 175 | + if err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + defer resp.Body.Close() |
| 179 | + |
| 180 | + // Create the file |
| 181 | + out, err := os.Create(filepath) |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + defer out.Close() |
| 186 | + |
| 187 | + buf := make([]byte, 1024) // buffer size |
| 188 | + for { |
| 189 | + n, err := resp.Body.Read(buf) |
| 190 | + if err != nil && err != io.EOF { |
| 191 | + return err |
| 192 | + } |
| 193 | + if n == 0 { |
| 194 | + break |
| 195 | + } |
| 196 | + |
| 197 | + if _, writeErr := out.Write(buf[:n]); writeErr != nil { |
| 198 | + return writeErr |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return nil |
| 203 | +} |
| 204 | + |
| 205 | + |
0 commit comments