-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipcat.go
93 lines (74 loc) · 2.67 KB
/
ipcat.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
/*
Author: zuoguocai@126.com
*/
package main
import (
"io"
"os"
log "github.com/sirupsen/logrus"
"net/http"
// "net/http/httputil"
"strings"
"go.elastic.co/apm/module/apmhttp"
)
func main() {
fs := http.FileServer(http.Dir("./nginx/live2d"))
mux := http.NewServeMux()
mux.Handle("/live2d/", http.StripPrefix("/live2d/", fs))
mux.HandleFunc("/", GetRealIP)
log.Info("Server starting ...")
var logFile string = "/var/log/ipcat/ipcat.log"
var file, err = os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Errorf("Could Not Open Log File : " + err.Error())
}
log.SetOutput(file)
//log.SetFormatter(&log.TextFormatter{})
log.SetFormatter(&log.JSONFormatter{})
if err := http.ListenAndServe(":5000", apmhttp.Wrap(mux));err != nil {
log.Errorf("Httpserver: ListenAndServe() error: %s", err)
}
}
func GetRealIP(w http.ResponseWriter, r *http.Request) {
//dump, _ := httputil.DumpRequest(r, false)
//log.Printf("%q\n", dump)
log.WithFields(log.Fields{
"ip": r.Header.Get("X-Real-Ip"),
"url": r.URL.String() ,
"referer": r.Header.Get("Referer"),
"method": r.Method,
"userAgent": r.Header.Get("User-Agent"),
}).Info()
head := `<!doctype html><html lang="zh"><head><meta charset="UTF-8"><title>DevOps Pipeline Demo</title></head><body><h1 align="center" style="color:red;">ipcat v4.0</h1>`
r1 := strings.Join([]string{"<h3 style='background-color:powderblue;'>","RemoteAddr: ",r.RemoteAddr,"</h3>"},"")
r2 := strings.Join([]string{"<h3 style='background-color:#DDA0DD;'>","X-Original-Forwarded-For: ",r.Header.Get("X-Original-Forwarded-For"),"</h3>"},"")
r3 := strings.Join([]string{"<h3 style='background-color:powderblue;'>","X-Forwarded-For: ",r.Header.Get("X-Forwarded-For"),"</h3>"},"")
r4 := strings.Join([]string{"<h3 style='background-color:#DDA0DD;'>","X-Real-Ip: ",r.Header.Get("X-Real-Ip"),"</h3>"},"")
footer := `<script src="./live2d/L2Dwidget.min.js"></script><script type="text/javascript">L2Dwidget.init({
model: {
jsonPath: './live2d/tororo/assets/tororo.model.json',
},
display: {
superSample: 2,
width: 150,
height: 150,
position: 'right',
hOffset: 0,
vOffset: 0,
},
mobile: {
show: true,
scale: 1,
motion: true,
},
react: {
opacityDefault: 0.8,
opacityOnHover: 0.2,
}
})
</script></html>`
html := strings.Join([]string{head,r1,r2,r3,r4,footer},"")
if _,err := io.WriteString(w, html); err != nil {
log.Errorf("Httpserver: io WriteString error: %s", err)
}
}