Fix internal requests when gitea listens to unix socket or only external IP (#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
This commit is contained in:
Lauris BH 2017-08-03 18:32:13 +03:00 committed by Lunny Xiao
parent a4ca54425f
commit fa8d40faca
4 changed files with 34 additions and 12 deletions

View File

@ -5,7 +5,6 @@
package private package private
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -20,9 +19,7 @@ func GetProtectedBranchBy(repoID int64, branchName string) (*models.ProtectedBra
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/branch/%d/%s", repoID, branchName) reqURL := setting.LocalURL + fmt.Sprintf("api/internal/branch/%d/%s", repoID, branchName)
log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL) log.GitLogger.Trace("GetProtectedBranchBy: %s", reqURL)
resp, err := newRequest(reqURL, "GET").SetTLSClientConfig(&tls.Config{ resp, err := newInternalRequest(reqURL, "GET").Response()
InsecureSkipVerify: true,
}).Response()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,6 +8,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"net/http" "net/http"
"code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/httplib"
@ -34,15 +35,27 @@ func decodeJSONError(resp *http.Response) *Response {
return &res return &res
} }
func newInternalRequest(url, method string) *httplib.Request {
req := newRequest(url, method).SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
})
if setting.Protocol == setting.UnixSocket {
req.SetTransport(&http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return net.Dial("unix", setting.HTTPAddr)
},
})
}
return req
}
// UpdatePublicKeyUpdated update publick key updates // UpdatePublicKeyUpdated update publick key updates
func UpdatePublicKeyUpdated(keyID int64) error { func UpdatePublicKeyUpdated(keyID int64) error {
// Ask for running deliver hook and test pull request tasks. // Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID) reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL) log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)
resp, err := newRequest(reqURL, "POST").SetTLSClientConfig(&tls.Config{ resp, err := newInternalRequest(reqURL, "POST").Response()
InsecureSkipVerify: true,
}).Response()
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,7 +5,6 @@
package private package private
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -25,9 +24,7 @@ func PushUpdate(opt models.PushUpdateOptions) error {
return err return err
} }
resp, err := newRequest(reqURL, "POST").Body(body).SetTLSClientConfig(&tls.Config{ resp, err := newInternalRequest(reqURL, "POST").Body(body).Response()
InsecureSkipVerify: true,
}).Response()
if err != nil { if err != nil {
return err return err
} }

View File

@ -658,7 +658,22 @@ func NewContext() {
AppSubURL = strings.TrimSuffix(url.Path, "/") AppSubURL = strings.TrimSuffix(url.Path, "/")
AppSubURLDepth = strings.Count(AppSubURL, "/") AppSubURLDepth = strings.Count(AppSubURL, "/")
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(string(Protocol) + "://localhost:" + HTTPPort + "/") var defaultLocalURL string
switch Protocol {
case UnixSocket:
defaultLocalURL = "http://unix/"
case FCGI:
defaultLocalURL = AppURL
default:
defaultLocalURL = string(Protocol) + "://"
if HTTPAddr == "0.0.0.0" {
defaultLocalURL += "localhost"
} else {
defaultLocalURL += HTTPAddr
}
defaultLocalURL += ":" + HTTPPort + "/"
}
LocalURL = sec.Key("LOCAL_ROOT_URL").MustString(defaultLocalURL)
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(workDir) StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)