-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
265 lines (236 loc) · 6.43 KB
/
serve.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2022 Martin Proffitt <mproffitt@choclab.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"github.com/hokaccha/go-prettyjson"
"github.com/notapipeline/bwv/pkg/bitw"
"gopkg.in/yaml.v2"
)
const DefaultPort = 6277
type server struct {
Whitelist []string `yaml:"whitelist"`
Cert string `yaml:"cert"`
Key string `yaml:"key"`
Port int `yaml:"port"`
ApiKeys map[string]string `yaml:"apikeys"`
}
func (s *server) IsSecure() (secure bool) {
secure = false
if s.Cert != "" && s.Key != "" {
secure = true
}
return
}
func getConfigPath() string {
home, _ := os.UserHomeDir()
return fmt.Sprintf("%s/.config/bwv/server.yaml", home)
}
func unique(what []string) (unique []string) {
unique = make([]string, 0, len(what))
m := map[string]bool{}
for _, v := range what {
if !m[v] {
m[v] = true
unique = append(unique, v)
}
}
return
}
func (s *server) parseFields(c bitw.DecryptedCipher, fieldString string) (keys []string, values map[string]interface{}) {
var (
ok bool = false
fields = unique(strings.Split(fieldString, ","))
)
keys = make([]string, 0)
values = make(map[string]interface{})
for _, f := range fields {
var v interface{}
if v, ok = c.Fields[f]; ok && v != "" {
keys = append(keys, f)
values[f] = v
}
}
return
}
func (s *server) parseProperties(c bitw.DecryptedCipher, propertyString string) (keys []string, values map[string]interface{}) {
var properties = unique(strings.Split(propertyString, ","))
keys = make([]string, 0)
values = make(map[string]interface{})
for _, p := range properties {
var v interface{}
if v = c.Get(p); v != "" && v != nil {
keys = append(keys, p)
values[p] = v
}
}
return
}
func (s *server) getHttpPath(w http.ResponseWriter, r *http.Request) {
var (
addr string = strings.Split(r.RemoteAddr, ":")[0]
useWhitelist bool = len(s.Whitelist) != 0
matched bool = !useWhitelist || containsIp("127.0.0.0/24", addr)
auth []string = strings.Split(r.Header.Get("Authorization"), " ")
)
if useWhitelist {
for _, ip := range s.Whitelist {
if ip == addr || containsIp(ip, addr) {
matched = true
break
}
}
}
log.Println(addr, useWhitelist, matched)
if !matched || len(auth) != 2 || auth[0] != "Bearer" || !s.checkApiKey(addr, auth[1]) {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "Denied request from ip %s\n", addr)
return
}
switch r.Method {
case "GET":
var (
path string = strings.TrimLeft(r.URL.Path, "/")
c interface{}
ok bool
params url.Values = r.URL.Query()
)
if strings.TrimRight(path, "/") == "bwvreload" {
s.load()
w.WriteHeader(http.StatusNoContent)
return
}
log.Printf("[GET] %s %+v from %s\n", path, params, addr)
if c, ok = bitw.Get(path); !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Path '%s' not found\n", path)
return
}
var (
value interface{}
fieldKeys, propertyKeys []string
fieldValues, propertyValues map[string]interface{}
useFields, useProperties bool = false, false
)
if fields, ok := params["field"]; ok {
useFields = true
fieldKeys, fieldValues = s.parseFields(c.([]bitw.DecryptedCipher)[0], strings.Join(fields, ","))
}
if properties, ok := params["property"]; ok {
useProperties = true
propertyKeys, propertyValues = s.parseProperties(c.([]bitw.DecryptedCipher)[0], strings.Join(properties, ","))
}
if len(propertyKeys) == 0 && len(fieldKeys) > 0 {
value = fieldValues
if len(fieldKeys) == 1 {
value = fieldValues[fieldKeys[0]]
}
} else if len(fieldKeys) == 0 && len(propertyKeys) > 0 {
value = propertyValues
if len(propertyKeys) == 1 {
value = propertyValues[propertyKeys[0]]
}
}
var length = len(append(propertyKeys, fieldKeys...))
if (useFields || useProperties) && length == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Bad request for path %s\n", path)
return
}
if length > 1 {
if propertyValues == nil {
propertyValues = make(map[string]interface{})
}
for k, v := range fieldValues {
propertyValues[k] = v
}
value = propertyValues
}
if value != nil {
c = value
if length == 1 {
c = map[string]interface{}{
"value": value,
}
}
}
formatter := prettyjson.Formatter{
DisabledColor: true,
Indent: 4,
Newline: "\n",
StringMaxLength: 0,
}
s, e := formatter.Marshal(c)
if e != nil {
log.Fatal(e)
}
fmt.Fprint(w, string(s))
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Invalid method")
}
}
func (s *server) load() error {
var configPath string = getConfigPath()
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
return nil
}
yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
log.Printf("Loading config file %s\n", configPath)
return yaml.Unmarshal(yamlFile, s)
}
func (s *server) listenAndServe() {
var (
listener net.Listener
err error
port int = DefaultPort
)
if err := s.load(); err != nil {
log.Fatal(fmt.Sprintf("Invalid config file"), err)
}
sm := http.NewServeMux()
sm.HandleFunc("/", s.getHttpPath)
if s.Port == 0 {
s.Port = DefaultPort
s.save()
}
if listener, err = net.Listen("tcp4", fmt.Sprintf(":%d", s.Port)); err != nil {
log.Fatal(err)
}
<-syncComplete
if s.Cert != "" && s.Key != "" {
log.Printf("Listening for secure connections on :%d (whitelist %+v)\n", port, s.Whitelist)
log.Fatal(http.ServeTLS(listener, sm, s.Cert, s.Key))
} else {
if len(s.Whitelist) == 0 {
log.Fatal("Cowardly - refusing to start unsecure credential server without a whitelist")
return
}
log.Printf("Listening for unsecured connections on :%d (whitelist %+v)\n", port, s.Whitelist)
log.Fatal(http.Serve(listener, sm))
}
}