-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathyandex.go
49 lines (40 loc) · 1.29 KB
/
yandex.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
package netemx
import (
"net"
"net/http"
"github.com/ooni/netem"
)
// YandexHandlerFactory implements yandex.com.
func YandexHandlerFactory() HTTPHandlerFactory {
return HTTPHandlerFactoryFunc(func(env NetStackServerFactoryEnv, stack *netem.UNetStack) http.Handler {
return YandexHandler()
})
}
// YandexHandler returns the [http.Handler] for yandex.com.
func YandexHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Alt-Svc", `h3=":443"`)
w.Header().Add("Date", "Thu, 24 Aug 2023 14:35:29 GMT")
// According to Go documentation, the host header is removed from the
// header fields and included as (*Request).Host
//
// Empirically, this field could either contain an host name or it could
// be an endpoint, i.e., it could also contain an optional port
host := r.Host
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
switch host {
case "ya.ru":
_, _ = w.Write([]byte(ExampleWebPage))
case "yandex.com":
w.Header().Add("Location", "https://ya.ru/")
w.WriteHeader(http.StatusPermanentRedirect)
case "xn--d1acpjx3f.xn--p1ai":
w.Header().Add("Location", "https://yandex.com/")
w.WriteHeader(http.StatusPermanentRedirect)
default:
w.WriteHeader(http.StatusBadRequest)
}
})
}