@@ -94,6 +94,10 @@ func NewOutlineConnection(server string, port int, password string, method strin
94
94
}
95
95
}
96
96
97
+ func NewOutlineKey () * OutlineKey {
98
+ return & OutlineKey {}
99
+ }
100
+
97
101
func (vpn * OutlineVPN ) GetKeys () ([]OutlineKey , error ) {
98
102
request := fasthttp .AcquireRequest ()
99
103
response := fasthttp .AcquireResponse ()
@@ -141,7 +145,7 @@ func (vpn *OutlineVPN) GetKey(id string) (*OutlineKey, error) {
141
145
142
146
// If key is added, status code always must be 200
143
147
if response .StatusCode () != fasthttp .StatusOK {
144
- return & result , errors .New ("unable to retrieve keys " )
148
+ return & result , errors .New ("unable to retrieve key data " )
145
149
}
146
150
147
151
// Trying unmarshal response body as `OutlineKey`
@@ -152,6 +156,21 @@ func (vpn *OutlineVPN) GetKey(id string) (*OutlineKey, error) {
152
156
return & result , nil
153
157
}
154
158
159
+ func (vpn * OutlineVPN ) KeyExists (id string ) bool {
160
+ key , err := vpn .GetKey (id )
161
+ return err == nil && key .IsInitialized ()
162
+ }
163
+
164
+ func (vpn * OutlineVPN ) GetOrCreateKey (id string ) (* OutlineKey , error ) {
165
+ if vpn .KeyExists (id ) {
166
+ return vpn .GetKey (id )
167
+ }
168
+
169
+ key := NewOutlineKey ()
170
+ key .ID = id
171
+ return vpn .AddKey (key )
172
+ }
173
+
155
174
func (vpn * OutlineVPN ) AddKey (key * OutlineKey ) (* OutlineKey , error ) {
156
175
request := fasthttp .AcquireRequest ()
157
176
response := fasthttp .AcquireResponse ()
@@ -310,8 +329,8 @@ func (vpn *OutlineVPN) GetServerInfo() (*ServerInfo, error) {
310
329
}
311
330
312
331
func (key * OutlineKey ) AsSource () (* OutlineConnectionSource , error ) {
313
- if key .AccessURL == "" {
314
- return nil , errors .New ("unable to retrieve key's access url" )
332
+ if ! key .IsInitialized () {
333
+ return nil , errors .New ("unable to retrieve key access url" )
315
334
}
316
335
317
336
// Parse the access url
@@ -343,3 +362,7 @@ func (key *OutlineKey) AsSource() (*OutlineConnectionSource, error) {
343
362
344
363
return NewOutlineConnection (host , port , data [1 ], data [0 ]), nil
345
364
}
365
+
366
+ func (key * OutlineKey ) IsInitialized () bool {
367
+ return key .AccessURL != ""
368
+ }
0 commit comments