Skip to content

Instantly share code, notes, and snippets.

@YouSysAdmin
Last active March 25, 2020 19:40
Show Gist options
  • Save YouSysAdmin/1f18f5cda44b1da3b3e7e43081a08ad8 to your computer and use it in GitHub Desktop.
Save YouSysAdmin/1f18f5cda44b1da3b3e7e43081a08ad8 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const hackTheBoxURL = "https://www.hackthebox.eu/api/invite/generate"
const hackTheBoxRegisterURL = "https://www.hackthebox.eu/register"
type respData struct {
Success int `json:"success"`
Data struct{
Code string `json:"code"`
Format string `json:"format"`
} `json:"data"`
Status int `json:"0"`
}
func main() {
data,err := inviteReq(hackTheBoxURL)
if err != nil {
log.Fatalln(err)
}
decode, err := base64.StdEncoding.DecodeString(data.Data.Code)
fmt.Printf("Success: %t\r\n" +
"Code: %s\r\n" +
"Register here: %s\r\n",
itob(data.Success),decode, hackTheBoxRegisterURL)
}
// Get invite data from the hackthebox API
func inviteReq(url string) (respData respData,err error){
buff := []byte{}
reader := bytes.NewReader(buff)
// Make POST req to hackerthebox API
resp, err := http.Post(url, "application/json",reader)
if err != nil {
return respData,err
}
defer resp.Body.Close()
// Read response data
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return respData,err
}
// Unmarshal response JSON
//{"success":1,"data":{"code":"BASE64ENCODED","format":"encoded"},"0":200}
err = json.Unmarshal(body,&respData)
if err != nil {
return respData,err
}
return respData,nil
}
// Sugar
func itob(i int) bool {
if i == 1 {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment