Add HTTP port to config file

This commit is contained in:
Chteufleur 2016-06-20 19:20:13 +02:00
parent b9f3db7998
commit df795bf0ce
5 changed files with 132 additions and 132 deletions

View File

@ -1,14 +1,14 @@
package http
import (
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
)
const (
@ -16,68 +16,65 @@ const (
LogError = "\t[HTTP ERROR]\t"
LogDebug = "\t[HTTP DEBUG]\t"
PARAM_JID = "jid"
METHOD_ACCESS = "method"
DOMAIN_ACCESS = "domain"
TRANSACTION_ID = "transaction_id"
PARAM_JID = "jid"
METHOD_ACCESS = "method"
DOMAIN_ACCESS = "domain"
TRANSACTION_ID = "transaction_id"
ROUTE_ROOT = "/"
ROUTE_AUTH = "/toto"
ROUTE_ROOT = "/"
ROUTE_AUTH = "/auth"
RETURN_VALUE_OK = "OK"
RETURN_VALUE_NOK = "NOK"
RETURN_VALUE_OK = "OK"
RETURN_VALUE_NOK = "NOK"
)
var (
HttpPortBind = 9090
HttpPortBind = 9090
ChanRequest = make(chan interface{}, 5)
TimeoutSec = 60
ChanRequest = make(chan interface{}, 5)
TimeoutSec = 60
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
// TODO
fmt.Fprintf(w, "Welcome to HTTP authentification over XMPP")
// TODO
fmt.Fprintf(w, "Welcome to HTTP authentification over XMPP")
}
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)
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)
chanAnswer := make(chan bool)
ChanRequest <- jid
ChanRequest <- method
ChanRequest <- domain
ChanRequest <- transaction
ChanRequest <- chanAnswer
ChanRequest <- jid
ChanRequest <- method
ChanRequest <- domain
ChanRequest <- transaction
ChanRequest <- chanAnswer
select {
case answer = <- chanAnswer:
w.WriteHeader(http.StatusOK)
case <- time.After(time.Duration(TimeoutSec) * time.Second):
w.WriteHeader(http.StatusForbidden)
delete(xmpp.WaitMessageAnswers, transaction)
}
select {
case answer = <-chanAnswer:
w.WriteHeader(http.StatusOK)
case <-time.After(time.Duration(TimeoutSec) * time.Second):
w.WriteHeader(http.StatusForbidden)
delete(xmpp.WaitMessageAnswers, transaction)
}
}
func Run() {
log.Printf("%sRunning", LogInfo)
log.Printf("%sRunning", LogInfo)
http.HandleFunc(ROUTE_ROOT, indexHandler)
http.HandleFunc(ROUTE_AUTH, authHandler)
http.HandleFunc(ROUTE_ROOT, indexHandler)
http.HandleFunc(ROUTE_AUTH, authHandler)
port := strconv.Itoa(HttpPortBind)
log.Printf("%sListenning on port %s", LogInfo, port)
err := http.ListenAndServe(":"+port, nil) // set listen port
if err != nil {
log.Fatal("%sListenAndServe: ", LogError, err)
}
port := strconv.Itoa(HttpPortBind)
log.Printf("%sListenning on port %s", LogInfo, port)
err := http.ListenAndServe(":"+port, nil) // set listen port
if err != nil {
log.Fatal("%sListenAndServe: ", LogError, err)
}
}

View File

@ -4,3 +4,5 @@ xmpp_server_port=5347
xmpp_hostname=xmppsteam.kingpenguin.tk
xmpp_secret=xmpp4steam_password
xmpp_debug=true
http_port=9090

100
main.go
View File

