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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// Program azurda is a fake s3 server implementation. All objects are generated on the fly from Stable Diffusion.
//
// This is intended to be used as a "shadow bucket" endpoint with Tigris so that Tigris can "fall through" to Azurda if the object is not found in the real bucket.
package main
import (
"bytes"
"context"
"embed"
"flag"
"fmt"
"image"
"image/jpeg"
_ "image/png"
"log/slog"
"net/http"
"os"
"regexp"
"time"
"github.com/a-h/templ"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
"within.website/x/internal"
"within.website/x/web/stablediffusion"
"within.website/x/xess"
)
//go:generate go run github.com/a-h/templ/cmd/templ@latest generate
var (
accessKey = flag.String("access-key", "", "Access key for the client to use")
secretKey = flag.String("secret-key", "", "Secret key for the client to use")
bucketName = flag.String("bucket-name", "fallthrough", "The bucket name to expect from Tigris")
bind = flag.String("bind", ":8085", "address to bind to")
internalBind = flag.String("internal-bind", ":8086", "address to bind internal services (metrics, etc) to")
sdServerURL = flag.String("stablediffusion-server-url", "http://xe-automatic1111.internal:8080", "URL for the Stable Diffusion API used with the default client")
isHexRegex = regexp.MustCompile(`[a-fA-F0-9]+$`)
authErrors = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "azurda_auth_errors",
Help: "Number of auth errors encountered while serving requests.",
}, []string{"kind"})
requestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "azurda_request_duration_seconds",
Help: "The duration of requests in seconds.",
Buckets: []float64{0.1, 0.25, 0.5, 1, 2.5, 5, 10},
}, []string{"method"})
stableDiffusionHits = promauto.NewCounter(prometheus.CounterOpts{
Name: "azurda_stable_diffusion_hits",
Help: "Number of hits to the stable diffusion endpoint.",
})
stableDiffusionCreationErrors = promauto.NewGauge(prometheus.GaugeOpts{
Name: "azurda_stable_diffusion_creation_errors",
Help: "Number of errors encountered while creating a stable diffusion image.",
})
//go:embed static
static embed.FS
)
func main() {
internal.HandleStartup()
stablediffusion.Default.APIServer = *sdServerURL
slog.Info("starting azurda",
"bind", *bind,
"internalBind", *internalBind,
"bucket", *bucketName,
"accessKey", *accessKey,
"hasSecretKey", *secretKey != "",
"stableDiffusionURL", stablediffusion.Default.APIServer,
)
if *accessKey == "" {
fmt.Println("access-key is required")
os.Exit(2)
}
if *secretKey == "" {
fmt.Println("secret-key is required")
os.Exit(2)
}
mux := http.NewServeMux()
xess.Mount(mux)
mux.Handle("/{$}", templ.Handler(xess.Base(
"Azurda",
headerJS(),
nil,
body(),
footer(),
)))
mux.Handle("/static/", http.FileServerFS(static))
mux.HandleFunc("GET fallthrough.azurda.within.website/{hash}", ServeStableDiffusion)
http.Handle("/metrics", promhttp.Handler())
g, _ := errgroup.WithContext(context.Background())
g.Go(func() error {
slog.Info("starting internal server", "bind", *internalBind)
return http.ListenAndServe(*internalBind, nil)
})
g.Go(func() error {
slog.Info("starting server", "bind", *bind)
return http.ListenAndServe(*bind, SpewMiddleware(mux))
})
if err := g.Wait(); err != nil {
slog.Error("error doing work", "err", err)
os.Exit(1)
}
}
func SpewMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slog.Debug("got request", "method", r.Method, "url", r.URL.String(), "headers", r.Header)
next.ServeHTTP(w, r)
})
}
func ServeStableDiffusion(w http.ResponseWriter, r *http.Request) {
hash := r.PathValue("hash")
if !isHexRegex.MatchString(hash) {
http.Error(w, "the input must be a hexadecimal string", http.StatusBadRequest)
return
}
prompt, seed := hallucinatePrompt(hash)
imgs, err := stablediffusion.Default.Generate(r.Context(), stablediffusion.SimpleImageRequest{
Prompt: "headshot, portrait, masterpiece, best quality, " + prompt,
NegativePrompt: "person in distance, worst quality, low quality, medium quality, deleted, lowres, comic, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry",
Seed: seed,
SamplerName: "DPM++ 2M Karras",
BatchSize: 1,
NIter: 1,
Steps: 20,
CfgScale: 7,
Width: 256,
Height: 256,
SNoise: 1,
OverrideSettingsRestoreAfterwards: true,
})
if err != nil {
stableDiffusionCreationErrors.Add(1)
http.Error(w, "Not found", http.StatusNotFound)
slog.Error("can't fabricate image", "err", err)
return
}
stableDiffusionHits.Add(1)
img, _, err := image.Decode(bytes.NewBuffer(imgs.Images[0]))
if err != nil {
stableDiffusionCreationErrors.Add(1)
http.Error(w, "can't decode image", http.StatusInternalServerError)
slog.Error("can't decode image", "err", err)
return
}
buf := &bytes.Buffer{}
if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: 75}); err != nil {
stableDiffusionCreationErrors.Add(1)
http.Error(w, "can't encode image", http.StatusInternalServerError)
slog.Error("can't encode image", "err", err)
return
}
imgs.Images[0] = buf.Bytes()
w.Header().Set("content-type", "image/jpeg")
w.Header().Set("content-length", fmt.Sprint(len(imgs.Images[0])))
w.Header().Set("expires", time.Now().Add(30*24*time.Hour).Format(http.TimeFormat))
w.Header().Set("Cache-Control", "max-age:2630000") // one month
w.WriteHeader(http.StatusOK)
w.Write(imgs.Images[0])
}
// func AWSValidationMiddleware(accessKey, secretKey string, next http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// //slog.Debug("incoming request", "method", r.Method, "path", r.URL.Path, "headers", r.Header)
// if r.Header.Get("Authorization") == "" {
// http.Error(w, "missing Authorization header", http.StatusUnauthorized)
// authErrors.WithLabelValues("missing").Inc()
// return
// }
//
// //slog.Debug("auth header", "header", r.Header.Get("Authorization"))
//
// sp := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
// if len(sp) != 2 {
// http.Error(w, "malformed Authorization header", http.StatusUnauthorized)
// slog.Error("malformed auth header")
// authErrors.WithLabelValues("malformed").Inc()
// return
// }
//
// if sp[0] != "AWS4-HMAC-SHA256" {
// http.Error(w, "unsupported authorization type", http.StatusUnauthorized)
// slog.Error("unsupported auth type", "type", sp[0])
// authErrors.WithLabelValues("unsupported").Inc()
// return
// }
//
// authPartSlice := strings.SplitN(sp[1], ", ", 3)
// slog.Debug("auth parts", "parts", authPartSlice)
// if len(authPartSlice) != 3 {
// http.Error(w, "malformed Authorization header auth parts", http.StatusUnauthorized)
// authErrors.WithLabelValues("malformed_authparts").Inc()
// return
// }
//
// authParts := map[string]string{}
// for _, part := range authPartSlice {
// sp := strings.SplitN(part, "=", 2)
// if len(sp) != 2 {
// http.Error(w, "malformed Authorization header auth part", http.StatusUnauthorized)
// slog.Debug("malformed auth part", "part", part)
// authErrors.WithLabelValues("malformed_authpart").Inc()
// return
// }
//
// authParts[strings.ToLower(sp[0])] = strings.Trim(sp[1], "\"")
// }
//
// if authParts["credential"] == "" {
// http.Error(w, "missing credential in Authorization header", http.StatusUnauthorized)
// slog.Debug("missing credential in auth header")
// authErrors.WithLabelValues("missing_credential").Inc()
// return
// }
//
// if authParts["signature"] == "" {
// http.Error(w, "missing signature in Authorization header", http.StatusUnauthorized)
// slog.Debug("missing signature in auth header")
// authErrors.WithLabelValues("missing_signature").Inc()
// return
// }
//
// if authParts["signedheaders"] == "" {
// http.Error(w, "missing signedheaders in Authorization header", http.StatusUnauthorized)
// slog.Debug("missing signedheaders in auth header")
// authErrors.WithLabelValues("missing_signedheaders").Inc()
// return
// }
//
// if !strings.Contains(authParts["credential"], accessKey) {
// http.Error(w, "access key mismatch", http.StatusUnauthorized)
// authErrors.WithLabelValues("access_key_mismatch").Inc()
// return
// }
//
// req := r.Clone(r.Context())
// req.Header.Del("Authorization")
//
// req = awsauth.Sign4(req, awsauth.Credentials{
// AccessKeyID: accessKey,
// SecretAccessKey: secretKey,
// })
//
// fmt.Println("Theirs: ", r.Header.Get("Authorization"))
// fmt.Println("Ours: ", req.Header.Get("Authorization"))
//
// if req.Header.Get("Authorization") != r.Header.Get("Authorization") {
// http.Error(w, "failed to sign request", http.StatusUnauthorized)
// return
// }
//
// next.ServeHTTP(w, r)
// })
// }
|