-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbw.go
173 lines (162 loc) · 3.96 KB
/
bw.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
/*
* Copyright 2022 Martin Proffitt <[email protected]>
*
* 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 (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"github.com/coreos/go-systemd/v22/dbus"
"github.com/hokaccha/go-prettyjson"
"github.com/notapipeline/bwv/pkg/bitw"
"github.com/notapipeline/bwv/pkg/unix"
)
var (
loginResponse *bitw.LoginResponse
authToken string
appName string = "bwv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage bwv [ serve [ --whitelist 127.0.0.1,192.168.1.1,...] <path>")
return
}
var (
path string
c interface{}
s server = server{}
err error
)
path = os.Args[1]
switch path {
case "serve":
s.listenAndServe()
case "install":
if err = unix.InstallService(appName); err == nil {
unix.StartService(appName)
}
case "remove":
err = unix.RemoveService(appName)
case "start":
err = unix.StartService(appName)
case "stop":
err = unix.StopService(appName)
case "status":
var status *dbus.UnitStatus
status, err = unix.ServiceStatus(appName)
if err == nil {
fmt.Println(status.SubState)
}
case "genkey":
if len(os.Args) < 3 {
fmt.Println("Please specify an IP or CIDR range for this key")
return
}
if len(strings.Split(os.Args[2], ",")) != 1 {
fmt.Println("Only a single IP or CIDR range should be provided")
return
}
s.load()
if err = parseWhitelist(&s, os.Args[2], false); err == nil {
s.AddApiKey(os.Args[2])
}
case "revoke":
if len(os.Args) < 3 {
fmt.Println("Please specify the api key or ip/range to revoke")
return
}
s.load()
if ip := s.RevokeApiKey(os.Args[2]); ip != "" {
err = parseWhitelist(&s, ip, true)
}
case "whitelist":
s.load()
err = parseWhitelist(&s, os.Args[2], false)
case "drop":
s.load()
err = parseWhitelist(&s, os.Args[2], true)
default:
log.SetOutput(ioutil.Discard)
s.load()
if c = client(path, s.Port, s.IsSecure(), false); c == nil {
return
}
s, e := prettyjson.Marshal(c)
if e != nil {
log.Fatal(e)
}
fmt.Println(string(s))
return
}
if err != nil {
log.Fatal(err)
}
var reloadCommands []string = []string{
"genkey", "revoke",
"whitelist", "drop",
}
if contains(os.Args[1], reloadCommands) && isRunning() {
client("bwvreload", s.Port, s.IsSecure(), false)
}
}
func parseWhitelist(s *server, w string, remove bool) error {
var whitelist []string = strings.Split(w, ",")
for _, addr := range whitelist {
if net.ParseIP(os.Args[2]) == nil {
ip, n, err := net.ParseCIDR(addr)
if err != nil {
return fmt.Errorf("Invalid IP or cidr range %s\n", addr)
}
if !n.IP.Equal(ip) {
return fmt.Errorf("Cidr range %s is invalid, should be %s\n", addr, n)
}
}
if remove {
var newWhitelist []string = make([]string, 0)
for _, netw := range s.Whitelist {
if netw == addr {
continue
} else if containsIp(netw, addr) {
return fmt.Errorf("Address %s is part of CIDR range %s which is not being removed", addr, netw)
}
newWhitelist = append(newWhitelist, netw)
}
s.Whitelist = newWhitelist
} else {
var exists bool = false
for _, netw := range s.Whitelist {
if addr == netw || containsIp(netw, addr) {
exists = true
}
}
if !exists {
s.Whitelist = append(s.Whitelist, addr)
}
}
}
s.save()
return nil
}
func isRunning() bool {
var (
status *dbus.UnitStatus
err error
)
status, err = unix.ServiceStatus(appName)
return err == nil && status.SubState == "running"
}