diff --git a/cmd/web.go b/cmd/web.go index 36e3ddf..57f53c5 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -11,6 +11,7 @@ import ( "github.com/ouqiang/gocron/modules/logger" "github.com/ouqiang/gocron/service" "github.com/ouqiang/gocron/models" + "github.com/ouqiang/gocron/modules/setting" ) // web服务器默认端口 @@ -56,6 +57,13 @@ func initModule() { if !app.Installed { return } + + config, err := setting.Read(app.AppConfig) + if err != nil { + logger.Fatal("读取应用配置失败", err) + } + app.Setting = config + models.Db = models.CreateDb() // 初始化定时任务 diff --git a/models/model.go b/models/model.go index 5e0058e..326a3bb 100644 --- a/models/model.go +++ b/models/model.go @@ -9,7 +9,6 @@ import ( "strings" "time" "github.com/ouqiang/gocron/modules/logger" - "github.com/ouqiang/gocron/modules/setting" "github.com/ouqiang/gocron/modules/app" ) @@ -124,23 +123,15 @@ func keepDbAlived(engine *xorm.Engine) { // 获取数据库配置 func getDbConfig() map[string]string { - config, err := setting.Read(app.AppConfig) - if err != nil { - logger.Fatal("获取应用配置失败", err) - } - section := config.Section("db") - if err != nil { - logger.Fatal("获取DB配置失败", err) - } var db map[string]string = make(map[string]string) - db["user"] = section.Key("user").String() - db["password"] = section.Key("password").String() - db["host"] = section.Key("host").String() - db["port"] = section.Key("port").String() - db["database"] = section.Key("database").String() - db["charset"] = section.Key("charset").String() - db["prefix"] = section.Key("prefix").String() - db["engine"] = section.Key("engine").String() + db["user"] = app.Setting.Key("db.user").String() + db["password"] = app.Setting.Key("db.password").String() + db["host"] = app.Setting.Key("db.host").String() + db["port"] = app.Setting.Key("db.port").String() + db["database"] = app.Setting.Key("db.database").String() + db["charset"] = app.Setting.Key("db.charset").String() + db["prefix"] = app.Setting.Key("db.prefix").String() + db["engine"] = app.Setting.Key("db.engine").String() return db } \ No newline at end of file diff --git a/modules/app/app.go b/modules/app/app.go index da9203c..7ed189f 100644 --- a/modules/app/app.go +++ b/modules/app/app.go @@ -6,6 +6,7 @@ import ( "github.com/ouqiang/gocron/modules/logger" "runtime" "github.com/ouqiang/gocron/modules/utils" + "gopkg.in/ini.v1" ) var ( @@ -15,6 +16,7 @@ var ( DataDir string // 存放session等 AppConfig string // 应用配置文件 Installed bool // 应用是否安装过 + Setting *ini.Section // 应用配置 ) func InitEnv() { @@ -47,7 +49,7 @@ func IsInstalled() bool { func CreateInstallLock() error { _, err := os.Create(ConfDir + "/install.lock") if err != nil { - logger.Error("创建安装锁文件失败") + logger.Error("创建安装锁文件conf/install.lock失败") } return err diff --git a/modules/httpclient/http_client.go b/modules/httpclient/http_client.go index c6d75c9..daf3234 100644 --- a/modules/httpclient/http_client.go +++ b/modules/httpclient/http_client.go @@ -62,7 +62,6 @@ func request(req *http.Request, timeout int) ResponseWrapper { } func setRequestHeader(req *http.Request) { - req.Header.Set("Connection", "keep-alive") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 golang/gocron") } diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 457a429..e40ffdb 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -5,39 +5,41 @@ import ( "gopkg.in/ini.v1" ) -// 读取配置 -func Read(filename string) (config *ini.File, err error) { - config, err = ini.Load(filename) - if err != nil { - return - } +const DefaultSection = "default" - return +// 读取配置 +func Read(filename string) (*ini.Section,error) { + config, err := ini.Load(filename) + if err != nil { + return nil, err + } + section := config.Section(DefaultSection) + + return section, nil } // 写入配置 -func Write(config map[string]map[string]string, filename string) error { +func Write(config map[string]string, filename string) error { if len(config) == 0 { return errors.New("参数不能为空") } file := ini.Empty() - for sectionName, items := range config { - if sectionName == "" { - return errors.New("节名称不能为空") + + section, err := file.NewSection(DefaultSection) + if err != nil { + return err + } + for key, value := range config { + if key == "" { + continue } - section, err := file.NewSection(sectionName) + _, err = section.NewKey(key, value) if err != nil { return err } - for key, value := range items { - _, err = section.NewKey(key, value) - if err != nil { - return err - } - } } - err := file.SaveTo(filename) + err = file.SaveTo(filename) return err } diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index e0ea10d..885d455 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -89,7 +89,7 @@ func Exec(sshConfig SSHConfig, cmd string) (output string, err error) { // 后台运行 if sshConfig.ExecTimeout < 0 { go session.CombinedOutput(cmd) - time.Sleep(5 * time.Second) + time.Sleep(3 * time.Second) return "", nil } // 不限制超时 diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 70c2c19..0430ed1 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -91,8 +91,9 @@ func ReplaceStrings(s string, old []string, replace []string) string { } func InStringSlice(slice []string, element string) bool { + element = strings.TrimSpace(element) for _, v := range slice { - if v == element { + if strings.TrimSpace(v) == element{ return true } } diff --git a/routers/install/install.go b/routers/install/install.go index 87926ac..081073f 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -7,8 +7,6 @@ import ( "github.com/ouqiang/gocron/modules/utils" "gopkg.in/macaron.v1" "strconv" - "github.com/ouqiang/gocron/modules/logger" - "github.com/go-macaron/binding" "fmt" "github.com/ouqiang/gocron/service" ) @@ -29,10 +27,6 @@ type InstallForm struct { AdminEmail string `binding:"Required;Email;MaxSize(50)"` } -func(f InstallForm) Error(ctx *macaron.Context, errs binding.Errors) { - logger.Error(errs) -} - func Create(ctx *macaron.Context) { if app.Installed { ctx.Redirect("/") @@ -61,6 +55,12 @@ func Store(ctx *macaron.Context, form InstallForm) string { return json.CommonFailure("数据库配置写入文件失败", err) } + appConfig, err := setting.Read(app.AppConfig) + if err != nil { + return json.CommonFailure("读取应用配置失败", err) + } + app.Setting = appConfig + models.Db = models.CreateDb() // 创建数据库表 migration := new(models.Migration) @@ -89,19 +89,18 @@ func Store(ctx *macaron.Context, form InstallForm) string { return json.Success("安装成功", nil) } -// 数据库配置写入文件 +// 配置写入文件 func writeConfig(form InstallForm) error { - dbConfig := map[string]map[string]string{ - "db": map[string]string{ - "engine": form.DbType, - "host": form.DbHost, - "port": strconv.Itoa(form.DbPort), - "user": form.DbUsername, - "password": form.DbPassword, - "database": form.DbName, - "prefix": form.DbTablePrefix, - "charset": "utf8", - }, + dbConfig := map[string]string{ + "db.engine": form.DbType, + "db.host": form.DbHost, + "db.port": strconv.Itoa(form.DbPort), + "db.user": form.DbUsername, + "db.password": form.DbPassword, + "db.database": form.DbName, + "db.prefix": form.DbTablePrefix, + "db.charset": "utf8", + "allow_ips" : "", } return setting.Write(dbConfig, app.AppConfig) diff --git a/routers/routers.go b/routers/routers.go index a5c867d..e2d8609 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -142,6 +142,7 @@ func RegisterMiddleware(m *macaron.Macaron) { m.Use(csrf.Csrfer()) m.Use(toolbox.Toolboxer(m)) checkAppInstall(m) + ipAuth(m) userAuth(m) setShareData(m) } @@ -159,6 +160,20 @@ func checkAppInstall(m *macaron.Macaron) { }) } +// IP验证, 通过反向代理访问gocron,需设置Header X-Real-IP才能获取到客户端真实IP +func ipAuth(m *macaron.Macaron) { + m.Use(func(ctx *macaron.Context) { + allowIpsStr := app.Setting.Key("allow_ips").String() + if allowIpsStr == "" { + return + } + clientIp := ctx.RemoteAddr() + if !utils.InStringSlice(allowIps, clientIp) { + ctx.Status(403) + } + }) +} + // 用户认证 func userAuth(m *macaron.Macaron) { m.Use(func(ctx *macaron.Context, sess session.Store) { @@ -167,7 +182,7 @@ func userAuth(m *macaron.Macaron) { } uri := ctx.Req.URL.Path found := false - excludePaths := []string{"/install", "/user/login", "/"} + excludePaths := []string{"/install", "/user/login", "/api"} for _, path := range excludePaths { if strings.HasPrefix(uri, path) { found = true @@ -199,14 +214,6 @@ func setShareData(m *macaron.Macaron) { }) } -// 管理员认证 -func adminAuth(ctx *macaron.Context, sess session.Store) { - if !user.IsAdmin(sess) { - ctx.Data["Title"] = "无权限访问此页面" - ctx.HTML(403, "error/no_permission") - } -} - func isAjaxRequest(ctx *macaron.Context) bool { req := ctx.Req.Header.Get("X-Requested-With") if req == "XMLHttpRequest" { diff --git a/service/task.go b/service/task.go index 0fe50f2..a05c0ef 100644 --- a/service/task.go +++ b/service/task.go @@ -57,7 +57,7 @@ func (task *Task) Initialize() { taskModel := new(models.Task) taskList, err := taskModel.ActiveList() if err != nil { - logger.Error("获取任务列表错误-", err.Error()) + logger.Error("定时任务初始化#获取任务列表错误-", err.Error()) return } if len(taskList) == 0 { diff --git a/templates/error/no_permission.html b/templates/error/no_permission.html deleted file mode 100644 index 3fdc4ca..0000000 --- a/templates/error/no_permission.html +++ /dev/null @@ -1,12 +0,0 @@ -{{{ template "common/header" . }}} - -{{{ template "common/footer" . }}} \ No newline at end of file diff --git a/templates/task/task_form.html b/templates/task/task_form.html index 74658ab..35ac58e 100644 --- a/templates/task/task_form.html +++ b/templates/task/task_form.html @@ -66,13 +66,13 @@ -
+
-
+
@@ -143,7 +143,7 @@
{{else}} - 邮箱配置

+ 邮箱配置

{{/each}} @@ -156,7 +156,7 @@
{{else}} - Slack配置 + Slack配置 {{/each}}