Skip to content

Commit 60aca42

Browse files
Merge branch 'main' into bump-db
2 parents 97f78de + fec8324 commit 60aca42

File tree

11 files changed

+81
-3
lines changed

11 files changed

+81
-3
lines changed

cmd/serv.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func fail(userMessage, logMessage string, args ...interface{}) {
8181
}
8282
}
8383

84+
if len(logMessage) > 0 {
85+
_ = private.SSHLog(true, fmt.Sprintf(logMessage+": ", args...))
86+
}
87+
8488
os.Exit(1)
8589
}
8690

custom/conf/app.example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ ROUTER = console
444444
;ACCESS_LOG_TEMPLATE = {{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"
445445
;;
446446
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447+
;;
448+
;; SSH log (Creates log from ssh git request)
449+
;;
450+
;ENABLE_SSH_LOG = false
451+
;;
447452
;; Other Settings
448453
;;
449454
;; Print Stacktraces with logs. (Rarely helpful.) Either "Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "None"

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ Default templates for project boards:
657657
- `ROUTER`: **console**: The mode or name of the log the router should log to. (If you set this to `,` it will log to default gitea logger.)
658658
NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take effect. Configure each mode in per mode log subsections `\[log.modename.router\]`.
659659
- `ENABLE_ACCESS_LOG`: **false**: Creates an access.log in NCSA common log format, or as per the following template
660+
- `ENABLE_SSH_LOG`: **false**: save ssh log to log file
660661
- `ACCESS`: **file**: Logging mode for the access logger, use a comma to separate values. Configure each mode in per mode log subsections `\[log.modename.access\]`. By default the file mode will log to `$ROOT_PATH/access.log`. (If you set this to `,` it will log to the default gitea logger.)
661662
- `ACCESS_LOG_TEMPLATE`: **`{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`**: Sets the template used to create the access log.
662663
- The following variables are available:

modules/private/hook.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package private
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"net/http"
1011
"net/url"
@@ -57,6 +58,12 @@ type HookOptions struct {
5758
IsDeployKey bool
5859
}
5960

61+
// SSHLogOption ssh log options
62+
type SSHLogOption struct {
63+
IsError bool
64+
Message string
65+
}
66+
6067
// HookPostReceiveResult represents an individual result from PostReceive
6168
type HookPostReceiveResult struct {
6269
Results []HookPostReceiveBranchResult
@@ -146,3 +153,27 @@ func SetDefaultBranch(ownerName, repoName, branch string) error {
146153
}
147154
return nil
148155
}
156+
157+
// SSHLog sends ssh error log response
158+
func SSHLog(isErr bool, msg string) error {
159+
reqURL := setting.LocalURL + "api/internal/ssh/log"
160+
req := newInternalRequest(reqURL, "POST")
161+
req = req.Header("Content-Type", "application/json")
162+
163+
jsonBytes, _ := json.Marshal(&SSHLogOption{
164+
IsError: isErr,
165+
Message: msg,
166+
})
167+
req.Body(jsonBytes)
168+
169+
req.SetTimeout(60*time.Second, 60*time.Second)
170+
resp, err := req.Response()
171+
if err != nil {
172+
return fmt.Errorf("unable to contact gitea: %v", err)
173+
}
174+
defer resp.Body.Close()
175+
if resp.StatusCode != http.StatusOK {
176+
return fmt.Errorf("Error returned from gitea: %v", decodeJSONError(resp).Err)
177+
}
178+
return nil
179+
}

modules/setting/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func newLogService() {
287287

288288
options := newDefaultLogOptions()
289289
options.bufferLength = Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
290+
EnableSSHLog = Cfg.Section("log").Key("ENABLE_SSH_LOG").MustBool(false)
290291

291292
description := LogDescription{
292293
Name: log.DEFAULT,

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ var (
319319
DisableRouterLog bool
320320
RouterLogLevel log.Level
321321
EnableAccessLog bool
322+
EnableSSHLog bool
322323
AccessLogTemplate string
323324
EnableXORMLog bool
324325

routers/private/internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func Routes() *web.Route {
5555

5656
r.Post("/ssh/authorized_keys", AuthorizedPublicKeyByContent)
5757
r.Post("/ssh/{id}/update/{repoid}", UpdatePublicKeyInRepo)
58+
r.Post("/ssh/log", bind(private.SSHLogOption{}), SSHLog)
5859
r.Post("/hook/pre-receive/{owner}/{repo}", bind(private.HookOptions{}), HookPreReceive)
5960
r.Post("/hook/post-receive/{owner}/{repo}", bind(private.HookOptions{}), HookPostReceive)
6061
r.Post("/hook/set-default-branch/{owner}/{repo}/{branch}", SetDefaultBranch)

routers/private/ssh_log.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package private
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/private"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/modules/web"
15+
)
16+
17+
// SSHLog hook to response ssh log
18+
func SSHLog(ctx *context.PrivateContext) {
19+
if !setting.EnableSSHLog {
20+
ctx.Status(http.StatusOK)
21+
return
22+
}
23+
24+
opts := web.GetForm(ctx).(*private.SSHLogOption)
25+
26+
if opts.IsError {
27+
log.Error("ssh: %v", opts.Message)
28+
ctx.Status(http.StatusOK)
29+
return
30+
}
31+
32+
log.Debug("ssh: %v", opts.Message)
33+
ctx.Status(http.StatusOK)
34+
}

templates/repo/diff/comments.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{ $createdStr:= TimeSinceUnix .CreatedUnix $.root.Lang }}
44
<div class="comment" id="{{.HashTag}}">
55
{{if .OriginalAuthor }}
6-
<span class="avatar"><img src="{{AppSubUrl}}/img/avatar_default.png"></span>
6+
<span class="avatar"><img src="{{AppSubUrl}}/assets/img/avatar_default.png"></span>
77
{{else}}
88
<a class="avatar" {{if gt .Poster.ID 0}}href="{{.Poster.HomeLink}}"{{end}}>
99
{{avatar .Poster}}

templates/repo/issue/view_content.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ui class="ui timeline">
1414
<div id="{{.Issue.HashTag}}" class="timeline-item comment first">
1515
{{if .Issue.OriginalAuthor }}
16-
<span class="timeline-avatar"><img src="{{AppSubUrl}}/img/avatar_default.png"></span>
16+
<span class="timeline-avatar"><img src="{{AppSubUrl}}/assets/img/avatar_default.png"></span>
1717
{{else}}
1818
<a class="timeline-avatar" {{if gt .Issue.Poster.ID 0}}href="{{.Issue.Poster.HomeLink}}"{{end}}>
1919
{{avatar .Issue.Poster}}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{if eq .Type 0}}
1414
<div class="timeline-item comment" id="{{.HashTag}}">
1515
{{if .OriginalAuthor }}
16-
<span class="timeline-avatar"><img src="{{AppSubUrl}}/img/avatar_default.png"></span>
16+
<span class="timeline-avatar"><img src="{{AppSubUrl}}/assets/img/avatar_default.png"></span>
1717
{{else}}
1818
<a class="timeline-avatar" {{if gt .Poster.ID 0}}href="{{.Poster.HomeLink}}"{{end}}>
1919
{{avatar .Poster}}

0 commit comments

Comments
 (0)