Skip to content

Commit 44d59b1

Browse files
committed
Add config local_auth for api module
1 parent 15ec995 commit 44d59b1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal/api/api.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func Init() {
2424
Listen string `yaml:"listen"`
2525
Username string `yaml:"username"`
2626
Password string `yaml:"password"`
27+
LocalAuth bool `yaml:"local_auth"`
2728
BasePath string `yaml:"base_path"`
2829
StaticDir string `yaml:"static_dir"`
2930
Origin string `yaml:"origin"`
@@ -65,7 +66,7 @@ func Init() {
6566
}
6667

6768
if cfg.Mod.Username != "" {
68-
Handler = middlewareAuth(cfg.Mod.Username, cfg.Mod.Password, Handler) // 2nd
69+
Handler = middlewareAuth(cfg.Mod.Username, cfg.Mod.Password, cfg.Mod.LocalAuth, Handler) // 2nd
6970
}
7071

7172
if log.Trace().Enabled() {
@@ -203,9 +204,13 @@ func middlewareLog(next http.Handler) http.Handler {
203204
})
204205
}
205206

206-
func middlewareAuth(username, password string, next http.Handler) http.Handler {
207+
func isLoopback(remoteAddr string) bool {
208+
return strings.HasPrefix(remoteAddr, "127.") || strings.HasPrefix(remoteAddr, "[::1]") || remoteAddr == "@"
209+
}
210+
211+
func middlewareAuth(username, password string, localAuth bool, next http.Handler) http.Handler {
207212
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
208-
if !strings.HasPrefix(r.RemoteAddr, "127.") && !strings.HasPrefix(r.RemoteAddr, "[::1]") && r.RemoteAddr != "@" {
213+
if localAuth || !isLoopback(r.RemoteAddr) {
209214
user, pass, ok := r.BasicAuth()
210215
if !ok || user != username || pass != password {
211216
w.Header().Set("Www-Authenticate", `Basic realm="go2rtc"`)

0 commit comments

Comments
 (0)