From 0950fc69f9e47ad1380f3a2042c9520e0574a9c2 Mon Sep 17 00:00:00 2001 From: ouqiang Date: Fri, 26 Jan 2018 21:08:52 +0800 Subject: [PATCH] =?UTF-8?q?data,=20conf,=20log=E7=9B=AE=E5=BD=95=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +- data/.gitkeep | 0 log/.gitkeep | 0 modules/ssh/ssh.go | 139 --------------------------------------------- package.json | 28 +++++++++ routers/routers.go | 9 +-- 6 files changed, 36 insertions(+), 146 deletions(-) delete mode 100644 data/.gitkeep delete mode 100644 log/.gitkeep delete mode 100644 modules/ssh/ssh.go create mode 100644 package.json diff --git a/.gitignore b/.gitignore index e58329e..ab76740 100644 --- a/.gitignore +++ b/.gitignore @@ -25,9 +25,9 @@ _testmain.go .DS_Store .idea -log/* -data/* -conf/* +log +data +conf profile/* public/resource/javascript/vue.js gocron diff --git a/data/.gitkeep b/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/log/.gitkeep b/log/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go deleted file mode 100644 index da9ab81..0000000 --- a/modules/ssh/ssh.go +++ /dev/null @@ -1,139 +0,0 @@ -package ssh - -import ( - "errors" - "fmt" - "golang.org/x/crypto/ssh" - "net" - "time" -) - -type HostAuthType int8 // 认证方式 - -const ( - HostPassword = 1 // 密码认证 - HostPublicKey = 2 // 公钥认证 -) - -const SSHConnectTimeout = 10 - -type SSHConfig struct { - AuthType HostAuthType - User string - Password string - PrivateKey string - Host string - Port int - ExecTimeout int // 执行超时时间 -} - -type Result struct { - Output string - Err error -} - -func parseSSHConfig(sshConfig SSHConfig) (config *ssh.ClientConfig, err error) { - timeout := time.Duration(SSHConnectTimeout) * time.Second - // 密码认证 - if sshConfig.AuthType == HostPassword { - config = &ssh.ClientConfig{ - User: sshConfig.User, - Auth: []ssh.AuthMethod{ - ssh.Password(sshConfig.Password), - }, - Timeout: timeout, - HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { - return nil - }, - } - - return - } - - signer, err := ssh.ParsePrivateKey([]byte(sshConfig.PrivateKey)) - if err != nil { - return - } - - // 公钥认证 - config = &ssh.ClientConfig{ - User: sshConfig.User, - Auth: []ssh.AuthMethod{ - ssh.PublicKeys(signer), - }, - Timeout: timeout, - HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { - return nil - }, - } - - return -} - -// 执行shell命令 -func Exec(sshConfig SSHConfig, cmd string) (output string, err error) { - client, err := getClient(sshConfig) - if err != nil { - return "", err - } - defer client.Close() - - session, err := client.NewSession() - - if err != nil { - return "", err - } - defer session.Close() - - // 后台运行 - if sshConfig.ExecTimeout < 0 { - go session.CombinedOutput(cmd) - time.Sleep(10 * time.Second) - return "", nil - } - // 不限制超时 - if sshConfig.ExecTimeout == 0 { - outputByte, execErr := session.CombinedOutput(cmd) - output = string(outputByte) - err = execErr - return - } - - var resultChan chan Result = make(chan Result) - var timeoutChan chan bool = make(chan bool) - go func() { - output, err := session.CombinedOutput(cmd) - resultChan <- Result{string(output), err} - }() - // todo 等待超时后,如何停止远程正在执行的任务, 使用timeout命令,但不具有通用性 - go triggerTimeout(timeoutChan, sshConfig.ExecTimeout) - select { - case result := <-resultChan: - output = result.Output - err = result.Err - case <-timeoutChan: - output = "" - err = errors.New("timeout") - } - - return -} - -func getClient(sshConfig SSHConfig) (*ssh.Client, error) { - config, err := parseSSHConfig(sshConfig) - if err != nil { - return nil, err - } - addr := fmt.Sprintf("%s:%d", sshConfig.Host, sshConfig.Port) - - return ssh.Dial("tcp", addr, config) -} - -func triggerTimeout(ch chan bool, timeout int) { - // 最长执行时间不能超过24小时 - if timeout <= 0 || timeout > 86400 { - timeout = 86400 - } - time.Sleep(time.Duration(timeout) * time.Second) - close(ch) -} diff --git a/package.json b/package.json new file mode 100644 index 0000000..309414b --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "gocron", + "version": "1.0.0", + "description": "[![Build Status](https://travis-ci.org/ouqiang/gocron.png)](https://travis-ci.org/ouqiang/gocron) # gocron - 定时任务管理系统", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ouqiang/gocron.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ouqiang/gocron/issues" + }, + "homepage": "https://github.com/ouqiang/gocron#readme", + "devDependencies": { + "cz-conventional-changelog": "^2.0.0" + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + } + } +} diff --git a/routers/routers.go b/routers/routers.go index 159295e..55cf84d 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -22,6 +22,7 @@ import ( "strconv" "strings" "time" + "net/http" ) // 静态文件目录 @@ -112,7 +113,7 @@ func Register(m *macaron.Macaron) { m.NotFound(func(ctx *macaron.Context) { if isGetRequest(ctx) && !isAjaxRequest(ctx) { ctx.Data["Title"] = "404 - NOT FOUND" - ctx.HTML(404, "error/404") + ctx.HTML(http.StatusNotFound, "error/404") } else { json := utils.JsonResponse{} ctx.Resp.Write([]byte(json.Failure(utils.NotFound, "您访问的地址不存在"))) @@ -123,7 +124,7 @@ func Register(m *macaron.Macaron) { logger.Debug("500错误") if isGetRequest(ctx) && !isAjaxRequest(ctx) { ctx.Data["Title"] = "500 - INTERNAL SERVER ERROR" - ctx.HTML(500, "error/500") + ctx.HTML(http.StatusInternalServerError, "error/500") } else { json := utils.JsonResponse{} ctx.Resp.Write([]byte(json.Failure(utils.ServerError, "网站暂时无法访问,请稍后再试"))) @@ -202,7 +203,7 @@ func ipAuth(ctx *macaron.Context) { allowIps := strings.Split(allowIpsStr, ",") if !utils.InStringSlice(allowIps, clientIp) { logger.Warnf("非法IP访问-%s", clientIp) - ctx.Status(403) + ctx.Status(http.StatusForbidden) } } @@ -251,7 +252,7 @@ func urlAuth(ctx *macaron.Context, sess session.Store) { } } - ctx.Status(403) + ctx.Status(http.StatusUnauthorized) }