Skip to content

Commit 4609eba

Browse files
42wimlafrikslunny6543techknowlogick
authored
Fix ipv6 parsing (#12321)
* Fix ipv6 parsing * Update modules/setting/setting.go Co-authored-by: 6543 <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 415fc82 commit 4609eba

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cmd/web.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package cmd
77
import (
88
"context"
99
"fmt"
10+
"net"
1011
"net/http"
1112
_ "net/http/pprof" // Used for debugging if enabled and a web server is running
1213
"os"
@@ -156,7 +157,7 @@ func runWeb(ctx *cli.Context) error {
156157

157158
listenAddr := setting.HTTPAddr
158159
if setting.Protocol != setting.UnixSocket && setting.Protocol != setting.FCGIUnix {
159-
listenAddr += ":" + setting.HTTPPort
160+
listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort)
160161
}
161162
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
162163

models/repo.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
_ "image/jpeg"
1818
"image/png"
1919
"io/ioutil"
20+
"net"
2021
"net/url"
2122
"os"
2223
"path"
@@ -969,12 +970,21 @@ func (repo *Repository) cloneLink(isWiki bool) *CloneLink {
969970
}
970971

971972
cl := new(CloneLink)
973+
974+
// if we have a ipv6 literal we need to put brackets around it
975+
// for the git cloning to work.
976+
sshDomain := setting.SSH.Domain
977+
ip := net.ParseIP(setting.SSH.Domain)
978+
if ip != nil && ip.To4() == nil {
979+
sshDomain = "[" + setting.SSH.Domain + "]"
980+
}
981+
972982
if setting.SSH.Port != 22 {
973-
cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", sshUser, setting.SSH.Domain, setting.SSH.Port, repo.OwnerName, repoName)
983+
cl.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", sshUser, net.JoinHostPort(setting.SSH.Domain, strconv.Itoa(setting.SSH.Port)), repo.OwnerName, repoName)
974984
} else if setting.Repository.UseCompatSSHURI {
975-
cl.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", sshUser, setting.SSH.Domain, repo.OwnerName, repoName)
985+
cl.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", sshUser, sshDomain, repo.OwnerName, repoName)
976986
} else {
977-
cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", sshUser, setting.SSH.Domain, repo.OwnerName, repoName)
987+
cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", sshUser, sshDomain, repo.OwnerName, repoName)
978988
}
979989
cl.HTTPS = ComposeHTTPSCloneURL(repo.OwnerName, repoName)
980990
return cl

modules/setting/setting.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,10 @@ func NewContext() {
626626
StaticURLPrefix = strings.TrimSuffix(sec.Key("STATIC_URL_PREFIX").MustString(AppSubURL), "/")
627627
AppSubURLDepth = strings.Count(AppSubURL, "/")
628628
// Check if Domain differs from AppURL domain than update it to AppURL's domain
629-
// TODO: Can be replaced with url.Hostname() when minimal GoLang version is 1.8
630-
urlHostname := strings.SplitN(appURL.Host, ":", 2)[0]
629+
urlHostname, _, err := net.SplitHostPort(appURL.Host)
630+
if err != nil {
631+
log.Fatal("Invalid host in ROOT_URL '%s': %s", appURL.Host, err)
632+
}
631633
if urlHostname != Domain && net.ParseIP(urlHostname) == nil {
632634
Domain = urlHostname
633635
}
@@ -643,11 +645,10 @@ func NewContext() {
643645
default:
644646
defaultLocalURL = string(Protocol) + "://"
645647
if HTTPAddr == "0.0.0.0" {
646-
defaultLocalURL += "localhost"
648+
defaultLocalURL += net.JoinHostPort("localhost", HTTPPort) + "/"
647649
} else {
648-
defaultLocalURL += HTTPAddr
650+
defaultLocalURL += net.JoinHostPort(HTTPAddr, HTTPPort) + "/"
649651
}
650-
defaultLocalURL += ":" + HTTPPort + "/"
651652
}
652653
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
653654
RedirectOtherPort = sec.Key("REDIRECT_OTHER_PORT").MustBool(false)

0 commit comments

Comments
 (0)