Add first part for XMPP side

This commit is contained in:
Chteufleur 2016-05-18 22:56:18 +02:00
parent 44a8e325c6
commit 4be53d2f7e
5 changed files with 175 additions and 18 deletions

View File

@ -9,6 +9,10 @@ import (
)
const (
LogInfo = "\t[HTTP INFO]\t"
LogError = "\t[HTTP ERROR]\t"
LogDebug = "\t[HTTP DEBUG]\t"
PARAM_JID = "jid"
METHOD_ACCESS = "method"
DOMAIN_ACCESS = "domain"
@ -32,12 +36,18 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
func authHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
jid := strings.Join(r.Form[PARAM_JID], "")
method := strings.Join(r.Form[METHOD_ACCESS], "")
domain := strings.Join(r.Form[DOMAIN_ACCESS], "")
transaction := strings.Join(r.Form[TRANSACTION_ID], "")
log.Printf("%sAuth %s", LogDebug, jid)
chanAnswer := make(chan bool)
ChanRequest <- strings.Join(r.Form[PARAM_JID], "")
ChanRequest <- strings.Join(r.Form[METHOD_ACCESS], "")
ChanRequest <- strings.Join(r.Form[DOMAIN_ACCESS], "")
ChanRequest <- strings.Join(r.Form[TRANSACTION_ID], "")
ChanRequest <- jid
ChanRequest <- method
ChanRequest <- domain
ChanRequest <- transaction
ChanRequest <- chanAnswer
answer := <- chanAnswer
@ -51,13 +61,14 @@ func authHandler(w http.ResponseWriter, r *http.Request) {
func Run() {
log.Printf("%sRunning", LogInfo)
http.HandleFunc("/", indexHandler) // set router
http.HandleFunc("/toto", authHandler)
port := strconv.Itoa(HttpPortBind)
log.Println("Listenning on port "+port)
log.Printf("%sListenning on port %s", LogInfo, port)
err := http.ListenAndServe(":"+port, nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
log.Fatal("%sListenAndServe: ", LogError, err)
}
}

View File

@ -0,0 +1,6 @@
# XMPP informations
xmpp_server_address=192.168.1.2
xmpp_server_port=5347
xmpp_hostname=xmppsteam.kingpenguin.tk
xmpp_secret=xmpp4steam_password
xmpp_debug=true

39
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"github.com/jimlawless/cfg"
@ -26,32 +27,46 @@ func init() {
if err != nil {
log.Fatal("Failed to load configuration file.", err)
}
// TODO make config
// XMPP config
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
xmpp.JidStr = mapConfig["xmpp_hostname"]
xmpp.Secret = mapConfig["xmpp_secret"]
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
}
func request() {
for {
jid := <- http.ChanRequest
log.Println(jid)
method := <- http.ChanRequest
log.Println(method)
domain := <- http.ChanRequest
log.Println(domain)
transaction := <- http.ChanRequest
log.Println(transaction)
client := new(xmpp.Client)
client.JID = getChanString(http.ChanRequest)
client.Method = getChanString(http.ChanRequest)
client.Domain = getChanString(http.ChanRequest)
client.Transaction = getChanString(http.ChanRequest)
chanResult := <- http.ChanRequest
// TODO make the XMPP request
if v, ok := chanResult.(chan bool); ok {
v <- false
client.ChanReply = v
}
go client.QueryClient()
}
}
func getChanString(c chan interface{}) string {
ret := ""
i := <- c
if v, ok := i.(string); ok {
ret = v
}
return ret
}
func main() {
// TODO start ressources
go http.Run()
go xmpp.Run()
go request()
sigchan := make(chan os.Signal, 1)

20
xmpp/client.go Normal file
View File

@ -0,0 +1,20 @@
package xmpp
import (
"log"
)
type Client struct {
JID string
Method string
Domain string
Transaction string
ChanReply chan bool
}
func (client *Client) QueryClient() {
log.Printf("%sQuery JID %s", LogInfo, client.JID)
client.ChanReply <- false
}

105
xmpp/xmpp.go Normal file
View File

@ -0,0 +1,105 @@
package xmpp
import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"log"
)
const (
LogInfo = "\t[XMPP COMPONENT INFO]\t"
LogError = "\t[XMPP COMPONENT ERROR]\t"
LogDebug = "\t[XMPP COMPONENT DEBUG]\t"
)
var (
Addr = "127.0.0.1:5347"
JidStr = ""
Secret = ""
SoftVersion = ""
jid xmpp.JID
stream = new(xmpp.Stream)
comp = new(xmpp.XMPP)
ChanAction = make(chan string)
Debug = true
)
func Run() {
log.Printf("%sRunning", LogInfo)
// Create stream and configure it as a component connection.
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream)
comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP)
mainXMPP()
log.Printf("%sReach main method's end", LogInfo)
go Run()
}
func mainXMPP() {
for x := range comp.In {
switch v := x.(type) {
case *xmpp.Presence:
case *xmpp.Message:
case *xmpp.Iq:
switch v.PayloadName().Space {
case xmpp.NSDiscoItems:
execDiscoCommand(v)
case xmpp.NSVCardTemp:
reply := v.Response(xmpp.IQTypeResult)
vcard := &xmpp.VCard{}
reply.PayloadEncode(vcard)
comp.Out <- reply
case xmpp.NSJabberClient:
reply := v.Response(xmpp.IQTypeResult)
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "HTTP authentification component", Version: SoftVersion})
comp.Out <- reply
default:
reply := v.Response(xmpp.IQTypeError)
reply.PayloadEncode(xmpp.NewError("cancel", xmpp.FeatureNotImplemented, ""))
comp.Out <- reply
}
default:
log.Printf("%srecv: %v", LogDebug, x)
}
}
}
func must(v interface{}, err error) interface{} {
if err != nil {
log.Fatal(LogError, err)
}
return v
}
func SendMessage(to, subject, message string) {
m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"}
if subject != "" {
m.Subject = subject
}
log.Printf("%sSenp message %v", LogInfo, m)
comp.Out <- m
}
func execDiscoCommand(iq *xmpp.Iq) {
log.Printf("%sDiscovery item iq received", LogInfo)
reply := iq.Response(xmpp.IQTypeResult)
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
reply.PayloadEncode(discoItem)
comp.Out <- reply
}