mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-27 10:42:36 +01:00
maintain (refactoring): refactor server code for easier maintenance
This commit is contained in:
parent
01d36f1601
commit
0350790cfb
8 changed files with 71 additions and 65 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net"
|
||||
|
|
@ -35,6 +36,23 @@ var HTTP = http.Client{
|
|||
}),
|
||||
}
|
||||
|
||||
var DefaultTLSConfig = tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
CipherSuites: []uint16{
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||
},
|
||||
PreferServerCipherSuites: true,
|
||||
CurvePreferences: []tls.CurveID{
|
||||
tls.CurveP256,
|
||||
tls.X25519,
|
||||
},
|
||||
}
|
||||
|
||||
func NewTransormedTransport(transport http.Transport) http.RoundTripper {
|
||||
return &TransformedTransport{ &transport }
|
||||
}
|
||||
|
|
|
|||
16
server/common/dummy.go
Normal file
16
server/common/dummy.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"io"
|
||||
slog "log"
|
||||
)
|
||||
|
||||
func NewNilLogger() *slog.Logger {
|
||||
return slog.New(dummyWriter{}, "", slog.LstdFlags)
|
||||
}
|
||||
type dummyWriter struct {
|
||||
io.Writer
|
||||
}
|
||||
func(this dummyWriter) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
|
@ -90,3 +90,24 @@ func SendErrorResult(res http.ResponseWriter, err error) {
|
|||
}(err.Error())
|
||||
encoder.Encode(APIErrorMessage{"error", m})
|
||||
}
|
||||
|
||||
func Page(stuff string) string {
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
|
||||
<style>
|
||||
html { background: #f4f4f4; color: #455164; font-size: 16px; font-family: -apple-system,system-ui,BlinkMacSystemFont,Roboto,"Helvetica Neue",Arial,sans-serif; }
|
||||
body { text-align: center; padding-top: 50px; text-align: center; }
|
||||
h1 { font-weight: 200; line-height: 1em; font-size: 40px; }
|
||||
p { opacity: 0.7; }
|
||||
span { font-size: 0.7em; opacity: 0.7; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
` + stuff + `
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ func IndexHandler(_path string) func(App, http.ResponseWriter, *http.Request) {
|
|||
urlObj, err := URL.Parse(req.URL.String())
|
||||
if err != nil {
|
||||
res.WriteHeader(http.StatusInternalServerError)
|
||||
res.Write([]byte(dumbPage("<h1>404 - Not Found</h1>")))
|
||||
res.Write([]byte(Page("<h1>404 - Not Found</h1>")))
|
||||
return
|
||||
}
|
||||
url := urlObj.Path
|
||||
|
|
@ -41,12 +41,12 @@ func IndexHandler(_path string) func(App, http.ResponseWriter, *http.Request) {
|
|||
strings.HasPrefix(url, "/view/") == false && strings.HasPrefix(url, "/files/") == false &&
|
||||
url != "/login" && url != "/logout" && strings.HasPrefix(url, "/admin") == false {
|
||||
res.WriteHeader(http.StatusNotFound)
|
||||
res.Write([]byte(dumbPage("<h1>404 - Not Found</h1>")))
|
||||
res.Write([]byte(Page("<h1>404 - Not Found</h1>")))
|
||||
return
|
||||
} else if ua := req.Header.Get("User-Agent"); strings.Contains(ua, "MSIE ") {
|
||||
res.WriteHeader(http.StatusBadRequest)
|
||||
res.Write([]byte(
|
||||
dumbPage(`
|
||||
Page(`
|
||||
<h1>Internet explorer is not yet supported</h1>
|
||||
<p>
|
||||
To provide the best possible experience for everyone else, we don't support IE at this time.
|
||||
|
|
@ -62,7 +62,7 @@ func IndexHandler(_path string) func(App, http.ResponseWriter, *http.Request) {
|
|||
}
|
||||
|
||||
func AboutHandler(ctx App, res http.ResponseWriter, req *http.Request) {
|
||||
t, _ := template.New("about").Parse(dumbPage(`
|
||||
t, _ := template.New("about").Parse(Page(`
|
||||
<h1> {{index .App 0}} <br>
|
||||
<span>({{index .App 1}} - {{index .App 2}})</span>
|
||||
</h1>
|
||||
|
|
@ -140,24 +140,3 @@ func hashFile (path string, n int) string {
|
|||
}
|
||||
return QuickHash(fmt.Sprintf("%s %d %d %s", path, stat.Size(), stat.Mode(), stat.ModTime()), n)
|
||||
}
|
||||
|
||||
func dumbPage (stuff string) string {
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
|
||||
<style>
|
||||
html { background: #f4f4f4; color: #455164; font-size: 16px; font-family: -apple-system,system-ui,BlinkMacSystemFont,Roboto,"Helvetica Neue",Arial,sans-serif; }
|
||||
body { text-align: center; padding-top: 50px; text-align: center; }
|
||||
h1 { font-weight: 200; line-height: 1em; font-size: 40px; }
|
||||
p { opacity: 0.7; }
|
||||
span { font-size: 0.7em; opacity: 0.7; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
` + stuff + `
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package plg_start_http
|
||||
package plg_starter_http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
@ -12,7 +12,7 @@ func init() {
|
|||
port := Config.Get("general.port").Int()
|
||||
|
||||
Hooks.Register.Starter(func (r *mux.Router) {
|
||||
Log.Info("[http] starting...")
|
||||
Log.Info("[http] starting ...")
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: r,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package plg_start_http2
|
||||
package plg_starter_http2
|
||||
|
||||
/*
|
||||
* In golang, HTTP2 server are written in the same way as HTTPS server, the only difference beeing
|
||||
|
|
@ -22,26 +22,12 @@ func init() {
|
|||
domain := Config.Get("general.host").String()
|
||||
|
||||
Hooks.Register.Starter(func (r *mux.Router) {
|
||||
Log.Info("[https] starting...%s", domain)
|
||||
Log.Info("[https] starting ...%s", domain)
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":https"),
|
||||
Handler: r,
|
||||
TLSConfig: &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
CipherSuites: []uint16{
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||
},
|
||||
PreferServerCipherSuites: true,
|
||||
CurvePreferences: []tls.CurveID{
|
||||
tls.CurveP256,
|
||||
tls.X25519,
|
||||
},
|
||||
},
|
||||
TLSConfig: &DefaultTLSConfig,
|
||||
ErrorLog: NewNilLogger(),
|
||||
}
|
||||
|
||||
switch domain {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package plg_start_https
|
||||
package plg_starter_https
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
|
@ -16,27 +16,13 @@ func init() {
|
|||
domain := Config.Get("general.host").String()
|
||||
|
||||
Hooks.Register.Starter(func (r *mux.Router) {
|
||||
Log.Info("[https] starting...%s", domain)
|
||||
Log.Info("[https] starting ...%s", domain)
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":https"),
|
||||
Handler: r,
|
||||
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
|
||||
TLSConfig: &tls.Config{
|
||||
MinVersion: tls.VersionTLS12,
|
||||
CipherSuites: []uint16{
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
||||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
||||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||
},
|
||||
PreferServerCipherSuites: true,
|
||||
CurvePreferences: []tls.CurveID{
|
||||
tls.CurveP256,
|
||||
tls.X25519,
|
||||
},
|
||||
},
|
||||
TLSConfig: &DefaultTLSConfig,
|
||||
ErrorLog: NewNilLogger(),
|
||||
}
|
||||
|
||||
switch domain {
|
||||
|
|
@ -60,7 +46,7 @@ func init() {
|
|||
}
|
||||
srv.TLSConfig.GetCertificate = mngr.GetCertificate
|
||||
}
|
||||
|
||||
|
||||
go func() {
|
||||
if err := srv.ListenAndServeTLS("", ""); err != nil {
|
||||
Log.Error("[https]: listen_serve %v", err)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package plg_start_tor
|
||||
package plg_starter_tor
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
|
|||
Loading…
Reference in a new issue