diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-03-01 00:34:35 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-03-01 00:34:35 +0100 |
| commit | a1519ff7685ac987f9d70b1fb49bf777028d49b0 (patch) | |
| tree | eca17170c17bb296320297e79d355a181be55866 | |
| parent | 48b669b8b37b18f6641a96d4b0986b5f1b9fef15 (diff) | |
Client + Exports + HTTP: Set a user-agent using the client's version
| -rw-r--r-- | client/client.go | 36 | ||||
| -rw-r--r-- | exports/exports.go | 3 | ||||
| -rw-r--r-- | internal/http/http.go | 12 |
3 files changed, 51 insertions, 0 deletions
diff --git a/client/client.go b/client/client.go index 6e25694..3c78100 100644 --- a/client/client.go +++ b/client/client.go @@ -9,6 +9,7 @@ import ( "github.com/eduvpn/eduvpn-common/internal/discovery" "github.com/eduvpn/eduvpn-common/internal/failover" "github.com/eduvpn/eduvpn-common/internal/fsm" + "github.com/eduvpn/eduvpn-common/internal/http" "github.com/eduvpn/eduvpn-common/internal/log" "github.com/eduvpn/eduvpn-common/internal/server" "github.com/eduvpn/eduvpn-common/internal/util" @@ -60,6 +61,34 @@ func isAllowedClientID(clientID string) bool { return false } +func userAgentName(clientID string) string { + switch clientID { + case "org.eduvpn.app.windows": + return "eduVPN Windows" + case "org.eduvpn.app.android": + return "eduVPN Android" + case "org.eduvpn.app.ios": + return "eduVPN iOS" + case "org.eduvpn.app.macos": + return "eduVPN MacOS" + case "org.eduvpn.app.linux": + return "eduVPN Linux" + case "org.letsconnect-vpn.app.windows": + return "Let's Connect! Windows" + case "org.letsconnect-vpn.app.android": + return "Let's Connect! Android" + case "org.letsconnect-vpn.app.ios": + return "Let's Connect! iOS" + case "org.letsconnect-vpn.app.macos": + return "Let's Connect! MacOS" + case "org.letsconnect-vpn.app.linux": + return "Let's Connect! Linux" + default: + return "unknown" + } + +} + // Client is the main struct for the VPN client. type Client struct { // The name of the client @@ -99,6 +128,7 @@ type Client struct { // It returns an error if initialization failed, for example when discovery cannot be obtained and when there are no servers. func (c *Client) Register( name string, + version string, directory string, language string, stateCallback func(FSMStateID, FSMStateID, interface{}) bool, @@ -118,6 +148,12 @@ func (c *Client) Register( return errors.Errorf("client ID is not allowed: '%v', see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php for a list of allowed IDs", name) } + if len([]rune(version)) > 10 { + return errors.Errorf("version is not allowed: '%s', must be max 10 characters", version) + } + + http.RegisterAgent(userAgentName(name), version) + c.Name = name // TODO: Verify language setting? diff --git a/exports/exports.go b/exports/exports.go index 6e50f65..d9ec122 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -108,12 +108,14 @@ func GetVPNState(name string) (*client.Client, error) { //export Register func Register( name *C.char, + version *C.char, configDirectory *C.char, language *C.char, stateCallback C.PythonCB, debug C.int, ) *C.error { nameStr := C.GoString(name) + versionStr := C.GoString(version) state, stateErr := GetVPNState(nameStr) if stateErr != nil { state = &client.Client{} @@ -128,6 +130,7 @@ func Register( PStateCallbacks[nameStr] = stateCallback registerErr := state.Register( nameStr, + versionStr, C.GoString(configDirectory), C.GoString(language), func(old client.FSMStateID, new client.FSMStateID, data interface{}) bool { diff --git a/internal/http/http.go b/internal/http/http.go index 995b36f..766a2db 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -14,6 +14,9 @@ import ( "github.com/go-errors/errors" ) +// UserAgent is the user agent that is used for requests +var UserAgent string + // URLParameters is a type used for the parameters in the URL. type URLParameters map[string]string @@ -188,6 +191,9 @@ func (c *Client) Do(method string, urlStr string, opts *OptionalParams) (http.He return nil, nil, errors.WrapPrefix(err, fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0) } + if UserAgent != "" { + req.Header.Add("User-Agent", UserAgent) + } // Make sure the headers contain all the parameters optionalHeaders(req, opts) @@ -239,3 +245,9 @@ func (e *StatusError) Error() string { e.Body, ) } + +// RegisterAgent registers the user agent for client and version +func RegisterAgent(client string, version string) { + UserAgent = fmt.Sprintf("%s/%s %s", client, version, "eduvpn-common/1.0.0") + +} |
