Skip to content

Commit

Permalink
cache config from api
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Mar 27, 2024
1 parent ad7501d commit 636e668
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
14 changes: 13 additions & 1 deletion internal/engine/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Session struct {
softwareName string
softwareVersion string
tempDir string
vpnConfig map[string]model.OOAPIVPNProviderConfig

// closeOnce allows us to call Close just once.
closeOnce sync.Once
Expand Down Expand Up @@ -178,6 +179,7 @@ func NewSession(ctx context.Context, config SessionConfig) (*Session, error) {
torArgs: config.TorArgs,
torBinary: config.TorBinary,
tunnelDir: config.TunnelDir,
vpnConfig: make(map[string]model.OOAPIVPNProviderConfig),
}
proxyURL := config.ProxyURL
if proxyURL != nil {
Expand Down Expand Up @@ -377,11 +379,21 @@ func (s *Session) FetchTorTargets(
// FetchOpenVPNConfig fetches openvpn config from the API.
func (s *Session) FetchOpenVPNConfig(
ctx context.Context, cc string) (map[string]model.OOAPIVPNProviderConfig, error) {
// TODO: need to cache only the requested provider, since it's different calls to the API.
if len(s.vpnConfig) > 0 {
return s.vpnConfig, nil
}

clnt, err := s.NewOrchestraClient(ctx)
if err != nil {
return nil, err
}
return clnt.FetchOpenVPNConfig(ctx, cc)
config, err := clnt.FetchOpenVPNConfig(ctx, cc)
if err != nil {
return nil, err
}
s.vpnConfig = config
return config, nil
}

// KeyValueStore returns the configured key-value store.
Expand Down
25 changes: 17 additions & 8 deletions internal/experiment/openvpn/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func newEndpointFromInputString(uri string) (*endpoint, error) {
return nil, fmt.Errorf("%w: expected provider as host: %s", ErrInvalidInput, parsedURL.Host)
}
if provider != "riseup" {
// because we are hardcoding at the moment. figure out a way to pass info for
// arbitrary providers as options instead
// I am hardcoding a single provider at the moment.
// I need to figure out a way to pass info for arbitrary providers as options instead.
return nil, fmt.Errorf("%w: unknown provider: %s", ErrInvalidInput, provider)
}

Expand Down Expand Up @@ -98,7 +98,8 @@ func newEndpointFromInputString(uri string) (*endpoint, error) {
return endpoint, nil
}

// String implements Stringer. This is a subset of the input URI scheme.
// String implements Stringer. This is a compact representation of the endpoint,
// which differs from the input URI scheme.
func (e *endpoint) String() string {
var proto string
if e.Obfuscation == "obfs4" {
Expand All @@ -109,16 +110,23 @@ func (e *endpoint) String() string {
return fmt.Sprintf("%s://%s:%s/%s", proto, e.IPAddr, e.Port, e.Transport)
}

// AsInputURI is a string representation of this endpoint. It contains more information than the endpoint itself.
// TODO: redo with latest format
// openvpn://provider.corp/?address=1.1.1.1:1194&transport=tcp
// AsInputURI is a string representation of this endpoint, as used in the experiment input URI format.
func (e *endpoint) AsInputURI() string {
var proto string
if e.Obfuscation == "obfs4" {
proto = e.Protocol + "+obfs4"
} else {
proto = e.Protocol
}

provider := e.Provider
if provider == "" {
provider = "unknown"
}
i := fmt.Sprintf("%s/?provider=%s", e.String(), provider)
return i

return fmt.Sprintf(
"%s://%s.corp/?address=%s:%s&transport=%s",
proto, provider, e.IPAddr, e.Port, e.Transport)
}

// endpointList is a list of endpoints.
Expand All @@ -128,6 +136,7 @@ type endpointList []*endpoint
// This is a hardcoded list for now, but the idea is that we can receive this from the check-in api in the future.
// In any case, having hardcoded endpoints is good as a fallback for the cases in which we cannot contact
// OONI's backend.
// TODO: hardcoded, setup as backup if we cannot contact API.
var allEndpoints = endpointList{
{
Provider: "riseup",
Expand Down
1 change: 1 addition & 0 deletions internal/probeservices/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

// FetchOpenVPNConfig returns valid configuration for the openvpn experiment.
func (c Client) FetchOpenVPNConfig(ctx context.Context, cc string) (map[string]model.OOAPIVPNProviderConfig, error) {
fmt.Println("FETCHING OPENVPN CONFIG>>>>")
_, auth, err := c.GetCredsAndAuth()
if err != nil {
return nil, err
Expand Down

0 comments on commit 636e668

Please sign in to comment.