From df795bf0ce7f36bf18c622db9d7f3cf739c0be6d Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Mon, 20 Jun 2016 19:20:13 +0200 Subject: [PATCH] Add HTTP port to config file --- http/http.go | 99 ++++++++++++++++++++++++------------------------ httpAuth.cfg | 2 + main.go | 100 +++++++++++++++++++++++++------------------------ xmpp/client.go | 59 ++++++++++++++--------------- xmpp/xmpp.go | 4 +- 5 files changed, 132 insertions(+), 132 deletions(-) diff --git a/http/http.go b/http/http.go index df22469..5b481d3 100644 --- a/http/http.go +++ b/http/http.go @@ -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) + } } diff --git a/httpAuth.cfg b/httpAuth.cfg index 72ffb10..16091e0 100644 --- a/httpAuth.cfg +++ b/httpAuth.cfg @@ -4,3 +4,5 @@ xmpp_server_port=5347 xmpp_hostname=xmppsteam.kingpenguin.tk xmpp_secret=xmpp4steam_password xmpp_debug=true + +http_port=9090 diff --git a/main.go b/main.go index c5c8832..c898d45 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/xmpp/client.go b/xmpp/client.go index 9fa698f..161d3f8 100644 --- a/xmpp/client.go +++ b/xmpp/client.go @@ -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 } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 34cc08a..7ce36ad 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -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.