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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nicolas Wavrant
re6stnet
Commits
08ce480b
Commit
08ce480b
authored
Jul 05, 2012
by
Guillaume Bury
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added log_message function
parent
5ded971e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
18 deletions
+30
-18
main.py
main.py
+25
-14
openvpn.py
openvpn.py
+5
-4
No files found.
main.py
View file @
08ce480b
...
...
@@ -7,7 +7,12 @@ import random
VIFIB_NET
=
"2001:db8:42::/48"
connection_dict
=
{}
# to remember current connections
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
def
log_message
(
message
,
verbose_level
):
if
config
.
verbose
>=
verbose_level
:
print
time
.
strftime
(
"%d-%m-%Y %H:%M:%S : "
+
message
)
# TODO : How do we get our vifib ip ?
...
...
@@ -28,6 +33,7 @@ def babel(network_ip, network_mask, verbose_level):
]
if
config
.
babel_state
:
args
+=
'-S'
,
config
.
babel_state
log_message
(
"Starting babel daemon"
,
2
)
return
subprocess
.
Popen
(
args
+
list
(
free_interface_set
))
def
getConfig
():
...
...
@@ -35,6 +41,10 @@ def getConfig():
parser
=
argparse
.
ArgumentParser
(
description
=
'Resilient virtual private network application'
)
_
=
parser
.
add_argument
_
(
'--server-log'
,
default
=
'/var/log/vifibnet.server.log'
,
help
=
'Path to openvpn server log file'
)
_
(
'--client-log'
,
default
=
'/var/log/'
,
help
=
'Path to openvpn client log directory'
)
_
(
'--client-count'
,
default
=
2
,
type
=
int
,
help
=
'the number servers the peers try to connect to'
)
# TODO : use maxpeer
...
...
@@ -66,34 +76,31 @@ def startNewConnection(n):
try
:
for
id
,
ip
,
port
,
proto
in
peer_db
.
execute
(
"SELECT id, ip, port, proto FROM peers WHERE used = 0 ORDER BY RANDOM() LIMIT ?"
,
(
n
,)):
if
config
.
verbose
>=
2
:
print
'Establishing a connection with %s'
%
ip
log_message
(
'Establishing a connection with id %s (%s:%s)'
%
(
id
,
ip
,
port
),
2
)
iface
=
free_interface_set
.
pop
()
connection_dict
[
id
]
=
(
openvpn
.
client
(
ip
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
))
,
iface
)
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
)
log_message
(
'Updating peers database'
,
3
)
peer_db
.
execute
(
"UPDATE peers SET used = 1 WHERE id = ?"
,
(
id
,))
except
KeyError
:
if
config
.
verbose
>=
2
:
print
"Can't establish connection with %s : no available interface"
%
ip
log_message
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
pass
except
Exception
:
traceback
.
print_exc
()
def
killConnection
(
id
):
try
:
if
config
.
verbose
>=
2
:
print
'Killing the connection with '
+
peer
log_message
(
'Killing the connection with id '
+
str
(
id
),
2
)
p
,
iface
=
connection_dict
.
pop
(
id
)
p
.
kill
()
free_interface_set
.
add
(
iface
)
log_message
(
'Updating peers database'
,
3
)
peer_db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
except
KeyError
:
if
config
.
verbose
>=
1
:
print
"Can't kill connection to "
+
peer
+
": no existing connection"
log_message
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
pass
except
Exception
:
if
config
.
verbose
>=
1
:
print
"Can't kill connection to "
+
peer
+
": uncaught error"
log_message
(
"Can't kill connection to "
+
peer
+
": uncaught error"
,
1
)
pass
...
...
@@ -115,7 +122,9 @@ def main():
# Setup database
global
peer_db
# stop using global variables for everything ?
log_message
(
'Connectiong to peers database'
,
4
)
peer_db
=
sqlite3
.
connect
(
config
.
db
,
isolation_level
=
None
)
log_message
(
'Initializing peers database'
,
4
)
peer_db
.
execute
(
"""CREATE TABLE IF NOT EXISTS peers
( id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT NOT NULL,
...
...
@@ -126,7 +135,9 @@ def main():
peer_db
.
execute
(
"UPDATE peers SET used = 0"
)
# Establish connections
serverProcess
=
openvpn
.
server
(
config
.
ip
,
'--dev'
,
'vifibnet'
)
log_message
(
'Starting openvpn server'
,
3
)
serverProcess
=
openvpn
.
server
(
config
.
ip
,
'--dev'
,
'vifibnet'
,
stdout
=
os
.
open
(
config
.
server_log
,
os
.
O_RDONLY
|
os
.
O_CREAT
))
startNewConnection
(
config
.
client_count
)
# main loop
...
...
openvpn.py
View file @
08ce480b
import
subprocess
import
os
def
openvpn
(
*
args
,
**
kw
):
args
=
[
'openvpn'
,
...
...
@@ -17,7 +18,7 @@ def openvpn(*args, **kw):
# TODO : set iface up when creating a server/client
# ! check working directory before launching up script ?
def
server
(
ip
,
*
args
):
def
server
(
ip
,
*
args
,
**
kw
):
return
openvpn
(
'--tls-server'
,
'--keepalive'
,
'10'
,
'60'
,
...
...
@@ -25,13 +26,13 @@ def server(ip, *args):
'--duplicate-cn'
,
# XXX : to be removed
'--up'
,
'up-server '
+
ip
,
'--dh'
,
config
.
dh
,
*
args
)
*
args
,
**
kw
)
def
client
(
serverIp
,
*
args
):
def
client
(
serverIp
,
*
args
,
**
kw
):
return
openvpn
(
'--nobind'
,
'--tls-client'
,
'--remote'
,
serverIp
,
'--up'
,
'up-client'
,
*
args
)
*
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