Public Repository

Last pushed: 2 years ago
Short Description
A tool to test reverse proxy and LDAP connection
Full Description

An Hello Wolrd HTTP Server in Go.

I use it as placeholder for testing reverse-proxy configuration or firewall rules.

To use it against container:8081 : docker run -d -p 8081:9999 ygbillet/http-hello.

URL

Available URL are :

  • /
  • /hello/<username>/
  • /ldap/

Path / check HTTP Header :

  • X-Forwarded-To
  • X-Real-Ip
  • X-Forwarded-Proto

Path /ldap/ check LDAP (not SSL) connection :

Server Code

package main // import "bitbucket.org/ygbillet/hello-http"

import (
    "fmt"
    "net/http"
    "strconv"

    "github.com/mavricknz/ldap"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "It's working\n")
    fmt.Fprintf(w, "Host:"+r.Host+"\n")
    fmt.Fprintf(w, "X-Forwarded-For :"+r.Header.Get("X-FORWARDED-FOR")+"\n")
    fmt.Fprintf(w, "X-Real-Ip :"+r.Header.Get("X-REAL-IP")+"\n")
    fmt.Fprintf(w, "X-Forwarded-Proto :"+r.Header.Get("X-FORWARDED-PROTO")+"\n")
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    remPartOfURL := r.URL.Path[len("/hello/"):] //get everything after the /hello/ part of the URL
    fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}

func ldapHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET" {
        fmt.Fprint(w, `<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form action="/ldap/" method="post">
    <label for="server">
        IP:
        <input name="server" id="server" type="text" />
    </label>
    <label for="port">
        Port:
        <input name="port" id="port" type="text" />
    </label>
    <label for="username">
        DN:
        <input name="username" id="username" type="text" />
    </label>
    <label for="password">
        Password:
        <input name="password" id="password" type="text" />
    </label>
    <br />
    <input type="submit" />
</form>
</body>
</html>`)
    } else {
        connect := "Connection to LDAP server : <b style='color:green'>OK</b><br />"
        result := "Bind to LDAP : <b style='color:green'>OK</b>"
        fmt.Fprint(w, "<!DOCTYPE html><html><head><title></title></head><body>")
        server := r.FormValue("server")
        username := r.FormValue("username")
        password := r.FormValue("password")
        port, err := strconv.ParseInt(r.FormValue("port"), 0, 16)
        if err != nil {
            fmt.Fprintf(w, "Cannot convert port %s\n", err.Error())
            return
        }
        l := ldap.NewLDAPConnection(server, uint16(port))
        err = l.Connect()
        if err != nil {
            connect = "Connection to LDAP server : <b style='color:red'>Failed</b><br />"
        }
        fmt.Fprint(w, connect)
        err = l.Bind(username, password)

        if err != nil {
            result = "Bind to LDAP : <b style='color:red'>Failed</b>"
        }
        l.Close()
        fmt.Fprint(w, result)
        fmt.Fprint(w, "</body></html>")
    }

}

func main() {
    fmt.Print("Running on 0.0.0.0:9999")
    http.HandleFunc("/", indexHandler)
    http.HandleFunc("/hello/", helloHandler)
    http.HandleFunc("/ldap/", ldapHandler)
    http.ListenAndServe("0.0.0.0:9999", nil)
}
Docker Pull Command
Owner
ygbillet

Comments (0)