通用设备检测库将解析任何用户代理并检测浏览器、操作系统

508 阅读2分钟

设备检测器-go

GitHub issues GitHub stars GitHub license

Device-detector-go是一个用Golang编写的精确的用户代理解析器和设备检测器,由最大和最新的开源用户代理数据库和js库支持。

Device-detector-go将解析任何用户代理并检测浏览器、操作系统、使用的设备(桌面、平板、移动、电视、汽车、控制台等)、品牌和型号。在go和浏览器中工作。

This library uses [device-detector-js](https://github.com/etienne-martin/device-detector-js) and originial library maintainer says;
This library is heavily tested and relies on over 10,000 tests to detect thousands of user agent strings, even from rare and obscure browsers and devices.

这是对device-detector-js的go移植(在v8引擎中直接使用这个库,并导出逻辑),并且相对基于Matomo device-detector规则。

为什么要做这个项目

有太多的go用户代理分析器,但没有一个像Matomo device-detector那样有强大的规则,而且只有一个Matomo的golang端口(detector gamebtc/devicedetector),但这个端口的工作速度很慢。这就太过分了。这个项目的目的是使用nodejs repo的速度。而不是从头开始实现go版本。

入门

安装

要在你的项目中使用device-detector-go,请运行:

go get github.com/umutbasal/device-detector-go

使用方法

GO导入

import (
 device detector "github.com/umutbasal/device-detector-go"
)

例子- 用户代理检测:

import (
  devicedetector "github.com/umutbasal/device-detector-go"
)

func main() {
  userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
  detector, err := devicedetector.NewDeviceDetector(devicedetector.DeviceDetectorOptions{})
  if err != nil {
    panic(err)
  }
  result, err := detector.Parse(user)
  if err != nil {
    panic(err)
  }

  fmt.Printf("%s\n", result)

}

输出:

{
 "client": {
  "type": "browser",
  "name": "Chrome Mobile",
  "version": "41.0",
  "engine": "Blink",
  "engineVersion": ""
 },
 "os": {
  "name": "Android",
  "version": "6.0",
  "platform": ""
 },
 "device": {
  "type": "smartphone",
  "brand": "Google",
  "model": "Nexus 5X"
 },
 "bot": null
} 

例子--僵尸检测:

import (
 devicedetector "github.com/umutbasal/device-detector-go"
)

func main() {
 userAgent := "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36"
 detector, err := devicedetector.NewBotDetector(devicedetector.DeviceDetectorOptions{})
 if err != nil {
  panic(err)
 }
 result, err := detector.Parse(user)
 if err != nil {
  panic(err)
 }

 fmt.Printf("%s\n", result)
}

输出

{
 "name": "Googlebot",
 "category": "Search bot",
 "url": "http://www.google.com/bot.html",
 "producer": {
  "name": "Google Inc.",
  "url": "http://www.google.com"
 }
}
 

设备检测器-GO能够检测到的东西

在原始jsrepo中有一个列表