From d3104b4fe1681744bf3b23283c610c24166ba950 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Thu, 30 Apr 2015 16:49:04 +0300 Subject: [PATCH] Ability to bind human readable name to the peer Signed-off-by: Sergey Matveev --- doc/user.texi | 18 +++++++++--------- identify.go | 13 +++++++++++++ utils/newclient.sh | 10 ++++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/doc/user.texi b/doc/user.texi index dbbda98..b177e75 100644 --- a/doc/user.texi +++ b/doc/user.texi @@ -34,8 +34,10 @@ or terminated. Server needs to know only the address to listen on and path to directory containing peers information. This directory must contain subdirectories with the names equal to client's identifications. Each of them must have -key file with corresponding authentication key, up.sh script that has to -print interface's name on the first line and optional down.sh. +@strong{key} file with corresponding authentication key, @strong{up.sh} +script that has to print interface's name on the first output line. +Optionally there can be @code{down.sh} that will be executed when client +disconnects, and @code{name} file containing human readable client's name. @menu * Example usage:: @@ -61,20 +63,19 @@ convenience. @example % ./utils/newclient.sh Alice -peers/9b40701bdaf522f2b291cb039490312/Alice +9b40701bdaf522f2b291cb039490312 @end example @code{9b40701bdaf522f2b291cb039490312} is client's identification. -@code{Alice} is just an empty file that can help to search them like -this: @verb{|find peers -name Alice|}. @code{key} file inside peer's -directory contains authentication key. +@code{peers/9b40701bdaf522f2b291cb039490312/name} contains @emph{Alice}, +@code{peers/9b40701bdaf522f2b291cb039490312/key} contains authentication key and +@code{peers/9b40701bdaf522f2b291cb039490312/up.sh} contains currently +dummy empty up-script. GNU/Linux IPv4 client-server example: @example -server% echo "#!/bin/sh" > peers/CLIENTID/up.sh server% echo "echo tap10" >> peers/CLIENTID/up.sh -server% chmod 500 peers/CLIENTID/up.sh server% ip addr add 192.168.0.1/24 dev wlan0 server% tunctl -t tap10 server% ip link set mtu 1462 dev tap10 @@ -107,7 +108,6 @@ $tap=$(ifconfig tap create) ifconfig $tap inet6 fc00::1/96 mtu 1462 up echo $tap EOF -server% chmod 500 peers/CLIENTID/up.sh server% ifconfig em0 inet6 fe80::1/64 server% GOMAXPROC=4 govpn-server -bind fe80::1%em0 @end example diff --git a/identify.go b/identify.go index e4baea1..6c16da0 100644 --- a/identify.go +++ b/identify.go @@ -21,8 +21,11 @@ package govpn import ( "crypto/subtle" "encoding/hex" + "io/ioutil" "log" "os" + "path" + "strings" "sync" "time" @@ -40,6 +43,16 @@ func (id PeerId) String() string { return hex.EncodeToString(id[:]) } +// Return human readable name of the peer. +// It equals either to peers/PEER/name file contents or PEER's hex. +func (id PeerId) MarshalJSON() ([]byte, error) { + result := id.String() + if name, err := ioutil.ReadFile(path.Join(PeersPath, result, "name")); err == nil { + result = strings.TrimRight(string(name), "\n") + } + return []byte(`"` + result + `"`), nil +} + type cipherCache map[PeerId]*xtea.Cipher var ( diff --git a/utils/newclient.sh b/utils/newclient.sh index 620aac7..68469b6 100755 --- a/utils/newclient.sh +++ b/utils/newclient.sh @@ -9,8 +9,8 @@ getrand() [ -n "$1" ] || { cat < EOF @@ -22,5 +22,7 @@ peerid=$(getrand 16) umask 077 mkdir -p peers/$peerid getrand 32 > peers/$peerid/key -touch peers/$peerid/$1 -echo peers/$peerid/$1 +echo $username > peers/$peerid/name +echo '#!/bin/sh' > peers/$peerid/up.sh +chmod 700 peers/$peerid/up.sh +echo $peerid -- 2.44.0