Skip to content

Commit 800b5f5

Browse files
committed
Add information from active machine with info command
1 parent 504c3c4 commit 800b5f5

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

cmd/info.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"text/tabwriter"
99
"time"
1010

11+
"github.com/AlecAivazis/survey/v2"
1112
"github.com/GoToolSharing/htb-cli/utils"
1213
"github.com/kyokomi/emoji/v2"
1314
"github.com/spf13/cobra"
@@ -16,6 +17,47 @@ import (
1617
var machineParam []string
1718
var challengeParam []string
1819

20+
func checkActiveMachine() {
21+
machine_id := utils.GetActiveMachineID(proxyParam)
22+
status := "Not defined"
23+
retired_status := "Not defined"
24+
if machine_id != "" {
25+
log.Println("Active machine found !")
26+
log.Println("Machine ID:", machine_id)
27+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.Debug)
28+
fmt.Fprintln(w, "Name\tOS\tActive\tDifficulty\tStars\tIP\tStatus\tRelease")
29+
url := "https://www.hackthebox.com/api/v4/machine/profile/" + machine_id
30+
resp, err := utils.HtbRequest(http.MethodGet, url, proxyParam, nil)
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
info := utils.ParseJsonMessage(resp, "info")
35+
data := info.(map[string]interface{})
36+
if data["authUserInUserOwns"] == nil && data["authUserInRootOwns"] == nil {
37+
status = emoji.Sprint(":x:User - :x:Root")
38+
} else if data["authUserInUserOwns"] == true && data["authUserInRootOwns"] == nil {
39+
status = emoji.Sprint(":white_check_mark:User - :x:Root")
40+
} else if data["authUserInUserOwns"] == nil && data["authUserInRootOwns"] == true {
41+
status = emoji.Sprint(":x:User - :white_check_mark:Root")
42+
} else if data["authUserInUserOwns"] == true && data["authUserInRootOwns"] == true {
43+
status = emoji.Sprint(":white_check_mark:User - :white_check_mark:Root")
44+
}
45+
if data["retired"].(float64) == 0 {
46+
retired_status = emoji.Sprint(":white_check_mark:")
47+
} else {
48+
retired_status = emoji.Sprint(":x:")
49+
}
50+
t, err := time.Parse(time.RFC3339Nano, data["release"].(string))
51+
if err != nil {
52+
fmt.Println("Erreur when date parsing :", err)
53+
return
54+
}
55+
datetime := t.Format("2006-01-02")
56+
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", data["name"], data["os"], retired_status, data["difficultyText"], data["stars"], data["ip"], status, datetime)
57+
w.Flush()
58+
}
59+
}
60+
1961
var infoCmd = &cobra.Command{
2062
Use: "info",
2163
Short: "Showcase detailed machine information",
@@ -26,12 +68,24 @@ var infoCmd = &cobra.Command{
2668
cmd.Help()
2769
os.Exit(1)
2870
}
71+
var confirmation bool
72+
confirmation_message := "Do you want to check for active machine ?"
73+
prompt := &survey.Confirm{
74+
Message: confirmation_message,
75+
}
76+
if err := survey.AskOne(prompt, &confirmation); err != nil {
77+
log.Fatal(err)
78+
}
79+
if confirmation {
80+
checkActiveMachine()
81+
}
2982

3083
// Machines search
3184
if len(machineParam) > 0 {
3285
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.Debug)
3386
fmt.Fprintln(w, "Name\tOS\tActive\tDifficulty\tStars\tFirstUserBlood\tFirstRootBlood\tStatus\tRelease")
3487
status := "Not defined"
88+
retired_status := "Not defined"
3589
log.Println(machineParam)
3690
for index, _ := range machineParam {
3791
machine_id := utils.SearchItemIDByName(machineParam[index], proxyParam, "Machine")
@@ -53,15 +107,20 @@ var infoCmd = &cobra.Command{
53107
} else if data["authUserInUserOwns"] == true && data["authUserInRootOwns"] == true {
54108
status = emoji.Sprint(":white_check_mark:User - :white_check_mark:Root")
55109
}
110+
if data["retired"].(float64) == 0 {
111+
retired_status = emoji.Sprint(":white_check_mark:")
112+
} else {
113+
retired_status = emoji.Sprint(":x:")
114+
}
56115
t, err := time.Parse(time.RFC3339Nano, data["release"].(string))
57116
if err != nil {
58117
fmt.Println("Erreur when date parsing :", err)
59118
return
60119
}
61120
datetime := t.Format("2006-01-02")
62-
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)
121+
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"], retired_status, data["difficultyText"], data["stars"], data["firstUserBloodTime"], data["firstRootBloodTime"], status, datetime)
122+
w.Flush()
63123
}
64-
w.Flush()
65124
}
66125

67126
// Challenges search
@@ -98,8 +157,8 @@ var infoCmd = &cobra.Command{
98157
}
99158
datetime := t.Format("2006-01-02")
100159
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", data["name"], data["category_name"], retired_status, data["difficulty"], data["stars"], data["solves"], status, datetime)
160+
w.Flush()
101161
}
102-
w.Flush()
103162
}
104163
},
105164
}

cmd/stop.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"net/http"
7+
"os"
78

89
"github.com/GoToolSharing/htb-cli/utils"
910
"github.com/spf13/cobra"
@@ -14,6 +15,10 @@ var stopCmd = &cobra.Command{
1415
Short: "Stop the current machine",
1516
Run: func(cmd *cobra.Command, args []string) {
1617
machine_id := utils.GetActiveMachineID(proxyParam)
18+
if machine_id == "" {
19+
fmt.Println("No machine is running")
20+
os.Exit(0)
21+
}
1722
log.Println("Machine ID :", machine_id)
1823
machine_type := utils.GetMachineType(machine_id, "")
1924
log.Println("Machine Type :", machine_type)

utils/utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ func GetActiveMachineID(proxyURL string) string {
207207
}
208208
info := ParseJsonMessage(resp, "info")
209209
if info == nil {
210-
fmt.Println("No machine is running")
211-
os.Exit(0)
210+
return ""
212211
}
213212
return fmt.Sprintf("%.0f", info.(map[string]interface{})["id"].(float64))
214213
}

0 commit comments

Comments
 (0)