37 lines
885 B
Go
37 lines
885 B
Go
package server
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// clientIP mengambil IP klien dari X-Forwarded-For (bila ada) atau RemoteAddr.
|
|
func clientIP(r *http.Request) string {
|
|
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
|
|
parts := strings.Split(fwd, ",")
|
|
if ip := strings.TrimSpace(parts[0]); ip != "" {
|
|
return ip
|
|
}
|
|
}
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
return r.RemoteAddr
|
|
}
|
|
return host
|
|
}
|
|
|
|
// log mencatat satu entri ke log aktivitas (audit trail).
|
|
func (s *Server) log(actor, action, target, ip, result, detail string) {
|
|
s.store.LogActivity(actor, action, target, ip, result, detail)
|
|
}
|
|
|
|
// actorIP mengembalikan (username sesi, IP) untuk pencatatan log.
|
|
func (s *Server) actorIP(r *http.Request) (string, string) {
|
|
username, _ := s.sessionUser(r)
|
|
if username == "" {
|
|
username = "anonim"
|
|
}
|
|
return username, clientIP(r)
|
|
}
|