summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-13 12:12:22 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-13 12:12:22 +0200
commit5abf00ab87a55662eefc7716de52ead9749293c6 (patch)
tree1cfa64b99482d7cc08b1d7da5d6833b75f5f7714 /internal
parent57d6c2ac55a5fd1ea609c873d5410174b7cf6ca4 (diff)
Refactor: Adapt the API to the documentation
Diffstat (limited to 'internal')
-rw-r--r--internal/openvpn.go8
-rw-r--r--internal/server.go35
-rw-r--r--internal/wireguard.go12
3 files changed, 33 insertions, 22 deletions
diff --git a/internal/openvpn.go b/internal/openvpn.go
index 45beb51..7c4df18 100644
--- a/internal/openvpn.go
+++ b/internal/openvpn.go
@@ -2,20 +2,20 @@ package internal
import "fmt"
-func OpenVPNGetConfig(server Server) (string, error) {
+func OpenVPNGetConfig(server Server) (string, string, error) {
base, baseErr := server.GetBase()
if baseErr != nil {
- return "", &OpenVPNGetConfigError{Err: baseErr}
+ return "", "", &OpenVPNGetConfigError{Err: baseErr}
}
profile_id := base.Profiles.Current
configOpenVPN, _, configErr := APIConnectOpenVPN(server, profile_id)
if configErr != nil {
- return "", &OpenVPNGetConfigError{Err: configErr}
+ return "", "", &OpenVPNGetConfigError{Err: configErr}
}
- return configOpenVPN, nil
+ return configOpenVPN, "openvpn", nil
}
type OpenVPNGetConfigError struct {
diff --git a/internal/server.go b/internal/server.go
index c9e31af..95140af 100644
--- a/internal/server.go
+++ b/internal/server.go
@@ -305,19 +305,19 @@ func getCurrentProfile(server Server) (*ServerProfile, error) {
return nil, &ServerGetCurrentProfileNotFoundError{ProfileID: profileID}
}
-func getConfigWithProfile(server Server, forceTCP bool) (string, error) {
+func getConfigWithProfile(server Server, forceTCP bool) (string, string, error) {
base, baseErr := server.GetBase()
if baseErr != nil {
- return "", &ServerGetConfigWithProfileError{Err: baseErr}
+ return "", "", &ServerGetConfigWithProfileError{Err: baseErr}
}
if !base.FSM.HasTransition(HAS_CONFIG) {
- return "", &FSMWrongStateTransitionError{Got: base.FSM.Current, Want: HAS_CONFIG}
+ return "", "", &FSMWrongStateTransitionError{Got: base.FSM.Current, Want: HAS_CONFIG}
}
profile, profileErr := getCurrentProfile(server)
if profileErr != nil {
- return "", &ServerGetConfigWithProfileError{Err: profileErr}
+ return "", "", &ServerGetConfigWithProfileError{Err: profileErr}
}
supportsOpenVPN := profile.supportsOpenVPN()
@@ -325,15 +325,26 @@ func getConfigWithProfile(server Server, forceTCP bool) (string, error) {
// If forceTCP we must be able to get a config with OpenVPN
if forceTCP && supportsOpenVPN {
- return "", &ServerGetConfigForceTCPError{}
+ return "", "", &ServerGetConfigForceTCPError{}
}
+ var config string
+ var configType string
+ var configErr error
+
if supportsWireguard {
// A wireguard connect call needs to generate a wireguard key and add it to the config
// Also the server could send back an OpenVPN config if it supports OpenVPN
- return WireguardGetConfig(server, supportsOpenVPN)
+ config, configType, configErr = WireguardGetConfig(server, supportsOpenVPN)
+ } else {
+ config, configType, configErr = OpenVPNGetConfig(server)
+ }
+
+ if configErr != nil {
+ return "", "", &ServerGetConfigWithProfileError{Err: configErr}
}
- return OpenVPNGetConfig(server)
+
+ return config, configType, nil
}
func askForProfileID(server Server) error {
@@ -349,21 +360,21 @@ func askForProfileID(server Server) error {
return nil
}
-func GetConfig(server Server, forceTCP bool) (string, error) {
+func GetConfig(server Server, forceTCP bool) (string, string, error) {
base, baseErr := server.GetBase()
if baseErr != nil {
- return "", &ServerGetConfigError{Err: baseErr}
+ return "", "", &ServerGetConfigError{Err: baseErr}
}
if !base.FSM.InState(REQUEST_CONFIG) {
- return "", &FSMWrongStateError{Got: base.FSM.Current, Want: REQUEST_CONFIG}
+ return "", "", &FSMWrongStateError{Got: base.FSM.Current, Want: REQUEST_CONFIG}
}
// Get new profiles using the info call
// This does not override the current profile
infoErr := APIInfo(server)
if infoErr != nil {
- return "", &ServerGetConfigError{Err: infoErr}
+ return "", "", &ServerGetConfigError{Err: infoErr}
}
// If there was a profile chosen and it doesn't exist anymore, reset it
@@ -387,7 +398,7 @@ func GetConfig(server Server, forceTCP bool) (string, error) {
profileErr := askForProfileID(server)
if profileErr != nil {
- return "", &ServerGetConfigError{Err: profileErr}
+ return "", "", &ServerGetConfigError{Err: profileErr}
}
return getConfigWithProfile(server, forceTCP)
diff --git a/internal/wireguard.go b/internal/wireguard.go
index 7f8da38..6edad08 100644
--- a/internal/wireguard.go
+++ b/internal/wireguard.go
@@ -30,29 +30,29 @@ func wireguardConfigAddKey(config string, key wgtypes.Key) string {
return interface_re.ReplaceAllString(config, to_replace)
}
-func WireguardGetConfig(server Server, supportsOpenVPN bool) (string, error) {
+func WireguardGetConfig(server Server, supportsOpenVPN bool) (string, string, error) {
base, baseErr := server.GetBase()
if baseErr != nil {
- return "", &WireguardGetConfigError{Err: baseErr}
+ return "", "", &WireguardGetConfigError{Err: baseErr}
}
profile_id := base.Profiles.Current
wireguardKey, wireguardErr := wireguardGenerateKey()
if wireguardErr != nil {
- return "", &WireguardGetConfigError{Err: wireguardErr}
+ return "", "", &WireguardGetConfigError{Err: wireguardErr}
}
wireguardPublicKey := wireguardKey.PublicKey().String()
config, content, _, configErr := APIConnectWireguard(server, profile_id, wireguardPublicKey, supportsOpenVPN)
if configErr != nil {
- return "", &WireguardGetConfigError{Err: wireguardErr}
+ return "", "", &WireguardGetConfigError{Err: wireguardErr}
}
+ // FIXME: Store expiry
if content == "wireguard" {
- // FIXME: Store expiry
// This needs the go code a way to identify a connection
// Use the uuid of the connection e.g. on Linux
// This needs the client code to call the go code
@@ -60,7 +60,7 @@ func WireguardGetConfig(server Server, supportsOpenVPN bool) (string, error) {
config = wireguardConfigAddKey(config, wireguardKey)
}
- return config, nil
+ return config, content, nil
}
type WireguardGenerateKeyError struct {