/
server_render.go
128 lines (121 loc) · 3.56 KB
/
server_render.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
package server
import (
"bytes"
"encoding/json"
"net/http"
"strings"
M "github.com/sagernet/serenity/common/metadata"
"github.com/sagernet/serenity/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/go-chi/render"
)
func (s *Server) initializeRoutes() {
s.chiRouter.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET"},
AllowedHeaders: []string{"Content-Type", "Authorization"},
}).Handler)
s.chiRouter.Get("/", s.render)
s.chiRouter.Get("/{profileName}", s.render)
}
func (s *Server) render(writer http.ResponseWriter, request *http.Request) {
profileName := chi.URLParam(request, "profileName")
if profileName == "" {
// compatibility with legacy versions
profileName = request.URL.Query().Get("profile")
}
if strings.HasSuffix(profileName, "/") {
profileName = profileName[:len(profileName)-1]
}
var profile *Profile
if len(s.users) == 0 {
if profileName == "" {
profile = s.profile.DefaultProfile()
} else {
profile = s.profile.ProfileByName(profileName)
}
} else {
user := s.authorization(request)
if user == nil {
writer.WriteHeader(http.StatusUnauthorized)
s.accessLog(request, http.StatusUnauthorized, 0)
return
}
if len(user.Profile) == 0 {
writer.WriteHeader(http.StatusNotFound)
s.accessLog(request, http.StatusNotFound, 0)
return
}
if profileName == "" {
profileName = user.DefaultProfile
}
if profileName == "" {
profileName = user.Profile[0]
}
if !common.Contains(user.Profile, profileName) {
writer.WriteHeader(http.StatusNotFound)
s.accessLog(request, http.StatusNotFound, 0)
return
}
profile = s.profile.ProfileByName(profileName)
}
if profile == nil {
writer.WriteHeader(http.StatusNotFound)
s.accessLog(request, http.StatusNotFound, 0)
return
}
metadata := M.Detect(request.Header.Get("User-Agent"))
options, err := profile.Render(metadata)
if err != nil {
s.logger.Error(E.Cause(err, "render options"))
render.Status(request, http.StatusInternalServerError)
render.PlainText(writer, request, err.Error())
s.accessLog(request, http.StatusInternalServerError, len(err.Error()))
return
}
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(&options)
if err != nil {
s.logger.Error(E.Cause(err, "marshal options"))
render.Status(request, http.StatusInternalServerError)
render.PlainText(writer, request, err.Error())
s.accessLog(request, http.StatusInternalServerError, len(err.Error()))
return
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(buffer.Bytes())
s.accessLog(request, http.StatusOK, buffer.Len())
}
func (s *Server) accessLog(request *http.Request, responseCode int, responseLen int) {
var userString string
if username, password, ok := request.BasicAuth(); ok {
if responseCode == http.StatusUnauthorized {
userString = username + ":" + password
} else {
userString = username
}
}
s.logger.Debug("accepted ", request.RemoteAddr, " - ", userString, " \"", request.Method, " ", request.URL, " ", request.Proto, "\" ", responseCode, " ", responseLen, " \"", request.UserAgent(), "\"")
}
func (s *Server) authorization(request *http.Request) *option.User {
username, password, ok := request.BasicAuth()
if !ok {
return nil
}
users, loaded := s.userMap[username]
if !loaded {
return nil
}
for _, user := range users {
if user.Password == password {
return &user
}
}
return nil
}