Add bind address in config (IPv4 and IPv6)

This commit is contained in:
Chteufleur 2016-08-21 21:22:07 +02:00
parent a1cd475788
commit c788a29d46
4 changed files with 40 additions and 11 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -1,12 +1,14 @@
# XMPP informations (component)
xmpp_server_address=192.168.1.2
xmpp_server_port=5347
xmpp_jid=xmppsteam.kingpenguin.tk
xmpp_jid=xmppsteamm.kingpenguin.tk
xmpp_secret=xmpp4steam_password
xmpp_debug=true
xmpp_verify_cert_validity=true
# HTTP informations
http_bind_address_ipv4=127.0.0.1
http_bind_address_ipv6=[::1]
http_port=9090
https_port=9093
https_cert_path=./cert.pem

View File

@ -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"]