|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "text/tabwriter" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/GoToolSharing/htb-cli/utils" |
| 12 | + "github.com/kyokomi/emoji/v2" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var machineParam []string |
| 17 | + |
| 18 | +var infoCmd = &cobra.Command{ |
| 19 | + Use: "info", |
| 20 | + Short: "Showcase detailed machine information", |
| 21 | + Long: "Displays detailed information of the specified machines in a structured table.", |
| 22 | + Run: func(cmd *cobra.Command, args []string) { |
| 23 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.Debug) |
| 24 | + fmt.Fprintln(w, "Name\tOS\tActive\tDifficulty\tStars\tFirstUserBlood\tFirstRootBlood\tStatus\tRelease") |
| 25 | + status := "Not defined" |
| 26 | + log.Println(machineParam) |
| 27 | + for index, _ := range machineParam { |
| 28 | + machine_id := utils.SearchMachineIDByName(machineParam[index], proxyParam) |
| 29 | + |
| 30 | + url := "https://www.hackthebox.com/api/v4/machine/profile/" + machine_id |
| 31 | + resp, err := utils.HtbRequest(http.MethodGet, url, proxyParam, nil) |
| 32 | + if err != nil { |
| 33 | + log.Fatal(err) |
| 34 | + } |
| 35 | + info := utils.ParseJsonMessage(resp, "info") |
| 36 | + |
| 37 | + data := info.(map[string]interface{}) |
| 38 | + if data["authUserInUserOwns"] == nil && data["authUserInRootOwns"] == nil { |
| 39 | + status = emoji.Sprint(":x:User - :x:Root") |
| 40 | + } else if data["authUserInUserOwns"] == true && data["authUserInRootOwns"] == nil { |
| 41 | + status = emoji.Sprint(":white_check_mark:User - :x:Root") |
| 42 | + } else if data["authUserInUserOwns"] == nil && data["authUserInRootOwns"] == true { |
| 43 | + status = emoji.Sprint(":x:User - :white_check_mark:Root") |
| 44 | + } else if data["authUserInUserOwns"] == true && data["authUserInRootOwns"] == true { |
| 45 | + status = emoji.Sprint(":white_check_mark:User - :white_check_mark:Root") |
| 46 | + } |
| 47 | + t, err := time.Parse(time.RFC3339Nano, data["release"].(string)) |
| 48 | + if err != nil { |
| 49 | + fmt.Println("Erreur when date parsing :", err) |
| 50 | + return |
| 51 | + } |
| 52 | + datetime := t.Format("2006-01-02") |
| 53 | + fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", data["name"], data["os"], data["active"], data["difficultyText"], data["stars"], data["firstUserBloodTime"], data["firstRootBloodTime"], status, datetime) |
| 54 | + } |
| 55 | + w.Flush() |
| 56 | + |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +func init() { |
| 61 | + rootCmd.AddCommand(infoCmd) |
| 62 | + infoCmd.Flags().StringSliceVarP(&machineParam, "machine", "m", []string{}, "Machine name") |
| 63 | + infoCmd.MarkFlagRequired("machine") |
| 64 | +} |
0 commit comments