Compare commits
2 Commits
c146c8dae2
...
a85f476600
| Author | SHA1 | Date |
|---|---|---|
|
|
a85f476600 | |
|
|
2f301ffc80 |
24
main.go
24
main.go
|
|
@ -18,6 +18,7 @@ import (
|
||||||
const (
|
const (
|
||||||
Version = "v0.5-dev"
|
Version = "v0.5-dev"
|
||||||
configurationFilePath = "http-auth/httpAuth.conf"
|
configurationFilePath = "http-auth/httpAuth.conf"
|
||||||
|
langFilePath = "http-auth/messages.lang"
|
||||||
PathConfEnvVariable = "XDG_CONFIG_DIRS"
|
PathConfEnvVariable = "XDG_CONFIG_DIRS"
|
||||||
DefaultXdgConfigDirs = "/etc/xdg"
|
DefaultXdgConfigDirs = "/etc/xdg"
|
||||||
)
|
)
|
||||||
|
|
@ -32,6 +33,7 @@ func init() {
|
||||||
if !loadConfigFile() {
|
if !loadConfigFile() {
|
||||||
log.Fatal("Failed to load configuration file.")
|
log.Fatal("Failed to load configuration file.")
|
||||||
}
|
}
|
||||||
|
loadLangFile()
|
||||||
|
|
||||||
// HTTP config
|
// HTTP config
|
||||||
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
|
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
|
||||||
|
|
@ -98,6 +100,28 @@ func loadConfigFile() bool {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadLangFile() bool {
|
||||||
|
ret := false
|
||||||
|
envVariable := os.Getenv(PathConfEnvVariable)
|
||||||
|
if envVariable == "" {
|
||||||
|
envVariable = DefaultXdgConfigDirs
|
||||||
|
}
|
||||||
|
for _, path := range strings.Split(envVariable, ":") {
|
||||||
|
log.Println("Try to find messages lang file into " + path)
|
||||||
|
langFile := path + "/" + langFilePath
|
||||||
|
if _, err := os.Stat(langFile); err == nil {
|
||||||
|
// The config file exist
|
||||||
|
if cfg.Load(langFile, xmpp.MapLangs) == nil {
|
||||||
|
// And has been loaded succesfully
|
||||||
|
log.Println("Find messages lang file at " + langFile)
|
||||||
|
ret = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
go http.Run()
|
go http.Run()
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,27 @@ package xmpp
|
||||||
import (
|
import (
|
||||||
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
|
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
|
||||||
|
|
||||||
"crypto/rand"
|
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
||||||
|
|
||||||
REPLY_UNREACHABLE = "reply_unreachable"
|
REPLY_UNREACHABLE = "reply_unreachable"
|
||||||
REPLY_DENY = "reply_deny"
|
REPLY_DENY = "reply_deny"
|
||||||
REPLY_OK = "reply_ok"
|
REPLY_OK = "reply_ok"
|
||||||
|
|
||||||
TYPE_SEND_MESSAGE = "type_send_message"
|
TYPE_SEND_MESSAGE = "type_send_message"
|
||||||
TYPE_SEND_IQ = "type_send_iq"
|
TYPE_SEND_IQ = "type_send_iq"
|
||||||
|
|
||||||
|
TEMPLATE_DOMAIN = "_DOMAIN_"
|
||||||
|
TEMPLATE_METHOD = "_METHOD_"
|
||||||
|
TEMPLATE_VALIDATION_CODE = "_VALIDE_CODE_"
|
||||||
|
DEFAULT_MESSAGE = "_DOMAIN_ (with method _METHOD_) need to validate your identity, do you agree ?\nValidation code : _VALIDE_CODE_\nPlease check that this code is the same as on _DOMAIN_.\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
MapLangs = make(map[string]string)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Confirmation struct {
|
type Confirmation struct {
|
||||||
|
|
@ -33,15 +40,9 @@ type Confirmation struct {
|
||||||
|
|
||||||
func (confirmation *Confirmation) SendConfirmation() {
|
func (confirmation *Confirmation) SendConfirmation() {
|
||||||
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
|
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
|
||||||
isAutoGeneratedTranctionID := false
|
|
||||||
if confirmation.Transaction == "" {
|
|
||||||
// Random transaction ID generation
|
|
||||||
confirmation.Transaction = TransactionID()
|
|
||||||
isAutoGeneratedTranctionID = true
|
|
||||||
}
|
|
||||||
clientJID, _ := xmpp.ParseJID(confirmation.JID)
|
clientJID, _ := xmpp.ParseJID(confirmation.JID)
|
||||||
if clientJID.Resource == "" {
|
if clientJID.Resource == "" {
|
||||||
confirmation.askViaMessage(isAutoGeneratedTranctionID)
|
confirmation.askViaMessage()
|
||||||
} else {
|
} else {
|
||||||
confirmation.askViaIQ()
|
confirmation.askViaIQ()
|
||||||
}
|
}
|
||||||
|
|
@ -60,17 +61,10 @@ func (confirmation *Confirmation) askViaIQ() {
|
||||||
confirmation.IdMap = stanzaIDstr
|
confirmation.IdMap = stanzaIDstr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool) {
|
func (confirmation *Confirmation) askViaMessage() {
|
||||||
m := xmpp.Message{From: jid.Full(), To: confirmation.JID, Type: xmpp.MessageTypeNormal}
|
m := xmpp.Message{From: jid.Full(), To: confirmation.JID, Type: xmpp.MessageTypeNormal}
|
||||||
|
|
||||||
m.Thread = xmpp.SessionID()
|
m.Thread = xmpp.SessionID()
|
||||||
m.Body = confirmation.Domain + " (with method " + confirmation.Method + ") need to validate your identity, do you agree ?"
|
confirmation.setBodies(&m)
|
||||||
m.Body += "\nValidation code : " + confirmation.Transaction
|
|
||||||
if !isAutoGeneratedTranctionID {
|
|
||||||
// Send only if the transaction ID is not autogenerated
|
|
||||||
m.Body += "\nPlease check that this code is the same as on " + confirmation.Domain
|
|
||||||
}
|
|
||||||
m.Body += "\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
|
|
||||||
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
|
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
|
||||||
|
|
||||||
log.Printf("%sSend message %v", LogInfo, m)
|
log.Printf("%sSend message %v", LogInfo, m)
|
||||||
|
|
@ -81,13 +75,23 @@ func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool)
|
||||||
confirmation.IdMap = confirmation.Transaction
|
confirmation.IdMap = confirmation.Transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
func TransactionID() string {
|
func (confirmation *Confirmation) setBodies(message *xmpp.Message) {
|
||||||
var bytes = make([]byte, 8)
|
msg := DEFAULT_MESSAGE
|
||||||
if _, err := rand.Read(bytes); err != nil {
|
if len(MapLangs) == 0 {
|
||||||
panic(err)
|
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1)
|
||||||
|
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
|
||||||
|
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
|
||||||
|
message.Body = append(message.Body, xmpp.MessageBody{Lang: "en", Value: msg})
|
||||||
|
} else {
|
||||||
|
for key, val := range MapLangs {
|
||||||
|
msg = val
|
||||||
|
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1)
|
||||||
|
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
|
||||||
|
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
|
||||||
|
msg = strings.Replace(msg, "\\n", "\n", -1)
|
||||||
|
msg = strings.Replace(msg, "\\r", "\r", -1)
|
||||||
|
msg = strings.Replace(msg, "\\t", "\t", -1)
|
||||||
|
message.Body = append(message.Body, xmpp.MessageBody{Lang: key, Value: msg})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for k, v := range bytes {
|
|
||||||
bytes[k] = dictionary[v%byte(len(dictionary))]
|
|
||||||
}
|
|
||||||
return string(bytes)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
xmpp/xmpp.go
10
xmpp/xmpp.go
|
|
@ -100,10 +100,12 @@ func mainXMPP() {
|
||||||
} else {
|
} else {
|
||||||
// If body is the confirmation id, it will be considerated as accepted.
|
// If body is the confirmation id, it will be considerated as accepted.
|
||||||
// In order to be compatible with all confirmations.
|
// In order to be compatible with all confirmations.
|
||||||
confirmation := WaitMessageAnswers[v.Body]
|
if len(v.Body) > 0 {
|
||||||
jidFrom, _ := xmpp.ParseJID(v.From)
|
confirmation := WaitMessageAnswers[v.Body[0].Value]
|
||||||
if confirmation != nil && confirmation.JID == jidFrom.Bare() {
|
jidFrom, _ := xmpp.ParseJID(v.From)
|
||||||
processConfirm(v, confirmation)
|
if confirmation != nil && confirmation.JID == jidFrom.Bare() {
|
||||||
|
processConfirm(v, confirmation)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue