-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (103 loc) · 3.19 KB
/
main.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"runtime"
"strings"
)
func main() {
// Backend Server
bs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("---------------- Backend Server ------------------")
dump, err := httputil.DumpRequest(r, false)
if err != nil {
fmt.Printf("Backend httputil.DumpRequest failed: %w\n", err)
return
}
fmt.Printf(string(dump))
fmt.Println("--- Backend Server が受信した RemoteAddr ---")
fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)
fmt.Println()
ip := r.RemoteAddr
xff := r.Header.Get("X-Forwarded-For")
if xff != "" {
ips := strings.Split(xff, ",")
ip = strings.TrimSpace(ips[0])
}
fmt.Println("--- 最終的な X-Forwarded-For と送信元IP ---")
fmt.Printf("X-Forwarded-For: %s\n", xff)
fmt.Printf("Source IP: %s\n", ip)
fmt.Println()
if ip == "127.0.0.1" {
f, err := os.Open("flag.txt")
if err != nil {
fmt.Printf("os.Open failed: %v\n", err)
return
}
defer f.Close()
answer, err := ioutil.ReadAll(f)
if err != nil {
fmt.Printf("ioutil.ReadAll failed: %v\n", err)
return
}
fmt.Printf("正解!秘密の答えをレスポンスしました!\n")
fmt.Fprintf(w, string(answer))
} else {
fmt.Printf("残念!送信元IP が 127.0.0.1 になるようにリクエストを送ってください!(%s != 127.0.0.1)\n", ip)
fmt.Fprintf(w, "もっと頑張りましょう!")
}
fmt.Println("==========================")
}))
defer bs.Close()
// Middle Proxy Server
mp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("---------------- Middle Proxy ----------------")
dump, err := httputil.DumpRequest(r, false)
if err != nil {
fmt.Printf("Middle Proxy httputil.DumpRequest failed: %v\n", err)
return
}
fmt.Printf(string(dump))
fmt.Println("--- Middle Proxy が受信した RemoteAddr ---")
fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)
fmt.Println()
burl, err := url.Parse(bs.URL)
if err != nil {
fmt.Printf("Front Proxy url.Parse failed: %v\n", err)
return
}
httputil.NewSingleHostReverseProxy(burl).ServeHTTP(w, r)
}))
defer mp.Close()
// Front Proxy Server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("---------------- Front Proxy ----------------")
r.Header.Del("X-Forwarded-For")
dump, err := httputil.DumpRequest(r, false)
if err != nil {
fmt.Printf("Front Proxy httputil.DumpRequest failed: %v\n", err)
return
}
fmt.Printf(string(dump))
fmt.Println("--- Front Proxy が受信した RemoteAddr ---")
fmt.Printf("RemoteAddr: %s\n", r.RemoteAddr)
fmt.Println()
murl, err := url.Parse(mp.URL)
if err != nil {
fmt.Printf("Front Proxy url.Parse failed: %v\n", err)
return
}
httputil.NewSingleHostReverseProxy(murl).ServeHTTP(w, r)
})
// Run server
fmt.Printf("========================================================\n")
fmt.Printf("Welcome to Kanmu Office hour @ Go Conference 2021 Autumn\n")
fmt.Printf(`"Go" beyond your proxy! `)
fmt.Printf("Go version: %s\n", runtime.Version())
http.ListenAndServe(":8000", nil)
}