This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
122 lines (113 loc) · 2.76 KB
/
utils.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
118
119
120
121
122
package main
import (
"bytes"
"gShort/Config"
"gShort/DataBase"
rice "github.com/GeertJohan/go.rice"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"text/template"
"time"
)
// Increases the hitcount of a mapping and deletes the mapping if maxhit is reached
func hitCounter(config *Config.Config, mapping string) (err error) {
record, err := DataBase.IncreaseHitCount(config.MongoDB, mapping)
if err != nil {
log.Printf("Error while increasing hitcount: %v", err)
}
if record.MaxHitCount > 0 {
if record.HitCount >= record.MaxHitCount {
err = record.Delete(config.MongoDB)
if err != nil {
log.Printf("Error while deleting record: %v", err)
}
}
}
return
}
// Checks whether a box has a specific file
func boxHasFile(box *rice.Box, file string) bool {
if file == "/" {
return true
}
oo, err := box.Open(file)
if err != nil {
return false
}
oo.Close()
return true
}
func isValidUrl(rawurl string) bool {
_, err := url.ParseRequestURI(rawurl)
if err != nil {
return false
}
return true
}
// Generates a random string with the given length and charset
func generateStringWithCharset(length int, charset string) string {
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
func trimLeftChar(s string) string {
for i := range s {
if i > 0 {
return s[i:]
}
}
return s[:0]
}
func buildIndex(config *Config.Config) (index string, err error) {
box, err := rice.FindBox("website")
if err != nil {
return
}
tmpl, err := box.String("index.html")
if err != nil {
return
}
var indexTemplate *template.Template
indexTemplate, err = template.New("index").Parse(tmpl)
if err != nil {
return
}
var tpl bytes.Buffer
err = indexTemplate.Execute(&tpl, config)
if err != nil {
return
}
index = tpl.String()
return
}
// Prevent returning stuff like http://localhost/XXXX when port != 80
func buildMapping(config *Config.Config, mapping string) string {
if config.Protocol == "http" && config.Port != 80 {
mapping = "http://" + config.Domain + ":" + strconv.Itoa(config.Port) + "/" + mapping
} else if config.Protocol == "https" && config.Port != 443 {
mapping = "https://" + config.Domain + ":" + strconv.Itoa(config.Port) + "/" + mapping
} else {
mapping = config.Protocol + "://" + config.Domain + "/" + mapping
}
return mapping
}
// This function check if the host header matches the provided domain:port
// For requests on port 80 or 443 it won't check the port
func comingFromDomain(domain string, port int, r *http.Request) bool {
if port != 80 && port != 443 {
if r.Host == domain+":"+strconv.Itoa(port) {
return true
}
} else {
if r.Host == domain {
return true
}
}
return false
}