summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/server.go5
-rw-r--r--src/state.go41
-rw-r--r--src/wireguard.go23
3 files changed, 63 insertions, 6 deletions
diff --git a/src/server.go b/src/server.go
index 627843f..0ef3965 100644
--- a/src/server.go
+++ b/src/server.go
@@ -34,6 +34,11 @@ func (server *Server) Initialize(url string) error {
return nil
}
+// FIXME: Check validity of tokens
+func (server *Server) IsAuthenticated() bool {
+ return server.OAuth != nil
+}
+
func (server *Server) GetEndpoints() error {
url := server.BaseURL + "/.well-known/vpn-user-portal"
body, bodyErr := HTTPGet(url)
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 {
diff --git a/src/wireguard.go b/src/wireguard.go
index 9441c51..0d5967c 100644
--- a/src/wireguard.go
+++ b/src/wireguard.go
@@ -6,13 +6,13 @@ import (
"regexp"
)
-func WireguardGenerateKey() (wgtypes.Key, error) {
+func wireguardGenerateKey() (wgtypes.Key, error) {
key, error := wgtypes.GeneratePrivateKey()
return key, error
}
// FIXME: Instead of doing a regex replace, decide if we should use a parser
-func WireguardConfigAddKey(config string, key wgtypes.Key) string {
+func wireguardConfigAddKey(config string, key wgtypes.Key) string {
interface_section := "[Interface]"
interface_section_escaped := regexp.QuoteMeta(interface_section)
@@ -24,3 +24,22 @@ func WireguardConfigAddKey(config string, key wgtypes.Key) string {
to_replace := fmt.Sprintf("%s\nPrivateKey = %s", interface_section, key.String())
return interface_re.ReplaceAllString(config, to_replace)
}
+
+func (eduvpn *VPNState) WireguardGetConfig() (string, error) {
+ wireguardKey, wireguardErr := wireguardGenerateKey()
+
+ if wireguardErr != nil {
+ return "", wireguardErr
+ }
+
+ wireguardPublicKey := wireguardKey.PublicKey().String()
+ configWireguard, configErr := eduvpn.APIConnectWireguard(wireguardPublicKey)
+
+ if configErr != nil {
+ return "", configErr
+ }
+
+ configWireguardKey := wireguardConfigAddKey(configWireguard, wireguardKey)
+
+ return configWireguardKey, nil
+}