-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (65 loc) · 1.28 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
var appName = "ssh-find"
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func main() {
flag.Usage = func() {
fmt.Printf(`
%s v%s (%s)
Usage: %s <username>
Finds ssh public keys by username
`, appName, version, date, appName)
flag.PrintDefaults()
}
flag.Parse()
username := flag.Arg(0)
if len(username) < 1 {
fmt.Printf(`
%s v%s (%s)
Usage: %s <username>
Finds ssh public keys by username
`, appName, version, date, appName)
os.Exit(1)
}
fmt.Println(GetSSHKeys(username))
}
// GetSSHKeys from public git providers
func GetSSHKeys(username string) string {
providers := map[string]string{
"Github": "https://github.com/%s.keys",
"Gitlab": "https://gitlab.com/%s.keys",
}
foundKeys := ""
for provider, url := range providers {
url = fmt.Sprintf(url, username)
keys := getURLContent(url)
if len(keys) > 0 {
foundKeys += fmt.Sprintf("\n# %s (%s):\n%s", username, provider, keys)
}
}
return foundKeys
}
func getURLContent(url string) string {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode == http.StatusOK {
return fmt.Sprintf("%s", data)
}
return ""
}