forked from demostanis/gimmeasearx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgimmeasearx.go
220 lines (193 loc) · 5.65 KB
/
gimmeasearx.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"net/http"
"net/url"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/demostanis/gimmeasearx/internal/grade"
"github.com/demostanis/gimmeasearx/internal/instances"
findlatestversion "github.com/demostanis/gimmeasearx/internal/version"
"github.com/hashicorp/go-version"
"html/template"
"strings"
"time"
"io"
"os"
)
// Used by echo.
type Template struct {
templates *template.Template
}
// Used by echo to render templates.
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
var t = &Template{
templates: template.Must(template.ParseGlob("templates/*.html")),
}
var fetchedInstances *map[string]instances.Instance = nil
func main() {
e := echo.New()
e.Renderer = t
var fetch func()
fetch = func() {
resp, err := instances.Fetch();
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
fetchedInstances = &resp.Instances
for key, instance := range *fetchedInstances {
go func(key string, instance instances.Instance) {
if instances.Verify(key, instance) {
delete(*fetchedInstances, key)
}
}(key, instance)
}
}
fetch()
go func() {
for range time.Tick(time.Hour * 24) {
fetch()
}
}()
e.Use(middleware.Gzip())
e.Use(middleware.Recover())
e.GET("/", index)
e.GET("/search", search)
port, exists := os.LookupEnv("PORT")
if !exists {
port = ":8080"
}
e.Logger.Fatal(e.Start(port))
}
func search(c echo.Context) error {
params := parseParams(c)
torOnlyEnabled := params.torOnlyEnabled
torEnabled := params.torEnabled
gradesEnabled := params.gradesEnabled
blacklist := params.blacklist
preferences := params.preferences
minVersion := params.minVersion
customInstances := params.customInstances
randUrl, _ := instances.FindRandomInstance(fetchedInstances, gradesEnabled, blacklist, torEnabled, torOnlyEnabled, minVersion, customInstances)
if randUrl == nil {
return c.Render(http.StatusExpectationFailed, "index.html", map[string]bool{
"Error": true,
})
}
if fetchedInstances != nil {
return c.Redirect(http.StatusFound, *randUrl + "?preferences=" + url.QueryEscape(*preferences) + "&q=" + url.QueryEscape(c.QueryParam("q")))
} else {
return c.String(http.StatusTooEarly, "No instances available. Please try again in a few seconds.")
}
}
func index(c echo.Context) error {
params := parseParams(c)
torOnlyEnabled := params.torOnlyEnabled
torEnabled := params.torEnabled
gradesEnabled := params.gradesEnabled
blacklist := params.blacklist
preferences := params.preferences
minVersion := params.minVersion
latestVersion := params.latestVersion
customInstances := params.customInstances
data := map[string]interface{}{
"CurrentUrl": c.Request().URL.RequestURI(),
"OptionsSelected": map[string]interface{}{
"Tor": torEnabled,
"TorOnly": torOnlyEnabled,
"Blacklist": blacklist,
"Latest": latestVersion,
},
"Grades": grade.Grades(),
"GradesSelected": gradesEnabled,
"Preferences": preferences,
"MinVersion": minVersion.Original(),
"CustomInstances": customInstances,
}
if fetchedInstances != nil {
randUrl, isCustom := instances.FindRandomInstance(fetchedInstances, gradesEnabled, blacklist, torEnabled, torOnlyEnabled, minVersion, customInstances)
if randUrl == nil {
data["Error"] = true
return c.Render(http.StatusExpectationFailed, "index.html", data)
}
if isCustom {
data["Instance"] = instances.Instance{Comments: []string{"Custom instance"}}
data["InstanceUrl"] = randUrl
} else {
randInstance := (*fetchedInstances)[*randUrl]
data["Instance"] = randInstance
data["InstanceUrl"] = randUrl
data["GradeComment"] = grade.Comment(randInstance.Html.Grade)
}
return c.Render(http.StatusOK, "index.html", data)
} else {
data["Error"] = true
return c.Render(http.StatusTooEarly, "index.html", data)
}
}
// Params that may be specified in the URL.
type Params struct {
torEnabled bool
torOnlyEnabled bool
gradesEnabled []string
blacklist []string
preferences *string
minVersion version.Version
latestVersion bool
customInstances []string
}
func parseParams(c echo.Context) Params {
torOnlyEnabled := c.QueryParam("toronly") == "on"
torEnabled := torOnlyEnabled || c.QueryParam("tor") == "on"
latestVersion := c.QueryParam("latestversion") == "on"
minVersion, _ := version.NewVersion("0.0.0")
if !latestVersion {
r, err := version.NewVersion(c.QueryParam("minversion"))
if err != nil {
minVersion, _ = version.NewVersion("0.0.0")
} else {
minVersion = r
}
} else {
minVersion, _ = version.NewVersion(findlatestversion.Searx())
}
gradesEnabled := *new([]string)
for _, thisGrade := range grade.Grades() {
if c.QueryParam(thisGrade["Id"].(string)) == "on" {
gradesEnabled = append(gradesEnabled, thisGrade["Id"].(string))
}
}
if len(gradesEnabled) < 1 {
gradesEnabled = grade.Defaults()
}
blacklist := *new([]string)
if b := c.QueryParam("blacklist"); len(b) > 0 {
for _, s := range strings.Split(b, ";") {
if strings.TrimSpace(s) != "" {
blacklist = append(blacklist, strings.TrimSpace(s))
}
}
}
customInstances := *new([]string)
if b := c.QueryParam("custominstances"); len(b) > 0 {
for _, s := range strings.Split(b, ";") {
if strings.TrimSpace(s) != "" && strings.TrimSpace(s) != "https://myothercustom.instan.ce.ru.fr.es/" && strings.TrimSpace(s) != "https://mycustom.searx.instance/" {
customInstances = append(customInstances, strings.TrimSpace(s))
}
}
}
preferences := c.QueryParam("preferences")
return Params{
torEnabled,
torOnlyEnabled,
gradesEnabled,
blacklist,
&preferences,
*minVersion,
latestVersion,
customInstances,
}
}