-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirect.go
60 lines (50 loc) · 1.27 KB
/
redirect.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"google.golang.org/appengine/v2/log"
)
type RedirectionLog struct {
LogType string `json:"type"`
HttpRequest HttpRequest `json:"httpRequest"`
URL string `json:"url"`
Org string `json:"org,omitempty"`
Channel string `json:"channel,omitempty"`
Trace
}
type HttpRequest struct {
Method string `json:"method"`
UserAgent string `json:"userAgent"`
RemoteAddr string `json:"remoteAddr"`
}
func Redirect(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
url := q.Get("url")
if url == "" || strings.HasPrefix(url, ServerOrigin(r.Host)) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, http.StatusText(http.StatusBadRequest))
return
}
http.Redirect(w, r, url, http.StatusFound)
isSlackbot := strings.Contains(r.UserAgent(), "Slackbot")
if !isSlackbot {
t := GetTrace(r, os.Getenv("GOOGLE_CLOUD_PROJECT"))
l := RedirectionLog{
LogType: "redirect",
HttpRequest: HttpRequest{
Method: r.Method,
UserAgent: r.UserAgent(),
RemoteAddr: r.RemoteAddr,
},
URL: url,
Org: q.Get("org"),
Channel: q.Get("channel"),
Trace: t,
}
j, _ := json.Marshal(l)
log.Infof(r.Context(), "%s", j)
}
}