forked from sfproductlabs/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
177 lines (166 loc) · 5.04 KB
/
utils.go
File metadata and controls
177 lines (166 loc) · 5.04 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*===----------- utils.go - tracking utility written in go -------------===
*
*
* This file is licensed under the Apache 2 License. See LICENSE for details.
*
* Copyright (c) 2018 Andrew Grosser. All Rights Reserved.
*
* `...
* yNMMh`
* dMMMh`
* dMMMh`
* dMMMh`
* dMMMd`
* dMMMm.
* dMMMm.
* dMMMm. /hdy.
* ohs+` yMMMd. yMMM-
* .mMMm. yMMMm. oMMM/
* :MMMd` sMMMN. oMMMo
* +MMMd` oMMMN. oMMMy
* sMMMd` /MMMN. oMMMh
* sMMMd` /MMMN- oMMMd
* oMMMd` :NMMM- oMMMd
* /MMMd` -NMMM- oMMMm
* :MMMd` .mMMM- oMMMm`
* -NMMm. `mMMM: oMMMm`
* .mMMm. dMMM/ +MMMm`
* `hMMm. hMMM/ /MMMm`
* yMMm. yMMM/ /MMMm`
* oMMm. oMMMo -MMMN.
* +MMm. +MMMo .MMMN-
* +MMm. /MMMo .NMMN-
* ` +MMm. -MMMs .mMMN: `.-.
* /hys:` +MMN- -NMMy `hMMN: .yNNy
* :NMMMy` sMMM/ .NMMy yMMM+-dMMMo
* +NMMMh-hMMMo .mMMy +MMMmNMMMh`
* /dMMMNNMMMs .dMMd -MMMMMNm+`
* .+mMMMMMN: .mMMd `NMNmh/`
* `/yhhy: `dMMd /+:`
* `hMMm`
* `hMMm.
* .mMMm:
* :MMMd-
* -NMMh.
* ./:.
*
*===----------------------------------------------------------------------===
*/
package main
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"hash/fnv"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
)
////////////////////////////////////////
// hash
////////////////////////////////////////
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func sha(s string) string {
hasher := sha1.New()
hasher.Write([]byte(s))
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
////////////////////////////////////////
// filterUrl
// matchGroup is a 1 indexed array (0 is default last)
// returns last match if no group, or group
////////////////////////////////////////
func filterUrl(c *Configuration, s *string, matchGroup *int) error {
matches := regexFilterUrl.FindStringSubmatch(*s)
mi := len(matches)
if matchGroup == nil || *matchGroup == 0 {
//Take the last one by default
if mi > 0 {
*s = matches[mi-1]
return nil
}
} else {
if mi > *matchGroup {
*s = matches[*matchGroup]
return nil
}
}
//Fallback
filterUrlAppendix(s)
return fmt.Errorf("Mismatch Regex (Url Filter)")
}
func filterUrlAppendix(s *string) error {
if s != nil {
i := strings.Index(*s, "?")
if i > -1 {
*s = (*s)[:i]
}
}
return nil
}
func filterUrlPrefix(s *string) error {
if s != nil {
*s = strings.ToLower(*s)
i := strings.Index(*s, "https://")
if i > -1 {
*s = (*s)[i+6:]
return nil
}
i = strings.Index(*s, "http://")
if i > -1 {
*s = (*s)[i+5:]
}
}
return nil
}
func cleanInterfaceString(i interface{}) error {
s := &i
if temp, ok := (*s).(string); ok {
*s = strings.ToLower(strings.TrimSpace(temp))
}
return nil
}
func cleanString(s *string) error {
if s != nil && *s != "" {
*s = strings.ToLower(strings.TrimSpace(*s))
}
return nil
}
////////////////////////////////////////
// cacheDir in /tmp for SSL
////////////////////////////////////////
func cacheDir() (dir string) {
if u, _ := user.Current(); u != nil {
dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
//dir = filepath.Join(".", "cache-golang-autocert-"+u.Username)
fmt.Println("Saving cache-go-lang-autocert-u.username to: ", dir)
if err := os.MkdirAll(dir, 0700); err == nil {
return dir
}
}
return ""
}
func getIP(r *http.Request) string {
ip := r.Header.Get("X-Forwarded-For")
if ip == "" {
var err error
if ip, _, err = net.SplitHostPort(r.RemoteAddr); err != nil {
return r.RemoteAddr
}
}
return ip
}
func getHost(r *http.Request) string {
if addr, _, err := net.SplitHostPort(r.Host); err != nil {
return r.Host
} else {
return addr
}
}