forked from LunaNode/lobster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
123 lines (100 loc) · 3.15 KB
/
common.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
123
package lobster
import "github.com/LunaNode/lobster/utils"
import "fmt"
import "net/url"
import "net/http"
import "runtime/debug"
import "runtime"
import "strings"
import "unicode"
const MIN_USERNAME_LENGTH = 3
const MAX_USERNAME_LENGTH = 128
const MIN_PASSWORD_LENGTH = 6
const MAX_PASSWORD_LENGTH = 512
const MAX_VM_NAME_LENGTH = 64
const MAX_API_RESTRICTION = 512
const SESSION_UID_LENGTH = 64
const SESSION_COOKIE_NAME = "lobsterSession"
const PWRESET_EXPIRE_MINUTES = 60
const TIME_FORMAT = "2 January 2006 15:04:05 MST"
const DATE_FORMAT = "2 January 2006"
const MYSQL_TIME_FORMAT = "2006-01-02 15:04:05"
const API_MAX_REQUEST_LENGTH = 32 * 1024
const ALPHANUMERIC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// billing constants
const BILLING_PRECISION = 1000000 // credit is in units of 1/BILLING_PRECISION -dollars
const BILLING_DISPLAY_DECIMALS = 3
const MINIMUM_CREDIT = BILLING_PRECISION // minimum credit to do things like create VMs
// how frequently to bill virtual machines in hours
// note that this is NOT the billing granularity, which is set in configuration file
// instead, this determines how often to apply VM charges and do bandwidth accounting
const BILLING_VM_FREQUENCY = 1
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func RedirectMessage(w http.ResponseWriter, r *http.Request, target string, msg utils.Message) {
http.Redirect(w, r, target+"?message="+url.QueryEscape(msg.Text)+"&type="+url.QueryEscape(msg.Type), 303)
}
func RedirectMessageExtra(w http.ResponseWriter, r *http.Request, target string, msg utils.Message, extra map[string]string) {
values := url.Values{}
for k, v := range extra {
values.Set(k, v)
}
values.Set("message", msg.Text)
values.Set("type", msg.Type)
http.Redirect(w, r, target+"?"+values.Encode(), 303)
}
func isPrintable(s string) bool {
for _, c := range s {
if c == 0 || c > unicode.MaxASCII || !unicode.IsPrint(c) {
return false
}
}
return true
}
// Extracts IP address from http.Request.RemoteAddr (127.0.0.1:9999 -> 127.0.0.1)
func ExtractIP(ipport string) string {
return strings.Split(ipport, ":")[0]
}
// Report should be true unless this error handler is being used in an error reporting function.
func errorHandler(w http.ResponseWriter, r *http.Request, report bool) {
if re := recover(); re != nil {
debug.PrintStack()
if report {
stackBytes := make([]byte, 8192)
runtime.Stack(stackBytes, false)
if r != nil {
ReportError(re.(error), fmt.Sprintf("failed on %s (%s)", r.URL.Path, r.RemoteAddr), string(stackBytes))
} else {
ReportError(re.(error), "error", string(stackBytes))
}
}
if w != nil {
http.Error(w, "Encountered error.", http.StatusInternalServerError)
}
}
}
func gigaToBytes(x int) int64 {
return int64(x) * 1024 * 1024 * 1024
}
func stripAlphanumeric(s string) string {
var n []rune
for _, c := range []rune(s) {
if strings.ContainsRune(ALPHANUMERIC, c) {
n = append(n, c)
}
}
return string(n)
}
func wildcardMatcher(regex string, s string) bool {
if len(regex) == 0 {
return false
}
if regex[len(regex)-1] == '*' {
return strings.HasPrefix(s, regex[:len(regex)-1])
} else {
return regex == s
}
}