From 594e0b103136360e1bc19d5d6ac3767cf059056a Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Tue, 31 May 2016 22:06:32 +0200 Subject: [PATCH] Ask to JID to confirm request by replying to message --- http/http.go | 11 +++++++++-- main.go | 8 ++++++++ xmpp/client.go | 20 +++++++++++++++++++- xmpp/xmpp.go | 13 +++++++++++-- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/http/http.go b/http/http.go index c89bbb9..9aa9201 100644 --- a/http/http.go +++ b/http/http.go @@ -2,10 +2,11 @@ package http import ( "fmt" + "log" "net/http" "strconv" "strings" - "log" + "time" ) const ( @@ -26,6 +27,7 @@ var ( HttpPortBind = 9090 ChanRequest = make(chan interface{}, 5) + TimeoutSec = 60 ) @@ -50,8 +52,13 @@ func authHandler(w http.ResponseWriter, r *http.Request) { ChanRequest <- transaction ChanRequest <- chanAnswer - answer := <- chanAnswer ret := RETURN_VALUE_NOK + answer := false + select { + case answer = <- chanAnswer: + case <- time.After(time.Duration(TimeoutSec) * time.Second): + answer = false + } if answer { ret = RETURN_VALUE_OK } diff --git a/main.go b/main.go index cf975d7..c5c8832 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "log" "os" "os/signal" + "strconv" "syscall" "time" ) @@ -28,6 +29,13 @@ func init() { log.Fatal("Failed to load configuration file.", err) } + // HTTP config + httpTimeout, err := strconv.Atoi(mapConfig["http_timeoute_sec"]) + if err == nil { + log.Println("Define HTTP timeout to %d second", httpTimeout) + http.TimeoutSec = httpTimeout + } + // XMPP config xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"] xmpp.JidStr = mapConfig["xmpp_hostname"] diff --git a/xmpp/client.go b/xmpp/client.go index db93f47..ab22ac7 100644 --- a/xmpp/client.go +++ b/xmpp/client.go @@ -1,6 +1,8 @@ package xmpp import ( + "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" + "log" ) @@ -16,5 +18,21 @@ type Client struct { func (client *Client) QueryClient() { log.Printf("%sQuery JID %s", LogInfo, client.JID) - client.ChanReply <- false + client.askViaMessage() +} + +func (client *Client) askViaIQ() { + +} + +func (client *Client) askViaMessage() { + m := xmpp.Message{From: jid.Domain, To: client.JID, Type: "normal"} + + m.Thread = xmpp.SessionID() + m.Body = "Auth request for "+client.Domain+".\nTransaction identifier is: "+client.Transaction+"\nReply to this message to confirm the request." + + log.Printf("%sSenp message %v", LogInfo, m) + comp.Out <- m + + waitMessageAnswers[m.Thread] = client } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 6c960a8..80c60fd 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -25,6 +25,9 @@ var ( ChanAction = make(chan string) + waitMessageAnswers = make(map[string]*Client) + waitIQAnswers = make(map[string]*Client) + Debug = true ) @@ -47,6 +50,12 @@ func mainXMPP() { case *xmpp.Presence: case *xmpp.Message: + client := waitMessageAnswers[v.Thread] + if client != nil { + // TODO chek the answer + client.ChanReply <- true + } + delete(waitMessageAnswers, v.Thread) case *xmpp.Iq: switch v.PayloadName().Space { @@ -83,7 +92,7 @@ func must(v interface{}, err error) interface{} { return v } - +/* func SendMessage(to, subject, message string) { m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"} @@ -94,7 +103,7 @@ func SendMessage(to, subject, message string) { log.Printf("%sSenp message %v", LogInfo, m) comp.Out <- m } - +*/ func execDiscoCommand(iq *xmpp.Iq) { log.Printf("%sDiscovery item iq received", LogInfo)