1- package nginx
1+ package nginx_log
22
33import (
44"encoding/json"
5- "fmt"
6- "github.com/0xJacky/Nginx-UI/internal/cache"
7- "github.com/0xJacky/Nginx-UI/internal/helper"
85"github.com/0xJacky/Nginx-UI/internal/nginx"
9- "github.com/0xJacky/Nginx-UI/settings "
6+ "github.com/0xJacky/Nginx-UI/internal/nginx_log "
107"github.com/gin-gonic/gin"
118"github.com/gorilla/websocket"
129"github.com/hpcloud/tail"
@@ -70,7 +67,7 @@ func GetNginxLogPage(c *gin.Context) {
7067c .JSON (http .StatusInternalServerError , nginxLogPageResp {
7168Error : "log file is not regular file" ,
7269})
73- logger .Error ("log file is not regular file:" , logPath )
70+ logger .Errorf ("log file is not regular file: %s " , logPath )
7471return
7572}
7673
@@ -132,30 +129,7 @@ func GetNginxLogPage(c *gin.Context) {
132129})
133130}
134131
135- // isLogPathUnderWhiteList checks if the log path is under one of the paths in LogDirWhiteList
136- func isLogPathUnderWhiteList (path string ) bool {
137- cacheKey := fmt .Sprintf ("isLogPathUnderWhiteList:%s" , path )
138- res , ok := cache .Get (cacheKey )
139- // no cache, check it
140- if ! ok {
141- for _ , whitePath := range settings .NginxSettings .LogDirWhiteList {
142- if helper .IsUnderDirectory (path , whitePath ) {
143- cache .Set (cacheKey , true , 0 )
144- return true
145- }
146- }
147- return false
148- }
149- return res .(bool )
150- }
151-
152132func getLogPath (control * controlStruct ) (logPath string , err error ) {
153- if len (settings .NginxSettings .LogDirWhiteList ) == 0 {
154- err = errors .New ("The settings.NginxSettings.LogDirWhiteList has not been configured. " +
155- "For security reasons, please configure a whitelist of log directories. " +
156- "Please visit https://nginxui.com/guide/config-nginx.html for more information." )
157- return
158- }
159133switch control .Type {
160134case "site" :
161135var config * nginx.NgxConfig
@@ -167,12 +141,12 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
167141}
168142
169143if control .ServerIdx >= len (config .Servers ) {
170- err = errors . New ( "serverIdx out of range" )
144+ err = nginx_log . ErrServerIdxOutOfRange
171145return
172146}
173147
174148if control .DirectiveIdx >= len (config .Servers [control .ServerIdx ].Directives ) {
175- err = errors . New ( "DirectiveIdx out of range" )
149+ err = nginx_log . ErrDirectiveIdxOutOfRange
176150return
177151}
178152
@@ -181,12 +155,12 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
181155case "access_log" , "error_log" :
182156// ok
183157default :
184- err = errors . New ( "directive.Params neither access_log nor error_log" )
158+ err = nginx_log . ErrLogDirective
185159return
186160}
187161
188162if directive .Params == "" {
189- err = errors . New ( "directive.Params is empty" )
163+ err = nginx_log . ErrDirectiveParamsIsEmpty
190164return
191165}
192166
@@ -200,8 +174,7 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
200174path := nginx .GetErrorLogPath ()
201175
202176if path == "" {
203- err = errors .New ("settings.NginxLogSettings.ErrorLogPath is empty," +
204- " refer to https://nginxui.com/guide/config-nginx.html for more information" )
177+ err = nginx_log .ErrErrorLogPathIsEmpty
205178return
206179}
207180
@@ -210,18 +183,16 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
210183path := nginx .GetAccessLogPath ()
211184
212185if path == "" {
213- err = errors .New ("settings.NginxLogSettings.AccessLogPath is empty," +
214- " refer to https://nginxui.com/guide/config-nginx.html for more information" )
186+ err = nginx_log .ErrAccessLogPathIsEmpty
215187return
216188}
217189
218190logPath = path
219191}
220192
221193// check if logPath is under one of the paths in LogDirWhiteList
222- if ! isLogPathUnderWhiteList (logPath ) {
223- err = errors .New ("The log path is not under the paths in LogDirWhiteList." )
224- return "" , err
194+ if ! nginx_log .IsLogPathUnderWhiteList (logPath ) {
195+ return "" , nginx_log .ErrLogPathIsNotUnderTheLogDirWhiteList
225196}
226197return
227198}
0 commit comments