Skip to content
Snippets Groups Projects
Unverified Commit a0e67fe3 authored by Kali Kaneko's avatar Kali Kaneko
Browse files

[feat] lookup the config vars at runtime

- Resolves: #326
parent e591c314
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,6 @@ package main
import (
"C"
"log"
"unsafe"
"0xacab.org/leap/bitmask-vpn/pkg/backend"
......@@ -55,9 +54,7 @@ func SubscribeToEvent(event string, f unsafe.Pointer) {
//export InitializeBitmaskContext
func InitializeBitmaskContext(provider string, jsonPtr unsafe.Pointer, jsonLen C.int) {
log.Println("DEBUG: provider=", provider)
json := C.GoBytes(jsonPtr, jsonLen)
log.Println("DEBUG: json=", string(json))
opts := backend.InitOptsFromJSON(provider, string(json))
backend.InitializeBitmaskContext(opts)
}
......
......@@ -63,35 +63,30 @@ func SubscribeToEvent(event string, f unsafe.Pointer) {
}
type Providers struct {
Default string `json:"default"`
Data []InitOpts
Default string `json:"default"`
Data []bitmask.ProviderOpts `json:"providers"`
}
type InitOpts struct {
Provider string `json:"name"`
AppName string `json:"applicationName"`
BinaryName string `json:"binaryName"`
Auth string `json:"auth"`
ProviderURL string `json:"providerURL"`
TosURL string `json:"tosURL"`
HelpURL string `json:"helpURL"`
AskForDonations bool `json:"askForDonations"`
ProviderOptions *bitmask.ProviderOpts
SkipLaunch bool
}
func InitOptsFromJSON(provider, providersJSON string) *InitOpts {
opts := InitOpts{}
err := json.Unmarshal([]byte(providersJSON), &opts)
providers := Providers{}
err := json.Unmarshal([]byte(providersJSON), &providers)
if err != nil {
log.Println("ERROR: %v", err)
log.Println("ERROR while parsing json:", err)
}
return &opts
if len(providers.Data) != 1 {
panic("BUG: we do not support multi-provider yet")
}
providerOpts := &providers.Data[0]
return &InitOpts{providerOpts, false}
}
func InitializeBitmaskContext(opts *InitOpts) {
p := bitmask.GetConfiguredProvider()
opts.Provider = p.Provider
opts.AppName = p.AppName
bitmask.ConfigureProvider(opts.ProviderOptions)
initOnce.Do(func() { initializeContext(opts) })
if ctx.bm != nil {
......
......@@ -2,8 +2,6 @@ package backend
import (
"time"
"0xacab.org/leap/bitmask-vpn/pkg/config"
)
// runDonationReminder checks every hour if we need to show the reminder,
......@@ -19,13 +17,6 @@ func runDonationReminder() {
}()
}
func wantDonations() bool {
if config.AskForDonations == "true" {
return true
}
return false
}
func needsDonationReminder() bool {
return ctx.cfg.NeedsDonationReminder()
}
......
......@@ -14,13 +14,17 @@ import (
// api uses the sync.Once primitive to call this.
func initializeContext(opts *InitOpts) {
var st status = off
// TODO - now there's really no need to dance between opts and config anymore
// but this was the simplest transition. We should probably keep the multi-provider config in the backend too, and just
// switch the "active" here in the ctx, after the user has selected one in the combobox.
ctx = &connectionCtx{
AppName: opts.AppName,
Provider: opts.Provider,
AppName: opts.ProviderOptions.AppName,
Provider: opts.ProviderOptions.Provider,
TosURL: config.TosURL,
HelpURL: config.HelpURL,
DonateURL: config.DonateURL,
AskForDonations: wantDonations(),
AskForDonations: config.AskForDonations,
DonateDialog: false,
Version: version.VERSION,
Status: st,
......
......@@ -20,6 +20,7 @@ import (
"log"
"os"
"path"
"strconv"
"github.com/jmshal/go-locale"
"golang.org/x/text/message"
......@@ -34,12 +35,46 @@ type ProviderInfo struct {
AppName string
}
type ProviderOpts struct {
Provider string `json:"name"`
AppName string `json:"applicationName"`
BinaryName string `json:"binaryName"`
Auth string `json:"auth"`
ProviderURL string `json:"providerURL"`
DonateURL string `json:"donateURL"`
ApiURL string `json:"apiURL"`
TosURL string `json:"tosURL"`
HelpURL string `json:"helpURL"`
GeolocationURL string `json:"geolocationAPI"`
AskForDonations string `json:"askForDonations"`
CaCert string `json:"caCertString"`
}
func GetConfiguredProvider() *ProviderInfo {
provider := config.Provider
appName := config.ApplicationName
return &ProviderInfo{provider, appName}
}
func ConfigureProvider(opts *ProviderOpts) {
config.Provider = opts.ProviderURL
config.ProviderName = opts.Provider
config.ApplicationName = opts.AppName
config.BinaryName = opts.BinaryName
config.Auth = opts.Auth
config.DonateURL = opts.DonateURL
config.HelpURL = opts.HelpURL
config.TosURL = opts.TosURL
config.APIURL = opts.ApiURL
config.GeolocationAPI = opts.GeolocationURL
config.CaCert = []byte(opts.CaCert)
wantsDonations, err := strconv.ParseBool(opts.AskForDonations)
if err == nil {
config.AskForDonations = wantsDonations
}
}
func InitializeLogger() {
_, err := config.ConfigureLogger(path.Join(config.LogPath))
if err != nil {
......
// Code generated by go generate; DO NOT EDIT.
// This file was generated by vendorize.py
// At 2020-08-20 13:39:50
package config
/* All these constants are defined in the vendor.conf file
*/
const (
Provider = "vpnlib.bitmask.net"
ApplicationName = "DemoLib"
BinaryName = "demo-lib"
Auth = "sip"
var (
Provider = ""
ProviderName = ""
ApplicationName = ""
BinaryName = ""
Auth = ""
DonateURL = ""
AskForDonations = "false"
HelpURL = "https://libraryvpn.org/"
TosURL = "https://libraryvpn.org/"
APIURL = "https://api.vpnlib.bitmask.net:4430/"
GeolocationAPI = "https://getmyip.vpnlib.bitmask.net/json"
AskForDonations = true
HelpURL = ""
TosURL = ""
APIURL = ""
GeolocationAPI = ""
)
var Version string
......@@ -28,12 +23,4 @@ CaCert : a string containing a representation of the provider CA, used to
config/[provider]-ca.crt
*/
var CaCert = []byte(`-----BEGIN CERTIFICATE-----
MIIBQzCB6aADAgECAgEBMAoGCCqGSM49BAMCMBcxFTATBgNVBAMTDExFQVAgUm9v
dCBDQTAeFw0yMDA4MDYxOTA3NDRaFw0yNTA4MDYxOTEyNDRaMBcxFTATBgNVBAMT
DExFQVAgUm9vdCBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABIG5POr4cAdK
kTavKpSJr8nW1V7HLpr27qKaShpk1TUy5ipaAlusmavGLxKsPE+i3AMlvf/f6ch3
1MjAtIf5rYujJjAkMA4GA1UdDwEB/wQEAwICpDASBgNVHRMBAf8ECDAGAQH/AgEB
MAoGCCqGSM49BAMCA0kAMEYCIQDXj280LNZbSbi0Y2WvtQrJBUw4wdm8qAeOeuH7
6XiLEwIhAPBRsmst/ujcChsG2t6LpG+p8s4rfIfh8YLo/4qrcc5p
-----END CERTIFICATE-----`)
var CaCert = []byte("")
......@@ -40,10 +40,6 @@ const (
certPathv1 = "1/cert"
certPathv3 = "3/cert"
authPathv3 = "3/auth"
certAPI = config.APIURL + certPathv1
certAPI3 = config.APIURL + certPathv3
authAPI = config.APIURL + authPathv3
)
type Bonafide struct {
......@@ -185,11 +181,11 @@ func (b *Bonafide) getURL(object string) string {
if b.apiURL == "" {
switch object {
case "cert":
return certAPI
return config.APIURL + certPathv1
case "certv3":
return certAPI3
return config.APIURL + certPathv3
case "auth":
return authAPI
return config.APIURL + authPathv3
}
} else {
switch object {
......
......@@ -14,11 +14,6 @@ import (
"0xacab.org/leap/bitmask-vpn/pkg/config"
)
const (
eip1API = config.APIURL + "1/config/eip-service.json"
eip3API = config.APIURL + "3/config/eip-service.json"
)
type eipService struct {
Gateways []gatewayV3
Locations map[string]location
......@@ -83,6 +78,7 @@ func (b *Bonafide) setupAuthentication(i interface{}) {
}
func (b *Bonafide) fetchEipJSON() error {
eip3API := config.APIURL + "3/config/eip-service.json"
resp, err := b.client.Post(eip3API, "", nil)
for err != nil {
log.Printf("Error fetching eip v3 json: %v", err)
......@@ -98,6 +94,7 @@ func (b *Bonafide) fetchEipJSON() error {
buf := make([]byte, 128)
resp.Body.Read(buf)
log.Printf("Error fetching eip v3 json")
eip1API := config.APIURL + "1/config/eip-service.json"
resp, err = b.client.Post(eip1API, "", nil)
if err != nil {
return err
......
......@@ -29,7 +29,10 @@ import (
)
const (
systemOpenvpnPath = "/usr/sbin/openvpn"
systemOpenvpnPath = "/usr/sbin/openvpn"
)
var (
snapOpenvpnPath = "/snap/bin/" + config.BinaryName + ".openvpn"
snapBitmaskRootPath = "/snap/bin/" + config.BinaryName + ".bitmask-root"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment