Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
botfreak
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Capricelli
botfreak
Commits
4042952354af
Commit
4042952354af
authored
3 years ago
by
Thomas Capricelli
Browse files
Options
Downloads
Patches
Plain Diff
new importer, for "IPsum" aggregated list
parent
20d829d54d35
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main/management/commands/import_from_ipsum.py
+54
-0
54 additions, 0 deletions
main/management/commands/import_from_ipsum.py
with
54 additions
and
0 deletions
main/management/commands/import_from_ipsum.py
0 → 100644
+
54
−
0
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment