Skip to content

Commit fa8d40f

Browse files
lafrikslunny
authored andcommitted
Fix internal requests when gitea listens to unix socket or only external IP (gogs#2234)
* Fix internal requests when gitea listens to unix socket or only external IP * When Gitea is set to listen using FastCGI use AppURL for LocalURL
1 parent a4ca544 commit fa8d40f

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

modules/private/branch.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package private
66

77
import (
8-
"crypto/tls"
98
"encoding/json"
109
"fmt"
1110

@@ -20,9 +19,7 @@ func GetProtectedBranchBy(repoID int64, branchName string) (*models.ProtectedBra
2019
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/branch/%d/%s", repoID, branchName)
2120
log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL)
2221

23-
resp, err := newRequest(reqURL, "GET").SetTLSClientConfig(&tls.Config{
24-
InsecureSkipVerify: true,
25-
}).Response()
22+
resp, err := newInternalRequest(reqURL, "GET").Response()
2623
if err != nil {
2724
return nil, err
2825
}

modules/private/internal.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"crypto/tls"
99
"encoding/json"
1010
"fmt"
11+
"net"
1112
"net/http"
1213

1314
"code.gitea.io/gitea/modules/httplib"
@@ -34,15 +35,27 @@ func decodeJSONError(resp *http.Response) *Response {
3435
return &res
3536
}
3637

38+
func newInternalRequest(url, method string) *httplib.Request {
39+
req := newRequest(url, method).SetTLSClientConfig(&tls.Config{
40+
InsecureSkipVerify: true,
41+
})
42+
if setting.Protocol == setting.UnixSocket {
43+
req.SetTransport(&http.Transport{
44+
Dial: func(_, _ string) (net.Conn, error) {
45+
return net.Dial("unix", setting.HTTPAddr)
46+
},
47+
})
48+
}
49+
return req
50+
}
51+
3752
// UpdatePublicKeyUpdated update publick key updates
3853
func UpdatePublicKeyUpdated(keyID int64) error {
3954
// Ask for running deliver hook and test pull request tasks.
4055
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
4156
log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)
4257

43-
resp, err := newRequest(reqURL, "POST").SetTLSClientConfig(&tls.Config{
44-
InsecureSkipVerify: true,
45-
}).Response()
58+
resp, err := newInternalRequest(reqURL, "POST").Response()
4659
if err != nil {
4760
return err
4861
}

modules/private/push_update.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package private
66

77
import (
8-
"crypto/tls"
98
"encoding/json"
109
"fmt"
1110

@@ -25,9 +24,7 @@ func PushUpdate(opt models.PushUpdateOptions) error {
2524
return err
2625
}
2726

28-
resp, err := newRequest(reqURL, "POST").Body(body).SetTLSClientConfig(&tls.Config{
29-
InsecureSkipVerify: true,
30-
}).Response()
27+
resp, err := newInternalRequest(reqURL, "POST").Body(body).Response()
3128
if err != nil {
3229
return err
3330
}

modules/setting/setting.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,22 @@ func NewContext() {
658658
AppSubURL = strings.TrimSuffix(url.Path, "/")
659659
AppSubURLDepth = strings.Count(AppSubURL, "/")
660660

661-
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(string(Protocol) + "://localhost:" + HTTPPort + "/")
661+
var defaultLocalURL string
662+
switch Protocol {
663+
case UnixSocket:
664+
defaultLocalURL = "http://unix/"
665+
case FCGI:
666+
defaultLocalURL = AppURL
667+
default:
668+
defaultLocalURL = string(Protocol) + "://"
669+
if HTTPAddr == "0.0.0.0" {
670+
defaultLocalURL += "localhost"
671+
} else {
672+
defaultLocalURL += HTTPAddr
673+
}
674+
defaultLocalURL += ":" + HTTPPort + "/"
675+
}
676+
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
662677
OfflineMode = sec.Key("OFFLINE_MODE").MustBool()
663678
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
664679
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)

0 commit comments

Comments
 (0)