-
Notifications
You must be signed in to change notification settings - Fork 113
/
agentctx.go
109 lines (97 loc) · 3.16 KB
/
agentctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018-2024 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package appctx
import (
"context"
"strings"
ua "github.com/mileusna/useragent"
"google.golang.org/grpc/metadata"
)
// UserAgentHeader is the header used for the user agent.
const (
UserAgentHeader = "x-user-agent"
WebUserAgent = "web"
GrpcUserAgent = "grpc"
MobileUserAgent = "mobile"
DesktopUserAgent = "desktop"
)
// ContextGetUserAgent returns the user agent if set in the given context.
// see https://github.com/grpc/grpc-go/issues/1100
func ContextGetUserAgent(ctx context.Context) (*ua.UserAgent, bool) {
if userAgentStr, ok := ContextGetUserAgentString(ctx); ok {
userAgent := ua.Parse(userAgentStr)
return &userAgent, true
}
return nil, false
}
// ContextGetUserAgentString returns the user agent string if set in the given context.
func ContextGetUserAgentString(ctx context.Context) (string, bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return "", false
}
userAgentLst, ok := md[UserAgentHeader]
if !ok {
userAgentLst, ok = md["user-agent"]
if !ok {
return "", false
}
}
if len(userAgentLst) == 0 {
return "", false
}
return userAgentLst[0], true
}
// ContextGetUserAgentCategory returns the category of the user agent
// (i.e. if it is a web, mobile, desktop or grpc user agent).
func ContextGetUserAgentCategory(ctx context.Context) (string, bool) {
agent, ok := ContextGetUserAgent(ctx)
if !ok {
return "", false
}
switch {
case isWeb(agent):
return WebUserAgent, true
case isMobile(agent):
return MobileUserAgent, true
case isDesktop(agent):
return DesktopUserAgent, true
case isGRPC(agent):
return GrpcUserAgent, true
default:
return "", false
}
}
func isWeb(ua *ua.UserAgent) bool {
return ua.IsChrome() || ua.IsEdge() || ua.IsFirefox() || ua.IsSafari() ||
ua.IsInternetExplorer() || ua.IsOpera() || ua.IsOperaMini()
}
// isMobile returns true if the useragent is generated by the mobile.
func isMobile(ua *ua.UserAgent) bool {
// workaround as the library does not recognise iOS string inside the user agent
isIOS := ua.IsIOS() || strings.Contains(ua.String, "iOS")
return !isWeb(ua) && (ua.IsAndroid() || isIOS)
}
// isDesktop returns true if the useragent is generated by a desktop application.
func isDesktop(ua *ua.UserAgent) bool {
return ua.Desktop && !isWeb(ua)
}
// isGRPC returns true if the useragent is generated by a grpc client.
func isGRPC(ua *ua.UserAgent) bool {
return strings.HasPrefix(ua.Name, "grpc")
}