-
Notifications
You must be signed in to change notification settings - Fork 0
/
getIdByName.go
58 lines (49 loc) · 1.18 KB
/
getIdByName.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package htb
import (
"../config"
"net/http"
"net/url"
"io/ioutil"
"time"
"encoding/json"
"strings"
"fmt"
)
type User struct{
Id int `json:"id"`
Username string `json:"username"`
}
func GetIdByName(username string) int{
/*
Get ID of user by his username
*/
client := &http.Client{
Timeout: time.Second * 10,
}
// Get request for userID
data := url.Values{}
data.Set("username", username)
req, err := http.NewRequest("POST", "https://www.hackthebox.eu/api/user/id?api_token="+config.Htb.ApiToken, strings.NewReader(data.Encode()))
req.Header.Add("User-Agent", config.USERAGENT)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
print(err)
return 0
}
defer resp.Body.Close()
// If valid
if resp.StatusCode != 200{
fmt.Println("[!] error getting user id of:", username)
return 0
}
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
print(err)
return 0
}
var result User
json.Unmarshal(body, &result)
return result.Id
}