273b70bacc
## Checklist - [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md) - [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885 - [x] related ooni/spec pull request: N/A Location of the issue tracker: https://github.com/ooni/probe ## Description This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package. The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity. An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other. This is what I want to move in `internal/model`: - [x] most important interfaces from `internal/netxlite` - [x] everything that was previously part of `internal/engine/model` - [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package probeservices
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
)
|
|
|
|
// State is the state stored inside the state file
|
|
type State struct {
|
|
ClientID string
|
|
Expire time.Time
|
|
Password string
|
|
Token string
|
|
}
|
|
|
|
// Auth returns an authentication structure, if possible, otherwise
|
|
// it returns nil, meaning that you should login again.
|
|
func (s State) Auth() *LoginAuth {
|
|
if s.Token == "" {
|
|
return nil
|
|
}
|
|
if time.Now().Add(30 * time.Second).After(s.Expire) {
|
|
return nil
|
|
}
|
|
return &LoginAuth{Expire: s.Expire, Token: s.Token}
|
|
}
|
|
|
|
// Credentials returns login credentials, if possible, otherwise it
|
|
// returns nil, meaning that you should create an account.
|
|
func (s State) Credentials() *LoginCredentials {
|
|
if s.ClientID == "" {
|
|
return nil
|
|
}
|
|
if s.Password == "" {
|
|
return nil
|
|
}
|
|
return &LoginCredentials{ClientID: s.ClientID, Password: s.Password}
|
|
}
|
|
|
|
// StateFile is the orchestra state file. It is backed by
|
|
// a generic key-value store configured by the user.
|
|
type StateFile struct {
|
|
Store model.KeyValueStore
|
|
key string
|
|
}
|
|
|
|
// NewStateFile creates a new state file backed by a key-value store
|
|
func NewStateFile(kvstore model.KeyValueStore) StateFile {
|
|
return StateFile{key: "orchestra.state", Store: kvstore}
|
|
}
|
|
|
|
// SetMockable is a mockable version of Set
|
|
func (sf StateFile) SetMockable(s State, mf func(interface{}) ([]byte, error)) error {
|
|
data, err := mf(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sf.Store.Set(sf.key, data)
|
|
}
|
|
|
|
// Set saves the current state on the key-value store.
|
|
func (sf StateFile) Set(s State) error {
|
|
return sf.SetMockable(s, json.Marshal)
|
|
}
|
|
|
|
// GetMockable is a mockable version of Get
|
|
func (sf StateFile) GetMockable(sfget func(string) ([]byte, error),
|
|
unmarshal func([]byte, interface{}) error) (State, error) {
|
|
value, err := sfget(sf.key)
|
|
if err != nil {
|
|
return State{}, err
|
|
}
|
|
var state State
|
|
if err := unmarshal(value, &state); err != nil {
|
|
return State{}, err
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
// Get returns the current state. In case of any error with the
|
|
// underlying key-value store, we return an empty state.
|
|
func (sf StateFile) Get() (state State) {
|
|
state, _ = sf.GetMockable(sf.Store.Get, json.Unmarshal)
|
|
return
|
|
}
|