Browse Source

Add an option to allow redirect of http port 80 to https. (#1928)

* Add an option to allow redirect of http port 80 to https.

This is an "opt in" option (default is to not redirect).  It will only redirect
if protocol is https and the new REDIRECT_PORT_80 option is set to true.

The Port to redirect in previous commit was hardcoded to 80, now it can be
specified in the app.ini, defaulting to 80.  The boolean option to turn
redirection on has been changed to REDIRECT_OTHER_PORT to be logically
consistent with the new port option.

Signed-off-by: Mike Fellows <mike.fellows@shaw.ca>
tags/v1.4.0-rc1
Mike Fellows 6 years ago
parent
commit
fabf3f2fc2
3 changed files with 33 additions and 0 deletions
  1. 23
    0
      cmd/web.go
  2. 6
    0
      custom/conf/app.ini.sample
  3. 4
    0
      modules/setting/setting.go

+ 23
- 0
cmd/web.go View File

}, },
} }


func runHTTPRedirector() {
source := fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.PortToRedirect)
dest := strings.TrimSuffix(setting.AppURL, "/")
log.Info("Redirecting: %s to %s", source, dest)

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
target := dest + r.URL.Path
if len(r.URL.RawQuery) > 0 {
target += "?" + r.URL.RawQuery
}
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
})

var err = runHTTP(source, context2.ClearHandler(handler))

if err != nil {
log.Fatal(4, "Failed to start port redirection: %v", err)
}
}

func runWeb(ctx *cli.Context) error { func runWeb(ctx *cli.Context) error {
if ctx.IsSet("config") { if ctx.IsSet("config") {
setting.CustomConf = ctx.String("config") setting.CustomConf = ctx.String("config")
case setting.HTTP: case setting.HTTP:
err = runHTTP(listenAddr, context2.ClearHandler(m)) err = runHTTP(listenAddr, context2.ClearHandler(m))
case setting.HTTPS: case setting.HTTPS:
if setting.RedirectOtherPort {
go runHTTPRedirector()
}
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m)) err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
case setting.FCGI: case setting.FCGI:
listener, err := net.Listen("tcp", listenAddr) listener, err := net.Listen("tcp", listenAddr)

+ 6
- 0
custom/conf/app.ini.sample View File

; Listen address. Either a IPv4/IPv6 address or the path to a unix socket. ; Listen address. Either a IPv4/IPv6 address or the path to a unix socket.
HTTP_ADDR = 0.0.0.0 HTTP_ADDR = 0.0.0.0
HTTP_PORT = 3000 HTTP_PORT = 3000
; If REDIRECT_OTHER_PORT is true, and PROTOCOL is set to https an http server
; will be started on PORT_TO_REDIRECT and redirect request to the main
; ROOT_URL. Defaults are false for REDIRECT_OTHER_PORT and 80 for
; PORT_TO_REDIRECT.
REDIRECT_OTHER_PORT = false
PORT_TO_REDIRECT = 80
; Permission for unix socket ; Permission for unix socket
UNIX_SOCKET_PERMISSION = 666 UNIX_SOCKET_PERMISSION = 666
; Local (DMZ) URL for Gitea workers (such as SSH update) accessing web service. ; Local (DMZ) URL for Gitea workers (such as SSH update) accessing web service.

+ 4
- 0
modules/setting/setting.go View File

HTTPAddr string HTTPAddr string
HTTPPort string HTTPPort string
LocalURL string LocalURL string
RedirectOtherPort bool
PortToRedirect string
OfflineMode bool OfflineMode bool
DisableRouterLog bool DisableRouterLog bool
CertFile string CertFile string
defaultLocalURL += ":" + HTTPPort + "/" defaultLocalURL += ":" + HTTPPort + "/"
} }
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL) LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
RedirectOtherPort = sec.Key("REDIRECT_OTHER_PORT").MustBool(false)
PortToRedirect = sec.Key("PORT_TO_REDIRECT").MustString("80")
OfflineMode = sec.Key("OFFLINE_MODE").MustBool() OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool() DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath) StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath)

Loading…
Cancel
Save