summaryrefslogtreecommitdiff
path: root/src/state.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 12:10:47 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-03-23 12:10:47 +0100
commitb9b2659908d5fe8afcc74f2769a8da7bab243018 (patch)
treec8d524cb99cd98d326d78b78ce988395e4fb3e26 /src/state.go
parented6073f2c2c6600063f2e5062937b7a2a1162eb2 (diff)
Add wrapping functionality for getting a wireguard config
Diffstat (limited to 'src/state.go')
-rw-r--r--src/state.go41
1 files changed, 37 insertions, 4 deletions
diff --git a/src/state.go b/src/state.go
index ea268ec..cd8fa19 100644
--- a/src/state.go
+++ b/src/state.go
@@ -2,8 +2,9 @@ package eduvpn
type VPNState struct {
// Info passed by the client
- ConfigDirectory string `json:"-"`
- Name string `json:"-"`
+ ConfigDirectory string `json:"-"`
+ Name string `json:"-"`
+ StateCallback func(string, string, string) `json:"-"`
// The chosen server
Server *Server `json:"server"`
@@ -12,11 +13,12 @@ type VPNState struct {
DiscoList *DiscoList `json:"disco"`
}
-func Register(state *VPNState, name string, directory string, stateCallback func(string, string, string)) error {
+func (state *VPNState) Register(name string, directory string, stateCallback func(string, string, string)) error {
state.Name = name
state.ConfigDirectory = directory
+ state.StateCallback = stateCallback
- stateCallback("START", "REGISTERED", "app registered")
+ state.StateCallback("Start", "Registered", "app registered")
// Try to load the previous configuration
if state.LoadConfig() != nil {
@@ -26,6 +28,37 @@ func Register(state *VPNState, name string, directory string, stateCallback func
return nil
}
+func (state *VPNState) Connect(url string) (string, error) {
+ if state.Server == nil {
+ state.Server = &Server{}
+ }
+ initializeErr := state.Server.Initialize(url)
+
+ if initializeErr != nil {
+ return "", initializeErr
+ }
+
+ if !state.Server.IsAuthenticated() {
+ authURL, authInitializeErr := state.InitializeOAuth()
+
+ if authInitializeErr != nil {
+ return "", authInitializeErr
+ }
+
+ state.StateCallback("Registered", "OAuthInitialized", authURL)
+ oauthErr := state.FinishOAuth()
+
+ if oauthErr != nil {
+ return "", oauthErr
+ }
+
+ state.StateCallback("OAuthInitialized", "OAuthFinished", "finished oauth")
+ state.WriteConfig()
+ }
+
+ return state.WireguardGetConfig()
+}
+
var VPNStateInstance *VPNState
func GetVPNState() *VPNState {