]> git.ipfire.org Git - thirdparty/suricata-update.git/commitdiff
use a custom user agent
authorJason Ish <ish@unx.ca>
Fri, 1 Dec 2017 19:52:32 +0000 (13:52 -0600)
committerJason Ish <ish@unx.ca>
Fri, 1 Dec 2017 20:37:51 +0000 (14:37 -0600)
Includes Suricata-Update version, Suricata version, OS name
and processor architecture.

suricata/update/engine.py
suricata/update/main.py
suricata/update/net.py

index c890f9e3d40935f907bd068ed31dc06ceb31452f..bc4ef24a09fad99d7fd512671856184fba4d22cd 100644 (file)
@@ -91,7 +91,7 @@ def get_path(program="suricata"):
     return None
 
 def parse_version(buf):
-    m = re.search("((\d+)\.(\d+)(\.(\d+))?(\w+)?)", str(buf).strip())
+    m = re.search("((\d+)\.(\d+)(\.(\d+))?([\w\-]+)?)", str(buf).strip())
     if m:
         full = m.group(1)
         major = int(m.group(2))
index 39dea7fbf8a8fe784b2bf0a8b6f4672a08559c4c..4ce6913fcac6f67d1753db0f191d0c644fb3d32e 100644 (file)
@@ -1261,6 +1261,8 @@ def _main():
         suricata_version = suricata.update.engine.parse_version(
             DEFAULT_SURICATA_VERSION)
 
+    suricata.update.net.set_user_agent_suricata_version(suricata_version.full)
+
     file_tracker = FileTracker()
 
     disable_matchers = []
index f7ed6534d57a9c3fa68fe8064b16ccedc30b6cb4..fa85beec7e006092b66cb9c247428d26614b9545 100644 (file)
 
 """ Module for network related operations. """
 
+import platform
+
 try:
     # Python 3.3...
-    from urllib.request import urlopen
+    from urllib.request import urlopen, build_opener
     from urllib.error import HTTPError
 except ImportError:
     # Python 2.6, 2.7.
-    from urllib2 import urlopen
+    from urllib2 import urlopen, build_opener
     from urllib2 import HTTPError
 
+from suricata.update.version import version
+
 # Number of bytes to read at a time in a GET request.
 GET_BLOCK_SIZE = 8192
 
+user_agent_suricata_verison = "Unknown"
+
+def set_user_agent_suricata_version(version):
+    global user_agent_suricata_verison
+    user_agent_suricata_verison = version
+
+def build_user_agent():
+    params = []
+
+    uname_system = platform.uname()[0]
+
+    params.append("OS: %s" % (uname_system))
+    params.append("CPU: %s" % (platform.processor()))
+    params.append("Python: %s" % (platform.python_version()))
+
+    if uname_system == "Linux":
+        distribution = platform.linux_distribution()
+        params.append("Dist: %s/%s" % (
+            str(distribution[0]), str(distribution[1])))
+
+    return "Suricata-Update/%s (%s)" % (
+        version, "; ".join(params))
+
 def get(url, fileobj, progress_hook=None):
     """ Perform a GET request against a URL writing the contents into
     the provideded file like object.
@@ -44,7 +71,14 @@ def get(url, fileobj, progress_hook=None):
       provided fileobj may occur.
     """
 
-    remote = urlopen(url)
+    user_agent = build_user_agent()
+
+    opener = build_opener()
+    opener.addheaders = [
+        ("User-Agent", build_user_agent()),
+    ]
+
+    remote = opener.open(url)
     info = remote.info()
     try:
         content_length = int(info["content-length"])