diff --git a/client/identity.go b/client/identity.go index 1978aa3..ac8fa09 100644 --- a/client/identity.go +++ b/client/identity.go @@ -31,6 +31,7 @@ type Identity struct { OwnedDevices PeerList `json:"owned_devices,omitempty"` StaticMtkServerPaths []InternalServerList `json:"static_mtk_server_paths,omitempty"` DynamicMtkServeRules []string `json:"dynamic_mtk_serve_rules,omitempty"` + InvitationTimeout int `json:"invitation_timeout,omitempty"` unlockedHiddenPeers PeerList } diff --git a/server/invitation.go b/server/invitation.go new file mode 100644 index 0000000..3260fad --- /dev/null +++ b/server/invitation.go @@ -0,0 +1,70 @@ +package server + +import ( + "errors" + "math/rand" + "time" + + "github.com/go-redis/redis" +) + +func (r *RedisRouter) CreateInvitation(invitation []byte, timeout int, password string, serverTimeout int, urlLen int) (string, time.Time) { + id := r.createShortUrl(urlLen) + if timeout > serverTimeout { + timeout = serverTimeout + } + r.Client.Set("mwiv:"+id, invitation, time.Duration(timeout*1000000)) + if len(password) > 0 { + r.Client.Set("mwpw:"+id, password, time.Duration(timeout*1000000)) + } + return id, time.Now().Add(time.Duration(timeout * 1000000)).UTC() +} + +func (r *RedisRouter) GetInvitation(id string, password string) ([]byte, error) { + passRequired := false + expectedpass, err := r.Client.Get("mwpw:" + id).Result() + if err != nil { + passRequired = false + } else { + passRequired = true + } + if passRequired && password != expectedpass { + return nil, errors.New("invitation get : auth failed, wrong password") + } + mwiv, err := r.Client.Get("mwiv:" + id).Result() + if err != nil { + return nil, err + } + return []byte(mwiv), nil +} + +func (r *RedisRouter) AnswerInvitation(id string, timeout int, invitation []byte, serverTimeout int) time.Time { + if timeout > serverTimeout { + timeout = serverTimeout + } + r.Client.Set("mwan:"+id, invitation, time.Duration(timeout*1000000)) + return time.Now().Add(time.Duration(timeout * 1000000)).UTC() +} + +func (r *RedisRouter) GetAnswer(id string) ([]byte, error) { + mwan, err := r.Client.Get("mwiv:" + id).Result() + if err != nil { + return nil, err + } + return []byte(mwan), nil +} + +func (r *RedisRouter) createShortUrl(length int) string { + url := "" + alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + // for not in redis + for { + for i := 1; i <= length; i++ { + url += string(alphabet[rand.Intn(61)]) + } + if r.Client.Get("mwiv:"+url).Err() == redis.Nil { + break + } + } + return url +}