@ -1,15 +1,15 @@
package main
import (
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"github.com/jimlawless/cfg"
"github.com/jimlawless/cfg"
"log"
"os"
"log"
"os"
"os/signal"
"strconv"
"strconv"
"syscall"
"time"
)
@ -24,67 +24,71 @@ var (
)
func init() {
err := cfg.Load(configurationFilePath, mapConfig)
err := cfg.Load(configurationFilePath, mapConfig)
if err != nil {
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
}
// 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
}
httpPort, err := strconv.Atoi(mapConfig["http_port"])
if err == nil {
log.Println("Define HTTP port to %d", httpPort)
http.HttpPortBind = httpPort
}
// 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"
// 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 {
client := new(xmpp.Client)
for {
client := new(xmpp.Client)
client.JID = getChanString(http.ChanRequest)
client.Method = getChanString(http.ChanRequest)
client.Domain = getChanString(http.ChanRequest)
client.Transaction = getChanString(http.ChanRequest)
client.JID = getChanString(http.ChanRequest)
client.Method = getChanString(http.ChanRequest)
client.Domain = getChanString(http.ChanRequest)
client.Transaction = getChanString(http.ChanRequest)
chanResult := <- http.ChanRequest
if v, ok := chanResult.(chan bool); ok {
client.ChanReply = v
}
chanResult := <-http.ChanRequest
if v, ok := chanResult.(chan bool); ok {
client.ChanReply = v
}
go client.QueryClient()
}
go client.QueryClient()
}
}
func getChanString(c chan interface{}) string {
ret := ""
i := <- c
if v, ok := i.(string); ok {
ret = v
}
return ret
ret := ""
i := <-c
if v, ok := i.(string); ok {
ret = v
}
return ret
}
func main() {
go http.Run()
go xmpp.Run()
go request()
go http.Run()
go xmpp.Run()
go request()
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
signal.Notify(sigchan, os.Kill)
<-sigchan
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
signal.Notify(sigchan, os.Kill)
<-sigchan
// TODO close all ressources
// TODO close all ressources
log.Println("Exit main()")
time.Sleep(1 * time.Second)
log.Println("Exit main()")
time.Sleep(1 * time.Second)
}

View File

@ -1,50 +1,49 @@
package xmpp
import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"log"
"strconv"
"log"
"strconv"
)
type Client struct {
JID string
Method string
Domain string
Transaction string
JID string
Method string
Domain string
Transaction string
ChanReply chan bool
ChanReply chan bool
}
func (client *Client) QueryClient() {
log.Printf("%sQuery JID %s", LogInfo, client.JID)
clientJID, _ := xmpp.ParseJID(client.JID)
if clientJID.Resource == "" {
client.askViaMessage()
} else {
client.askViaIQ()
}
log.Printf("%sQuery JID %s", LogInfo, client.JID)
clientJID, _ := xmpp.ParseJID(client.JID)
if clientJID.Resource == "" {
client.askViaMessage()
} else {
client.askViaIQ()
}
}
func (client *Client) askViaIQ() {
stanzaID++
stanzaIDstr := strconv.Itoa(stanzaID)
m := xmpp.Iq{Type: xmpp.IQTypeGet, To: client.JID, From: jid.Domain, Id: stanzaIDstr}
confirm := &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
m.PayloadEncode(confirm)
WaitMessageAnswers[stanzaIDstr] = client
comp.Out <- m
stanzaID++
stanzaIDstr := strconv.Itoa(stanzaID)
m := xmpp.Iq{Type: xmpp.IQTypeGet, To: client.JID, From: jid.Domain, Id: stanzaIDstr}
confirm := &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
m.PayloadEncode(confirm)
WaitMessageAnswers[stanzaIDstr] = client
comp.Out <- m
}
func (client *Client) askViaMessage() {
m := xmpp.Message{From: jid.Domain, To: client.JID, Type: "normal"}
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."
m.Confir = &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
m.Thread = xmpp.SessionID()
m.Body = "Auth request for " + client.Domain + ".\nTransaction identifier is: " + client.Transaction + "\nReply to this message to confirm the request."
m.Confir = &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
log.Printf("%sSenp message %v", LogInfo, m)
WaitMessageAnswers[client.Transaction] = client
comp.Out <- m
log.Printf("%sSenp message %v", LogInfo, m)
WaitMessageAnswers[client.Transaction] = client
comp.Out <- m
}

View File

@ -27,13 +27,11 @@ var (
ChanAction = make(chan string)
WaitMessageAnswers = make(map[string]*Client)
WaitMessageAnswers = make(map[string]*Client)
Debug = true
)
func Run() {
log.Printf("%sRunning", LogInfo)
// Create stream and configure it as a component connection.