instances.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 CYBERDEViL
  4. #
  5. # This file is part of Searx-Qt.
  6. #
  7. # Searx-Qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-Qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. import time
  22. from searxqt.core.handler import HandlerProto, NetworkTypes
  23. from searxqt.core.instanceVersions import parseVersionString
  24. from searxqt.core.requests import JsonResult, ErrorType
  25. from searxqt.core import jsonVerify
  26. class SearchEngine:
  27. def __init__(self, name, data):
  28. """Model for a search engine data.
  29. @param name: Name of the search engine
  30. @type name: str
  31. @param data: Data of the search engine
  32. @type data: dict
  33. """
  34. self._name = name
  35. self._data = data
  36. def __repr__(self): return self._name
  37. @property
  38. def name(self):
  39. """
  40. @return: returns the name of this search engine.
  41. @rtype: str
  42. """
  43. return self._name
  44. class TLS:
  45. def __init__(self, data):
  46. """Model for a instance it's TLS data.
  47. @param data: dict with the instance it's TLS data.
  48. @type data: dict
  49. """
  50. self._data = data
  51. @property
  52. def data(self): return self._data
  53. @property
  54. def version(self):
  55. """Returns the TLS version used.
  56. @return:
  57. @rtype: str
  58. """
  59. return self.data.get('version', "")
  60. @property
  61. def certificate(self):
  62. """Returns a TLSCertificate object.
  63. @return:
  64. @rtype: TLSCertificate
  65. """
  66. return TLSCertificate(self.data.get('certificate', {}))
  67. class TLSCertificate:
  68. def __init__(self, data):
  69. """Model for a instance it's TLS certificate-data.
  70. @param data: dict with the instance it's TLS certificate-data.
  71. @type data: dict
  72. """
  73. self._data = data
  74. @property
  75. def data(self): return self._data
  76. @property
  77. def version(self):
  78. """
  79. @return:
  80. @rtype: int
  81. """
  82. return self.data.get('version', 0)
  83. @property
  84. def issuer(self):
  85. """
  86. @return:
  87. @rtype: TLSCertificateIssuer
  88. """
  89. return TLSCertificateIssuer(self.data.get('issuer', {}))
  90. class TLSCertificateIssuer:
  91. def __init__(self, data):
  92. """Model for a instance it's TLS certificate-issuer-data.
  93. @param data: dict with the instance it's
  94. TLS certificate-issuer-data.
  95. @type data: dict
  96. """
  97. self._data = data
  98. @property
  99. def data(self): return self._data
  100. @property
  101. def commonName(self):
  102. """
  103. @rtype: str
  104. """
  105. return self.data.get('commonName', "")
  106. @property
  107. def countryName(self):
  108. """
  109. @rtype: str
  110. """
  111. return self.data.get('countryName', "")
  112. @property
  113. def organizationName(self):
  114. """
  115. @rtype: str
  116. """
  117. return self.data.get('organizationName', "")
  118. class Network:
  119. def __init__(self, data):
  120. """Model for a instance it's network data.
  121. @param data: dict with the instance it's network data.
  122. @type data: dict
  123. """
  124. self._data = data
  125. @property
  126. def data(self): return self._data
  127. @property
  128. def ipv6(self):
  129. """
  130. @return: If ipv6 is enabled on this instance.
  131. @rtype: bool
  132. """
  133. return self.data.get('ipv6', False)
  134. @property
  135. def ips(self):
  136. """
  137. @return: A list with NetworkIP objects
  138. @rtype: list
  139. """
  140. return [NetworkIP(ip, data)
  141. for ip, data in self.data.get('ips', {}).items()]
  142. @property
  143. def asnPrivacy(self):
  144. """
  145. @return: -1 no asn privacy, 0 asn privacy, -2 unknown.
  146. @rtype: int
  147. """
  148. return self.data.get('asn_privacy', -2)
  149. class NetworkIP:
  150. def __init__(self, ip, data):
  151. """Model for a network ip data.
  152. @param ip: ip address
  153. @type ip: str
  154. @param data: dict with a network ip data.
  155. @type data: dict
  156. """
  157. self._ip = ip
  158. self._data = data
  159. def __repr__(self):
  160. return (
  161. "IP: {0} Reverse: {1} FieldType: {2} asnCidr: {3}"
  162. .format(self.ip, self.reverse, self.fieldType, self.asnCidr)
  163. )
  164. def __str__(self): return repr(self)
  165. @property
  166. def ip(self):
  167. """
  168. @return: ip address
  169. @rtype: str
  170. """
  171. return self._ip
  172. @property
  173. def data(self): return self._data
  174. @property
  175. def reverse(self):
  176. """
  177. @return:
  178. @rtype: str
  179. """
  180. return self.data.get('reverse', None)
  181. @property
  182. def fieldType(self):
  183. """
  184. @return: Record type
  185. @rtype: str
  186. """
  187. return self.data.get('field_type', None)
  188. @property
  189. def asnCidr(self):
  190. """
  191. @return:
  192. @rtype: str
  193. """
  194. return self.data.get('asn_cidr', None)
  195. @property
  196. def httpsPort(self):
  197. """
  198. @return:
  199. @rtype: bool
  200. """
  201. return self.data.get('https_port', None)
  202. class Instance:
  203. def __init__(self, url, data):
  204. """Model for a SearX instance.
  205. @param url: Url of the instance
  206. @type url: str
  207. @param data: Data of the instance
  208. @type data: dict
  209. """
  210. self._url = url
  211. self._data = data
  212. def __str__(self): return self.url
  213. def __repr__(self): return str(self)
  214. @property
  215. def data(self): return self._data
  216. @property
  217. def url(self):
  218. """
  219. @return: returns the url of this instance.
  220. @rtype: str
  221. """
  222. return self._url
  223. @property
  224. def main(self):
  225. """Returns False when not set.
  226. @return: ?
  227. @rtype: bool
  228. """
  229. return self._data.get('main', False)
  230. @property
  231. def networkType(self):
  232. """ Network type; see core/handler.py:NetworkTypes
  233. @return: Network protocol used (Web, Tor, I2P from NetworkTypes)
  234. @rtype: int
  235. """
  236. return self._data.get('network_type', 0)
  237. @property
  238. def version(self):
  239. """Returns a empty string when none found.
  240. @return: Returns the instance it's version.
  241. @rtype: InstanceVersion
  242. """
  243. return parseVersionString(self._data.get('version', ''))
  244. @property
  245. def engines(self):
  246. """
  247. Returns a empty string when none found.
  248. @return: Returns a list with SearchEngine objects
  249. @rtype: list
  250. """
  251. return [SearchEngine(name, data) for name, data in self._data.get(
  252. 'engines', {}).items()]
  253. @property
  254. def tls(self):
  255. """
  256. @rtype: TLS
  257. """
  258. return TLS(self._data.get('tls', {}))
  259. @property
  260. def network(self):
  261. """
  262. @rtype: Network
  263. """
  264. return Network(self._data.get('network', {}))
  265. class Stats2Result(JsonResult):
  266. v_str = jsonVerify.Value(str)
  267. v_int = jsonVerify.Value(int)
  268. v_float = jsonVerify.Value(float)
  269. v_bool = jsonVerify.Value(bool)
  270. v_noneStr = jsonVerify.MultiValue((jsonVerify.NoneType, str))
  271. v_noneInt = jsonVerify.MultiValue((jsonVerify.NoneType, int))
  272. v_noneFloat = jsonVerify.MultiValue((jsonVerify.NoneType, float))
  273. v_noneBoolStr = jsonVerify.MultiValue((jsonVerify.NoneType, bool, str))
  274. TimingStructure = {
  275. "success_percentage": v_float,
  276. "all": {
  277. "median": v_float,
  278. "stdev": v_float,
  279. "value": v_float,
  280. "mean": v_float
  281. },
  282. "server": {
  283. "median": v_float,
  284. "stdev": v_float,
  285. "value": v_float,
  286. "mean": v_float
  287. },
  288. "load": {
  289. "median": v_float,
  290. "stdev": v_float,
  291. "value": v_float,
  292. "mean": v_float,
  293. "mean": v_float
  294. },
  295. "error": v_str
  296. }
  297. ExpectedStructure = {
  298. "metadata": {
  299. "timestamp": v_int,
  300. "ips": {
  301. "": {
  302. "reverse": v_noneStr,
  303. "field_type": v_str,
  304. "asn_cidr": v_str
  305. }
  306. },
  307. "ipv6": v_bool
  308. },
  309. "instances": {
  310. "": {
  311. "analytics": v_bool,
  312. "comments": [v_str],
  313. "alternativeUrls": {},
  314. "docs_url": v_noneStr,
  315. "contact_url": v_noneBoolStr,
  316. "main": v_bool,
  317. "network_type": v_noneStr,
  318. "http": {
  319. "status_code": v_noneInt,
  320. "error": v_noneStr,
  321. "grade": v_noneStr,
  322. "gradeUrl": v_noneStr
  323. },
  324. "version": v_noneStr,
  325. "git_url": v_noneStr,
  326. "error": v_noneStr,
  327. "timing": {
  328. "initial": TimingStructure,
  329. "search": TimingStructure,
  330. "search_wp": TimingStructure,
  331. "search_go": TimingStructure
  332. },
  333. "tls": {
  334. "version": v_str,
  335. "certificate": {
  336. "issuer": {
  337. "commonName": v_str,
  338. "countryName": v_str,
  339. "organizationName": v_str
  340. },
  341. "subject": {
  342. "commonName": v_noneStr,
  343. "countryName": v_noneStr,
  344. "organizationName": v_noneStr,
  345. "altName": v_str
  346. },
  347. "serialNumber": v_str,
  348. "notBefore": v_str,
  349. "notAfter": v_str,
  350. "OCSP": [v_str],
  351. "caIssuers": [v_str],
  352. "sha256": v_str,
  353. "signatureAlgorithm": v_str,
  354. "crlDistributionPoints": [v_str],
  355. "version": v_int
  356. },
  357. "grade": v_str,
  358. "gradeUrl": v_str
  359. },
  360. "html": {},
  361. "network": {
  362. "dnssec": v_int,
  363. "asn_privacy": v_int,
  364. "ips": {
  365. "": {
  366. "https_port": v_bool,
  367. "field_type": v_str,
  368. "reverse": v_noneStr,
  369. "asn_cidr": v_noneStr,
  370. "whois_error": v_str,
  371. "https_port_error": v_str
  372. }
  373. },
  374. "ipv6": v_bool
  375. },
  376. "engines": {
  377. "": {
  378. "error_rate": v_noneInt,
  379. "errors": [v_int],
  380. "checker": {},
  381. "categories": [v_str]
  382. }
  383. }
  384. }
  385. },
  386. "engines": {
  387. "": {
  388. "categories": [v_str],
  389. "language_support": v_bool,
  390. "paging": v_bool,
  391. "safesearch": v_bool,
  392. "time_range_support": v_bool,
  393. "shortcut": v_str,
  394. "stats": {
  395. "instance_count": v_int,
  396. "stats_count": v_int,
  397. "error_rate": v_noneFloat
  398. }
  399. }
  400. },
  401. "engine_errors": [v_str],
  402. "categories": [v_str],
  403. "hashes": [{
  404. "count": v_int,
  405. "hash": v_str,
  406. "unknown": v_bool,
  407. "forks": [v_int]
  408. }],
  409. "cidrs": {
  410. "": {
  411. "asn": v_noneStr,
  412. "asn_country_code": v_noneStr,
  413. "network_country": v_noneStr,
  414. "asn_privacy": v_int,
  415. "asn_description": v_noneStr
  416. }
  417. },
  418. "forks": [v_str]
  419. }
  420. def __init__(self, url, response, err="", errType=ErrorType.Success):
  421. JsonResult.__init__(self, url, response, err=err, errType=errType)
  422. class Stats2(HandlerProto):
  423. """ This class holds the instances.json data and will be passed
  424. to other classes (Instances, Engines)
  425. """
  426. URL = "https://searx.space/"
  427. def __init__(self, requestsHandler):
  428. HandlerProto.__init__(self, requestsHandler)
  429. def updateInstances(self):
  430. """Fetches instances.json from a searx-stats2 instance.
  431. @param requestKwargs: Kwargs to pass to requests.get
  432. @type requestKwargs: dict
  433. @return: A tuple (bool Success/Failed, str Message)
  434. @rtype: tuple
  435. """
  436. url = Stats2.URL.rstrip('/') + '/data/instances.json'
  437. result = self.requestsHandler.get(url, ResultType=Stats2Result)
  438. if result:
  439. self.setData(result.json())
  440. self._lastUpdated = time.time()
  441. # Processing (use our own definition of network types
  442. for url, data in self.instances.items():
  443. data.update({"network_type": NetworkTypes.netTypeFromUrl(url)})
  444. return result