-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
148 lines (127 loc) · 3.2 KB
/
api.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
package steamscreenshots
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"slices"
)
func (s *Server) handler_api_cache(w http.ResponseWriter, r *http.Request) {
if !s.checkApiKey(w, r) {
return
}
fmt.Println("serving image.cache")
raw, err := json.Marshal(s.ImageCache.Games)
if err != nil {
fmt.Println(err)
sendApiError(w, ApiError{
Code: http.StatusInternalServerError,
Message: "JSON Marshal error",
})
return
}
w.WriteHeader(http.StatusOK)
w.Write(raw)
}
func (s *Server) handler_api_upload(w http.ResponseWriter, r *http.Request) {
if !s.checkApiKey(w, r) {
return
}
appid := r.PathValue("appid")
filename := r.PathValue("filename")
if appid == "" || filename == "" {
fmt.Println("appid or filename missing")
sendApiError(w, ApiError{
Code: http.StatusBadRequest,
Message: "appid or filename missing",
})
return
}
fullname := filepath.Join(s.settings.ImageDirectory, appid, filename)
err := os.MkdirAll(filepath.Dir(fullname), 0755)
if err != nil {
fmt.Println(err)
sendApiError(w, ApiError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("unable to create appid folder %s: %s", appid, err.Error()),
})
return
}
output, err := os.Create(fullname)
if err != nil {
fmt.Println(err)
sendApiError(w, ApiError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("unable to create image file: %s", err.Error()),
})
return
}
defer output.Close()
_, err = io.Copy(output, r.Body)
if err != nil {
fmt.Println(err)
sendApiError(w, ApiError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("unable to read image: %s", err.Error()),
})
output.Close()
err = os.Remove(fullname)
if err != nil {
fmt.Println("unable to remove incomplete file %s: %s", fullname, err.Error())
}
return
}
fmt.Printf("[%s] %s uploaded\n", appid, filename)
s.newImages <- NewImage{AppId: appid, Filename: filename}
}
// checkApiKey returns True if the key is valid
func (s *Server) checkApiKey(w http.ResponseWriter, r *http.Request) bool {
if s.settings.ApiWhitelist == nil || len(s.settings.ApiWhitelist) == 0 {
fmt.Println("No IP addresses in API Whitelist")
w.WriteHeader(http.StatusUnauthorized)
return false
}
found := false
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
fmt.Printf("Error splitting host and port for %q: %s\n", r.RemoteAddr, err)
w.WriteHeader(http.StatusUnauthorized)
return false
}
if slices.Contains(s.settings.ApiWhitelist, host) {
found = true
}
if !found && host == "127.0.0.1" {
realIp := r.Header.Get("X-Real-Ip")
if realIp != "" && slices.Contains(s.settings.ApiWhitelist, realIp) {
found = true
}
}
if !found {
fmt.Printf("IP %q not in API whitelist\n", host)
w.WriteHeader(http.StatusUnauthorized)
return false
}
key := r.Header.Get("api-key")
if key != s.settings.ApiKey {
fmt.Printf("invalid or missing api key: %q\n", key)
w.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}
type ApiError struct {
Code int
Message string
}
func sendApiError(w http.ResponseWriter, errmsg ApiError) {
encoded, err := json.Marshal(errmsg)
if err != nil {
panic(err)
}
w.WriteHeader(errmsg.Code)
w.Write(encoded)
}