Skip to content

Commit

Permalink
Check that account exists before posting version
Browse files Browse the repository at this point in the history
This was requested by Lee in WDT due to errors on their end when a
version update is received for a callsign without a valid account.

Closes la5nta#213
  • Loading branch information
martinhpedersen committed Sep 12, 2020
1 parent c076791 commit 69eaf70
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
14 changes: 14 additions & 0 deletions internal/cmsapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
RootURL = "https://api.winlink.org"
PathVersionAdd = "/version/add"
PathGatewayStatus = "/gateway/status.json"
PathAccountExists = "/account/exists"

// Issued December 2017 by the WDT for use with Pat
AccessKey = "1880278F11684B358F36845615BD039A"
Expand Down Expand Up @@ -59,6 +60,19 @@ func (v VersionAdd) Post() error {
return nil
}

func AccountExists(callsign string) (bool, error) {
url := RootURL + PathAccountExists + "?key=" + AccessKey + "&callsign=" + url.QueryEscape(callsign)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
var obj struct{ CallsignExists bool }
return obj.CallsignExists, json.NewDecoder(resp.Body).Decode(&obj)
}

type GatewayStatus struct {
ServerName string `json:"ServerName"`
ErrorCode int `json:"ErrorCode"`
Expand Down
42 changes: 40 additions & 2 deletions version_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,58 @@ import (
"github.com/la5nta/pat/internal/cmsapi"
)

func accountExistsCached(callsign string) (bool, error) {
var cache struct {
Expires time.Time
AccountExists bool
}

f, err := os.OpenFile(path.Join(appDir, fmt.Sprintf(".cached_account_check_%s.json", callsign)), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return false, err
}
json.NewDecoder(f).Decode(&cache)
if time.Since(cache.Expires) < 0 {
return cache.AccountExists, nil
}
defer func() {
f.Truncate(0)
f.Seek(0, 0)
json.NewEncoder(f).Encode(cache)
}()

exists, err := cmsapi.AccountExists(callsign)
if !exists || err != nil {
// Let's try again in 48 hours
cache.Expires = time.Now().Add(48 * time.Hour)
return false, err
}

// Keep this response for a month. It will probably not change.
cache.Expires = time.Now().Add(30 * 24 * time.Hour)
cache.AccountExists = exists
return exists, err
}

func postVersionUpdate() error {
var lastUpdated time.Time
file, err := os.OpenFile(path.Join(appDir, "last_version_report.json"), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()

json.NewDecoder(file).Decode(&lastUpdated)

if time.Since(lastUpdated) < 24*time.Hour {
return nil
}

// WDT do not want us to post version reports for callsigns without a registered account
if exists, err := accountExistsCached(fOptions.MyCall); err != nil {
return err
} else if !exists {
return nil
}

v := cmsapi.VersionAdd{
Callsign: fOptions.MyCall,
Program: AppName,
Expand Down

0 comments on commit 69eaf70

Please sign in to comment.