Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Debian Stretch for mongo #695

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/install_mongo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if [ -f /etc/debian_version ]; then
./install_mongodb_ub16.sh
elif [ "$(lsb_release -r -s)" == "18.04" ]; then
./install_mongodb_ub18.sh
elif [ "$(lsb_release -c -s)" == "stretch" ]; then
./install_mongodb_db9.sh
else
echo -e "ERROR: Unknown OS\nExiting!"
exit -1
Expand Down
33 changes: 33 additions & 0 deletions scripts/install_mongodb_db9.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# Install MongoDB for Debian 9 Stretch.

set -e
set -x

wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -

echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

apt-get update
apt-get install -y mongodb-org

sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mongod.conf

cat > /etc/systemd/system/mongodb.service <<EOF
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target
EOF

systemctl start mongodb
systemctl status mongodb
systemctl enable mongodb

26 changes: 22 additions & 4 deletions server/mhn/ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import socket
import struct
from mhn.api.models import Sensor
import geoip2.database

flag_cache = SimpleCache(threshold=1000, default_timeout=300)
sensor_cache = SimpleCache(threshold=1000, default_timeout=300)

geoip2_reader = geoip2.database.Reader(MHN_SERVER_HOME+'/../../GeoLite2-City.mmdb')

def is_RFC1918_addr(ip):
# 10.0.0.0 = 167772160
# 172.16.0.0 = 2886729728
Expand All @@ -30,14 +33,13 @@ def is_RFC1918_addr(ip):

return False


def get_flag_ip(ipaddr):
if is_RFC1918_addr(ipaddr):
return constants.DEFAULT_FLAG_URL

flag = flag_cache.get(ipaddr)
if not flag:
flag = _get_flag_ip(ipaddr)
flag = _get_flag_ip_localdb(ipaddr)
flag_cache.set(ipaddr, flag)
return flag

Expand All @@ -52,19 +54,35 @@ def get_sensor_name(sensor_id):
print 'Name: %s' % sensor_name
return sensor_name

def _get_flag_ip_localdb(ipaddr):
flag_path = '/static/img/flags-iso/shiny/64/{}.png'
try:
r = geoip2_reader.city(ipaddr)
ccode = r.country.iso_code
except Exception:
app.logger.warning("Could not determine flag for ip (LOCALDB): {}".format(ipaddr))
return constants.DEFAULT_FLAG_URL
else:
# Constructs the flag source using country code
flag = flag_path.format(ccode.upper())
if os.path.exists(MHN_SERVER_HOME +"/mhn"+flag):
return flag
else:
return constants.DEFAULT_FLAG_URL

def _get_flag_ip(ipaddr):
"""
Returns an static address where the flag is located.
Defaults to static immge: '/static/img/unknown.png'
"""
flag_path = '/static/img/flags-iso/shiny/64/{}.png'
geo_api = 'https://geospray.threatstream.com/ip/{}'
geo_api = 'https://geospray.threatstream.com/ip/{}'
try:
# Using threatstream's geospray API to get
# the country code for this IP address.
r = requests.get(geo_api.format(ipaddr))
ccode = r.json()['countryCode']
except Exception:
except Exception:
app.logger.warning("Could not determine flag for ip: {}".format(ipaddr))
return constants.DEFAULT_FLAG_URL
else:
Expand Down