Skip to content

Commit 1f9397f

Browse files
committed
Add more methods for working with keys
1 parent 726dd2b commit 1f9397f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

outline.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ func NewOutlineConnection(server string, port int, password string, method strin
9494
}
9595
}
9696

97+
func NewOutlineKey() *OutlineKey {
98+
return &OutlineKey{}
99+
}
100+
97101
func (vpn *OutlineVPN) GetKeys() ([]OutlineKey, error) {
98102
request := fasthttp.AcquireRequest()
99103
response := fasthttp.AcquireResponse()
@@ -141,7 +145,7 @@ func (vpn *OutlineVPN) GetKey(id string) (*OutlineKey, error) {
141145

142146
// If key is added, status code always must be 200
143147
if response.StatusCode() != fasthttp.StatusOK {
144-
return &result, errors.New("unable to retrieve keys")
148+
return &result, errors.New("unable to retrieve key data")
145149
}
146150

147151
// Trying unmarshal response body as `OutlineKey`
@@ -152,6 +156,21 @@ func (vpn *OutlineVPN) GetKey(id string) (*OutlineKey, error) {
152156
return &result, nil
153157
}
154158

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+
155174
func (vpn *OutlineVPN) AddKey(key *OutlineKey) (*OutlineKey, error) {
156175
request := fasthttp.AcquireRequest()
157176
response := fasthttp.AcquireResponse()
@@ -310,8 +329,8 @@ func (vpn *OutlineVPN) GetServerInfo() (*ServerInfo, error) {
310329
}
311330

312331
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")
315334
}
316335

317336
// Parse the access url
@@ -343,3 +362,7 @@ func (key *OutlineKey) AsSource() (*OutlineConnectionSource, error) {
343362

344363
return NewOutlineConnection(host, port, data[1], data[0]), nil
345364
}
365+
366+
func (key *OutlineKey) IsInitialized() bool {
367+
return key.AccessURL != ""
368+
}

0 commit comments

Comments
 (0)