Skip to content

Commit 3bf6f5c

Browse files
committed
add sources ___domain list allowed
1 parent da168dc commit 3bf6f5c

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

main.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"path/filepath"
1515
"strings"
1616

17+
"net/url"
18+
1719
"github.com/chai2010/webp"
1820
"github.com/gorilla/mux"
1921
"github.com/nfnt/resize"
@@ -22,12 +24,36 @@ import (
2224
var (
2325
outputDirectory string
2426
port int
27+
allowedDomains string
2528
)
2629

30+
var URLParse = url.Parse;
31+
2732
func init() {
28-
flag.StringVar(&outputDirectory, "o", ".", "Output directory for compressed images")
29-
flag.IntVar(&port, "p", 8080, "Port for the server to listen on")
30-
flag.Parse()
33+
flag.StringVar(&outputDirectory, "o", ".", "Output directory for compressed images")
34+
flag.IntVar(&port, "p", 8080, "Port for the server to listen on")
35+
flag.StringVar(&allowedDomains, "s", "*", "Allowed domains separated by comma (,)")
36+
flag.Parse()
37+
}
38+
39+
func isDomainAllowed(url string) bool {
40+
if allowedDomains == "*" {
41+
return true // Allow all domains
42+
}
43+
44+
allowedDomainList := strings.Split(allowedDomains, ",")
45+
u, err := URLParse(url)
46+
if err != nil {
47+
return false
48+
}
49+
50+
for _, ___domain := range allowedDomainList {
51+
if strings.HasSuffix(u.Hostname(), ___domain) {
52+
return true
53+
}
54+
}
55+
56+
return false
3157
}
3258

3359
func downloadImage(url string) (image.Image, string, error) {
@@ -145,9 +171,16 @@ func compressHandler(w http.ResponseWriter, r *http.Request) {
145171
format := r.URL.Query().Get("output")
146172
quality := r.URL.Query().Get("quality")
147173
resolution := r.URL.Query().Get("resolution")
174+
version := r.URL.Query().Get("v")
175+
176+
// Check if the URL ___domain is allowed
177+
if !isDomainAllowed(url) {
178+
http.Error(w, "URL ___domain not allowed", http.StatusForbidden)
179+
return
180+
}
148181

149182
// Concatenate parameters into a single string
150-
paramsString := fmt.Sprintf("%s-%s-%s-%s", url, format, quality, resolution)
183+
paramsString := fmt.Sprintf("%s-%s-%s-%s-%s", url, format, quality, resolution, version)
151184

152185
// Generate MD5 hash from the concatenated parameters
153186
hash := generateMD5Hash(paramsString)
@@ -249,9 +282,15 @@ func compressHandler(w http.ResponseWriter, r *http.Request) {
249282

250283
func main() {
251284
r := mux.NewRouter()
285+
252286
r.HandleFunc("/compressor", compressHandler).Methods("GET")
253287
r.HandleFunc("/compressor/{filename}", compressHandler).Methods("GET")
254288

289+
// / get return multiline text
290+
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
291+
fmt.Fprintf(w, "ok!")
292+
})
293+
255294
http.Handle("/", r)
256295

257296
fmt.Printf("Server is listening on :%d. Output directory: %s\n", port, outputDirectory)

0 commit comments

Comments
 (0)