mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-21 08:53:50 +08:00
580e21dd2e
Gitea instance keeps reporting a lot of errors like "LFS SSH transfer connection denied, pure SSH protocol is disabled". When starting debugging the problem, there are more problems found. Try to address most of them: * avoid unnecessary server side error logs (change `fail()` to not log them) * figure out the broken tests/user2/lfs.git (added comments) * avoid `migratePushMirrors` failure when a repository doesn't exist (ignore them) * avoid "Authorization" (internal&lfs) header conflicts, remove the tricky "swapAuth" and use "X-Gitea-Internal-Auth" * make internal token comparing constant time (it wasn't a serous problem because in a real world it's nearly impossible to timing-attack the token, but good to fix and backport) * avoid duplicate routers (introduce AddOwnerRepoGitLFSRoutes) * avoid "internal (private)" routes using session/web context (they should use private context) * fix incorrect "path" usages (use "filepath") * fix incorrect mocked route point handling (need to check func nil correctly) * split some tests from "git general tests" to "git misc tests" (to keep "git_general_test.go" simple) Still no correct result for Git LFS SSH tests. So the code is kept there (`tests/integration/git_lfs_ssh_test.go`) and a FIXME explains the details.
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package private
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/modules/httplib"
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/proxyprotocol"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
)
|
|
|
|
// Response is used for internal request response (for user message and error message)
|
|
type Response struct {
|
|
Err string `json:"err,omitempty"` // server-side error log message, it won't be exposed to end users
|
|
UserMsg string `json:"user_msg,omitempty"` // meaningful error message for end users, it will be shown in git client's output.
|
|
}
|
|
|
|
func getClientIP() string {
|
|
sshConnEnv := strings.TrimSpace(os.Getenv("SSH_CONNECTION"))
|
|
if len(sshConnEnv) == 0 {
|
|
return "127.0.0.1"
|
|
}
|
|
return strings.Fields(sshConnEnv)[0]
|
|
}
|
|
|
|
func newInternalRequest(ctx context.Context, url, method string, body ...any) *httplib.Request {
|
|
if setting.InternalToken == "" {
|
|
log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q.
|
|
Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf)
|
|
}
|
|
|
|
req := httplib.NewRequest(url, method).
|
|
SetContext(ctx).
|
|
Header("X-Real-IP", getClientIP()).
|
|
Header("X-Gitea-Internal-Auth", fmt.Sprintf("Bearer %s", setting.InternalToken)).
|
|
SetTLSClientConfig(&tls.Config{
|
|
InsecureSkipVerify: true,
|
|
ServerName: setting.Domain,
|
|
})
|
|
|
|
if setting.Protocol == setting.HTTPUnix {
|
|
req.SetTransport(&http.Transport{
|
|
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
var d net.Dialer
|
|
conn, err := d.DialContext(ctx, "unix", setting.HTTPAddr)
|
|
if err != nil {
|
|
return conn, err
|
|
}
|
|
if setting.LocalUseProxyProtocol {
|
|
if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
|
|
_ = conn.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
return conn, err
|
|
},
|
|
})
|
|
} else if setting.LocalUseProxyProtocol {
|
|
req.SetTransport(&http.Transport{
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
var d net.Dialer
|
|
conn, err := d.DialContext(ctx, network, address)
|
|
if err != nil {
|
|
return conn, err
|
|
}
|
|
if err = proxyprotocol.WriteLocalHeader(conn); err != nil {
|
|
_ = conn.Close()
|
|
return nil, err
|
|
}
|
|
return conn, err
|
|
},
|
|
})
|
|
}
|
|
|
|
if len(body) == 1 {
|
|
req.Header("Content-Type", "application/json")
|
|
jsonBytes, _ := json.Marshal(body[0])
|
|
req.Body(jsonBytes)
|
|
} else if len(body) > 1 {
|
|
log.Fatal("Too many arguments for newInternalRequest")
|
|
}
|
|
|
|
req.SetTimeout(10*time.Second, 60*time.Second)
|
|
return req
|
|
}
|