summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-24 01:37:34 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-24 02:11:57 -0400
commitdbc93a011f442d09a18b7ae4ab34a8cfe64431dc (patch)
tree493dcd5a3639ef00b8f92c391727ddad7bdd2e71
parent6cf0e264b121b754bf8006914c23d3e45f9d0415 (diff)
downloadgetwtxt-dbc93a011f442d09a18b7ae4ab34a8cfe64431dc.tar.gz
checking for X-Forwarded-For header when adding user IP to context
-rw-r--r--http.go10
1 files changed, 9 insertions, 1 deletions
diff --git a/http.go b/http.go
index 8ba0442..98e0cb5 100644
--- a/http.go
+++ b/http.go
@@ -8,12 +8,20 @@ import (
 	"strings"
 )
 
-// Attaches a request's IP address to the request's context
+// Attaches a request's IP address to the request's context.
+// If getwtxt is behind a reverse proxy, get the last entry
+// in the X-Forwarded-For HTTP header as the user IP.
 func newCtxUserIP(ctx context.Context, r *http.Request) context.Context {
 
 	base := strings.Split(r.RemoteAddr, ":")
 	uip := base[0]
 
+	if _, ok := r.Header["X-Forwarded-For"]; ok {
+		proxied := r.Header["X-Forwarded-For"]
+		base = strings.Split(proxied[len(proxied)-1], ":")
+		uip = base[0]
+	}
+
 	return context.WithValue(ctx, ctxKey, uip)
 }
 
ae8'>a66c4a26 ^
8fa87054 ^
83868c7a ^
8fa87054 ^


a66c4a26 ^

f027adc0 ^
5c210a96 ^

3de15ddd ^

fb275079 ^

5c210a96 ^
3d566884 ^
e30d16cb ^
621a1a39 ^
9bc5d95c ^
c44b726e ^
465bff73 ^




f8e96a97 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                    
              
 

                                                                 
 











                                                                       
 

                                                                              

                                                                 
     
               
                                                                            


                                                                

        
 

                                                                

                                                           

                                                              
                              
    
                                        
                   
                  
                                                              




                                                               

              
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590