This commit is contained in:
meera 2026-07-12 21:06:08 +03:00
parent 03c3c58811
commit 470b98d69c
2 changed files with 11 additions and 3 deletions

View File

@ -1,3 +1,3 @@
# thinIRC # Gocirc
a lightweight IRC server written in Golang a lightweight IRC server written in Golang

12
http.go
View File

@ -33,6 +33,7 @@ func startHTTP(cfg Config) {
func handleKick(w http.ResponseWriter, r *http.Request) { func handleKick(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
if !ok || !checkCredentials(user, pass) || !globalOps[user] { if !ok || !checkCredentials(user, pass) || !globalOps[user] {
w.Header().Set("WWW-Authenticate", `Basic realm="API"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
@ -88,6 +89,7 @@ func handleKick(w http.ResponseWriter, r *http.Request) {
func handleModeAPI(w http.ResponseWriter, r *http.Request) { func handleModeAPI(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
if !ok || !checkCredentials(user, pass) || !globalOps[user] { if !ok || !checkCredentials(user, pass) || !globalOps[user] {
w.Header().Set("WWW-Authenticate", `Basic realm="API"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
@ -142,7 +144,9 @@ func handleModeAPI(w http.ResponseWriter, r *http.Request) {
Status: "ok", Status: "ok",
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp) enc := json.NewEncoder(w)
enc.SetIndent(" ", " ")
enc.Encode(resp)
} }
type ChannelInfo struct { type ChannelInfo struct {
@ -154,6 +158,7 @@ type ChannelInfo struct {
func handleListAPI(w http.ResponseWriter, r *http.Request) { func handleListAPI(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
if !ok || !checkCredentials(user, pass) || !globalOps[user] { if !ok || !checkCredentials(user, pass) || !globalOps[user] {
w.Header().Set("WWW-Authenticate", `Basic realm="API"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }
@ -177,13 +182,16 @@ func handleListAPI(w http.ResponseWriter, r *http.Request) {
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(list); err != nil { enc := json.NewEncoder(w)
enc.SetIndent(" ", " ")
if err := enc.Encode(list); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func handleMOTDreload(w http.ResponseWriter, r *http.Request) { func handleMOTDreload(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
if !ok || !checkCredentials(user, pass) || !globalOps[user] { if !ok || !checkCredentials(user, pass) || !globalOps[user] {
w.Header().Set("WWW-Authenticate", `Basic realm="API"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
} }