2021-01-25 05:07:35 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-02-08 11:15:35 +05:30
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2021-01-25 05:07:35 +05:30
|
|
|
"net/http"
|
2022-02-08 11:15:35 +05:30
|
|
|
"os"
|
2021-05-13 01:28:55 +05:30
|
|
|
"strconv"
|
2021-01-25 05:07:35 +05:30
|
|
|
"strings"
|
|
|
|
|
2022-01-01 15:13:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2021-01-25 05:07:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/caddyserver/certmagic"
|
|
|
|
)
|
|
|
|
|
2022-02-08 11:15:35 +05:30
|
|
|
func getCARoot(path string) (*x509.CertPool, error) {
|
|
|
|
r, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode(r)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no PEM found in the file %s", path)
|
|
|
|
}
|
|
|
|
caRoot, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
certPool.AddCert(caRoot)
|
|
|
|
return certPool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runACME(listenAddr string, m http.Handler) error {
|
2021-01-25 05:07:35 +05:30
|
|
|
// If HTTP Challenge enabled, needs to be serving on port 80. For TLSALPN needs 443.
|
2021-07-08 17:08:13 +05:30
|
|
|
// Due to docker port mapping this can't be checked programmatically
|
2021-01-25 05:07:35 +05:30
|
|
|
// TODO: these are placeholders until we add options for each in settings with appropriate warning
|
|
|
|
enableHTTPChallenge := true
|
|
|
|
enableTLSALPNChallenge := true
|
2021-05-13 01:28:55 +05:30
|
|
|
altHTTPPort := 0
|
2021-05-14 20:09:10 +05:30
|
|
|
altTLSALPNPort := 0
|
2021-05-13 01:28:55 +05:30
|
|
|
|
|
|
|
if p, err := strconv.Atoi(setting.PortToRedirect); err == nil {
|
|
|
|
altHTTPPort = p
|
|
|
|
}
|
2021-05-14 20:09:10 +05:30
|
|
|
if p, err := strconv.Atoi(setting.HTTPPort); err == nil {
|
|
|
|
altTLSALPNPort = p
|
|
|
|
}
|
2021-01-25 05:07:35 +05:30
|
|
|
|
|
|
|
magic := certmagic.NewDefault()
|
2022-02-08 11:15:35 +05:30
|
|
|
magic.Storage = &certmagic.FileStorage{Path: setting.AcmeLiveDirectory}
|
|
|
|
// Try to use private CA root if provided, otherwise defaults to system's trust
|
|
|
|
var certPool *x509.CertPool
|
|
|
|
if setting.AcmeCARoot != "" {
|
|
|
|
var err error
|
|
|
|
certPool, err = getCARoot(setting.AcmeCARoot)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Failed to parse CA Root certificate, using default CA trust: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 05:07:35 +05:30
|
|
|
myACME := certmagic.NewACMEManager(magic, certmagic.ACMEManager{
|
2022-02-08 11:15:35 +05:30
|
|
|
CA: setting.AcmeURL,
|
|
|
|
TrustedRoots: certPool,
|
|
|
|
Email: setting.AcmeEmail,
|
|
|
|
Agreed: setting.AcmeTOS,
|
2021-01-25 05:07:35 +05:30
|
|
|
DisableHTTPChallenge: !enableHTTPChallenge,
|
|
|
|
DisableTLSALPNChallenge: !enableTLSALPNChallenge,
|
2021-05-14 20:09:10 +05:30
|
|
|
ListenHost: setting.HTTPAddr,
|
|
|
|
AltTLSALPNPort: altTLSALPNPort,
|
2021-05-13 01:28:55 +05:30
|
|
|
AltHTTPPort: altHTTPPort,
|
2021-01-25 05:07:35 +05:30
|
|
|
})
|
|
|
|
|
2021-04-23 02:12:33 +05:30
|
|
|
magic.Issuers = []certmagic.Issuer{myACME}
|
2021-01-25 05:07:35 +05:30
|
|
|
|
|
|
|
// this obtains certificates or renews them if necessary
|
2022-02-08 11:15:35 +05:30
|
|
|
err := magic.ManageSync(graceful.GetManager().HammerContext(), []string{setting.Domain})
|
2021-01-25 05:07:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := magic.TLSConfig()
|
2021-07-13 22:47:46 +05:30
|
|
|
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
|
2021-01-25 05:07:35 +05:30
|
|
|
|
2021-11-20 11:42:43 +05:30
|
|
|
if version := toTLSVersion(setting.SSLMinimumVersion); version != 0 {
|
|
|
|
tlsConfig.MinVersion = version
|
|
|
|
}
|
|
|
|
if version := toTLSVersion(setting.SSLMaximumVersion); version != 0 {
|
|
|
|
tlsConfig.MaxVersion = version
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set curve preferences
|
|
|
|
if curves := toCurvePreferences(setting.SSLCurvePreferences); len(curves) > 0 {
|
|
|
|
tlsConfig.CurvePreferences = curves
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set cipher suites
|
|
|
|
if ciphers := toTLSCiphers(setting.SSLCipherSuites); len(ciphers) > 0 {
|
|
|
|
tlsConfig.CipherSuites = ciphers
|
|
|
|
}
|
|
|
|
|
2021-01-25 05:07:35 +05:30
|
|
|
if enableHTTPChallenge {
|
|
|
|
go func() {
|
|
|
|
log.Info("Running Let's Encrypt handler on %s", setting.HTTPAddr+":"+setting.PortToRedirect)
|
|
|
|
// all traffic coming into HTTP will be redirect to HTTPS automatically (LE HTTP-01 validation happens here)
|
2022-01-20 23:16:10 +05:30
|
|
|
err := runHTTP("tcp", setting.HTTPAddr+":"+setting.PortToRedirect, "Let's Encrypt HTTP Challenge", myACME.HTTPChallengeHandler(http.HandlerFunc(runLetsEncryptFallbackHandler)))
|
2021-01-25 05:07:35 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to start the Let's Encrypt handler on port %s: %v", setting.PortToRedirect, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-11-03 06:03:54 +05:30
|
|
|
return runHTTPSWithTLSConfig("tcp", listenAddr, "Web", tlsConfig, m)
|
2021-01-25 05:07:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func runLetsEncryptFallbackHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "GET" && r.Method != "HEAD" {
|
|
|
|
http.Error(w, "Use HTTPS", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove the trailing slash at the end of setting.AppURL, the request
|
|
|
|
// URI always contains a leading slash, which would result in a double
|
|
|
|
// slash
|
|
|
|
target := strings.TrimSuffix(setting.AppURL, "/") + r.URL.RequestURI()
|
2022-03-23 10:24:07 +05:30
|
|
|
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
|
2021-01-25 05:07:35 +05:30
|
|
|
}
|