Skip to content

Commit

Permalink
feat: add client IP logging and implement map iteration utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
PlusOne committed Feb 12, 2025
1 parent ce4bcda commit 62ec2c0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,10 @@ func (p *WorkerPool) Shutdown() {
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
ips := getClientIPs(r)
for _, ip := range ips {
log.WithFields(logrus.Fields{"client_ip": ip}).Info("Incoming connection")
}
if r.Method == http.MethodPost && strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
absFilename, err := sanitizeFilePath(conf.Server.StoragePath, strings.TrimPrefix(r.URL.Path, "/"))
if err != nil {
Expand Down Expand Up @@ -2760,3 +2764,21 @@ func setupRouter() *http.ServeMux {
router.HandleFunc("/", handleRequest)
return router
}

func getClientIPs(r *http.Request) []string {
var ips []string
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
parts := strings.Split(xff, ",")
for _, part := range parts {
ips = append(ips, strings.TrimSpace(part))
}
}
if rip := r.Header.Get("X-Real-IP"); rip != "" {
ips = append(ips, rip)
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil && host != "" {
ips = append(ips, host)
}
return ips
}
69 changes: 69 additions & 0 deletions lib/maps/iter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package maps

// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

type Seq2[K comparable, V any] func(yield func(K, V) bool)
type Seq[K any] func(yield func(K) bool)

func All[Map ~map[K]V, K comparable, V any](m Map) Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, v := range m {
if !yield(k, v) {
return
}
}
}
}

// All returns an iterator over key-value pairs from m.
// The iteration order is not specified and is not guaranteed
// to be the same from one call to the next.

func Insert[Map ~map[K]V, K comparable, V any](m Map, seq Seq2[K, V]) {
seq(func(k K, v V) bool {
m[k] = v
return true
})
}

// Insert adds the key-value pairs from seq to m.
// If a key in seq already exists in m, its value will be overwritten.

func Collect[K comparable, V any](seq Seq2[K, V]) map[K]V {
m := make(map[K]V)
Insert(m, seq)
return m
}

// Collect collects key-value pairs from seq into a new map
// and returns it.

func Keys[Map ~map[K]V, K comparable, V any](m Map) Seq[K] {
return func(yield func(K) bool) {
for k := range m {
if !yield(k) {
return
}
}
}
}

// Keys returns an iterator over keys in m.
// The iteration order is not specified and is not guaranteed
// to be the same from one call to the next.

func Values[Map ~map[K]V, K comparable, V any](m Map) Seq[V] {
return func(yield func(V) bool) {
for _, v := range m {
if !yield(v) {
return
}
}
}
}

// Values returns an iterator over values in m.
// The iteration order is not specified and is not guaranteed
// to be the same from one call to the next.

0 comments on commit 62ec2c0

Please sign in to comment.