Skip to content

Commit fabf3f2

Browse files
MCFbkcsoft
authored andcommitted
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 <[email protected]>
1 parent f5155b9 commit fabf3f2

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

cmd/web.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ and it takes care of all the other things for you`,
5151
},
5252
}
5353

54+
func runHTTPRedirector() {
55+
source := fmt.Sprintf("%s:%s", setting.HTTPAddr, setting.PortToRedirect)
56+
dest := strings.TrimSuffix(setting.AppURL, "/")
57+
log.Info("Redirecting: %s to %s", source, dest)
58+
59+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
target := dest + r.URL.Path
61+
if len(r.URL.RawQuery) > 0 {
62+
target += "?" + r.URL.RawQuery
63+
}
64+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
65+
})
66+
67+
var err = runHTTP(source, context2.ClearHandler(handler))
68+
69+
if err != nil {
70+
log.Fatal(4, "Failed to start port redirection: %v", err)
71+
}
72+
}
73+
5474
func runWeb(ctx *cli.Context) error {
5575
if ctx.IsSet("config") {
5676
setting.CustomConf = ctx.String("config")
@@ -124,6 +144,9 @@ func runWeb(ctx *cli.Context) error {
124144
case setting.HTTP:
125145
err = runHTTP(listenAddr, context2.ClearHandler(m))
126146
case setting.HTTPS:
147+
if setting.RedirectOtherPort {
148+
go runHTTPRedirector()
149+
}
127150
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
128151
case setting.FCGI:
129152
listener, err := net.Listen("tcp", listenAddr)

custom/conf/app.ini.sample

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ ROOT_URL = %(PROTOCOL)s://%(DOMAIN)s:%(HTTP_PORT)s/
109109
; Listen address. Either a IPv4/IPv6 address or the path to a unix socket.
110110
HTTP_ADDR = 0.0.0.0
111111
HTTP_PORT = 3000
112+
; If REDIRECT_OTHER_PORT is true, and PROTOCOL is set to https an http server
113+
; will be started on PORT_TO_REDIRECT and redirect request to the main
114+
; ROOT_URL. Defaults are false for REDIRECT_OTHER_PORT and 80 for
115+
; PORT_TO_REDIRECT.
116+
REDIRECT_OTHER_PORT = false
117+
PORT_TO_REDIRECT = 80
112118
; Permission for unix socket
113119
UNIX_SOCKET_PERMISSION = 666
114120
; Local (DMZ) URL for Gitea workers (such as SSH update) accessing web service.

modules/setting/setting.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ var (
9696
HTTPAddr string
9797
HTTPPort string
9898
LocalURL string
99+
RedirectOtherPort bool
100+
PortToRedirect string
99101
OfflineMode bool
100102
DisableRouterLog bool
101103
CertFile string
@@ -741,6 +743,8 @@ func NewContext() {
741743
defaultLocalURL += ":" + HTTPPort + "/"
742744
}
743745
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
746+
RedirectOtherPort = sec.Key("REDIRECT_OTHER_PORT").MustBool(false)
747+
PortToRedirect = sec.Key("PORT_TO_REDIRECT").MustString("80")
744748
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
745749
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
746750
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(AppWorkPath)

0 commit comments

Comments
 (0)