From 0b0611fc4568f54123d5bbc3337d8357fab99346 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sun, 21 Aug 2016 21:16:35 +0200 Subject: [PATCH] Add bind address in config (IPv4 and IPv6) --- README.md | 4 ++++ http/http.go | 35 +++++++++++++++++++++++++---------- httpAuth.cfg | 2 ++ main.go | 8 ++++++++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 38aa031..11baf6f 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,13 @@ HTTP * https_cert_path : Path to the certificate file (default: ./cert.pem) * https_key_path : Path to the key file (default: ./key.pem) * http_timeout_sec : Define a timeout if user did not give an answer to the request (default: 60) + * http_bind_address_ipv4 : Bind address on IPv4 (default: 127.0.0.1) + * http_bind_address_ipv6 : Bind address on IPv6 (default: [::1]) __Bold config__ are mandatory. +If ``http_bind_address_ipv4`` is set to ``0.0.0.0``, it will bind all address on IPv4 __AND__ IPv6. + ### Usage To ask authorization, just send an HTTP request to the path ``/auth`` with parameters: * __jid__ : JID of the user (user@host/resource or user@host) diff --git a/http/http.go b/http/http.go index df01d76..7776652 100644 --- a/http/http.go +++ b/http/http.go @@ -44,6 +44,9 @@ var ( ChanRequest = make(chan interface{}, 5) TimeoutSec = 60 // 1 min MaxTimeout = 300 // 5 min + + BindAddressIPv4 = "127.0.0.1" + BindAddressIPv6 = "[::1]" ) func init() { @@ -125,32 +128,44 @@ func Run() { http.HandleFunc(ROUTE_AUTH, authHandler) if HttpPortBind > 0 { - go runHttp() + go runHttp(BindAddressIPv4) + if BindAddressIPv4 != "0.0.0.0" { + go runHttp(BindAddressIPv6) + } } else if HttpPortBind == 0 { HttpPortBind = rand.Intn(MAX_PORT_VAL) - go runHttp() + go runHttp(BindAddressIPv4) + if BindAddressIPv4 != "0.0.0.0" { + go runHttp(BindAddressIPv6) + } } if HttpsPortBind > 0 { - go runHttps() + go runHttps(BindAddressIPv4) + if BindAddressIPv6 != "0.0.0.0" { + go runHttps(BindAddressIPv6) + } } else if HttpsPortBind == 0 { HttpsPortBind = rand.Intn(MAX_PORT_VAL) - go runHttps() + go runHttps(BindAddressIPv4) + if BindAddressIPv6 != "0.0.0.0" { + go runHttps(BindAddressIPv6) + } } } -func runHttp() { +func runHttp(bindAddress string) { port := strconv.Itoa(HttpPortBind) - log.Printf("%sHTTP listenning on port %s", LogInfo, port) - err := http.ListenAndServe(":"+port, nil) + log.Printf("%sHTTP listenning on %s:%s", LogInfo, bindAddress, port) + err := http.ListenAndServe(bindAddress+":"+port, nil) if err != nil { log.Fatal("%sListenAndServe: ", LogError, err) } } -func runHttps() { +func runHttps(bindAddress string) { port := strconv.Itoa(HttpsPortBind) - log.Printf("%sHTTPS listenning on port %s", LogInfo, port) - err := http.ListenAndServeTLS(":"+port, CertPath, KeyPath, nil) + log.Printf("%sHTTPS listenning on %s:%s", LogInfo, bindAddress, port) + err := http.ListenAndServeTLS(bindAddress+":"+port, CertPath, KeyPath, nil) if err != nil { log.Fatal("%sListenAndServe: ", LogError, err) } diff --git a/httpAuth.cfg b/httpAuth.cfg index 02fb041..b39d5eb 100644 --- a/httpAuth.cfg +++ b/httpAuth.cfg @@ -7,6 +7,8 @@ xmpp_debug=true xmpp_verify_cert_validity=true # HTTP informations +http_bind_address_ipv4=192.168.1.143 +http_bind_address_ipv6=[2001:41d0:fe26:eb00:a8c0:6a57:f3d4:d07c] http_port=9090 https_port=9093 https_cert_path=./cert.pem diff --git a/main.go b/main.go index 1c820d5..35e7bbf 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,14 @@ func init() { http.CertPath = mapConfig["https_cert_path"] http.KeyPath = mapConfig["https_key_path"] } + bindAddressIPv4 := mapConfig["http_bind_address_ipv4"] + if bindAddressIPv4 != "" { + http.BindAddressIPv4 = bindAddressIPv4 + } + bindAddressIPv6 := mapConfig["http_bind_address_ipv6"] + if bindAddressIPv6 != "" { + http.BindAddressIPv6 = bindAddressIPv6 + } // XMPP config xmpp_server_address := mapConfig["xmpp_server_address"]