diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-23 16:41:15 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-03-23 16:41:15 +0100 |
| commit | f1e5096b7827d82ab5b2df10080a2ad9223f2665 (patch) | |
| tree | d9d2d05f11daab95abcda1721ea278652d09357b | |
| parent | b9b2659908d5fe8afcc74f2769a8da7bab243018 (diff) | |
Return headers in HTTP for wireguard expiry
| -rw-r--r-- | cli/main.go | 2 | ||||
| -rw-r--r-- | src/api.go | 22 | ||||
| -rw-r--r-- | src/discovery.go | 4 | ||||
| -rw-r--r-- | src/http.go | 20 | ||||
| -rw-r--r-- | src/oauth.go | 4 | ||||
| -rw-r--r-- | src/server.go | 2 | ||||
| -rw-r--r-- | src/wireguard.go | 7 |
7 files changed, 32 insertions, 29 deletions
diff --git a/cli/main.go b/cli/main.go index d5b51e8..b9c3bf8 100644 --- a/cli/main.go +++ b/cli/main.go @@ -62,7 +62,7 @@ func main() { if wireguardErr != nil { log.Fatal(wireguardErr) } - configString, configErr := state.APIConnectWireguard(wireguardKey.PublicKey().String()) + configString, configExpires, configErr := state.APIConnectWireguard(wireguardKey.PublicKey().String()) if configErr != nil { log.Fatal(configErr) } @@ -7,7 +7,7 @@ import ( ) // Authenticated wrappers on top of HTTP -func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, opts *HTTPOptionalParams) ([]byte, error) { +func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, opts *HTTPOptionalParams) (http.Header, []byte, error) { // Ensure optional is not nil as we will fill it with headers if opts == nil { opts = &HTTPOptionalParams{} @@ -18,7 +18,7 @@ func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, oauthErr := eduvpn.EnsureTokensOAuth() if oauthErr != nil { - return nil, oauthErr + return nil, nil, oauthErr } headerKey := "Authorization" @@ -28,14 +28,14 @@ func (eduvpn *VPNState) apiAuthenticatedWithOpts(method string, endpoint string, } else { opts.Headers = &http.Header{headerKey: {headerValue}} } - body, bodyErr := HTTPMethodWithOpts(method, url, opts) + header, body, bodyErr := HTTPMethodWithOpts(method, url, opts) if bodyErr != nil { - return nil, bodyErr + return header, nil, bodyErr } - return body, nil + return header, body, nil } -func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, error) { +func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, string, error) { headers := &http.Header{ "content-type": {"application/x-www-form-urlencoded"}, "accept": {"application/x-wireguard-profile"}, @@ -45,13 +45,11 @@ func (eduvpn *VPNState) APIConnectWireguard(pubkey string) (string, error) { "profile_id": {"default"}, "public_key": {pubkey}, } - body, bodyErr := eduvpn.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) + header, body, bodyErr := eduvpn.apiAuthenticatedWithOpts(http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if bodyErr != nil { - return "", bodyErr + return "", "", bodyErr } - return string(body), nil -} -func (eduvpn *VPNState) APIInfo() ([]byte, error) { - return eduvpn.apiAuthenticatedWithOpts(http.MethodGet, "/info", nil) + expires := header.Get("expires") + return string(body), expires, nil } diff --git a/src/discovery.go b/src/discovery.go index fa109c2..a788438 100644 --- a/src/discovery.go +++ b/src/discovery.go @@ -42,7 +42,7 @@ func getDiscoFile(jsonFile string) (string, error) { // Get json data discoURL := "https://disco.eduvpn.org/v2/" fileURL := discoURL + jsonFile - fileBody, fileErr := HTTPGet(fileURL) + _, fileBody, fileErr := HTTPGet(fileURL) if fileErr != nil { return "", &DiscoFileError{fileURL, fileErr} @@ -51,7 +51,7 @@ func getDiscoFile(jsonFile string) (string, error) { // Get signature sigFile := jsonFile + ".minisig" sigURL := discoURL + sigFile - sigBody, sigFileErr := HTTPGet(sigURL) + _, sigBody, sigFileErr := HTTPGet(sigURL) if sigFileErr != nil { return "", &DiscoSigFileError{URL: sigURL, Err: sigFileErr} diff --git a/src/http.go b/src/http.go index 5366c7e..8cb32b2 100644 --- a/src/http.go +++ b/src/http.go @@ -81,19 +81,19 @@ func HTTPConstructURL(baseURL string, parameters URLParameters) (string, error) } // Convenience functions -func HTTPGet(url string) ([]byte, error) { +func HTTPGet(url string) (http.Header, []byte, error) { return HTTPMethodWithOpts(http.MethodGet, url, nil) } -func HTTPPost(url string, body url.Values) ([]byte, error) { +func HTTPPost(url string, body url.Values) (http.Header, []byte, error) { return HTTPMethodWithOpts(http.MethodGet, url, &HTTPOptionalParams{Body: body}) } -func HTTPGetWithOpts(url string, opts *HTTPOptionalParams) ([]byte, error) { +func HTTPGetWithOpts(url string, opts *HTTPOptionalParams) (http.Header, []byte, error) { return HTTPMethodWithOpts(http.MethodGet, url, opts) } -func HTTPPostWithOpts(url string, opts *HTTPOptionalParams) ([]byte, error) { +func HTTPPostWithOpts(url string, opts *HTTPOptionalParams) (http.Header, []byte, error) { return HTTPMethodWithOpts(http.MethodPost, url, opts) } @@ -126,14 +126,14 @@ func httpOptionalBodyReader(opts *HTTPOptionalParams) io.Reader { return nil } -func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) ([]byte, error) { +func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) (http.Header, []byte, error) { // Make sure the url contains all the parameters // This can return an error, // it already has the right error so so we don't wrap it further url, urlErr := httpOptionalURL(url, opts) if urlErr != nil { - return nil, urlErr + return nil, nil, urlErr } // Create a client @@ -142,7 +142,7 @@ func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) ([] // Create request object with the body reader generated from the optional arguments req, reqErr := http.NewRequest(method, url, httpOptionalBodyReader(opts)) if reqErr != nil { - return nil, &HTTPRequestCreateError{URL: url, Err: reqErr} + return nil, nil, &HTTPRequestCreateError{URL: url, Err: reqErr} } // Make sure the headers contain all the parameters @@ -151,7 +151,7 @@ func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) ([] // Do request resp, respErr := client.Do(req) if respErr != nil { - return nil, &HTTPResourceError{URL: url, Err: respErr} + return nil, nil, &HTTPResourceError{URL: url, Err: respErr} } // Request successful, make sure body is closed at the end @@ -160,9 +160,9 @@ func HTTPMethodWithOpts(method string, url string, opts *HTTPOptionalParams) ([] // Return a string body, readErr := ioutil.ReadAll(resp.Body) if readErr != nil { - return nil, &HTTPReadError{URL: url, Err: readErr} + return resp.Header, nil, &HTTPReadError{URL: url, Err: readErr} } // Return the body in bytes and signal that there was no error - return body, nil + return resp.Header, body, nil } diff --git a/src/oauth.go b/src/oauth.go index eb93c00..f0d5b4a 100644 --- a/src/oauth.go +++ b/src/oauth.go @@ -123,7 +123,7 @@ func (oauth *OAuth) getTokensWithAuthCode(authCode string) error { "content-type": {"application/x-www-form-urlencoded"}} opts := &HTTPOptionalParams{Headers: headers, Body: data} current_time := generateTimeSeconds() - body, bodyErr := HTTPPostWithOpts(reqURL, opts) + _, body, bodyErr := HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return bodyErr } @@ -160,7 +160,7 @@ func (oauth *OAuth) getTokensWithRefresh() error { "content-type": {"application/x-www-form-urlencoded"}} opts := &HTTPOptionalParams{Headers: headers, Body: data} current_time := generateTimeSeconds() - body, bodyErr := HTTPPostWithOpts(reqURL, opts) + _, body, bodyErr := HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return bodyErr } diff --git a/src/server.go b/src/server.go index 0ef3965..8f8706e 100644 --- a/src/server.go +++ b/src/server.go @@ -41,7 +41,7 @@ func (server *Server) IsAuthenticated() bool { func (server *Server) GetEndpoints() error { url := server.BaseURL + "/.well-known/vpn-user-portal" - body, bodyErr := HTTPGet(url) + _, body, bodyErr := HTTPGet(url) if bodyErr != nil { return bodyErr diff --git a/src/wireguard.go b/src/wireguard.go index 0d5967c..5491764 100644 --- a/src/wireguard.go +++ b/src/wireguard.go @@ -33,12 +33,17 @@ func (eduvpn *VPNState) WireguardGetConfig() (string, error) { } wireguardPublicKey := wireguardKey.PublicKey().String() - configWireguard, configErr := eduvpn.APIConnectWireguard(wireguardPublicKey) + configWireguard, _, configErr := eduvpn.APIConnectWireguard(wireguardPublicKey) if configErr != nil { return "", configErr } + // 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 + configWireguardKey := wireguardConfigAddKey(configWireguard, wireguardKey) return configWireguardKey, nil |
