-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdevice_detection.go
More file actions
192 lines (164 loc) · 4.65 KB
/
device_detection.go
File metadata and controls
192 lines (164 loc) · 4.65 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"net/http"
"regexp"
"strings"
)
// DeviceCapabilities represents the streaming capabilities of a device
type DeviceCapabilities struct {
SupportsHLS bool
IsMobile bool
IsIOS bool
IsAndroid bool
UserAgent string
PreferredCodec string
}
// iOS user agent patterns for detection - prioritizing User Agent string
var iosPatterns = []*regexp.Regexp{
// Primary iOS device patterns (prioritized)
regexp.MustCompile(`(?i)iphone`),
regexp.MustCompile(`(?i)ipad`),
regexp.MustCompile(`(?i)ipod`),
// macOS Safari patterns - also supports HLS natively
regexp.MustCompile(`(?i)macintosh.*safari.*version`),
regexp.MustCompile(`(?i)mac os x.*safari.*version`),
}
// Android user agent patterns
var androidPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)android`),
}
// Mobile device patterns
var mobilePatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)mobile`),
regexp.MustCompile(`(?i)tablet`),
regexp.MustCompile(`(?i)iphone`),
regexp.MustCompile(`(?i)ipad`),
regexp.MustCompile(`(?i)ipod`),
regexp.MustCompile(`(?i)android`),
regexp.MustCompile(`(?i)blackberry`),
regexp.MustCompile(`(?i)windows phone`),
}
// DetectDeviceCapabilities analyzes the HTTP request to determine device capabilities
// Prioritizes User Agent string detection as requested
func DetectDeviceCapabilities(r *http.Request) DeviceCapabilities {
if r == nil {
return DeviceCapabilities{
SupportsHLS: false,
IsMobile: false,
IsIOS: false,
IsAndroid: false,
UserAgent: "",
PreferredCodec: "flv",
}
}
userAgent := r.Header.Get("User-Agent")
capabilities := DeviceCapabilities{
UserAgent: userAgent,
}
// Primary detection via User Agent string
// Detect iOS devices first
for _, pattern := range iosPatterns {
if pattern.MatchString(userAgent) {
capabilities.IsIOS = true
// iOS devices have native HLS support
capabilities.SupportsHLS = true
capabilities.PreferredCodec = "hls"
break
}
}
// Detect Android devices
if !capabilities.IsIOS { // Only check if not already detected as iOS
for _, pattern := range androidPatterns {
if pattern.MatchString(userAgent) {
capabilities.IsAndroid = true
// Android devices may support HLS via hls.js
capabilities.SupportsHLS = true
capabilities.PreferredCodec = "hls" // Prefer HLS for mobile
break
}
}
}
// Set defaults for Desktop or unknown devices
if !capabilities.IsIOS && !capabilities.IsAndroid {
// Desktop browsers - prefer FLV
capabilities.SupportsHLS = true // via hls.js
capabilities.PreferredCodec = "flv"
}
// Detect mobile devices -- we'll adjust quality settings based on this
for _, pattern := range mobilePatterns {
if pattern.MatchString(userAgent) {
capabilities.IsMobile = true
break
}
}
return capabilities
}
// GetAcceptHeader returns the appropriate Accept header for the streaming format
func GetAcceptHeader(format string) string {
switch format {
case "hls":
return "application/vnd.apple.mpegurl"
default:
return "video/x-flv"
}
}
// IsHLSPlaylistRequest checks if the request is for an HLS playlist
func IsHLSPlaylistRequest(r *http.Request) bool {
if r == nil {
return false
}
path := strings.ToLower(r.URL.Path)
// Check if it's explicitly an m3u8 file
if strings.HasSuffix(path, ".m3u8") {
return true
}
// Check if it contains playlist in the path
if strings.Contains(path, "playlist") {
return true
}
// Check Accept header for HLS content type
if r.Header.Get("Accept") == "application/vnd.apple.mpegurl" {
return true
}
// Check if format=hls parameter is present (for /live?format=hls)
if r.URL.Query().Get("format") == "hls" {
return true
}
return false
}
// IsHLSSegmentRequest checks if the request is for an HLS segment
func IsHLSSegmentRequest(r *http.Request) bool {
if r == nil {
return false
}
path := strings.ToLower(r.URL.Path)
return strings.HasSuffix(path, ".ts") && strings.Contains(path, "segment")
}
// GetContentTypeForFormat returns the appropriate Content-Type header for the format
func GetContentTypeForFormat(format string) string {
switch format {
case "hls":
return "application/vnd.apple.mpegurl"
case "ts":
return "video/mp2t"
default:
return "video/x-flv"
}
}
// ValidateUserAgent performs basic validation on the User-Agent string
func ValidateUserAgent(userAgent string) bool {
if userAgent == "" {
return false
}
// Basic validation - check for reasonable length and common patterns
if len(userAgent) > 1000 {
return false
}
lowerUA := strings.ToLower(userAgent)
for _, pattern := range settings.UABotPatterns {
if strings.Contains(lowerUA, pattern) {
return false
}
}
return true
}