-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsssaas-golang.go
190 lines (158 loc) · 3.67 KB
/
sssaas-golang.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package sssaas
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/SSSaaS/sssa-golang"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"sync"
"time"
)
func FromYAML(content []byte, key string) (string, error) {
var output map[string]map[string]interface{}
err := yaml.Unmarshal(content, &output)
if err != nil {
return "", err
}
var obj Config
if _, ok := output[key]["remote"]; ok {
for i := range output[key]["remote"].([]interface{}) {
obj.Remote = append(obj.Remote, output[key]["remote"].([]interface{})[i].(string))
}
}
if _, ok := output[key]["shares"]; ok {
for i := range output[key]["shares"].([]interface{}) {
obj.Remote = append(obj.Remote, output[key]["shares"].([]interface{})[i].(string))
}
}
if _, ok := output[key]["local"]; ok {
obj.Local = output[key]["local"].(string)
}
if _, ok := output[key]["minimum"]; ok {
obj.Minimum = output[key]["minimum"].(int)
}
if _, ok := output[key]["timeout"]; ok {
obj.Minimum = output[key]["timeout"].(int)
}
return FromConfig(obj)
}
func FromConfig(obj Config) (string, error) {
if obj.Local != "" {
fh, err := os.Open(obj.Local)
if err != nil {
return "", err
}
defer fh.Close()
r := bufio.NewReader(fh)
line, err := r.ReadString('\n')
for err == nil {
last := len(line) - 1
share := line[:last]
if sssa.IsValidShare(share) {
obj.Shares = append(obj.Shares, share)
}
line, err = r.ReadString('\n')
}
if line != "" && sssa.IsValidShare(line) {
obj.Shares = append(obj.Shares, line)
}
}
return GetSecret(obj.Remote, obj.Shares, obj.Minimum, obj.Timeout)
}
func GetSecret(endpoints []string, shares []string, minimum int, timeout int) (string, error) {
var results []string = shares
var wg sync.WaitGroup
var global_err []error
var done bool = false
if timeout == 0 {
timeout = 300
}
duration := time.Duration(time.Duration(timeout) * time.Second)
for i := range endpoints {
wg.Add(1)
go func() {
client := &http.Client{
Timeout: duration,
}
req, err := http.NewRequest("GET", endpoints[i], nil)
if err != nil {
global_err = append(global_err, err)
if !done {
wg.Done()
}
return
}
req.Header.Set("User-Agent", "sssaas-golang v0 v0.0.1")
res, err := client.Do(req)
if err != nil {
global_err = append(global_err, err)
if !done {
wg.Done()
}
return
}
if res.StatusCode != 200 {
global_err = append(global_err, errors.New(strconv.Itoa(res.StatusCode)+":"+res.Status))
if !done {
wg.Done()
}
} else {
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
global_err = append(global_err, err)
}
current := response{}
err = json.Unmarshal([]byte(data), ¤t)
if err != nil {
global_err = append(global_err, err)
}
for j := range current.SharedSecrets {
results = append(results, current.SharedSecrets[j])
}
}
results = removeDuplicates(results)
if len(results) >= minimum && !done {
done = true
for _ = range endpoints {
wg.Done()
}
}
if !done {
wg.Done()
}
}()
}
wg.Wait()
results = removeDuplicates(results)
if len(results) < minimum {
fmt.Println("results")
if len(global_err) >= 1 {
return "", global_err[0]
} else {
return "", errors.New("Could not meet minimum shares!")
}
}
return sssa.Combine(results), nil
}
func removeDuplicates(data []string) []string {
results := data
sort.Strings(results)
clen := len(results)
i := 0
for i < clen-1 {
if results[i] == results[i+1] {
results = append(results[:i], results[i+1:]...)
clen = len(results)
} else {
i += 1
}
}
return results
}