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
b4c5fdd0
Commit
b4c5fdd0
authored
Jul 12, 2012
by
Ulysse Beaugnon
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
https://git.erp5.org/repos/vifibnet
parents
3309f6f5
cdd5c554
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
34 deletions
+58
-34
client-connect
client-connect
+1
-0
openvpn.py
openvpn.py
+3
-2
registry.py
registry.py
+17
-5
vifibnet.py
vifibnet.py
+37
-27
No files found.
client-connect
View file @
b4c5fdd0
...
...
@@ -37,4 +37,5 @@ import os, sys
'untrusted_port'
:
'59345'
,
'verb'
:
'3'
}
open
(
sys
.
argv
[
2
],
'w'
).
write
(
'push "setenv external_ip %s"
\
n
'
%
os
.
environ
[
trusted_ip
])
os
.
write
(
int
(
sys
.
argv
[
1
]),
'%(script_type)s %(common_name)s
\
n
'
%
os
.
environ
)
openvpn.py
View file @
b4c5fdd0
...
...
@@ -36,11 +36,12 @@ def server(ip, pipe_fd, *args, **kw):
'--max-clients'
,
str
(
config
.
max_clients
),
*
args
,
**
kw
)
def
client
(
serverIp
,
*
args
,
**
kw
):
def
client
(
serverIp
,
pipe_fd
,
*
args
,
**
kw
):
return
openvpn
(
'--nobind'
,
'--
tls-
client'
,
'--client'
,
'--remote'
,
serverIp
,
'--up'
,
'up-client'
,
'--ipchange'
,
'ipchange '
+
str
(
pipe_fd
),
*
args
,
**
kw
)
registry.py
View file @
b4c5fdd0
#!/usr/bin/env python
import
argparse
,
math
,
random
,
smtplib
,
sqlite3
,
string
,
time
import
argparse
,
math
,
random
,
smtplib
,
sqlite3
,
string
,
struct
,
socket
,
time
from
email.mime.text
import
MIMEText
from
functools
import
wraps
from
SimpleXMLRPCServer
import
SimpleXMLRPCServer
,
SimpleXMLRPCRequestHandler
...
...
@@ -20,6 +20,10 @@ class main(object):
parser
=
argparse
.
ArgumentParser
(
description
=
'Peer discovery http server for vifibnet'
)
_
=
parser
.
add_argument
_
(
'--prefix'
,
required
=
True
,
help
=
'Prefix of the network deployed ( example : 2001:db8:42'
)
_
(
'--prefix-len'
,
required
=
True
,
type
=
int
,
help
=
'Prefix length'
)
_
(
'--db'
,
required
=
True
,
help
=
'Path to database file'
)
_
(
'--ca'
,
required
=
True
,
...
...
@@ -140,15 +144,23 @@ class main(object):
return
crypto
.
dump_certificate
(
crypto
.
FILETYPE_PEM
,
self
.
ca
)
def
declare
(
self
,
handler
,
address
):
# guess prefix from handler.client_address
ip1
,
ip2
=
struct
.
unpack
(
'>QQ'
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
handler
.
client_address
)))
prefix
=
bin
(
ip1
)[
2
:]
+
bin
(
ip2
)]
2
:]
client_address
,
_
=
handler
.
client_address
# For Testing purposes only
client_address
=
"2001:db8:42::"
assert
(
client_address
.
startswith
(
self
.
config
.
prefix
))
ip1
,
ip2
=
struct
.
unpack
(
'>QQ'
,
socket
.
inet_pton
(
socket
.
AF_INET6
,
client_address
))
ip1
=
bin
(
ip1
)[
2
:].
rjust
(
64
,
'0'
)
ip2
=
bin
(
ip2
)[
2
:].
rjust
(
64
,
'0'
)
prefix
=
(
ip1
+
ip2
)[
self
.
config
.
prefix_len
:]
prefix
,
=
self
.
db
.
execute
(
"SELECT prefix FROM vifib WHERE prefix <= ? ORDER BY prefix DESC"
,
(
prefix
,)).
next
()
ip
,
port
,
proto
=
address
self
.
db
.
execute
(
"INSERT INTO peers VALUES (?,?,?,?)"
,
(
prefix
,
ip
,
port
,
proto
))
self
.
db
.
execute
(
"INSERT
OR REPLACE
INTO peers VALUES (?,?,?,?)"
,
(
prefix
,
ip
,
port
,
proto
))
def
getPeerList
(
self
,
handler
,
n
,
address
):
assert
0
<
n
<
1000
print
"declaring new node"
self
.
declare
(
handler
,
address
)
print
"sending peers"
return
self
.
db
.
execute
(
"SELECT ip, port, proto FROM peers ORDER BY random() LIMIT ?"
,
(
n
,)).
fetchall
()
...
...
vifibnet.py
View file @
b4c5fdd0
#!/usr/bin/env python
import
argparse
,
errno
,
os
,
select
,
sqlite3
,
subprocess
,
sys
,
time
,
xmlrpclib
import
argparse
,
errno
,
math
,
os
,
select
,
sqlite3
,
subprocess
,
sys
,
time
,
xmlrpclib
from
OpenSSL
import
crypto
import
traceback
import
upnpigd
import
openvpn
import
random
import
log
VIFIB_NET
=
"2001:db8:42::/48"
VIFIB_NET
=
"2001:db8:42:"
VIFIB_LEN
=
48
connection_dict
=
{}
# to remember current connections we made
free_interface_set
=
set
((
'client1'
,
'client2'
,
'client3'
,
'client4'
,
'client5'
,
'client6'
,
'client7'
,
'client8'
,
'client9'
,
'client10'
))
# 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,
# Or maybe we just don't care
class
PeersDB
:
def
__init__
(
self
,
dbPath
):
self
.
proxy
=
xmlrpclib
.
ServerProxy
(
'http://%s:%u'
%
(
config
.
server
,
config
.
server_port
))
...
...
@@ -36,9 +39,11 @@ class PeersDB:
self
.
populateDB
(
100
)
def
populateDB
(
self
,
n
):
log
.
log
(
'Populating Peers DB'
,
2
)
(
ip
,
port
)
=
upnpigd
.
GetExternalInfo
(
1194
)
proto
=
'udp'
self
.
db
.
executemany
(
"INSERT INTO peers (ip, port, proto) VALUES ?"
,
self
.
proxy
.
getPeerList
(
n
,
port
,
proto
))
new_peer_list
=
self
.
proxy
.
getPeerList
(
n
,
(
ip
,
port
,
proto
))
self
.
db
.
executemany
(
"INSERT INTO peers (ip, port, proto) VALUES (?,?,?)"
,
new_peer_list
)
def
getUnusedPeers
(
self
,
nPeers
):
return
self
.
db
.
execute
(
"SELECT id, ip, port, proto FROM peers WHERE used = 0 "
...
...
@@ -52,19 +57,21 @@ class PeersDB:
log
.
log
(
'Updating peers database : unusing peer '
+
str
(
id
),
5
)
self
.
db
.
execute
(
"UPDATE peers SET used = 0 WHERE id = ?"
,
(
id
,))
# TODO: do everything using 'binary' strings
def
ipFromPrefix
(
prefix
,
prefix_len
):
tmp
=
hew
(
int
(
prefix
,
2
))[
2
::]
tmp
=
hex
(
int
(
prefix
))[
2
:]
tmp
=
tmp
.
rjust
(
int
((
math
.
ceil
(
float
(
prefix_len
)
/
4
))),
'0'
)
ip
=
VIFIB_NET
for
i
in
xrange
(
0
,
len
(
i
p
),
4
):
for
i
in
xrange
(
0
,
len
(
tm
p
),
4
):
ip
+=
tmp
[
i
:
i
+
4
]
+
':'
ip
+=
':'
return
ip
+
':'
def
startBabel
(
**
kw
):
args
=
[
'babeld'
,
'-C'
,
'redistribute local ip %s'
%
(
config
.
ip
),
'-C'
,
'redistribute local deny'
,
# Route VIFIB ip adresses
'-C'
,
'in ip %s
'
%
VIFIB_NET
,
'-C'
,
'in ip %s
:/%u'
%
(
VIFIB_NET
,
VIFIB_LEN
)
,
# Route only addresse in the 'local' network,
# or other entire networks
#'-C', 'in ip %s' % (config.ip),
...
...
@@ -85,9 +92,9 @@ def getConfig():
_
=
parser
.
add_argument
_
(
'--server'
,
required
=
True
,
help
=
'Address for peer discovery server'
)
_
(
'--server-port'
,
required
=
True
,
_
(
'--server-port'
,
required
=
True
,
type
=
int
,
help
=
'Peer discovery server port'
)
_
(
'-
-log-directory
'
,
default
=
'/var/log'
,
_
(
'-
l'
,
'--log
'
,
default
=
'/var/log'
,
help
=
'Path to vifibnet logs directory'
)
_
(
'--client-count'
,
default
=
2
,
type
=
int
,
help
=
'Number of client connections'
)
...
...
@@ -109,32 +116,33 @@ def getConfig():
_
(
'--cert'
,
required
=
True
,
help
=
'Path to the certificate file'
)
# Temporary args - to be removed
# Can be removed, should ip be a global variable ?
_
(
'--ip'
,
required
=
True
,
help
=
'IPv6 of the server'
)
# Openvpn options
_
(
'openvpn_args'
,
nargs
=
argparse
.
REMAINDER
,
help
=
"Common OpenVPN options (e.g. certificates)"
)
openvpn
.
config
=
config
=
parser
.
parse_args
()
log
.
verbose
=
config
.
verbose
with
open
(
config
.
cert
,
'r'
)
as
f
:
cert
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
)
cert
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
.
read
()
)
subject
=
cert
.
get_subject
()
prefix_txt
,
prefix_len_txt
=
subject
.
serialNumber
.
split
(
'/'
)
prefix
=
int
(
prefix_txt
)
prefix_len
=
int
(
prefix_len_txt
)
ip
=
ipFromPrefix
(
prefix
)
print
ip
prefix
,
prefix_len
=
subject
.
serialNumber
.
split
(
'/'
)
ip
=
ipFromPrefix
(
prefix
,
int
(
prefix_len
))
log
.
log
(
'Intranet ip : %s'
%
(
ip
,),
3
)
if
config
.
openvpn_args
[
0
]
==
"--"
:
del
config
.
openvpn_args
[
0
]
config
.
openvpn_args
.
append
(
'--cert'
)
config
.
openvpn_args
.
append
(
config
.
cert
)
log
.
log
(
"Configuration completed"
,
1
)
def
startNewConnection
(
n
):
def
startNewConnection
(
n
,
write_pipe
):
try
:
for
id
,
ip
,
port
,
proto
in
peers_db
.
getUnusedPeers
(
n
):
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
(
os
.
path
.
join
(
config
.
log
_directory
,
'vifibnet.client.%s.log'
%
(
id
,)),
connection_dict
[
id
]
=
(
openvpn
.
client
(
ip
,
write_pipe
,
'--dev'
,
iface
,
'--proto'
,
proto
,
'--rport'
,
str
(
port
),
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
,
'vifibnet.client.%s.log'
%
(
id
,)),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
)
),
iface
)
peers_db
.
usePeer
(
id
)
...
...
@@ -180,12 +188,15 @@ def refreshConnections():
startNewConnection
(
config
.
client_count
-
len
(
connection_dict
))
def
handle_message
(
msg
):
script_type
,
common_name
=
msg
.
split
()
script_type
,
arg
=
msg
.
split
()
if
script_type
==
'client-connect'
:
log
.
log
(
'Incomming connection from %s'
%
(
common_name
,),
3
)
log
.
log
(
'Incomming connection from %s'
%
(
arg
,),
3
)
# TODO : check if we are not already connected to it
elif
script_type
==
'client-disconnect'
:
log
.
log
(
'%s has disconnected'
%
(
common_name
,),
3
)
log
.
log
(
'%s has disconnected'
%
(
arg
,),
3
)
elif
script_type
==
'ipchange'
:
# TODO: save the external ip received
log
.
log
(
'External Ip : '
+
arg
,
3
)
else
:
log
.
log
(
'Unknow message recieved from the openvpn pipe : '
+
msg
,
1
)
...
...
@@ -193,7 +204,7 @@ def main():
# Get arguments
getConfig
()
log
.
verbose
=
config
.
verbose
# TODO:
get proto to
use ?
# TODO:
how do we decide which protocol we
use ?
(
externalIp
,
externalPort
)
=
upnpigd
.
GetExternalInfo
(
1194
)
# Setup database
...
...
@@ -202,8 +213,7 @@ def main():
# Launch babel on all interfaces
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
)
babel
=
startBabel
(
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
,
'vifibnet.babeld.log'
),
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
.
log
(
'Creating pipe for openvpn events'
,
3
)
...
...
@@ -213,8 +223,8 @@ def main():
# Establish connections
log
.
log
(
'Starting openvpn server'
,
3
)
serverProcess
=
openvpn
.
server
(
config
.
ip
,
write_pipe
,
'--dev'
,
'vifibnet'
,
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
_directory
,
'vifibnet.server.log'
),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
))
startNewConnection
(
config
.
client_count
)
stdout
=
os
.
open
(
os
.
path
.
join
(
config
.
log
,
'vifibnet.server.log'
),
os
.
O_WRONLY
|
os
.
O_CREAT
|
os
.
O_TRUNC
))
startNewConnection
(
config
.
client_count
,
write_pipe
)
# Timed refresh initializing
next_refresh
=
time
.
time
()
+
config
.
refresh_time
...
...
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