okami.mom/web-extract-go
git clone https://git.nkp.pet/okami.mom/web-extract-go
8af1784
main
1package app 2 3import ( 4"context" 5"crypto/sha256" 6"crypto/subtle" 7"io/fs" 8"log/slog" 9"mime" 10"net/http" 11"strconv" 12"strings" 13"sync" 14"time" 15 16 adminui"web-extract/web" 17) 18 19const adminCookie = "web_extract_admin" 20const adminSessionTTL = 12 * time .Hour 21 22// Sessions are bounded and process-local: logout revokes them and restart clears them. 23type adminAuth struct { 24mu sync.Mutex 25sessions map [string ]time.Time 26failures int 27window time.Time 28} 29 30func (s * Service )handler () http.Handler { 31api := s .apiHandler () 32mux := http .NewServeMux () 33admin := s .adminHandler (api ) 34mux .Handle ("/admin" ,admin ) 35mux .Handle ("/admin/" ,admin ) 36mux .Handle ("/" ,api ) 37return mux 38} 39 40func (s * Service )adminHandler (api http.Handler ) http.Handler { 41auth := & adminAuth {sessions :make (map [string ]time.Time )} 42assets ,err := fs .Sub (adminui .Files ,"dist" ) 43if err != nil { 44panic (err ) 45 } 46files := http .StripPrefix ("/admin/" ,http .FileServer (http .FS (assets ))) 47routes := http .NewServeMux () 48routes .HandleFunc ("POST /admin/api/login" ,func (w http.ResponseWriter ,r * http.Request ) { 49var input struct { 50Password string `json:"password"` 51 } 52body ,err := readBounded (r .Body ,4096 ) 53if err != nil || strictJSON (body ,& input )!= nil { 54adminError (w ,400 ,"provide a password as json" ) 55return 56 } 57auth .mu .Lock () 58defer auth .mu .Unlock () 59now := time .Now () 60if now .Sub (auth .window )>= time .Minute { 61auth .window ,auth .failures = now ,0 62 } 63if auth .failures >= 10 { 64w .Header ().Set ("Retry-After" ,"60" ) 65adminError (w ,429 ,"too many login attempts; try again in a minute" ) 66return 67 } 68got ,want := sha256 .Sum256 ([]byte (input .Password )),sha256 .Sum256 ([]byte (s .cfg .AdminPassword )) 69if subtle .ConstantTimeCompare (got [:],want [:])!= 1 { 70auth .failures ++ 71adminError (w ,401 ,"incorrect password" ) 72return 73 } 74for token ,expiry := range auth .sessions { 75if ! expiry .After (now ) { 76delete (auth .sessions ,token ) 77 } 78 } 79if len (auth .sessions )>= 128 { 80adminError (w ,429 ,"too many active sessions; sign out of another browser" ) 81return 82 } 83token := newID ()+ newID () 84auth .sessions [token ]= now .Add (adminSessionTTL ) 85s .adminSetCookie (w ,r ,token ,int (adminSessionTTL .Seconds ())) 86writeJSON (w ,200 ,map [string ]bool {"authenticated" :true }) 87 }) 88 89protected := http .NewServeMux () 90protected .HandleFunc ("GET /admin/api/session" ,func (w http.ResponseWriter ,r * http.Request ) { 91writeJSON (w ,200 ,map [string ]bool {"authenticated" :true }) 92 }) 93protected .HandleFunc ("POST /admin/api/logout" ,func (w http.ResponseWriter ,r * http.Request ) { 94cookie ,_ := r .Cookie (adminCookie ) 95auth .mu .Lock () 96delete (auth .sessions ,cookie .Value ) 97auth .mu .Unlock () 98s .adminSetCookie (w ,r ,"" ,- 1 ) 99writeJSON (w ,200 ,map [string ]bool {"authenticated" :false }) 100 }) 101protected .HandleFunc ("GET /admin/api/overview" ,func (w http.ResponseWriter ,r * http.Request ) { 102data ,err := s .adminOverview (r .Context ()) 103adminRespond (w ,data ,err ) 104 }) 105protected .HandleFunc ("GET /admin/api/requests" ,func (w http.ResponseWriter ,r * http.Request ) { 106before := int64 (0 ) 107if raw := r .URL .Query ().Get ("before" );raw != "" { 108var err error 109before ,err = strconv .ParseInt (raw ,10 ,64 ) 110if err != nil || before < 1 { 111adminError (w ,400 ,"invalid pagination cursor" ) 112return 113 } 114 } 115endpoint := r .URL .Query ().Get ("endpoint" ) 116if endpoint != "" && endpoint != "search" && endpoint != "contents" && endpoint != "answer" { 117adminError (w ,400 ,"invalid endpoint filter" ) 118return 119 } 120data ,err := s .adminRequests (r .Context (),before ,endpoint ,r .URL .Query ().Get ("errors" )== "true" ) 121adminRespond (w ,data ,err ) 122 }) 123protected .HandleFunc ("GET /admin/api/requests/{id}" ,func (w http.ResponseWriter ,r * http.Request ) { 124id := r .PathValue ("id" ) 125if len (id )> 128 { 126adminError (w ,400 ,"invalid request id" ) 127return 128 } 129data ,err := s .log .adminTrace (r .Context (),id ) 130if err == nil && len (data .Requests )== 0 { 131adminError (w ,404 ,"request not found" ) 132return 133 } 134adminRespond (w ,data ,err ) 135 }) 136// Reuse the exact public API validation, execution, response, and training logs. 137// The API token never travels to the browser, and cookie auth never grants /mcp access. 138for _ ,endpoint := range []string {"search" ,"contents" ,"answer" } { 139protected .HandleFunc ("POST /admin/api/" + endpoint ,func (w http.ResponseWriter ,r * http.Request ) { 140r = r .Clone (r .Context ()) 141r .URL .Path = "/" + endpoint 142r .Header .Set ("x-api-key" ,s .cfg .Token ) 143r .Header .Set ("x-caller" ,"admin-dashboard" ) 144api .ServeHTTP (w ,r ) 145 }) 146 } 147protected .HandleFunc ("/" ,func (w http.ResponseWriter ,r * http.Request ) {adminError (w ,404 ,"unknown admin endpoint" ) }) 148routes .Handle ("/admin/api/" ,http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 149cookie ,err := r .Cookie (adminCookie ) 150auth .mu .Lock () 151valid := err == nil && auth .sessions [cookieValue (cookie )].After (time .Now ()) 152auth .mu .Unlock () 153if ! valid { 154adminError (w ,401 ,"sign in to continue" ) 155return 156 } 157ctx ,cancel := context .WithTimeout (r .Context (),s .cfg .RequestTimeout ) 158defer cancel () 159protected .ServeHTTP (w ,r .WithContext (ctx )) 160 })) 161routes .HandleFunc ("/admin" ,func (w http.ResponseWriter ,r * http.Request ) { 162http .Redirect (w ,r ,"/admin/" ,http .StatusTemporaryRedirect ) 163 }) 164routes .HandleFunc ("/admin/" ,func (w http.ResponseWriter ,r * http.Request ) { 165if r .Method != "GET" && r .Method != "HEAD" { 166w .Header ().Set ("Allow" ,"GET, HEAD" ) 167adminError (w ,405 ,"use get" ) 168return 169 } 170if r .URL .Path != "/admin/" && ! strings .HasPrefix (r .URL .Path ,"/admin/assets/" ) { 171http .NotFound (w ,r ) 172return 173 } 174files .ServeHTTP (w ,r ) 175 }) 176csrf := http .NewCrossOriginProtection () 177csrf .SetDenyHandler (http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 178adminError (w ,403 ,"cross-origin requests are not allowed" ) 179 })) 180handler := csrf .Handler (routes ) 181return http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) { 182if s .cfg .AdminPassword == "" { 183http .NotFound (w ,r ) 184return 185 } 186w .Header ().Set ("Cache-Control" ,"no-store" ) 187w .Header ().Set ("X-Content-Type-Options" ,"nosniff" ) 188w .Header ().Set ("X-Frame-Options" ,"DENY" ) 189w .Header ().Set ("Referrer-Policy" ,"no-referrer" ) 190w .Header ().Set ("Content-Security-Policy" ,"default-src 'none'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self' data:; font-src 'self'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'" ) 191if r .Method == "POST" { 192media ,_ ,_ := mime .ParseMediaType (r .Header .Get ("Content-Type" )) 193if media != "application/json" { 194adminError (w ,415 ,"use application/json" ) 195return 196 } 197 } 198handler .ServeHTTP (w ,r ) 199 }) 200} 201 202func cookieValue (c * http.Cookie )string { 203if c == nil { 204return "" 205 } 206return c .Value 207} 208 209func (s * Service )adminSetCookie (w http.ResponseWriter ,r * http.Request ,value string ,age int ) { 210http .SetCookie (w ,& http.Cookie {Name :adminCookie ,Value :value ,Path :"/admin" ,HttpOnly :true , 211Secure :s .cfg .AdminCookieSecure || r .TLS != nil ,SameSite :http .SameSiteStrictMode ,MaxAge :age }) 212} 213 214func adminError (w http.ResponseWriter ,status int ,message string ) { 215writeJSON (w ,status ,map [string ]string {"error" :message }) 216} 217func adminRespond (w http.ResponseWriter ,data any ,err error ) { 218if err != nil { 219slog .Error ("admin query failed" ,"error" ,err ) 220adminError (w ,500 ,"could not read the activity log; try refreshing" ) 221return 222 } 223writeJSON (w ,200 ,data ) 224}