feat(GODT-2575): Add dev info to cookies.

This commit is contained in:
Jakub
2023-04-20 15:24:16 +02:00
parent ce5a559926
commit fed503501d
5 changed files with 142 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -35,6 +36,7 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/crash" "github.com/ProtonMail/proton-bridge/v3/internal/crash"
"github.com/ProtonMail/proton-bridge/v3/internal/events" "github.com/ProtonMail/proton-bridge/v3/internal/events"
"github.com/ProtonMail/proton-bridge/v3/internal/focus" "github.com/ProtonMail/proton-bridge/v3/internal/focus"
"github.com/ProtonMail/proton-bridge/v3/internal/frontend/theme"
"github.com/ProtonMail/proton-bridge/v3/internal/locations" "github.com/ProtonMail/proton-bridge/v3/internal/locations"
"github.com/ProtonMail/proton-bridge/v3/internal/logging" "github.com/ProtonMail/proton-bridge/v3/internal/logging"
"github.com/ProtonMail/proton-bridge/v3/internal/sentry" "github.com/ProtonMail/proton-bridge/v3/internal/sentry"
@ -426,6 +428,10 @@ func withCookieJar(vault *vault.Vault, fn func(http.CookieJar) error) error {
return fmt.Errorf("could not create cookie jar: %w", err) return fmt.Errorf("could not create cookie jar: %w", err)
} }
if err := setDeviceCookies(persister); err != nil {
return fmt.Errorf("could not set device cookies: %w", err)
}
// Persist the cookies to the vault when we close. // Persist the cookies to the vault when we close.
defer func() { defer func() {
logrus.Debug("Persisting cookies") logrus.Debug("Persisting cookies")
@ -437,3 +443,21 @@ func withCookieJar(vault *vault.Vault, fn func(http.CookieJar) error) error {
return fn(persister) return fn(persister)
} }
func setDeviceCookies(jar *cookies.Jar) error {
url, err := url.Parse(constants.APIHost)
if err != nil {
return err
}
for name, value := range map[string]string{
"hhn": sentry.GetProtectedHostname(),
"tz": sentry.GetTimeZone(),
"lng": sentry.GetSystemLang(),
"clr": string(theme.DefaultTheme()),
} {
jar.SetCookies(url, []*http.Cookie{{Name: name, Value: value, Secure: true}})
}
return nil
}

View File

@ -20,6 +20,7 @@
package bridge package bridge
import ( import (
"crypto/tls"
"net/http" "net/http"
"os" "os"
@ -36,6 +37,14 @@ func newAPIOptions(
transport http.RoundTripper, transport http.RoundTripper,
panicHandler async.PanicHandler, panicHandler async.PanicHandler,
) []proton.Option { ) []proton.Option {
if allow := os.Getenv("BRIDGE_ALLOW_PROXY"); allow != "" {
transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
opt := defaultAPIOptions(apiURL, version, cookieJar, transport, panicHandler) opt := defaultAPIOptions(apiURL, version, cookieJar, transport, panicHandler)
if host := os.Getenv("BRIDGE_API_HOST"); host != "" { if host := os.Getenv("BRIDGE_API_HOST"); host != "" {

View File

@ -0,0 +1,32 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build !windows
// +build !windows
package sentry
import "os"
func GetSystemLang() string {
lang := os.Getenv("LC_ALL")
if lang == "" {
lang = os.Getenv("LANG")
}
return lang
}

View File

@ -0,0 +1,67 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build windows
// +build windows
package sentry
import (
"syscall"
"unsafe"
)
const (
defaultLocaleUser = "GetUserDefaultLocaleName" // https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getuserdefaultlocalename
defaultLocaleSystem = "GetSystemDefaultLocaleName" // https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getsystemdefaultlocalename
localeNameMaxLength = 85 // https://learn.microsoft.com/en-us/windows/win32/intl/locale-name-constants
)
func getLocale(dll *syscall.DLL, procName string) (string, error) {
proc, err := dll.FindProc(procName)
if err != nil {
return "errProc", err
}
b := make([]uint16, localeNameMaxLength)
r, _, err := proc.Call(uintptr(unsafe.Pointer(&b[0])), uintptr(localeNameMaxLength))
if r == 0 || err != nil {
return "errCall", err
}
return syscall.UTF16ToString(b), nil
}
func GetSystemLang() string {
dll, err := syscall.LoadDLL("kernel32")
if err != nil {
return "errDll"
}
defer func() {
_ = dll.Release()
}()
if lang, err := getLocale(dll, defaultLocaleUser); err == nil {
return lang
}
lang, _ := getLocale(dll, defaultLocaleSystem)
return lang
}

View File

@ -18,7 +18,6 @@
package sentry package sentry
import ( import (
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -29,6 +28,7 @@ import (
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gluon/reporter" "github.com/ProtonMail/gluon/reporter"
"github.com/ProtonMail/proton-bridge/v3/internal/constants" "github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/ProtonMail/proton-bridge/v3/pkg/algo"
"github.com/ProtonMail/proton-bridge/v3/pkg/restarter" "github.com/ProtonMail/proton-bridge/v3/pkg/restarter"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -50,7 +50,7 @@ func init() { //nolint:gochecknoinits
Release: constants.AppVersion(appVersion), Release: constants.AppVersion(appVersion),
BeforeSend: EnhanceSentryEvent, BeforeSend: EnhanceSentryEvent,
Transport: sentrySyncTransport, Transport: sentrySyncTransport,
ServerName: getProtectedHostname(), ServerName: GetProtectedHostname(),
Environment: constants.BuildEnv, Environment: constants.BuildEnv,
MaxBreadcrumbs: 50, MaxBreadcrumbs: 50,
} }
@ -61,7 +61,7 @@ func init() { //nolint:gochecknoinits
sentry.ConfigureScope(func(scope *sentry.Scope) { sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetFingerprint([]string{"{{ default }}"}) scope.SetFingerprint([]string{"{{ default }}"})
scope.SetUser(sentry.User{ID: getProtectedHostname()}) scope.SetUser(sentry.User{ID: GetProtectedHostname()})
}) })
sentry.Logger = log.New( sentry.Logger = log.New(
@ -81,12 +81,17 @@ type Identifier interface {
GetUserAgent() string GetUserAgent() string
} }
func getProtectedHostname() string { func GetProtectedHostname() string {
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
return "Unknown" return "Unknown"
} }
return fmt.Sprintf("%x", sha256.Sum256([]byte(hostname))) return algo.HashBase64SHA256(hostname)
}
func GetTimeZone() string {
zone, offset := time.Now().Zone()
return fmt.Sprintf("%s%+d", zone, offset/3600)
} }
// NewReporter creates new sentry reporter with appName and appVersion to report. // NewReporter creates new sentry reporter with appName and appVersion to report.