-
-
Save hightemp/403acfae29b41101a27d12d0c108fb49 to your computer and use it in GitHub Desktop.
Simple HTTP proxy with authorization on golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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