From 44e1b38f0f8afcd7a289542d1096d24decc28c32 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 15 Nov 2019 18:05:28 +0800 Subject: [PATCH] update at 2019-11-15 18:05:28 by ehlxr --- .gitignore | 3 ++ Dockerfile | 8 ++++ LICENSE | 21 +++++++++ Makefile | 75 ++++++++++++++++++++++++++++++ README.md | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 13 ++++++ main.go | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ version | 1 + 8 files changed, 387 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go create mode 100644 version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..24e88b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +logs +go.sum +dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f2461f5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM ehlxr/alpine + +LABEL maintainer="ehlxr " + +COPY ./dist/ddgo_linux_amd64 /usr/local/bin/ddgo + + +ENTRYPOINT ["/usr/local/bin/ddgo"] \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..28bc9df --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright © 2019 ehlxr + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3bf3a0b --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +BUILD_VERSION := $(shell cat version) +BUILD_TIME := $(shell date "+%F %T") +COMMIT_SHA1 := $(shell git rev-parse HEAD) +ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +DIST_DIR := $(ROOT_DIR)/dist/ + +#VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/ +VERSION_PATH := main +LD_APP_NAMW := -X '$(VERSION_PATH).AppName=$(shell basename `pwd`)' +LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)' +LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)' +LD_GO_VERSION := -X '$(VERSION_PATH).GoVersion=`go version`' +LD_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)' +LD_FLAGS := "$(LD_APP_NAMW) $(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_VERSION) -w -s" + +RELEASE_VERSION = $(version) +REGISTRY_URL = $(url) + +ifeq ("$(RELEASE_VERSION)","") + RELEASE_VERSION := $(shell echo `date "+%Y%m%d_%H%M%S"`) +endif + +.PHONY : build release clean install upx docker-push docker + +build: +ifneq ($(shell type gox >/dev/null 2>&1;echo $$?), 0) + @echo "Can't find gox command, will start installation..." + cd ~ && go get -v -u github.com/mitchellh/gox && cd $(ROOT_DIR) +endif + @# $(if $(findstring 0,$(shell type gox >/dev/null 2>&1;echo $$?)),,echo "Can't find gox command, will start installation...";GO111MODULE=off go get -v -u github.com/mitchellh/gox) + gox -ldflags $(LD_FLAGS) -osarch="darwin/amd64 linux/386 linux/amd64 windows/amd64" \ + -output="$(DIST_DIR){{.Dir}}_{{.OS}}_{{.Arch}}" + +docker: build upx +ifneq ("$(REGISTRY_URL)","") + @echo ========== current docker tag is: $(RELEASE_VERSION) ========== + + docker build -t $(REGISTRY_URL)/monitor_server:$(RELEASE_VERSION) -f Dockerfile . +else + @echo "url arg should not be empty" +endif + +docker-push: docker + docker push $(REGISTRY_URL)/monitor_server:$(RELEASE_VERSION) + +clean: + rm -rf $(DIST_DIR)* + +install: + go install -ldflags $(LD_FLAGS) + +# 如果一个规则是以 .IGNORE 作为目标的,那么这个规则中所有命令都将会忽略错误 +.IGNORE: + upx + +# 压缩。需要安装 https://github.com/upx/upx +upx: + @# 在命令前面加上 "-",表示不管该命令出不出错,后面的命令都将继续执行下去 + @# -upx $(DIST_DIR)** + upx $(DIST_DIR)** + +release: build upx +ifneq ($(shell type ghr >/dev/null 2>&1;echo $$?), 0) + @echo "Can't find ghr command, will start installation..." + cd ~ && go get -v -u github.com/tcnksm/ghr && cd $(ROOT_DIR) +endif + @# $(if $(findstring 0,$(shell type ghr >/dev/null 2>&1;echo $$?)),,echo "Can't find ghr command, will start installation...";GO111MODULE=off go get -v -u github.com/tcnksm/ghr) + ghr -u ehlxr -t $(GITHUB_RELEASE_TOKEN) -replace -delete --debug ${BUILD_VERSION} $(DIST_DIR) + +# this tells 'make' to export all variables to child processes by default. +.EXPORT_ALL_VARIABLES: + +GO111MODULE = on +GOPROXY = https://goproxy.cn,direct +GOSUMDB = sum.golang.google.cn diff --git a/README.md b/README.md new file mode 100644 index 0000000..201b967 --- /dev/null +++ b/README.md @@ -0,0 +1,132 @@ +# JWT +> This is a simple tool to sign, verify and show JSON Web Tokens ([JWT](http://jwt.io/)) from the command line, base [jwt-go](https://github.com/dgrijalva/jwt-go). + +[![jwt](https://asciinema.org/a/P0O3XBCslMNam0UduazwPhB6o.png)](https://asciinema.org/a/P0O3XBCslMNam0UduazwPhB6o?autoplay=1) + +# Install + +build with go get + +``` +➜ go get -u github.com/ehlxr/jwt +``` + +build with make + +``` +➜ git clone https://github.com/ehlxr/jwt.git + +➜ cd jwt && make install +``` + +or download [releases](https://github.com/ehlxr/jwt/releases) binary package. + +# Usage + +``` +➜ jwt +JWT(Json Web Token) 工具 +用于生成、验证、查看 JWT + +Usage: + jwt [command] + +Available Commands: + help Help about any command + show JWT Token 查看 + sign JWT 签名 + verify JWT token 验证 + version Print version + +Flags: + --config string config file (default is $HOME/.jwt.yaml) + -h, --help help for jwt + +Use "jwt [command] --help" for more information about a command. + +``` + +## JWT version + +``` +➜ jwt version + + __ __ __ ______ + /\ \ /\ \ _ \ \ /\__ _\ + _\_\ \ \ \ \/ ".\ \ \/_/\ \/ +/\_____\ \ \__/".~\_\ \ \_\ +\/_____/ \/_/ \/_/ \/_/ + + + +Name: jwt +Version: v1.0.2 +BuildTime: 2019-10-02 16:56:20 +GitCommit: c546aaaee1b6a6b03eabf396f9cab01718e22104 +GoVersion: go version go1.13.1 darwin/amd64 +``` + +## sign JWT + +``` +➜ jwt sign -h + +签名 JWT token 并复制到剪切板 +标记 * 号的 flag 为必须项 + +Usage: + jwt sign [flags] + +Flags: + -c, --claims argList add additional claims. may be used more than once (default {}) + -d, --data string * path or json to claims object to sign, '-' to read from clipboard, or '+' to use only -claim args + -H, --header argList add additional header params. may be used more than once (default {}) + -h, --help help for sign + -k, --key string * path of keyfile or key argument + +Global Flags: + --config string config file (default is $HOME/.jwt.yaml) +``` + +## show JWT + +``` +➜ jwt show -h + +查看 JWT Token 内容 +标记 * 号的 flag 为必须项 + +Usage: + jwt show [flags] + +Flags: + -h, --help help for show + -t, --token string * path or arg of JWT token to verify, '-' to read from clipboard + +Global Flags: + --config string config file (default is $HOME/.jwt.yaml) +``` + +## verify JWT + +``` +➜ jwt verify -h + +验证 JWT token 是否有效 +标记 * 号的 flag 为必须项 + +Usage: + jwt verify [flags] + +Flags: + -h, --help help for verify + -k, --key string * path of keyfile or key argument + -t, --token string * path or arg of JWT token to verify, '-' to read from clipboard + +Global Flags: + --config string config file (default is $HOME/.jwt.yaml) +``` + +## Thanks to the following organizations for providing open source licenses + +[图片名称](https://www.jetbrains.com/?from=jwt) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3451c85 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module github.com/ehlxr/monitor + +go 1.13 + +require ( + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/hpcloud/tail v1.0.0 + github.com/jessevdk/go-flags v1.4.1-0.20181221193153-c0795c8afcf4 + golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect + gopkg.in/fsnotify.v1 v1.4.7 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect + unknwon.dev/clog/v2 v2.0.0-beta.5 +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..d60e69c --- /dev/null +++ b/main.go @@ -0,0 +1,134 @@ +package main + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "fmt" + "github.com/hpcloud/tail" + "github.com/jessevdk/go-flags" + "io/ioutil" + log "unknwon.dev/clog/v2" + + "net/http" + "os" + "regexp" +) + +var ( + AppName string + Version string + BuildTime string + GitCommit string + GoVersion string + + versionTpl = `%s +Name: %s +Version: %s +BuildTime: %s +GitCommit: %s +GoVersion: %s + +` + bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K" + + opts struct { + MonitorFile string `short:"m" long:"monitor-file" env:"MONITOR_FILE" description:"The file to be monitored" required:"true"` + KeyWord string `short:"k" long:"key-word" env:"KEY_WORD" description:"Key word to be filter" required:"true"` + WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingtalk" required:"true"` + Version bool `short:"v" long:"version" description:"Show version info"` + } +) + +func init() { + initLog() +} + +func main() { + parseArg() + + tf, err := tail.TailFile(opts.MonitorFile, + tail.Config{ + Follow: true, + Location: &tail.SeekInfo{Offset: 0, Whence: 2}, + }) + if err != nil { + log.Fatal("Tail file %+v", err) + } + + for line := range tf.Lines { + if ok, _ := regexp.Match(opts.KeyWord, []byte(line.Text)); ok { + log.Info("%s", dingToInfo(line.Text)) + } + + } +} + +func initLog() { + err := log.NewConsole() + if err != nil { + panic("unable to create new logger: " + err.Error()) + } +} + +func parseArg() { + parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash) + if AppName != "" { + parser.Name = AppName + } + + if _, err := parser.Parse(); err != nil { + if opts.Version { + printVersion() + os.Exit(0) + } + + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { + _, _ = fmt.Fprintln(os.Stdout, err) + os.Exit(0) + } + + _, _ = fmt.Fprintln(os.Stderr, err) + + parser.WriteHelp(os.Stderr) + + os.Exit(1) + } +} + +func dingToInfo(msg string) []byte { + content, data := make(map[string]string), make(map[string]interface{}) + + content["content"] = msg + data["msgtype"] = "text" + data["text"] = content + b, _ := json.Marshal(data) + + log.Info("send to %s data <%s>", + opts.WebHookUrl, + b) + + resp, err := http.Post(opts.WebHookUrl, + "application/json", + bytes.NewBuffer(b)) + if err != nil { + log.Error("send request to %s %+v", + opts.WebHookUrl, + err) + + } + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + log.Info("send to %s data <%s> result is %s", + opts.WebHookUrl, + b, + body) + return body +} + +// printVersion Print out version information +func printVersion() { + banner, _ := base64.StdEncoding.DecodeString(bannerBase64) + fmt.Printf(versionTpl, banner, AppName, Version, BuildTime, GitCommit, GoVersion) +} diff --git a/version b/version new file mode 100644 index 0000000..95e94cd --- /dev/null +++ b/version @@ -0,0 +1 @@ +v0.0.1 \ No newline at end of file