Reorganize code to be reusable.

This commit is contained in:
Chteufleur 2017-03-17 23:43:20 +01:00
parent 5fa7fe5235
commit 558f9d6029
3 changed files with 58 additions and 43 deletions

View File

@ -42,8 +42,6 @@ var (
KeyPath = "./key.pem"
ChanRequest = make(chan interface{}, 5)
TimeoutSec = 60 // 1 min
MaxTimeout = 300 // 5 min
BindAddressIPv4 = "127.0.0.1"
BindAddressIPv6 = "[::1]"
@ -72,52 +70,24 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
}
timeoutStr := strings.Join(r.Form[TIMEOUTE], "")
log.Printf("%sAuth %s", LogInfo, jid)
timeout, err := strconv.Atoi(timeoutStr)
if err != nil || timeout <= 0 {
timeout = TimeoutSec
}
if timeout > MaxTimeout {
timeout = MaxTimeout
if err != nil {
timeout = 0
}
chanAnswer := make(chan string)
answer := xmpp.Confirm(jid, method, domain, transaction, timeout)
switch answer {
case xmpp.REPLY_OK:
w.WriteHeader(http.StatusOK)
confirmation := new(xmpp.Confirmation)
confirmation.JID = jid
confirmation.Method = method
confirmation.Domain = domain
confirmation.Transaction = transaction
confirmation.ChanReply = chanAnswer
confirmation.SendConfirmation()
select {
case answer := <-chanAnswer:
switch answer {
case xmpp.REPLY_OK:
w.WriteHeader(http.StatusOK)
case xmpp.REPLY_DENY:
w.WriteHeader(http.StatusUnauthorized)
case xmpp.REPLY_UNREACHABLE:
w.WriteHeader(StatusUnreachable)
default:
w.WriteHeader(StatusUnknownError)
}
case <-time.After(time.Duration(timeout) * time.Second):
case xmpp.REPLY_DENY:
w.WriteHeader(http.StatusUnauthorized)
}
switch confirmation.TypeSend {
case xmpp.TYPE_SEND_IQ:
log.Printf("%sDelete IQ", LogDebug)
delete(xmpp.WaitIqMessages, confirmation.IdMap)
case xmpp.REPLY_UNREACHABLE:
w.WriteHeader(StatusUnreachable)
case xmpp.TYPE_SEND_MESSAGE:
log.Printf("%sDelete Message", LogDebug)
delete(xmpp.WaitMessageAnswers, confirmation.IdMap)
default:
w.WriteHeader(StatusUnknownError)
}
}

View File

@ -37,9 +37,9 @@ func init() {
// HTTP config
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
if err == nil && httpTimeout > 0 && httpTimeout < http.MaxTimeout {
if err == nil && httpTimeout > 0 && httpTimeout < xmpp.MaxTimeout {
log.Println("Define HTTP timeout to " + strconv.Itoa(httpTimeout) + " second")
http.TimeoutSec = httpTimeout
xmpp.TimeoutSec = httpTimeout
}
httpPort, err := strconv.Atoi(mapConfig["http_port"])
if err == nil {

View File

@ -6,12 +6,14 @@ import (
"log"
"strconv"
"strings"
"time"
)
const (
REPLY_UNREACHABLE = "reply_unreachable"
REPLY_DENY = "reply_deny"
REPLY_OK = "reply_ok"
REPLY_TIMEOUT = "reply_timeout"
TYPE_SEND_MESSAGE = "type_send_message"
TYPE_SEND_IQ = "type_send_iq"
@ -24,6 +26,9 @@ const (
var (
MapLangs = make(map[string]string)
TimeoutSec = 60 // 1 min
MaxTimeout = 300 // 5 min
)
type Confirmation struct {
@ -38,6 +43,46 @@ type Confirmation struct {
ChanReply chan string
}
func Confirm(jid, method, domain, transaction string, timeout int) string {
// TODO check param validity
ret := ""
log.Printf("%sAuth %s", LogInfo, jid)
chanAnswer := make(chan string)
if timeout <= 0 {
timeout = TimeoutSec
}
if timeout > MaxTimeout {
timeout = MaxTimeout
}
confirmation := new(Confirmation)
confirmation.JID = jid
confirmation.Method = method
confirmation.Domain = domain
confirmation.Transaction = transaction
confirmation.ChanReply = chanAnswer
confirmation.SendConfirmation()
select {
case answer := <-chanAnswer:
ret = answer
case <-time.After(time.Duration(timeout) * time.Second):
ret = REPLY_TIMEOUT
}
switch confirmation.TypeSend {
case TYPE_SEND_IQ:
log.Printf("%sDelete IQ", LogDebug)
delete(WaitIqMessages, confirmation.IdMap)
case TYPE_SEND_MESSAGE:
log.Printf("%sDelete Message", LogDebug)
delete(WaitMessageAnswers, confirmation.IdMap)
}
return ret
}
func (confirmation *Confirmation) SendConfirmation() {
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
clientJID, _ := xmpp.ParseJID(confirmation.JID)