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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Kirill Smelkov
re6stnet
Commits
6dc66be1
Commit
6dc66be1
authored
Jul 09, 2012
by
Ulysse Beaugnon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Log is now on a new module so it can be included anywhere
parent
093e464e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
23 deletions
+32
-23
log.py
log.py
+6
-0
propagation.py
propagation.py
+5
-3
vifibnet.py
vifibnet.py
+21
-20
No files found.
log.py
0 → 100644
View file @
6dc66be1
import
time
def
log
(
message
,
verbose_level
):
if
verbose
>=
verbose_level
:
print
time
.
strftime
(
"%d-%m-%Y %H:%M:%S : "
+
message
)
propagation.py
View file @
6dc66be1
import
socket
import
uuid
import
log
# create an upd socket
# listen on it for incoming messages and forward them
...
...
@@ -20,7 +21,7 @@ class Ring:
def
__init__
(
self
,
entryPoint
):
# initialize the connection
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET6
66666
,
socket
.
SOCK_DGRAM
)
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET6
,
socket
.
SOCK_DGRAM
)
self
.
sock
.
bind
((
''
,
0
))
self
.
me
=
RingMember
(
uuid
.
uuid1
().
int
,
''
,
self
.
sock
.
getsockname
()[
1
])
# TODO : get the address
# to enter the ring
...
...
@@ -29,16 +30,17 @@ class Ring:
self
.
successor
=
self
.
me
else
:
self
.
send
(
'FIND_SUCCESSOR '
+
str
(
self
.
me
.
id
)
+
' '
+
self
.
me
.
toString
(),
entrypoint
)
log
.
log
(
'Init the ring with me = '
+
self
.
me
.
toString
(),
3
)
# TODO :
def
handleMessages
(
self
):
# TODO : switch to log
print
'Handling messages ...'
log
.
log
(
'Handling messages ...'
,
3
)
pass
def
send
(
self
,
message
,
target
):
# TODO : switch to log
print
'Sending : '
+
message
+
' to '
+
target
.
toString
(
)
log
.
log
(
'Sending : '
+
message
+
' to '
+
target
.
toString
(),
5
)
self
.
sock
.
sendTo
(
message
,
(
target
.
ip
,
target
.
port
))
def
findSuccessor
(
self
,
id
,
sender
):
...
...
vifibnet.py
View file @
6dc66be1
...
...
@@ -5,24 +5,21 @@ import upnpigd
import
openvpn
import
random
import
propagation
import
log
VIFIB_NET
=
"2001:db8:42::/48"
connection_dict
=
{}
# to remember current connections we made
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 ?
# TODO : flag in some way the peers that are connected to us so we don't connect to them
# Or maybe we just don't care,
class
PeersDB
:
def
__init__
(
self
,
dbPath
):
log
_message
(
'Connectiong to peers database'
,
4
)
log
.
log
(
'Connectiong to peers database'
,
4
)
self
.
db
=
sqlite3
.
connect
(
dbPath
,
isolation_level
=
None
)
log
_message
(
'Initializing peers database'
,
4
)
log
.
log
(
'Initializing peers database'
,
4
)
self
.
db
.
execute
(
"""CREATE TABLE IF NOT EXISTS peers
( id INTEGER PRIMARY KEY AUTOINCREMENT,
ip TEXT NOT NULL,
...
...
@@ -37,11 +34,11 @@ class PeersDB:
"ORDER BY RANDOM() LIMIT ?"
,
(
nPeers
,))
def
usePeer
(
self
,
id
):
log
_message
(
'Updating peers database : using peer '
+
str
(
id
),
5
)
log
.
log
(
'Updating peers database : using peer '
+
str
(
id
),
5
)
self
.
db
.
execute
(
"UPDATE peers SET used = 1 WHERE id = ?"
,
(
id
,))
def
unusePeer
(
self
,
id
):
log
_message
(
'Updating peers database : unusing peer '
+
str
(
id
),
5
)
log
.
log
(
'Updating peers database : unusing peer '
+
str
(
id
),
5
)
self
.
db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
...
...
@@ -103,37 +100,37 @@ def getConfig():
def
startNewConnection
(
n
):
try
:
for
id
,
ip
,
port
,
proto
in
peers_db
.
getUnusedPeers
(
n
):
log
_message
(
'Establishing a connection with id %s (%s:%s)'
%
(
id
,
ip
,
port
),
2
)
log
.
log
(
'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
),
stdout
=
os
.
open
(
'%s/vifibnet.client.%s.log'
%
(
config
.
log_directory
,
id
),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
)
),
iface
)
peers_db
.
usePeer
(
id
)
except
KeyError
:
log
_message
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
log
.
log
(
"Can't establish connection with %s : no available interface"
%
ip
,
2
)
pass
except
Exception
:
traceback
.
print_exc
()
def
killConnection
(
id
):
try
:
log
_message
(
'Killing the connection with id '
+
str
(
id
),
2
)
log
.
log
(
'Killing the connection with id '
+
str
(
id
),
2
)
p
,
iface
=
connection_dict
.
pop
(
id
)
p
.
kill
()
free_interface_set
.
add
(
iface
)
peers_db
.
unusePeer
(
id
)
except
KeyError
:
log
_message
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
log
.
log
(
"Can't kill connection to "
+
peer
+
": no existing connection"
,
1
)
pass
except
Exception
:
log
_message
(
"Can't kill connection to "
+
peer
+
": uncaught error"
,
1
)
log
.
log
(
"Can't kill connection to "
+
peer
+
": uncaught error"
,
1
)
pass
def
checkConnections
():
for
id
in
connection_dict
.
keys
():
p
,
iface
=
connection_dict
[
id
]
if
p
.
poll
()
!=
None
:
log
_message
(
'Connection with %s has failed with return code %s'
%
(
id
,
p
.
returncode
),
3
)
log
.
log
(
'Connection with %s has failed with return code %s'
%
(
id
,
p
.
returncode
),
3
)
free_interface_set
.
add
(
iface
)
peers_db
.
unusePeer
(
id
)
del
connection_dict
[
id
]
...
...
@@ -153,16 +150,17 @@ def refreshConnections():
def
handle_message
(
msg
):
script_type
,
common_name
=
msg
.
split
()
if
script_type
==
'client-connect'
:
log
_message
(
'Incomming connection from %s'
%
(
common_name
,),
3
)
log
.
log
(
'Incomming connection from %s'
%
(
common_name
,),
3
)
# TODO : check if we are not already connected to it
elif
script_type
==
'client-disconnect'
:
log
_message
(
'%s has disconnected'
%
(
common_name
,),
3
)
log
.
log
(
'%s has disconnected'
%
(
common_name
,),
3
)
else
:
log
_message
(
'Unknow message recieved from the openvpn pipe : '
+
msg
,
1
)
log
.
log
(
'Unknow message recieved from the openvpn pipe : '
+
msg
,
1
)
def
main
():
# Get arguments
getConfig
()
log
.
verbose
=
config
.
verbose
(
externalIp
,
externalPort
)
=
upnpigd
.
GetExternalInfo
(
1194
)
# Setup database
...
...
@@ -170,17 +168,17 @@ def main():
peers_db
=
PeersDB
(
config
.
db
)
# Launch babel on all interfaces
log
_message
(
'Starting babel'
,
3
)
log
.
log
(
'Starting babel'
,
3
)
babel
=
startBabel
(
stdout
=
os
.
open
(
'%s/babeld.log'
%
(
config
.
log_directory
,),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
),
stderr
=
subprocess
.
STDOUT
)
# Create and open read_only pipe to get connect/disconnect events from openvpn
log
_message
(
'Creating pipe for openvpn events'
,
3
)
log
.
log
(
'Creating pipe for openvpn events'
,
3
)
r_pipe
,
write_pipe
=
os
.
pipe
()
read_pipe
=
os
.
fdopen
(
r_pipe
)
# Establish connections
log
_message
(
'Starting openvpn server'
,
3
)
log
.
log
(
'Starting openvpn server'
,
3
)
serverProcess
=
openvpn
.
server
(
config
.
ip
,
write_pipe
,
'--dev'
,
'vifibnet'
,
stdout
=
os
.
open
(
'%s/vifibnet.server.log'
%
(
config
.
log_directory
,),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
))
startNewConnection
(
config
.
client_count
)
...
...
@@ -188,6 +186,9 @@ def main():
# Timed refresh initializing
next_refresh
=
time
.
time
()
+
config
.
refresh_time
# initializing the ring to propagate the peers
ring
=
propagation
.
Ring
(
None
)
# main loop
try
:
while
True
:
...
...
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