-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
216 lines (188 loc) · 6.47 KB
/
types.go
File metadata and controls
216 lines (188 loc) · 6.47 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gogobot
import (
"context"
"fmt"
"net/http"
)
// State represents the source collection state
type State int
const (
StateSuccess State = iota
StateUndefined
StateNotFunction
StateUnexpectedBehaviour
StateNull
)
// BotKind represents different types of detected bots
type BotKind string
const (
BotKindAwesomium BotKind = "awesomium"
BotKindCef BotKind = "cef"
BotKindCefSharp BotKind = "cefsharp"
BotKindCoachJS BotKind = "coachjs"
BotKindElectron BotKind = "electron"
BotKindFMiner BotKind = "fminer"
BotKindGeb BotKind = "geb"
BotKindNightmareJS BotKind = "nightmarejs"
BotKindPhantomas BotKind = "phantomas"
BotKindPhantomJS BotKind = "phantomjs"
BotKindRhino BotKind = "rhino"
BotKindSelenium BotKind = "selenium"
BotKindSequentum BotKind = "sequentum"
BotKindSlimerJS BotKind = "slimerjs"
BotKindWebDriverIO BotKind = "webdriverio"
BotKindWebDriver BotKind = "webdriver"
BotKindHeadlessChrome BotKind = "headless_chrome"
BotKindPlaywright BotKind = "playwright"
BotKindPuppeteer BotKind = "puppeteer"
BotKindCurl BotKind = "curl"
BotKindWget BotKind = "wget"
BotKindBot BotKind = "bot"
BotKindCrawler BotKind = "crawler"
BotKindSpider BotKind = "spider"
BotKindGPTBot BotKind = "gptbot"
BotKindChatGPT BotKind = "chatgpt"
BotKindOpenAI BotKind = "openai"
BotKindClaude BotKind = "claude"
BotKindAIAgent BotKind = "ai_agent"
BotKindUnknown BotKind = "unknown"
)
// BotDetectionResult represents the result of bot detection
type BotDetectionResult struct {
Bot bool `json:"bot"`
BotKind BotKind `json:"botKind,omitempty"`
}
// BrowserName represents different browser types
type BrowserName string
const (
BrowserChrome BrowserName = "Chrome"
BrowserFirefox BrowserName = "Firefox"
BrowserSafari BrowserName = "Safari"
BrowserEdge BrowserName = "Edge"
BrowserIE BrowserName = "IE"
BrowserOpera BrowserName = "Opera"
BrowserSamsung BrowserName = "Samsung"
BrowserUCBrowser BrowserName = "UCBrowser"
BrowserYandex BrowserName = "Yandex"
BrowserVivaldi BrowserName = "Vivaldi"
BrowserBrave BrowserName = "Brave"
BrowserUnknown BrowserName = "Unknown"
)
// BrowserInfo represents parsed browser information
type BrowserInfo struct {
Name BrowserName `json:"name"`
Version string `json:"version"`
BotKind BotKind `json:"botKind,omitempty"`
RawUA string `json:"rawUserAgent,omitempty"`
}
// IsAIAgent returns true if the browser is detected as an AI agent
func (b *BrowserInfo) IsAIAgent() bool {
switch b.BotKind {
case BotKindGPTBot, BotKindChatGPT, BotKindOpenAI, BotKindClaude, BotKindAIAgent:
return true
default:
return false
}
}
// IsBot returns true if the browser is detected as a bot
func (b *BrowserInfo) IsBot() bool {
return b.BotKind != "" && b.BotKind != BotKindUnknown
}
// Component represents a data component with state and value
type Component[T any] interface {
GetState() State
GetValue() T
GetError() string
}
// SuccessComponent represents a successful component
type SuccessComponent[T any] struct {
State State
Value T
}
func (c SuccessComponent[T]) GetState() State { return c.State }
func (c SuccessComponent[T]) GetValue() T { return c.Value }
func (c SuccessComponent[T]) GetError() string { return "" }
// ErrorComponent represents a failed component
type ErrorComponent[T any] struct {
State State
Error string
}
func (c ErrorComponent[T]) GetState() State { return c.State }
func (c ErrorComponent[T]) GetValue() T { var zero T; return zero }
func (c ErrorComponent[T]) GetError() string { return c.Error }
// ComponentDict holds all collected components
type ComponentDict struct {
UserAgent Component[string]
XForwardedFor Component[string]
XRealIP Component[string]
AcceptLanguage Component[string]
AcceptEncoding Component[string]
AcceptCharset Component[string]
Accept Component[string]
Connection Component[string]
CacheControl Component[string]
UpgradeInsecure Component[bool]
DNT Component[string]
Headers Component[map[string][]string]
ContentLength Component[int64]
RequestMethod Component[string]
RequestPath Component[string]
RequestQuery Component[string]
RemoteAddr Component[string]
HeaderOrder Component[[]string]
HeaderCount Component[int]
MissingCommonHeaders Component[[]string]
}
// DetectionDict holds detection results for each detector
type DetectionDict struct {
UserAgent BotDetectionResult
Headers BotDetectionResult
HeaderOrder BotDetectionResult
HeaderCount BotDetectionResult
MissingHeaders BotDetectionResult
AcceptHeaders BotDetectionResult
Connection BotDetectionResult
ContentLength BotDetectionResult
}
// BotDetectorInterface defines the interface for bot detectors
type BotDetectorInterface interface {
Detect() BotDetectionResult
Collect(*http.Request) (*ComponentDict, error)
GetComponents() *ComponentDict
GetDetections() *DetectionDict
}
// BotdError represents errors during bot detection
type BotdError struct {
State State
Message string
}
func (e BotdError) Error() string {
return fmt.Sprintf("BotdError (state: %d): %s", e.State, e.Message)
}
// NewBotdError creates a new BotdError
func NewBotdError(state State, message string) *BotdError {
return &BotdError{
State: state,
Message: message,
}
}
// DetectorFunc is a function that performs bot detection on components
type DetectorFunc func(*ComponentDict) *BotDetectionResult
// SourceFunc is a function that collects data from an HTTP request
type SourceFunc[T any] func(*http.Request) Component[T]
// Context keys for storing detection results
type contextKey string
const (
DetectionResultKey contextKey = "gogobot_detection_result"
ComponentsKey contextKey = "gogobot_components"
)
// GetResultFromContext retrieves the detection result from request context
func GetResultFromContext(ctx context.Context) (*BotDetectionResult, bool) {
result, ok := ctx.Value(DetectionResultKey).(*BotDetectionResult)
return result, ok
}
// GetComponentsFromContext retrieves the components from request context
func GetComponentsFromContext(ctx context.Context) (*ComponentDict, bool) {
components, ok := ctx.Value(ComponentsKey).(*ComponentDict)
return components, ok
}