-
Notifications
You must be signed in to change notification settings - Fork 8
/
instance.go
131 lines (117 loc) · 3.93 KB
/
instance.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
// Copyright 2021 The VPN House Authors. All rights reserved.
// Use of this source code is governed by a AGPL-style
// license that can be found in the LICENSE file.
package httpapi
import (
"io"
"net/http"
"github.com/go-chi/chi/v5"
tunnelAPI "github.com/vpnhouse/api/go/server/tunnel"
adminAPI "github.com/vpnhouse/api/go/server/tunnel_admin"
mgmtAPI "github.com/vpnhouse/api/go/server/tunnel_mgmt"
"github.com/vpnhouse/tunnel/internal/authorizer"
"github.com/vpnhouse/tunnel/internal/frontend"
"github.com/vpnhouse/tunnel/internal/manager"
"github.com/vpnhouse/tunnel/internal/runtime"
"github.com/vpnhouse/tunnel/internal/storage"
"github.com/vpnhouse/tunnel/pkg/auth"
"github.com/vpnhouse/tunnel/pkg/ipam"
"github.com/vpnhouse/tunnel/pkg/keystore"
"github.com/vpnhouse/tunnel/pkg/xerror"
"go.uber.org/zap"
)
type TunnelAPI struct {
runtime *runtime.TunnelRuntime
manager *manager.Manager
adminJWT *auth.JWTMaster
authorizer authorizer.JWTAuthorizer
storage *storage.Storage
keystore keystore.Keystore
ippool *ipam.IPAM
running bool
}
func NewTunnelHandlers(
runtime *runtime.TunnelRuntime,
manager *manager.Manager,
adminJWT *auth.JWTMaster,
jwtAuthorizer authorizer.JWTAuthorizer,
storage *storage.Storage,
keystore keystore.Keystore,
ip4am *ipam.IPAM,
) *TunnelAPI {
instance := &TunnelAPI{
runtime: runtime,
manager: manager,
adminJWT: adminJWT,
authorizer: authorizer.WithEntitlement(jwtAuthorizer, authorizer.Wireguard),
storage: storage,
keystore: keystore,
ippool: ip4am,
running: true,
}
return instance
}
func (tun *TunnelAPI) RegisterHandlers(r chi.Router) {
tun.addStaticHandler(r)
// admin API
adminAPI.HandlerWithOptions(tun, adminAPI.ChiServerOptions{
BaseRouter: r,
Middlewares: []adminAPI.MiddlewareFunc{
tun.adminAuthMiddleware,
tun.initialSetupMiddleware,
tun.versionRestrictionsMiddleware,
},
})
if tun.runtime.Features.WithPublicAPI() {
tunnelAPI.HandlerWithOptions(tun, tunnelAPI.ChiServerOptions{
BaseRouter: r,
})
}
if tun.runtime.Features.WithFederation() {
mgmtAPI.HandlerWithOptions(tun, mgmtAPI.ChiServerOptions{
BaseRouter: r,
Middlewares: []mgmtAPI.MiddlewareFunc{
tun.federationAuthMiddleware,
},
})
}
}
func (tun *TunnelAPI) addStaticHandler(r chi.Router) {
staticRoot := frontend.StaticRoot
if tun.runtime.Settings.AdminAPI != nil && len(tun.runtime.Settings.AdminAPI.StaticRoot) > 0 {
// use the filesystem directory if we have the directory configured
// (useful for the local development), otherwise - serve from the
// embedded static root.
staticRoot = http.Dir(tun.runtime.Settings.AdminAPI.StaticRoot)
}
tun.addStaticRoute(r, staticRoot)
}
func (tun *TunnelAPI) addStaticRoute(r chi.Router, staticFiles http.FileSystem) {
webfs := http.FileServer(staticFiles)
// handle frontend redirects
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
nfrw := &skipNotFoundWriter{ResponseWriter: w}
webfs.ServeHTTP(nfrw, r)
// note that skipNotFoundWriter acts as a normal Writer
// for all status codes except the NotFound one.
// So handle it explicitly by our own.
if nfrw.status == http.StatusNotFound {
w.Header().Set("content-type", "text/html")
// try to load and serve the `index.html` instead of the non-existing file:
// we have to do so because very likely the client asks for some /path
// that is handled by the SPAs router. So we always have to serve
// the frontend app instead of the requested path.
fd, err := staticFiles.Open("index.html")
if err != nil {
zap.L().Warn("no `index.html` found on webFS, telling user that there is no frontend deployed")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Error: no frontend files deployed."))
return
}
defer fd.Close()
if _, err := io.Copy(w, fd); err != nil {
_ = xerror.WInternalError("xhttp", "failed to write index.html bytes to the http connection", err)
}
}
})
}