-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
84 lines (69 loc) · 1.54 KB
/
client.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 (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
func ClientMain(cnfg Config) {
for {
client := &http.Client{}
req, _ := http.NewRequest("GET", cnfg.Client.Target, nil)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
if err == nil {
if resp.StatusCode == http.StatusOK {
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var keys ResultKeys
err2 := json.Unmarshal(respBytes, &keys)
if err2 != nil {
panic(err2)
}
var approvedKeys []string
for x := range keys.Keys {
z := keys.Keys[x]
_, err = VerifySignedKey(z.Key, z.Signature)
if err == nil {
approvedKeys = append(approvedKeys, z.Key)
}
}
if len(approvedKeys) > 0 {
AuthKeyFileExists, _ := exists(cnfg.Client.AuthorizedKeyFile)
if AuthKeyFileExists == false {
f, err := os.Create(cnfg.Client.AuthorizedKeyFile)
if err == nil {
f.Close()
os.Chmod(cnfg.Client.AuthorizedKeyFile, 0644)
ioutil.WriteFile(cnfg.Client.AuthorizedKeyFile, []byte(parseKeys(approvedKeys)), 0644)
}
} else {
ioutil.WriteFile(cnfg.Client.AuthorizedKeyFile, []byte(parseKeys(approvedKeys)), 0644)
}
}
}
}
if resp.StatusCode == http.StatusOK {
time.Sleep(time.Second * 30)
} else {
time.Sleep(time.Second * 5)
}
}
}
func parseKeys(k []string) string {
var s string
for x := range k {
z := k[x]
z = strings.TrimSuffix(z, "\n")
s += z
s += "\n"
}
return s
}