Ask to JID to confirm request by replying to message

This commit is contained in:
Chteufleur 2016-05-31 22:06:32 +02:00
parent 4be53d2f7e
commit 594e0b1031
4 changed files with 47 additions and 5 deletions

View File

@ -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
}

View File

@ -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"]

View File

@ -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
}

View File

@ -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)