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

log real ip of requests from ssh #21216

Merged
merged 6 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions modules/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"net"
"net/http"
"os"
"strings"

"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/json"
Expand All @@ -18,13 +20,14 @@ import (
"code.gitea.io/gitea/modules/setting"
)

func newRequest(ctx context.Context, url, method string) *httplib.Request {
func newRequest(ctx context.Context, url, method, sourceIP string) *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)
}
return httplib.NewRequest(url, method).
SetContext(ctx).
Header("X-Real-IP", sourceIP).
Header("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
}

Expand All @@ -42,8 +45,16 @@ func decodeJSONError(resp *http.Response) *Response {
return &res
}

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) *httplib.Request {
req := newRequest(ctx, url, method).SetTLSClientConfig(&tls.Config{
req := newRequest(ctx, url, method, getClientIP()).SetTLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
ServerName: setting.Domain,
})
Expand Down
3 changes: 3 additions & 0 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
"code.gitea.io/gitea/services/repository/archiver"
"code.gitea.io/gitea/services/task"
"code.gitea.io/gitea/services/webhook"

chi_middleware "github.com/go-chi/chi/v5/middleware"
)

func mustInit(fn func() error) {
Expand Down Expand Up @@ -167,6 +169,7 @@ func GlobalInitInstalled(ctx context.Context) {
func NormalRoutes(ctx context.Context) *web.Route {
ctx, _ = templates.HTMLRenderer(ctx)
r := web.NewRoute()
r.Use(chi_middleware.RealIP)
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
for _, middle := range common.Middlewares() {
r.Use(middle)
}
Expand Down