-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
247 lines (203 loc) · 4.22 KB
/
database.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
package waygate
import (
"github.com/jmoiron/sqlx"
//"github.com/mattn/go-sqlite3"
)
type Forward struct {
Domain string `db:"domain"`
Protected bool `db:"protected"`
TargetAddress string `db:"target_address"`
}
type Database struct {
db *sqlx.DB
}
func NewDatabase(path string) (*Database, error) {
db, err := sqlx.Open("sqlite3", path)
if err != nil {
return nil, err
}
stmt := `
CREATE TABLE IF NOT EXISTS config(
jwks_json TEXT UNIQUE
);
`
_, err = db.Exec(stmt)
if err != nil {
return nil, err
}
stmt = `
SELECT COUNT(*) FROM config;
`
var numRows int
err = db.QueryRow(stmt).Scan(&numRows)
if err != nil {
return nil, err
}
if numRows == 0 {
stmt = `
INSERT INTO config DEFAULT VALUES;
`
_, err = db.Exec(stmt)
if err != nil {
return nil, err
}
}
s := &Database{
db: db,
}
return s, nil
}
func (d *Database) GetJWKS() (string, error) {
var jwks_json string
stmt := `
SELECT jwks_json FROM config;
`
err := d.db.QueryRow(stmt).Scan(&jwks_json)
if err != nil {
return "", err
}
return jwks_json, nil
}
func (d *Database) SetJWKS(jwks string) error {
stmt := `
UPDATE config SET jwks_json=?;
`
_, err := d.db.Exec(stmt, jwks)
if err != nil {
return err
}
return nil
}
type ClientDatabase struct {
db *sqlx.DB
}
func NewClientDatabase(path string) (*ClientDatabase, error) {
db, err := sqlx.Open("sqlite3", path)
if err != nil {
return nil, err
}
stmt := `
CREATE TABLE IF NOT EXISTS config(
server_uri TEXT DEFAULT "" UNIQUE NOT NULL,
token TEXT DEFAULT "" UNIQUE NOT NULL
);
`
_, err = db.Exec(stmt)
if err != nil {
return nil, err
}
stmt = `
SELECT COUNT(*) FROM config;
`
var numRows int
err = db.QueryRow(stmt).Scan(&numRows)
if err != nil {
return nil, err
}
if numRows == 0 {
stmt = `
INSERT INTO config DEFAULT VALUES;
`
_, err = db.Exec(stmt)
if err != nil {
return nil, err
}
}
stmt = `
CREATE TABLE IF NOT EXISTS forwards(
domain TEXT UNIQUE NOT NULL,
target_address TEXT NOT NULL,
protected BOOLEAN
);
`
_, err = db.Exec(stmt)
if err != nil {
return nil, err
}
s := &ClientDatabase{
db: db,
}
return s, nil
}
func (d *ClientDatabase) GetServerUri() (string, error) {
var value string
stmt := `
SELECT server_uri FROM config;
`
err := d.db.QueryRow(stmt).Scan(&value)
if err != nil {
return "", err
}
return value, nil
}
func (d *ClientDatabase) SetServerUri(serverUri string) error {
stmt := `
UPDATE config SET server_uri=?;
`
_, err := d.db.Exec(stmt, serverUri)
if err != nil {
return err
}
return nil
}
func (d *ClientDatabase) GetToken() (string, error) {
var value string
stmt := `
SELECT token FROM config;
`
err := d.db.QueryRow(stmt).Scan(&value)
if err != nil {
return "", err
}
return value, nil
}
func (d *ClientDatabase) SetToken(value string) error {
stmt := `
UPDATE config SET token=?;
`
_, err := d.db.Exec(stmt, value)
if err != nil {
return err
}
return nil
}
func (d *ClientDatabase) GetForwards() ([]*Forward, error) {
stmt := `
SELECT * FROM forwards;
`
var forwards []*Forward
err := d.db.Select(&forwards, stmt)
if err != nil {
return nil, err
}
return forwards, nil
}
func (s *ClientDatabase) GetForward(domain string) (*Forward, error) {
var forward Forward
stmt := "SELECT * FROM forwards WHERE domain = ?"
err := s.db.Get(&forward, stmt, domain)
if err != nil {
return nil, err
}
return &forward, nil
}
func (d *ClientDatabase) SetForward(f *Forward) error {
stmt := `
INSERT OR REPLACE INTO forwards(domain,target_address,protected) VALUES(?,?,?);
`
_, err := d.db.Exec(stmt, f.Domain, f.TargetAddress, f.Protected)
if err != nil {
return err
}
return nil
}
func (d *ClientDatabase) DeleteForwardByDomain(domain string) error {
stmt := `
DELETE FROM forwards WHERE domain = ?;
`
_, err := d.db.Exec(stmt, domain)
if err != nil {
return err
}
return nil
}