/// <summary> /// Parse the useragent string and/or ip address /// /// </summary> public void parse() { this.ipAddress = new IPAddress(); this.userAgent = new UserAgent(); dt.connect(this); if (dt.Connected) { if (this.ua != "") { this.parseUA(this.ua.Replace("'", "''")); this.ua = ""; } if (this.ip != "") { this.parseIP(this.ip.Replace("'", "''")); this.ip = ""; } } }
/// <summary> /// Check if useragent string and/or IP address is bot /// </summary> /// <param name="_useragent">user agent string</param> /// <param name="_ip">IP address v4 or v6</param> /// <returns>Dictionary</returns> public Dictionary <string, object> isBot(string _useragent = "", string _ip = "") { this.WriteDebug("isBot: start"); Dictionary <string, object> ret = new Dictionary <string, object>();; if (_useragent == "" && _ip == "") { this.WriteDebug("isBot: Missing mandatory parameter"); ret.Add("flag", 1); ret.Add("errortext", "missing mandatory parameter"); return(ret); } if (_ip != "" && !this.IsValidIp(_ip)) { this.WriteDebug("isBot: IP address is not valid"); ret.Add("flag", 2); ret.Add("errortext", "ip address is not valid"); return(ret); } dt.connect(this); if (!dt.Connected) { this.WriteDebug("Data file not found, download the data manually"); ret.Add("flag", 3); ret.Add("errortext", "data file not found"); return(ret); } bool botInfo = false; bool botInfoUA = false; bool botInfoIP = false; bool harmony = false; string botName = ""; string family = ""; string botURL = ""; if (_useragent != "") { this.WriteDebug("isBot: test useragent"); DataTable table1 = dt.selectQuery("SELECT name,family FROM c_robots where md5='" + this.CreateMD5(_useragent) + "'"); if (table1.Rows.Count > 0) { DataRow rowusr = table1.Rows[0]; botInfo = true; botInfoUA = true; botName = rowusr["name"].ToString(); family = rowusr["family"].ToString(); botURL = "http://udger.com/resources/ua-list/bot-detail?bot=" + family; } } if (_ip != "") { this.WriteDebug("isBot: test IP address"); DataTable table2 = dt.selectQuery("SELECT name,family from c_robots AS C JOIN bot_ip as B ON C.id=B.robot and B.md5='" + this.CreateMD5(_ip) + "' "); if (table2.Rows.Count > 0) { DataRow row = table2.Rows[0]; botInfo = true; botInfoIP = true; if (family == row["family"].ToString()) { harmony = true; } botName = row["name"].ToString(); botURL = "http://udger.com/resources/ua-list/bot-detail?bot=" + row["family"].ToString(); } } this.WriteDebug("isBot: completed"); ret.Add("flag", 0); ret.Add("is_bot", botInfo); ret.Add("bot_by_ua", botInfoUA); ret.Add("bot_by_ip", botInfoIP); ret.Add("harmony_ua_ip", harmony); ret.Add("bot_name", botName); ret.Add("bot_udger_url", botURL); return(ret); }