-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
86 lines (73 loc) · 2.28 KB
/
status.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
package controllers
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"github.com/revel/cron"
"github.com/tamurayoshiya/modules/jobs/app/jobs"
"github.com/revel/revel"
"net/http"
"strings"
)
type Jobs struct {
*revel.Controller
}
func (c *Jobs) Status() revel.Result {
remoteAddress := c.Request.RemoteAddr
if revel.Config.BoolDefault("jobs.auth", false) {
user, found_user := revel.Config.String("jobs.auth.user")
pass, found_pass := revel.Config.String("jobs.auth.pass")
// Verify that a username and password are given in the config file
if !found_pass || !found_user {
return c.unauthorized()
}
// Verify that the Authorization header is received and valid
auth := strings.Split(c.Request.GetHttpHeader("Authorization"), " ")
if len(auth) < 2 {
return c.unauthorized()
}
// Decode Authorization header
decoded, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
return c.unauthorized()
}
// Split Authorization header to user and password
str := strings.Split(string(decoded), ":")
// If SHA256 is enabled, hash received password
is_sha256 := revel.Config.BoolDefault("jobs.auth.sha256", false)
if is_sha256 {
hash := sha256.Sum256([]byte(str[1]))
str[1] = string(hex.EncodeToString(hash[:]))
pass = strings.ToLower(pass)
}
// Compare user and password
if user != str[0] || pass != str[1] {
c.Log.Warn("Attempted login to /@jobs with invalid credentials")
return c.unauthorized()
}
} else {
if revel.Config.BoolDefault("jobs.acceptproxyaddress", false) {
if proxiedAddress := c.Request.GetHttpHeader("X-Forwarded-For"); proxiedAddress != "" {
remoteAddress = proxiedAddress
}
}
if !strings.HasPrefix(remoteAddress, "127.0.0.1") &&
!strings.HasPrefix(remoteAddress, "::1") &&
!strings.HasPrefix(remoteAddress, "[::1]") {
return c.Forbidden("%s is not local", remoteAddress)
}
}
entries := jobs.MainCron.Entries()
return c.Render(entries)
}
func (c *Jobs) unauthorized() revel.Result {
c.Response.Status = http.StatusUnauthorized
c.Response.Out.Header().Set("WWW-Authenticate", "Basic realm=\"revel jobs\"")
return c.RenderError(errors.New("401: Not Authorized"))
}
func init() {
revel.TemplateFuncs["castjob"] = func(job cron.Job) *jobs.Job {
return job.(*jobs.Job)
}
}