-
Notifications
You must be signed in to change notification settings - Fork 339
/
controller.go
172 lines (142 loc) · 3.97 KB
/
controller.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Sshwifty - A Web SSH client
//
// Copyright (C) 2019-2023 Ni Rui <ranqus@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package controller
import (
"net/http"
"strings"
"time"
"github.com/nirui/sshwifty/application/command"
"github.com/nirui/sshwifty/application/configuration"
"github.com/nirui/sshwifty/application/log"
"github.com/nirui/sshwifty/application/server"
)
// Errors
var (
ErrNotFound = NewError(
http.StatusNotFound, "Page not found")
)
const (
assetsURLPrefix = "/sshwifty/assets/"
assetsURLPrefixLen = len(assetsURLPrefix)
)
// handler is the main service dispatcher
type handler struct {
hostNameChecker string
commonCfg configuration.Common
logger log.Logger
homeCtl home
socketCtl socket
socketVerifyCtl socketVerification
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
clientLogger := h.logger.Context("Client (%s)", r.RemoteAddr)
if len(h.commonCfg.HostName) > 0 {
hostPort := r.Host
if len(hostPort) <= 0 {
hostPort = r.URL.Host
}
if h.commonCfg.HostName != hostPort &&
!strings.HasPrefix(hostPort, h.hostNameChecker) {
clientLogger.Warning("Requested invalid host \"%s\", denied access",
r.Host)
serveFailure(
NewError(http.StatusForbidden, "Invalid host"), w, r, h.logger)
return
}
}
w.Header().Add("Date", time.Now().UTC().Format(time.RFC1123))
switch r.URL.Path {
case "/":
err = serveController(h.homeCtl, w, r, clientLogger)
case "/sshwifty/socket":
err = serveController(h.socketCtl, w, r, clientLogger)
case "/sshwifty/socket/verify":
err = serveController(h.socketVerifyCtl, w, r, clientLogger)
case "/robots.txt":
err = serveStaticCacheData(
"robots.txt",
staticFileExt(".txt"),
w,
r,
clientLogger)
case "/favicon.ico":
err = serveStaticCacheData(
"favicon.ico",
staticFileExt(".ico"),
w,
r,
clientLogger)
case "/manifest.json":
err = serveStaticCacheData(
"manifest.json",
staticFileExt(".json"),
w,
r,
clientLogger)
case "/browserconfig.xml":
err = serveStaticCacheData(
"browserconfig.xml",
staticFileExt(".xml"),
w,
r,
clientLogger)
default:
if strings.HasPrefix(r.URL.Path, assetsURLPrefix) &&
strings.ToUpper(r.Method) == "GET" {
err = serveStaticCacheData(
r.URL.Path[assetsURLPrefixLen:],
staticFileExt(r.URL.Path[assetsURLPrefixLen:]),
w,
r,
clientLogger)
} else {
err = ErrNotFound
}
}
if err == nil {
clientLogger.Info("Request completed: %s", r.URL.String())
return
}
clientLogger.Warning("Request ended with error: %s: %s",
r.URL.String(), err)
controllerErr, isControllerErr := err.(Error)
if isControllerErr {
serveFailure(controllerErr, w, r, h.logger)
return
}
serveFailure(
NewError(http.StatusInternalServerError, err.Error()), w, r, h.logger)
}
// Builder returns a http controller builder
func Builder(cmds command.Commands) server.HandlerBuilder {
return func(
commonCfg configuration.Common,
cfg configuration.Server,
logger log.Logger,
) http.Handler {
socketCtl := newSocketCtl(commonCfg, cfg, cmds)
return handler{
hostNameChecker: commonCfg.HostName + ":",
commonCfg: commonCfg,
logger: logger,
homeCtl: home{},
socketCtl: socketCtl,
socketVerifyCtl: newSocketVerification(socketCtl, cfg, commonCfg),
}
}
}