Last active
May 14, 2024 09:21
-
-
Save ijin82/fe2bec5bab8fc5d437f73ed5ef2c66d2 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