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
Xiaowu Zhang
re6stnet
Commits
3b161a08
Commit
3b161a08
authored
Jul 05, 2012
by
Guillaume Bury
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added fifo to get connect openvpn events
parent
08ce480b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
8 deletions
+18
-8
client-connect
client-connect
+3
-0
main.py
main.py
+13
-7
openvpn.py
openvpn.py
+2
-1
No files found.
client-connect
0 → 100755
View file @
3b161a08
#!/usr/bin/python -S
import
os
,
sys
os
.
write
(
int
(
sys
.
argv
[
1
]),
'hello !
\
n
'
)
main.py
View file @
3b161a08
#!/usr/bin/env python
#!/usr/bin/env python
import
argparse
,
errno
,
os
,
sqlite3
,
subprocess
,
sys
,
time
import
argparse
,
errno
,
os
,
s
elect
,
s
qlite3
,
subprocess
,
sys
,
time
import
traceback
import
traceback
import
upnpigd
import
upnpigd
import
openvpn
import
openvpn
...
@@ -7,7 +7,7 @@ import random
...
@@ -7,7 +7,7 @@ import random
VIFIB_NET
=
"2001:db8:42::/48"
VIFIB_NET
=
"2001:db8:42::/48"
connection_dict
=
{}
# to remember current connections
connection_dict
=
{}
# to remember current connections
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
def
log_message
(
message
,
verbose_level
):
def
log_message
(
message
,
verbose_level
):
...
@@ -80,7 +80,7 @@ def startNewConnection(n):
...
@@ -80,7 +80,7 @@ def startNewConnection(n):
iface
=
free_interface_set
.
pop
()
iface
=
free_interface_set
.
pop
()
connection_dict
[
id
]
=
(
openvpn
.
client
(
ip
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
),
connection_dict
[
id
]
=
(
openvpn
.
client
(
ip
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
),
stdout
=
os
.
open
(
config
.
client_log
+
'vifibnet.client.'
+
str
(
id
)
+
'.log'
,
os
.
O_RDONLY
|
os
.
O_CREAT
)
)
,
iface
)
stdout
=
os
.
open
(
config
.
client_log
+
'vifibnet.client.'
+
str
(
id
)
+
'.log'
,
os
.
O_RDONLY
|
os
.
O_CREAT
)
)
,
iface
)
log_message
(
'Updating peers database'
,
3
)
log_message
(
'Updating peers database'
,
5
)
peer_db
.
execute
(
"UPDATE peers SET used = 1 WHERE id = ?"
,
(
id
,))
peer_db
.
execute
(
"UPDATE peers SET used = 1 WHERE id = ?"
,
(
id
,))
except
KeyError
:
except
KeyError
:
log_message
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
log_message
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
...
@@ -94,7 +94,7 @@ def killConnection(id):
...
@@ -94,7 +94,7 @@ def killConnection(id):
p
,
iface
=
connection_dict
.
pop
(
id
)
p
,
iface
=
connection_dict
.
pop
(
id
)
p
.
kill
()
p
.
kill
()
free_interface_set
.
add
(
iface
)
free_interface_set
.
add
(
iface
)
log_message
(
'Updating peers database'
,
3
)
log_message
(
'Updating peers database'
,
5
)
peer_db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
peer_db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
except
KeyError
:
except
KeyError
:
log_message
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
log_message
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
...
@@ -134,17 +134,23 @@ def main():
...
@@ -134,17 +134,23 @@ def main():
peer_db
.
execute
(
"CREATE INDEX IF NOT EXISTS _peers_used ON peers(used)"
)
peer_db
.
execute
(
"CREATE INDEX IF NOT EXISTS _peers_used ON peers(used)"
)
peer_db
.
execute
(
"UPDATE peers SET used = 0"
)
peer_db
.
execute
(
"UPDATE peers SET used = 0"
)
# Create and open read_only pipe to get connect/disconnect events from openvpn
log_message
(
'Creating pipe for openvpn events'
,
3
)
r_pipe
,
write_pipe
=
os
.
pipe
()
read_pipe
=
os
.
fdopen
(
r_pipe
)
# Establish connections
# Establish connections
log_message
(
'Starting openvpn server'
,
3
)
log_message
(
'Starting openvpn server'
,
3
)
serverProcess
=
openvpn
.
server
(
config
.
ip
,
serverProcess
=
openvpn
.
server
(
config
.
ip
,
write_pipe
,
'--dev'
,
'vifibnet'
,
stdout
=
os
.
open
(
config
.
server_log
,
os
.
O_RDONLY
|
os
.
O_CREAT
))
'--dev'
,
'vifibnet'
,
stdout
=
os
.
open
(
config
.
server_log
,
os
.
O_RDONLY
|
os
.
O_CREAT
))
startNewConnection
(
config
.
client_count
)
startNewConnection
(
config
.
client_count
)
# main loop
# main loop
try
:
try
:
while
True
:
while
True
:
# TODO : use select to get openvpn events from pipes
ready
,
tmp1
,
tmp2
=
select
.
select
([
read_pipe
],
[],
[],
float
(
config
.
refresh_time
))
time
.
sleep
(
float
(
config
.
refresh_time
))
if
ready
:
log_message
(
read_pipe
.
readline
(),
0
)
refreshConnections
()
refreshConnections
()
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
return
0
return
0
...
...
openvpn.py
View file @
3b161a08
...
@@ -18,13 +18,14 @@ def openvpn(*args, **kw):
...
@@ -18,13 +18,14 @@ def openvpn(*args, **kw):
# TODO : set iface up when creating a server/client
# TODO : set iface up when creating a server/client
# ! check working directory before launching up script ?
# ! check working directory before launching up script ?
def
server
(
ip
,
*
args
,
**
kw
):
def
server
(
ip
,
pipe_fd
,
*
args
,
**
kw
):
return
openvpn
(
return
openvpn
(
'--tls-server'
,
'--tls-server'
,
'--keepalive'
,
'10'
,
'60'
,
'--keepalive'
,
'10'
,
'60'
,
'--mode'
,
'server'
,
'--mode'
,
'server'
,
'--duplicate-cn'
,
# XXX : to be removed
'--duplicate-cn'
,
# XXX : to be removed
'--up'
,
'up-server '
+
ip
,
'--up'
,
'up-server '
+
ip
,
'--client-connect'
,
'client-connect '
+
str
(
pipe_fd
),
'--dh'
,
config
.
dh
,
'--dh'
,
config
.
dh
,
*
args
,
**
kw
)
*
args
,
**
kw
)
...
...
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