Reorganize code to be reusable.
This commit is contained in:
parent
5fa7fe5235
commit
558f9d6029
52
http/http.go
52
http/http.go
|
|
@ -42,8 +42,6 @@ var (
|
||||||
KeyPath = "./key.pem"
|
KeyPath = "./key.pem"
|
||||||
|
|
||||||
ChanRequest = make(chan interface{}, 5)
|
ChanRequest = make(chan interface{}, 5)
|
||||||
TimeoutSec = 60 // 1 min
|
|
||||||
MaxTimeout = 300 // 5 min
|
|
||||||
|
|
||||||
BindAddressIPv4 = "127.0.0.1"
|
BindAddressIPv4 = "127.0.0.1"
|
||||||
BindAddressIPv6 = "[::1]"
|
BindAddressIPv6 = "[::1]"
|
||||||
|
|
@ -72,52 +70,24 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
timeoutStr := strings.Join(r.Form[TIMEOUTE], "")
|
timeoutStr := strings.Join(r.Form[TIMEOUTE], "")
|
||||||
log.Printf("%sAuth %s", LogInfo, jid)
|
|
||||||
timeout, err := strconv.Atoi(timeoutStr)
|
timeout, err := strconv.Atoi(timeoutStr)
|
||||||
if err != nil || timeout <= 0 {
|
if err != nil {
|
||||||
timeout = TimeoutSec
|
timeout = 0
|
||||||
}
|
|
||||||
if timeout > MaxTimeout {
|
|
||||||
timeout = MaxTimeout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
case xmpp.REPLY_DENY:
|
||||||
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):
|
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
}
|
|
||||||
|
|
||||||
switch confirmation.TypeSend {
|
case xmpp.REPLY_UNREACHABLE:
|
||||||
case xmpp.TYPE_SEND_IQ:
|
w.WriteHeader(StatusUnreachable)
|
||||||
log.Printf("%sDelete IQ", LogDebug)
|
|
||||||
delete(xmpp.WaitIqMessages, confirmation.IdMap)
|
|
||||||
|
|
||||||
case xmpp.TYPE_SEND_MESSAGE:
|
default:
|
||||||
log.Printf("%sDelete Message", LogDebug)
|
w.WriteHeader(StatusUnknownError)
|
||||||
delete(xmpp.WaitMessageAnswers, confirmation.IdMap)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
4
main.go
4
main.go
|
|
@ -37,9 +37,9 @@ func init() {
|
||||||
|
|
||||||
// HTTP config
|
// HTTP config
|
||||||
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
|
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")
|
log.Println("Define HTTP timeout to " + strconv.Itoa(httpTimeout) + " second")
|
||||||
http.TimeoutSec = httpTimeout
|
xmpp.TimeoutSec = httpTimeout
|
||||||
}
|
}
|
||||||
httpPort, err := strconv.Atoi(mapConfig["http_port"])
|
httpPort, err := strconv.Atoi(mapConfig["http_port"])
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,14 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
REPLY_UNREACHABLE = "reply_unreachable"
|
REPLY_UNREACHABLE = "reply_unreachable"
|
||||||
REPLY_DENY = "reply_deny"
|
REPLY_DENY = "reply_deny"
|
||||||
REPLY_OK = "reply_ok"
|
REPLY_OK = "reply_ok"
|
||||||
|
REPLY_TIMEOUT = "reply_timeout"
|
||||||
|
|
||||||
TYPE_SEND_MESSAGE = "type_send_message"
|
TYPE_SEND_MESSAGE = "type_send_message"
|
||||||
TYPE_SEND_IQ = "type_send_iq"
|
TYPE_SEND_IQ = "type_send_iq"
|
||||||
|
|
@ -24,6 +26,9 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
MapLangs = make(map[string]string)
|
MapLangs = make(map[string]string)
|
||||||
|
|
||||||
|
TimeoutSec = 60 // 1 min
|
||||||
|
MaxTimeout = 300 // 5 min
|
||||||
)
|
)
|
||||||
|
|
||||||
type Confirmation struct {
|
type Confirmation struct {
|
||||||
|
|
@ -38,6 +43,46 @@ type Confirmation struct {
|
||||||
ChanReply chan string
|
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() {
|
func (confirmation *Confirmation) SendConfirmation() {
|
||||||
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
|
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
|
||||||
clientJID, _ := xmpp.ParseJID(confirmation.JID)
|
clientJID, _ := xmpp.ParseJID(confirmation.JID)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue