From 9e3e7f22892c3504e6de9827af0fabd9b4b098ea Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Tue, 10 May 2022 11:53:08 +0200 Subject: API/Server: Correctly handle multiple protocol preference --- internal/api.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'internal/api.go') diff --git a/internal/api.go b/internal/api.go index b615976..45e025b 100644 --- a/internal/api.go +++ b/internal/api.go @@ -96,23 +96,33 @@ func APIInfo(server Server) error { return nil } -func APIConnectWireguard(server Server, profile_id string, pubkey string) (string, string, error) { +func APIConnectWireguard(server Server, profile_id string, pubkey string, supportsOpenVPN bool) (string, string, string, error) { headers := http.Header{ "content-type": {"application/x-www-form-urlencoded"}, "accept": {"application/x-wireguard-profile"}, } + if supportsOpenVPN { + headers.Add("accept", "application/x-openvpn-profile") + } + urlForm := url.Values{ "profile_id": {profile_id}, "public_key": {pubkey}, } header, connectBody, connectErr := apiAuthorizedRetry(server, http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { - return "", "", &APIConnectWireguardError{Err: connectErr} + return "", "", "", &APIConnectWireguardError{Err: connectErr} } expires := header.Get("expires") - return string(connectBody), expires, nil + contentType := header.Get("content-type") + + content := "openvpn" + if contentType == "application/x-wireguard-profile" { + content = "wireguard" + } + return string(connectBody), content, expires, nil } func APIConnectOpenVPN(server Server, profile_id string) (string, string, error) { @@ -124,6 +134,7 @@ func APIConnectOpenVPN(server Server, profile_id string) (string, string, error) urlForm := url.Values{ "profile_id": {profile_id}, } + header, connectBody, connectErr := apiAuthorizedRetry(server, http.MethodPost, "/connect", &HTTPOptionalParams{Headers: headers, Body: urlForm}) if connectErr != nil { return "", "", &APIConnectOpenVPNError{Err: connectErr} -- cgit v1.2.3