diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-17 14:00:24 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-17 14:00:24 +0200 |
| commit | 6dc7b64f634f6dcbeedea24c741382366a3c7b8c (patch) | |
| tree | 0b7bfc18de40e48f6b6fd9d349915a15fd45f9a3 /internal | |
| parent | 50ca9ce15aaaeefc564da38c88bba82e73d1e570 (diff) | |
API: Parse expiry
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api.go | 22 | ||||
| -rw-r--r-- | internal/openvpn.go | 6 | ||||
| -rw-r--r-- | internal/server.go | 2 | ||||
| -rw-r--r-- | internal/wireguard.go | 8 |
4 files changed, 28 insertions, 10 deletions
diff --git a/internal/api.go b/internal/api.go index 45e025b..5c2cf6d 100644 --- a/internal/api.go +++ b/internal/api.go @@ -96,7 +96,7 @@ func APIInfo(server Server) error { return nil } -func APIConnectWireguard(server Server, profile_id string, pubkey string, supportsOpenVPN bool) (string, string, string, error) { +func APIConnectWireguard(server Server, profile_id string, pubkey string, supportsOpenVPN bool) (string, string, int64, error) { headers := http.Header{ "content-type": {"application/x-www-form-urlencoded"}, "accept": {"application/x-wireguard-profile"}, @@ -112,20 +112,26 @@ func APIConnectWireguard(server Server, profile_id string, pubkey string, suppor } header, connectBody, connectErr := apiAuthorizedRetry(server, http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { - return "", "", "", &APIConnectWireguardError{Err: connectErr} + return "", "", 0, &APIConnectWireguardError{Err: connectErr} } expires := header.Get("expires") + + pTime, pTimeErr := http.ParseTime(expires) + if pTimeErr != nil { + return "", "", 0, &APIConnectWireguardError{Err: pTimeErr} + } + contentType := header.Get("content-type") content := "openvpn" if contentType == "application/x-wireguard-profile" { content = "wireguard" } - return string(connectBody), content, expires, nil + return string(connectBody), content, pTime.Unix(), nil } -func APIConnectOpenVPN(server Server, profile_id string) (string, string, error) { +func APIConnectOpenVPN(server Server, profile_id string) (string, int64, error) { headers := http.Header{ "content-type": {"application/x-www-form-urlencoded"}, "accept": {"application/x-openvpn-profile"}, @@ -137,11 +143,15 @@ func APIConnectOpenVPN(server Server, profile_id string) (string, string, error) header, connectBody, connectErr := apiAuthorizedRetry(server, http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { - return "", "", &APIConnectOpenVPNError{Err: connectErr} + return "", 0, &APIConnectOpenVPNError{Err: connectErr} } expires := header.Get("expires") - return string(connectBody), expires, nil + pTime, pTimeErr := http.ParseTime(expires) + if pTimeErr != nil { + return "", 0, &APIConnectOpenVPNError{Err: pTimeErr} + } + return string(connectBody), pTime.Unix(), nil } // This needs no further return value as it's best effort diff --git a/internal/openvpn.go b/internal/openvpn.go index 7c4df18..8f684ba 100644 --- a/internal/openvpn.go +++ b/internal/openvpn.go @@ -9,7 +9,11 @@ func OpenVPNGetConfig(server Server) (string, string, error) { return "", "", &OpenVPNGetConfigError{Err: baseErr} } profile_id := base.Profiles.Current - configOpenVPN, _, configErr := APIConnectOpenVPN(server, profile_id) + configOpenVPN, expires, configErr := APIConnectOpenVPN(server, profile_id) + + // Store start and end time + base.StartTime = GenerateTimeSeconds() + base.EndTime = expires if configErr != nil { return "", "", &OpenVPNGetConfigError{Err: configErr} diff --git a/internal/server.go b/internal/server.go index 95140af..d1fc433 100644 --- a/internal/server.go +++ b/internal/server.go @@ -13,6 +13,8 @@ type ServerBase struct { ProfilesRaw string `json:"profiles_raw"` Logger *FileLogger `json:"-"` FSM *FSM `json:"-"` + StartTime int64 `json:"start-time"` + EndTime int64 `json:"end-time"` } // An instute access server diff --git a/internal/wireguard.go b/internal/wireguard.go index 6edad08..00c9467 100644 --- a/internal/wireguard.go +++ b/internal/wireguard.go @@ -3,7 +3,6 @@ package internal import ( "fmt" "regexp" - "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -45,13 +44,16 @@ func WireguardGetConfig(server Server, supportsOpenVPN bool) (string, string, er } wireguardPublicKey := wireguardKey.PublicKey().String() - config, content, _, configErr := APIConnectWireguard(server, profile_id, wireguardPublicKey, supportsOpenVPN) + config, content, expires, configErr := APIConnectWireguard(server, profile_id, wireguardPublicKey, supportsOpenVPN) if configErr != nil { return "", "", &WireguardGetConfigError{Err: wireguardErr} } - // FIXME: Store expiry + // Store start and end time + base.StartTime = GenerateTimeSeconds() + base.EndTime = expires + if content == "wireguard" { // This needs the go code a way to identify a connection // Use the uuid of the connection e.g. on Linux |
