Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Thomas Capricelli
botfreak
Commits
4042952354af
Commit
12fd0f14
authored
Dec 13, 2021
by
Thomas Capricelli
Browse files
new importer, for "IPsum" aggregated list
parent
20d829d54d35
Changes
1
Hide whitespace changes
Inline
Side-by-side
main/management/commands/import_from_ipsum.py
0 → 100644
View file @
40429523
#
# -*- coding: utf-8
# vim: set fileencoding=utf-8
# system
from
ipaddress
import
ip_address
import
requests
# django
from
django.core.management.base
import
BaseCommand
,
CommandError
# projet
from
main.models
import
Reason
,
Server
,
IPWhiteList
,
ReportBadIP
feed_url
=
"https://raw.githubusercontent.com/stamparm/ipsum/master/ipsum.txt"
# We only import those with such a score, at least
MINIMUM_NUMBER
=
2
class
Command
(
BaseCommand
):
help
=
"""
Aggregated feed on github.
See https://github.com/stamparm/ipsum
"""
def
handle
(
self
,
*
args
,
**
options
):
self
.
reason
=
Reason
.
objects
.
get
(
name
=
"Imported"
)
self
.
server
,
_
=
Server
.
objects
.
get_or_create
(
name
=
"IPsum"
,
user
=
None
)
# Fetch data
# don't use stream=True: the file is not that big, but we are very slow to process it
# and the connection will break/timeout
r
=
requests
.
get
(
feed_url
)
for
line
in
r
.
iter_lines
():
line
=
line
.
decode
(
'ascii'
)
# ignore comments, blank lines
if
line
.
startswith
(
"#"
)
or
len
(
line
)
==
0
:
continue
# Format, the ip appears in <number> different sources/feeds
ip
,
number
=
line
.
split
()
number
=
int
(
number
)
if
number
<
MINIMUM_NUMBER
:
break
self
.
addoneip
(
ip
)
def
addoneip
(
self
,
ip
):
if
IPWhiteList
.
check_against
(
ip_address
(
ip
)):
self
.
stdout
.
write
(
self
.
style
.
WARNING
(
'%s: IP whitelisted, no report created'
%
ip
))
return
# Report ips
if
ReportBadIP
.
create_or_update_report
(
self
.
server
,
ip
,
self
.
reason
):
self
.
stdout
.
write
(
self
.
style
.
SUCCESS
(
'%s: new report created'
%
ip
))
else
:
self
.
stdout
.
write
(
self
.
style
.
WARNING
(
'%s: report updated'
%
ip
))
# vim: ai ts=4 sts=4 et sw=4
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment