1
// Copyright 2016 - 2020 The aurora Authors. All rights reserved. Use of this
2
// source code is governed by a MIT license that can be found in the LICENSE
3
// file.
4
//
5
// The aurora is a web-based beanstalkd queue server console written in Go
6
// and works on macOS, Linux and Windows machines. Main idea behind using Go
7
// for backend development is to utilize ability of the compiler to produce
8
// zero-dependency binaries for multiple platforms. aurora was created as an
9
// attempt to build very simple and portable application to work with local or
10
// remote beanstalkd server.
11

12
package main
13

14
import (
15
	"net"
16
	"net/http"
17
	"net/url"
18
	"sort"
19
	"strconv"
20
	"strings"
21
)
22

23
// readCookies read config property storage in cookie.
24
func readCookies(r *http.Request) {
25 10
	var servers, filters, tubeFilters []string
26 10
	var tubeSelectorValue string
27
	// Read servers in cookies
28 10
	beansServers, err := r.Cookie("beansServers")
29 10
	if err == nil {
30 10
		beansServersValue, _ := url.QueryUnescape(beansServers.Value)
31 10
		servers = strings.Split(beansServersValue, `;`)
32
	}
33
	// Read Filter in cookies
34 10
	filter, err := r.Cookie("filter")
35 10
	if err == nil {
36 10
		filterValue, _ := url.QueryUnescape(filter.Value)
37 10
		filters = strings.Split(filterValue, `,`)
38 10
		filters = removeArrayDuplicates(removeArrayEmpty(filters))
39 10
	} else {
40 10
		filters = []string{"current-connections", "current-jobs-buried", "current-jobs-delayed", "current-jobs-ready", "current-jobs-reserved", "current-jobs-urgent", "current-tubes"}
41
	}
42
	for _, v := range servers {
43 10
		_, err := url.ParseRequestURI(v)
44
		// Server address should be a valid URL or dotted decimal IPv4 or IPv6 form.
45 10
		if err != nil && net.ParseIP(strings.Split(v, `:`)[0]) == nil {
46 10
			continue
47
		}
48
		// Server port should be an integer.
49 10
		_, err = strconv.Atoi(strings.Split(v, `:`)[1])
50 10
		if err != nil {
51 0
			continue
52
		}
53 10
		selfConf.Servers = append(selfConf.Servers, v)
54
	}
55
	// Read Tube Filter in cookies
56 10
	tubeFilter, err := r.Cookie("tubefilter")
57 10
	if err == nil {
58 10
		tubeFilterValue, _ := url.QueryUnescape(tubeFilter.Value)
59 10
		tubeFilters = strings.Split(tubeFilterValue, `,`)
60 10
		tubeFilters = removeArrayDuplicates(removeArrayEmpty(tubeFilters))
61 10
	} else {
62 10
		tubeFilters = []string{"current-jobs-urgent", "current-jobs-ready", "current-jobs-reserved", "current-jobs-delayed", "current-jobs-buried", "total-jobs"}
63
	}
64 10
	tubeSelector, err := r.Cookie("tubeSelector")
65 10
	if err != nil {
66 10
		tubeSelectorValue = ""
67 10
	} else {
68 10
		tubeSelectorValue = tubeSelector.Value
69
	}
70

71 10
	selfConf.Servers = removeArrayDuplicates(removeArrayEmpty(selfConf.Servers))
72 10
	sort.Strings(selfConf.Servers)
73 10
	selfConf.Filter = filters
74 10
	selfConf.TubeFilters = tubeFilters
75 10
	selfConf.IsDisabledJSONDecode = readIntCookie(r, `isDisabledJsonDecode`, 0)
76 10
	selfConf.IsDisabledUnserialization = readIntCookie(r, `isDisabledUnserialization`, 0)
77 10
	selfConf.IsDisabledJobDataHighlight = readIntCookie(r, `isDisabledJobDataHighlight`, 0)
78 10
	selfConf.IsEnabledBase64Decode = readIntCookie(r, `isEnabledBase64Decode`, 0)
79 10
	selfConf.TubePauseSeconds = readIntCookie(r, `tubePauseSeconds`, -1)
80 10
	selfConf.AutoRefreshTimeoutMs = readIntCookie(r, `autoRefreshTimeoutMs`, 500)
81 10
	selfConf.SearchResultLimit = readIntCookie(r, `searchResultLimit`, 25)
82 10
	selfConf.TubeSelector = tubeSelectorValue
83
}
84

85
// readIntCookie return int value by the given string.
86
func readIntCookie(r *http.Request, name string, defaultValue int) int {
87 10
	cookie, err := r.Cookie(name)
88 10
	if err == nil {
89 10
		value, err := strconv.Atoi(cookie.Value)
90 10
		if err == nil {
91 10
			return value
92
		}
93
	}
94 10
	return defaultValue
95
}
96

97
// removeServerInCookie remove field in cookie by the given string.
98
func removeServerInCookie(server string, w http.ResponseWriter, r *http.Request) {
99
	for k, v := range selfConf.Servers {
100 10
		if v == server {
101 10
			selfConf.Servers = selfConf.Servers[:k+copy(selfConf.Servers[k:], selfConf.Servers[k+1:])]
102
		}
103
	}
104 10
	var serverInCookie string
105
	for _, v := range selfConf.Servers {
106 10
		serverInCookie += v + `;`
107
	}
108
	cookie := http.Cookie{
109 10
		Name:  `beansServers`,
110 10
		Value: url.QueryEscape(serverInCookie),
111
	}
112 10
	http.SetCookie(w, &cookie)
113
}

Read our documentation on viewing source code .

Loading