Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix internal requests when gitea listens to unix socket or only external IP #2234

Merged
merged 2 commits into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions modules/private/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package private

import (
"crypto/tls"
"encoding/json"
"fmt"

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

resp, err := newRequest(reqURL, "GET").SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
}).Response()
resp, err := newInternalRequest(reqURL, "GET").Response()
if err != nil {
return nil, err
}
Expand Down
19 changes: 16 additions & 3 deletions modules/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"

"code.gitea.io/gitea/modules/httplib"
Expand All @@ -34,15 +35,27 @@ func decodeJSONError(resp *http.Response) *Response {
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
func UpdatePublicKeyUpdated(keyID int64) error {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)

resp, err := newRequest(reqURL, "POST").SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
}).Response()
resp, err := newInternalRequest(reqURL, "POST").Response()
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions modules/private/push_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package private

import (
"crypto/tls"
"encoding/json"
"fmt"

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

resp, err := newRequest(reqURL, "POST").Body(body).SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
}).Response()
resp, err := newInternalRequest(reqURL, "POST").Body(body).Response()
if err != nil {
return err
}
Expand Down
17 changes: 16 additions & 1 deletion modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,22 @@ func NewContext() {
AppSubURL = strings.TrimSuffix(url.Path, "/")
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()
DisableRouterLog = sec.Key("DISABLE_ROUTER_LOG").MustBool()
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(workDir)
Expand Down