Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
b870cd79
Commit
b870cd79
authored
Sep 29, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(captiveportal, new) work in progress scriptbase
parent
cc88cada
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
35 deletions
+38
-35
arp.py
src/opnsense/scripts/OPNsense/CaptivePortal/lib/arp.py
+1
-3
db.py
src/opnsense/scripts/OPNsense/CaptivePortal/lib/db.py
+4
-4
ipfw.py
src/opnsense/scripts/OPNsense/CaptivePortal/lib/ipfw.py
+33
-28
No files found.
src/opnsense/scripts/OPNsense/CaptivePortal/lib/arp.py
View file @
b870cd79
...
...
@@ -29,7 +29,6 @@ import subprocess
class
ARP
(
object
):
def
__init__
(
self
):
""" construct new arp helper
:return: None
...
...
@@ -44,7 +43,7 @@ class ARP(object):
# parse arp table
self
.
_arp_table
=
dict
()
with
tempfile
.
NamedTemporaryFile
()
as
output_stream
:
subprocess
.
check_call
([
'/usr/sbin/arp'
,
'-an'
],
stdout
=
output_stream
,
stderr
=
subprocess
.
STDOUT
)
subprocess
.
check_call
([
'/usr/sbin/arp'
,
'-an'
],
stdout
=
output_stream
,
stderr
=
subprocess
.
STDOUT
)
output_stream
.
seek
(
0
)
for
line
in
output_stream
.
read
()
.
split
(
'
\n
'
):
if
line
.
find
(
'('
)
>
-
1
and
line
.
find
(
')'
)
>
-
1
:
...
...
@@ -71,4 +70,3 @@ class ARP(object):
return
self
.
_arp_table
[
address
]
else
:
return
None
src/opnsense/scripts/OPNsense/CaptivePortal/lib/db.py
View file @
b870cd79
...
...
@@ -55,7 +55,7 @@ class DB(object):
if
cur
.
fetchall
()[
0
][
0
]
==
0
:
# empty database, initialize database
init_script_filename
=
'
%
s/../sql/init.sql'
%
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
cur
.
executescript
(
open
(
init_script_filename
,
'rb'
)
.
read
())
cur
.
executescript
(
open
(
init_script_filename
,
'rb'
)
.
read
())
cur
.
close
()
def
add_client
(
self
,
zoneid
,
username
,
ip_address
,
mac_address
):
...
...
@@ -89,7 +89,7 @@ class DB(object):
if
cur
.
rowcount
==
0
:
cur
.
execute
(
"""insert into cp_clients(zoneid, sessionid, username, ip_address, mac_address, created)
values (:zoneid, :sessionid, :username, :ip_address, :mac_address, :created)
"""
,
response
)
"""
,
response
)
self
.
_connection
.
commit
()
return
response
...
...
src/opnsense/scripts/OPNsense/CaptivePortal/lib/ipfw.py
View file @
b870cd79
...
...
@@ -33,17 +33,18 @@ class IPFW(object):
def
__init__
(
self
):
pass
def
list_table
(
self
,
table_number
):
@
staticmethod
def
list_table
(
table_number
):
""" list ipfw table
:param table_number: ipfw table number
:return: list
"""
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
devnull
=
open
(
os
.
devnull
,
'w'
)
result
=
list
()
with
tempfile
.
NamedTemporaryFile
()
as
output_stream
:
subprocess
.
check_call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'list'
],
subprocess
.
check_call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'list'
],
stdout
=
output_stream
,
stderr
=
DEVNULL
)
stderr
=
devnull
)
output_stream
.
seek
(
0
)
for
line
in
output_stream
.
read
()
.
split
(
'
\n
'
):
result
.
append
(
line
.
split
(
' '
)[
0
])
...
...
@@ -58,42 +59,45 @@ class IPFW(object):
ipfw_tbl
=
self
.
list_table
(
table_number
)
if
address
.
find
(
'.'
)
>
-
1
and
address
.
find
(
'/'
)
==
-
1
:
# address given, search for /32 net in ipfw rules
if
'
%
s/32'
%
address
.
strip
()
in
ipfw_tbl
:
if
'
%
s/32'
%
address
.
strip
()
in
ipfw_tbl
:
return
True
elif
address
.
strip
()
in
ipfw_tbl
:
return
True
return
False
def
add_to_table
(
self
,
table_number
,
address
):
@
staticmethod
def
add_to_table
(
table_number
,
address
):
""" add new entry to ipfw table
:param table_number: ipfw table number
:param address: ip address or net to add to table
:return:
"""
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'add'
,
address
],
stdout
=
DEVNULL
,
stderr
=
DEVNULL
)
devnull
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'add'
,
address
],
stdout
=
devnull
,
stderr
=
devnull
)
def
delete_from_table
(
self
,
table_number
,
address
):
@
staticmethod
def
delete_from_table
(
table_number
,
address
):
""" remove entry from ipfw table
:param table_number: ipfw table number
:param address: ip address or net to add to table
:return:
"""
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'delete'
,
address
],
stdout
=
DEVNULL
,
stderr
=
DEVNULL
)
devnull
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'table'
,
table_number
,
'delete'
,
address
],
stdout
=
devnull
,
stderr
=
devnull
)
def
list_accounting_info
(
self
):
@
staticmethod
def
list_accounting_info
():
""" list accounting info per ip addres, addresses can't overlap in zone's so we just output all we know here
instead of trying to map addresses back to zones.
:return: list accounting info per ip address
"""
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
devnull
=
open
(
os
.
devnull
,
'w'
)
result
=
dict
()
with
tempfile
.
NamedTemporaryFile
()
as
output_stream
:
subprocess
.
check_call
([
'/sbin/ipfw'
,
'-aT'
,
'list'
],
subprocess
.
check_call
([
'/sbin/ipfw'
,
'-aT'
,
'list'
],
stdout
=
output_stream
,
stderr
=
DEVNULL
)
stderr
=
devnull
)
output_stream
.
seek
(
0
)
for
line
in
output_stream
.
read
()
.
split
(
'
\n
'
):
parts
=
line
.
split
()
...
...
@@ -116,7 +120,8 @@ class IPFW(object):
else
:
result
[
ip_address
][
'in_pkts'
]
+=
in_pkts
result
[
ip_address
][
'out_pkts'
]
+=
out_pkts
result
[
ip_address
][
'last_accessed'
]
=
max
(
result
[
ip_address
][
'last_accessed'
],
last_accessed
)
result
[
ip_address
][
'last_accessed'
]
=
max
(
result
[
ip_address
][
'last_accessed'
],
last_accessed
)
return
result
def
add_accounting
(
self
,
address
):
...
...
@@ -132,19 +137,19 @@ class IPFW(object):
if
acc_info
[
ip_address
][
'rule'
]
not
in
rule_ids
:
rule_ids
.
append
(
acc_info
[
ip_address
][
'rule'
])
new
Rule
id
=
-
1
new
_rule_
id
=
-
1
for
ruleId
in
range
(
30001
,
50000
):
if
ruleId
not
in
rule_ids
:
new
Rule
id
=
ruleId
new
_rule_
id
=
ruleId
break
# add accounting rule
if
new
Rule
id
!=
-
1
:
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'add'
,
str
(
new
Ruleid
),
'count'
,
'ip'
,
'from'
,
address
,
'to'
,
'any'
],
stdout
=
DEVNULL
,
stderr
=
DEVNULL
)
subprocess
.
call
([
'/sbin/ipfw'
,
'add'
,
str
(
new
Ruleid
),
'count'
,
'ip'
,
'from'
,
'any'
,
'to'
,
address
],
stdout
=
DEVNULL
,
stderr
=
DEVNULL
)
if
new
_rule_
id
!=
-
1
:
devnull
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'add'
,
str
(
new
_rule_id
),
'count'
,
'ip'
,
'from'
,
address
,
'to'
,
'any'
],
stdout
=
devnull
,
stderr
=
devnull
)
subprocess
.
call
([
'/sbin/ipfw'
,
'add'
,
str
(
new
_rule_id
),
'count'
,
'ip'
,
'from'
,
'any'
,
'to'
,
address
],
stdout
=
devnull
,
stderr
=
devnull
)
def
del_accounting
(
self
,
address
):
""" remove ip address from accounting rules
...
...
@@ -153,6 +158,6 @@ class IPFW(object):
"""
acc_info
=
self
.
list_accounting_info
()
if
address
in
acc_info
:
DEVNULL
=
open
(
os
.
devnull
,
'w'
)
devnull
=
open
(
os
.
devnull
,
'w'
)
subprocess
.
call
([
'/sbin/ipfw'
,
'delete'
,
str
(
acc_info
[
address
][
'rule'
])],
stdout
=
DEVNULL
,
stderr
=
DEVNULL
)
stdout
=
devnull
,
stderr
=
devnull
)
Write
Preview
Markdown
is supported
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