| 1 |
package uaparser |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"regexp" |
| 7 |
"sort" |
| 8 |
"sync" |
| 9 |
"sync/atomic" |
| 10 |
"time" |
| 11 |
|
| 12 |
"gopkg.in/yaml.v2" |
| 13 |
) |
| 14 |
|
| 15 |
type RegexesDefinitions struct { |
| 16 |
UA []*uaParser `yaml:"user_agent_parsers"` |
| 17 |
OS []*osParser `yaml:"os_parsers"` |
| 18 |
Device []*deviceParser `yaml:"device_parsers"` |
| 19 |
_ [4]byte |
| 20 |
sync.RWMutex |
| 21 |
} |
| 22 |
|
| 23 |
type UserAgentSorter []*uaParser |
| 24 |
|
| 25 |
func (a UserAgentSorter) Len() int { return len(a) } |
| 26 |
func (a UserAgentSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 27 |
func (a UserAgentSorter) Less(i, j int) bool { |
| 28 |
return atomic.LoadUint64(&a[i].MatchesCount) > atomic.LoadUint64(&a[j].MatchesCount) |
| 29 |
} |
| 30 |
|
| 31 |
type uaParser struct { |
| 32 |
Reg *regexp.Regexp |
| 33 |
Expr string `yaml:"regex"` |
| 34 |
Flags string `yaml:"regex_flag"` |
| 35 |
FamilyReplacement string `yaml:"family_replacement"` |
| 36 |
V1Replacement string `yaml:"v1_replacement"` |
| 37 |
V2Replacement string `yaml:"v2_replacement"` |
| 38 |
V3Replacement string `yaml:"v3_replacement"` |
| 39 |
_ [4]byte |
| 40 |
MatchesCount uint64 |
| 41 |
} |
| 42 |
|
| 43 |
func (ua *uaParser) setDefaults() { |
| 44 |
if ua.FamilyReplacement == "" { |
| 45 |
ua.FamilyReplacement = "$1" |
| 46 |
} |
| 47 |
if ua.V1Replacement == "" { |
| 48 |
ua.V1Replacement = "$2" |
| 49 |
} |
| 50 |
if ua.V2Replacement == "" { |
| 51 |
ua.V2Replacement = "$3" |
| 52 |
} |
| 53 |
if ua.V3Replacement == "" { |
| 54 |
ua.V3Replacement = "$4" |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
type OsSorter []*osParser |
| 59 |
|
| 60 |
func (a OsSorter) Len() int { return len(a) } |
| 61 |
func (a OsSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 62 |
func (a OsSorter) Less(i, j int) bool { |
| 63 |
return atomic.LoadUint64(&a[i].MatchesCount) > atomic.LoadUint64(&a[j].MatchesCount) |
| 64 |
} |
| 65 |
|
| 66 |
type osParser struct { |
| 67 |
Reg *regexp.Regexp |
| 68 |
Expr string `yaml:"regex"` |
| 69 |
Flags string `yaml:"regex_flag"` |
| 70 |
OSReplacement string `yaml:"os_replacement"` |
| 71 |
V1Replacement string `yaml:"os_v1_replacement"` |
| 72 |
V2Replacement string `yaml:"os_v2_replacement"` |
| 73 |
V3Replacement string `yaml:"os_v3_replacement"` |
| 74 |
V4Replacement string `yaml:"os_v4_replacement"` |
| 75 |
_ [4]byte |
| 76 |
MatchesCount uint64 |
| 77 |
} |
| 78 |
|
| 79 |
func (os *osParser) setDefaults() { |
| 80 |
if os.OSReplacement == "" { |
| 81 |
os.OSReplacement = "$1" |
| 82 |
} |
| 83 |
if os.V1Replacement == "" { |
| 84 |
os.V1Replacement = "$2" |
| 85 |
} |
| 86 |
if os.V2Replacement == "" { |
| 87 |
os.V2Replacement = "$3" |
| 88 |
} |
| 89 |
if os.V3Replacement == "" { |
| 90 |
os.V3Replacement = "$4" |
| 91 |
} |
| 92 |
if os.V4Replacement == "" { |
| 93 |
os.V4Replacement = "$5" |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
type DeviceSorter []*deviceParser |
| 98 |
|
| 99 |
func (a DeviceSorter) Len() int { return len(a) } |
| 100 |
func (a DeviceSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 101 |
func (a DeviceSorter) Less(i, j int) bool { |
| 102 |
return atomic.LoadUint64(&a[i].MatchesCount) > atomic.LoadUint64(&a[j].MatchesCount) |
| 103 |
} |
| 104 |
|
| 105 |
type deviceParser struct { |
| 106 |
Reg *regexp.Regexp |
| 107 |
Expr string `yaml:"regex"` |
| 108 |
Flags string `yaml:"regex_flag"` |
| 109 |
DeviceReplacement string `yaml:"device_replacement"` |
| 110 |
BrandReplacement string `yaml:"brand_replacement"` |
| 111 |
ModelReplacement string `yaml:"model_replacement"` |
| 112 |
_ [4]byte |
| 113 |
MatchesCount uint64 |
| 114 |
} |
| 115 |
|
| 116 |
func (device *deviceParser) setDefaults() { |
| 117 |
if device.DeviceReplacement == "" { |
| 118 |
device.DeviceReplacement = "$1" |
| 119 |
} |
| 120 |
if device.ModelReplacement == "" { |
| 121 |
device.ModelReplacement = "$1" |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
type Client struct { |
| 126 |
UserAgent *UserAgent |
| 127 |
Os *Os |
| 128 |
Device *Device |
| 129 |
} |
| 130 |
|
| 131 |
type Parser struct { |
| 132 |
RegexesDefinitions |
| 133 |
UserAgentMisses uint64 |
| 134 |
OsMisses uint64 |
| 135 |
DeviceMisses uint64 |
| 136 |
Mode int |
| 137 |
UseSort bool |
| 138 |
debugMode bool |
| 139 |
} |
| 140 |
|
| 141 |
const ( |
| 142 |
EOsLookUpMode = 1 |
| 143 |
EUserAgentLookUpMode = 2 |
| 144 |
EDeviceLookUpMode = 4 |
| 145 |
cMinMissesTreshold = 100000 |
| 146 |
cDefaultMissesTreshold = 500000 |
| 147 |
cDefaultMatchIdxNotOk = 20 |
| 148 |
cDefaultSortOption = false |
| 149 |
) |
| 150 |
|
| 151 |
var ( |
| 152 |
missesTreshold = uint64(500000) |
| 153 |
matchIdxNotOk = 20 |
| 154 |
) |
| 155 |
|
| 156 |
func (parser *Parser) mustCompile() { |
| 157 |
for _, p := range parser.UA { |
| 158 |
p.Reg = compileRegex(p.Flags, p.Expr) |
| 159 |
p.setDefaults() |
| 160 |
} |
| 161 |
for _, p := range parser.OS { |
| 162 |
p.Reg = compileRegex(p.Flags, p.Expr) |
| 163 |
p.setDefaults() |
| 164 |
} |
| 165 |
for _, p := range parser.Device { |
| 166 |
p.Reg = compileRegex(p.Flags, p.Expr) |
| 167 |
p.setDefaults() |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func NewWithOptions(regexFile string, mode, treshold, topCnt int, useSort, debugMode bool) (*Parser, error) { |
| 172 |
data, err := ioutil.ReadFile(regexFile) |
| 173 |
if nil != err { |
| 174 |
return nil, err |
| 175 |
} |
| 176 |
if topCnt >= 0 { |
| 177 |
matchIdxNotOk = topCnt |
| 178 |
} |
| 179 |
if treshold > cMinMissesTreshold { |
| 180 |
missesTreshold = uint64(treshold) |
| 181 |
} |
| 182 |
parser, err := NewFromBytes(data) |
| 183 |
if err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
parser.Mode = mode |
| 187 |
parser.UseSort = useSort |
| 188 |
parser.debugMode = debugMode |
| 189 |
return parser, nil |
| 190 |
} |
| 191 |
|
| 192 |
func New(regexFile string) (*Parser, error) { |
| 193 |
data, err := ioutil.ReadFile(regexFile) |
| 194 |
if nil != err { |
| 195 |
return nil, err |
| 196 |
} |
| 197 |
matchIdxNotOk = cDefaultMatchIdxNotOk |
| 198 |
missesTreshold = cDefaultMissesTreshold |
| 199 |
parser, err := NewFromBytes(data) |
| 200 |
if err != nil { |
| 201 |
return nil, err |
| 202 |
} |
| 203 |
return parser, nil |
| 204 |
} |
| 205 |
|
| 206 |
func NewFromSaved() *Parser { |
| 207 |
parser, err := NewFromBytes(DefinitionYaml) |
| 208 |
if err != nil { |
| 209 |
|
| 210 |
|
| 211 |
panic(err.Error()) |
| 212 |
} |
| 213 |
return parser |
| 214 |
} |
| 215 |
|
| 216 |
func NewFromBytes(data []byte) (*Parser, error) { |
| 217 |
parser := &Parser{ |
| 218 |
Mode: EOsLookUpMode | EUserAgentLookUpMode | EDeviceLookUpMode, |
| 219 |
} |
| 220 |
if err := yaml.Unmarshal(data, &parser.RegexesDefinitions); err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
|
| 224 |
parser.mustCompile() |
| 225 |
|
| 226 |
return parser, nil |
| 227 |
} |
| 228 |
|
| 229 |
func (parser *Parser) Parse(line string) *Client { |
| 230 |
cli := new(Client) |
| 231 |
var wg sync.WaitGroup |
| 232 |
if EUserAgentLookUpMode&parser.Mode == EUserAgentLookUpMode { |
| 233 |
wg.Add(1) |
| 234 |
go func() { |
| 235 |
defer wg.Done() |
| 236 |
parser.RLock() |
| 237 |
cli.UserAgent = parser.ParseUserAgent(line) |
| 238 |
parser.RUnlock() |
| 239 |
}() |
| 240 |
} |
| 241 |
if EOsLookUpMode&parser.Mode == EOsLookUpMode { |
| 242 |
wg.Add(1) |
| 243 |
go func() { |
| 244 |
defer wg.Done() |
| 245 |
parser.RLock() |
| 246 |
cli.Os = parser.ParseOs(line) |
| 247 |
parser.RUnlock() |
| 248 |
}() |
| 249 |
} |
| 250 |
if EDeviceLookUpMode&parser.Mode == EDeviceLookUpMode { |
| 251 |
wg.Add(1) |
| 252 |
go func() { |
| 253 |
defer wg.Done() |
| 254 |
parser.RLock() |
| 255 |
cli.Device = parser.ParseDevice(line) |
| 256 |
parser.RUnlock() |
| 257 |
}() |
| 258 |
} |
| 259 |
wg.Wait() |
| 260 |
if parser.UseSort == true { |
| 261 |
checkAndSort(parser) |
| 262 |
} |
| 263 |
return cli |
| 264 |
} |
| 265 |
|
| 266 |
func (parser *Parser) ParseUserAgent(line string) *UserAgent { |
| 267 |
ua := new(UserAgent) |
| 268 |
foundIdx := -1 |
| 269 |
found := false |
| 270 |
for i, uaPattern := range parser.UA { |
| 271 |
uaPattern.Match(line, ua) |
| 272 |
if len(ua.Family) > 0 { |
| 273 |
found = true |
| 274 |
foundIdx = i |
| 275 |
atomic.AddUint64(&uaPattern.MatchesCount, 1) |
| 276 |
break |
| 277 |
} |
| 278 |
} |
| 279 |
if !found { |
| 280 |
ua.Family = "Other" |
| 281 |
} |
| 282 |
if foundIdx > matchIdxNotOk { |
| 283 |
atomic.AddUint64(&parser.UserAgentMisses, 1) |
| 284 |
} |
| 285 |
return ua |
| 286 |
} |
| 287 |
|
| 288 |
func (parser *Parser) ParseOs(line string) *Os { |
| 289 |
os := new(Os) |
| 290 |
foundIdx := -1 |
| 291 |
found := false |
| 292 |
for i, osPattern := range parser.OS { |
| 293 |
osPattern.Match(line, os) |
| 294 |
if len(os.Family) > 0 { |
| 295 |
found = true |
| 296 |
foundIdx = i |
| 297 |
atomic.AddUint64(&osPattern.MatchesCount, 1) |
| 298 |
break |
| 299 |
} |
| 300 |
} |
| 301 |
if !found { |
| 302 |
os.Family = "Other" |
| 303 |
} |
| 304 |
if foundIdx > matchIdxNotOk { |
| 305 |
atomic.AddUint64(&parser.OsMisses, 1) |
| 306 |
} |
| 307 |
return os |
| 308 |
} |
| 309 |
|
| 310 |
func (parser *Parser) ParseDevice(line string) *Device { |
| 311 |
dvc := new(Device) |
| 312 |
foundIdx := -1 |
| 313 |
found := false |
| 314 |
for i, dvcPattern := range parser.Device { |
| 315 |
dvcPattern.Match(line, dvc) |
| 316 |
if len(dvc.Family) > 0 { |
| 317 |
found = true |
| 318 |
foundIdx = i |
| 319 |
atomic.AddUint64(&dvcPattern.MatchesCount, 1) |
| 320 |
break |
| 321 |
} |
| 322 |
} |
| 323 |
if !found { |
| 324 |
dvc.Family = "Other" |
| 325 |
} |
| 326 |
if foundIdx > matchIdxNotOk { |
| 327 |
atomic.AddUint64(&parser.DeviceMisses, 1) |
| 328 |
} |
| 329 |
return dvc |
| 330 |
} |
| 331 |
|
| 332 |
func checkAndSort(parser *Parser) { |
| 333 |
parser.Lock() |
| 334 |
if atomic.LoadUint64(&parser.UserAgentMisses) >= missesTreshold { |
| 335 |
if parser.debugMode { |
| 336 |
fmt.Printf("%s\tSorting UserAgents slice\n", time.Now()) |
| 337 |
} |
| 338 |
parser.UserAgentMisses = 0 |
| 339 |
sort.Sort(UserAgentSorter(parser.UA)) |
| 340 |
} |
| 341 |
parser.Unlock() |
| 342 |
parser.Lock() |
| 343 |
if atomic.LoadUint64(&parser.OsMisses) >= missesTreshold { |
| 344 |
if parser.debugMode { |
| 345 |
fmt.Printf("%s\tSorting OS slice\n", time.Now()) |
| 346 |
} |
| 347 |
parser.OsMisses = 0 |
| 348 |
sort.Sort(OsSorter(parser.OS)) |
| 349 |
} |
| 350 |
parser.Unlock() |
| 351 |
parser.Lock() |
| 352 |
if atomic.LoadUint64(&parser.DeviceMisses) >= missesTreshold { |
| 353 |
if parser.debugMode { |
| 354 |
fmt.Printf("%s\tSorting Device slice\n", time.Now()) |
| 355 |
} |
| 356 |
parser.DeviceMisses = 0 |
| 357 |
sort.Sort(DeviceSorter(parser.Device)) |
| 358 |
} |
| 359 |
parser.Unlock() |
| 360 |
} |
| 361 |
|
| 362 |
func compileRegex(flags, expr string) *regexp.Regexp { |
| 363 |
if flags == "" { |
| 364 |
return regexp.MustCompile(expr) |
| 365 |
} else { |
| 366 |
return regexp.MustCompile(fmt.Sprintf("(?%s)%s", flags, expr)) |
| 367 |
} |
| 368 |
} |