Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Joanne Hugé
re6stnet
Commits
0769f25f
Commit
0769f25f
authored
Sep 20, 2012
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Do not recreate client tap interfaces all the time
parent
76cbea0c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
12 deletions
+50
-12
re6st/tunnel.py
re6st/tunnel.py
+48
-10
re6stnet
re6stnet
+2
-2
No files found.
re6st/tunnel.py
View file @
0769f25f
import
logging
,
random
,
socket
,
time
import
logging
,
random
,
socket
,
subprocess
,
threading
,
time
from
collections
import
deque
from
itertools
import
chain
from
itertools
import
chain
from
.
import
plib
,
utils
from
.
import
plib
,
utils
...
@@ -63,8 +64,40 @@ class TunnelManager(object):
...
@@ -63,8 +64,40 @@ class TunnelManager(object):
self
.
_client_count
=
client_count
self
.
_client_count
=
client_count
self
.
_refresh_count
=
1
self
.
_refresh_count
=
1
self
.
free_interface_set
=
set
(
're6stnet'
+
str
(
i
)
self
.
new_iface_list
=
deque
(
're6stnet'
+
str
(
i
)
for
i
in
xrange
(
1
,
client_count
+
1
))
for
i
in
xrange
(
1
,
self
.
_client_count
+
1
))
self
.
_free_iface_list
=
[]
def
_tuntap
(
self
,
iface
=
None
):
if
iface
:
self
.
new_iface_list
.
appendleft
(
iface
)
action
=
'del'
else
:
iface
=
self
.
new_iface_list
.
popleft
()
action
=
'add'
args
=
'ip'
,
'tuntap'
,
action
,
'dev'
,
iface
,
'mode'
,
'tap'
logging
.
debug
(
'%r'
,
args
)
subprocess
.
call
(
args
)
return
iface
def
delInterfaces
(
self
):
iface_list
=
self
.
_free_iface_list
iface_list
+=
self
.
_iface_to_prefix
self
.
_iface_to_prefix
.
clear
()
while
iface_list
:
self
.
_tuntap
(
iface_list
.
pop
())
def
getFreeInterface
(
self
,
prefix
):
try
:
iface
=
self
.
_free_iface_list
.
pop
()
except
IndexError
:
iface
=
self
.
_tuntap
()
self
.
_iface_to_prefix
[
iface
]
=
prefix
return
iface
def
freeInterface
(
self
,
iface
):
self
.
_free_iface_list
.
append
(
iface
)
del
self
.
_iface_to_prefix
[
iface
]
def
refresh
(
self
):
def
refresh
(
self
):
logging
.
debug
(
'Checking tunnels...'
)
logging
.
debug
(
'Checking tunnels...'
)
...
@@ -76,6 +109,8 @@ class TunnelManager(object):
...
@@ -76,6 +109,8 @@ class TunnelManager(object):
self
.
_next_tunnel_refresh
=
time
.
time
()
+
self
.
_refresh_time
self
.
_next_tunnel_refresh
=
time
.
time
()
+
self
.
_refresh_time
self
.
_peer_db
.
log
()
self
.
_peer_db
.
log
()
self
.
_makeNewTunnels
(
remove
)
self
.
_makeNewTunnels
(
remove
)
if
remove
and
self
.
_free_iface_list
:
self
.
_tuntap
(
self
.
_free_iface_list
.
pop
())
self
.
next_refresh
=
time
.
time
()
+
5
self
.
next_refresh
=
time
.
time
()
+
5
def
_cleanDeads
(
self
):
def
_cleanDeads
(
self
):
...
@@ -95,13 +130,17 @@ class TunnelManager(object):
...
@@ -95,13 +130,17 @@ class TunnelManager(object):
logging
.
info
(
'Killing the connection with %u/%u...'
,
logging
.
info
(
'Killing the connection with %u/%u...'
,
int
(
prefix
,
2
),
len
(
prefix
))
int
(
prefix
,
2
),
len
(
prefix
))
connection
=
self
.
_connection_dict
.
pop
(
prefix
)
connection
=
self
.
_connection_dict
.
pop
(
prefix
)
self
.
freeInterface
(
connection
.
iface
)
p
=
connection
.
process
try
:
try
:
getattr
(
connection
.
process
,
'kill'
if
kill
else
'terminate'
)()
getattr
(
p
,
'kill'
if
kill
else
'terminate'
)()
except
OSError
:
except
OSError
:
# If the process is already exited
pass
# we already polled an exited process
pass
else
:
self
.
free_interface_set
.
add
(
connection
.
iface
)
t
=
threading
.
Timer
(
5
,
p
.
kill
)
del
self
.
_iface_to_prefix
[
connection
.
iface
]
kill
or
t
.
start
()
p
.
wait
()
t
.
cancel
()
logging
.
trace
(
'Connection with %u/%u killed'
,
logging
.
trace
(
'Connection with %u/%u killed'
,
int
(
prefix
,
2
),
len
(
prefix
))
int
(
prefix
,
2
),
len
(
prefix
))
...
@@ -112,10 +151,9 @@ class TunnelManager(object):
...
@@ -112,10 +151,9 @@ class TunnelManager(object):
assert
prefix
!=
self
.
_prefix
,
self
.
__dict__
assert
prefix
!=
self
.
_prefix
,
self
.
__dict__
logging
.
info
(
'Establishing a connection with %u/%u'
,
logging
.
info
(
'Establishing a connection with %u/%u'
,
int
(
prefix
,
2
),
len
(
prefix
))
int
(
prefix
,
2
),
len
(
prefix
))
iface
=
self
.
free_interface_set
.
pop
(
)
iface
=
self
.
getFreeInterface
(
prefix
)
self
.
_connection_dict
[
prefix
]
=
Connection
(
address
,
self
.
_write_pipe
,
self
.
_connection_dict
[
prefix
]
=
Connection
(
address
,
self
.
_write_pipe
,
self
.
_timeout
,
iface
,
prefix
,
self
.
_encrypt
,
self
.
_ovpn_args
)
self
.
_timeout
,
iface
,
prefix
,
self
.
_encrypt
,
self
.
_ovpn_args
)
self
.
_iface_to_prefix
[
iface
]
=
prefix
self
.
_peer_db
.
connecting
(
prefix
,
1
)
self
.
_peer_db
.
connecting
(
prefix
,
1
)
return
True
return
True
...
...
re6stnet
View file @
0769f25f
...
@@ -189,7 +189,7 @@ def main():
...
@@ -189,7 +189,7 @@ def main():
config
.
openvpn_args
,
timeout
,
config
.
tunnel_refresh
,
config
.
openvpn_args
,
timeout
,
config
.
tunnel_refresh
,
config
.
client_count
,
config
.
iface_list
,
network
,
prefix
,
config
.
client_count
,
config
.
iface_list
,
network
,
prefix
,
address
,
ip_changed
,
config
.
encrypt
)
address
,
ip_changed
,
config
.
encrypt
)
tunnel_interfaces
+=
tunnel_manager
.
free_interface_se
t
tunnel_interfaces
+=
tunnel_manager
.
new_iface_lis
t
else
:
else
:
tunnel_manager
=
write_pipe
=
None
tunnel_manager
=
write_pipe
=
None
...
@@ -262,7 +262,7 @@ def main():
...
@@ -262,7 +262,7 @@ def main():
# main loop
# main loop
if
tunnel_manager
is
None
:
if
tunnel_manager
is
None
:
sys
.
exit
(
os
.
WEXITSTATUS
(
os
.
wait
()[
1
]))
sys
.
exit
(
os
.
WEXITSTATUS
(
os
.
wait
()[
1
]))
cleanup
.
append
(
tunnel_manager
.
killAll
)
cleanup
+=
tunnel_manager
.
delInterfaces
,
tunnel_manager
.
killAll
while
True
:
while
True
:
next
=
tunnel_manager
.
next_refresh
next
=
tunnel_manager
.
next_refresh
if
forwarder
:
if
forwarder
:
...
...
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