Skip to content

Instantly share code, notes, and snippets.

@ijin82
Last active May 14, 2024 09:21
Show Gist options
  • Save ijin82/fe2bec5bab8fc5d437f73ed5ef2c66d2 to your computer and use it in GitHub Desktop.
Save ijin82/fe2bec5bab8fc5d437f73ed5ef2c66d2 to your computer and use it in GitHub Desktop.
Simple HTTP proxy with authorization on golang
package main
import (
"github.com/elazarl/goproxy"
"github.com/elazarl/goproxy/ext/auth"
"log"
"net/http"
"flag"
)
var portNumber = flag.String("port", "12345", "port number")
var basicAuthUser = flag.String("user", "admin", "basic auth user name")
var basicAuthPass = flag.String("pass", "secret", "basic auth user pass")
func main() {
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = false
if *basicAuthUser != "" && *basicAuthPass != "" {
log.Println("auth basic")
auth.ProxyBasic(proxy, "RELM", func(user, pass string) bool {
log.Println("auth basic do")
return user == *basicAuthUser && pass == *basicAuthPass
})
}
log.Println("listen:" + *portNumber)
log.Fatal(http.ListenAndServe(":" + *portNumber, proxy))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment