This commit is contained in:
ehlxr 2019-11-14 17:13:45 +08:00
parent e2fa0d4a38
commit 1e93285245
8 changed files with 296 additions and 14 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
**.log
/ddgo
go.sum
go.sum
dist

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM ehlxr/alpine
LABEL maintainer="ehlxr <ehlxr.me@gmail.com>"
COPY ./dist/ddgo /usr/local/bin/
COPY ./entrypoint.sh /entrypoint.sh
ENTRYPOINT ["sh", "/entrypoint.sh"]

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright © 2019 ehlxr <ehlxr.me@gmail.com>
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.

55
Makefile Normal file
View File

@ -0,0 +1,55 @@
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_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_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_VERSION) -w -s"
.PHONY : build release clean install upx
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}}"
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

132
README.md Normal file
View File

@ -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
[<img src="https://cdn.ehlxr.top/jetbrains.png" width = "200" height = "217" alt="图片名称" align=center />](https://www.jetbrains.com/?from=jwt)

6
entrypoint.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
# 须以特权模式运行docker run --privileged
sysctl net.core.somaxconn=1024
/usr/local/bin/ddgo $@

83
main.go
View File

@ -2,15 +2,36 @@ package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"io"
"io/ioutil"
"net/http"
"os"
"os/signal"
log "unknwon.dev/clog/v2"
)
var (
Version string
BuildTime string
GitCommit string
GoVersion string
versionTpl = `%s
Name: ddgo
Version: %s
BuildTime: %s
GitCommit: %s
GoVersion: %s
`
bannerBase64 = "DQogX19fXyAgX19fXyAgICBfX18gIF9fX19fIA0KKCAgXyBcKCAgXyBcICAvIF9fKSggIF8gICkNCiApKF8pICkpKF8pICkoIChfLS4gKShfKSggDQooX19fXy8oX19fXy8gIFxfX18vKF9fX19fKQ0K"
)
func init() {
err := log.NewConsole()
if err != nil {
@ -31,8 +52,9 @@ func init() {
}
var opts struct {
Addr string `short:"a" long:"addr" default:"0.0.0.0:10141" env:"ADDR" description:"Addr to listen on for http requests"`
Addr string `short:"a" long:"addr" default:"0.0.0.0:10141" env:"ADDR" description:"Addr to listen on for HTTP server"`
WebHookUrl string `short:"u" long:"webhook-url" env:"URL" description:"Webhook url of dingding" required:"true"`
Version bool `short:"v" long:"version" description:"Show version info"`
}
func dingToInfo(msg string) []byte {
@ -87,18 +109,53 @@ func send(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(info)
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
log.Fatal("parse arg %+v",
err)
}
// printVersion Print out version information
func printVersion() {
banner, _ := base64.StdEncoding.DecodeString(bannerBase64)
fmt.Printf(versionTpl, banner, Version, BuildTime, GitCommit, GoVersion)
}
http.HandleFunc("/", send)
log.Info("server on http://%s", opts.Addr)
if err := http.ListenAndServe(opts.Addr, nil); err != nil {
log.Fatal("ListenAndServe %+v",
err)
func parseArg() {
if _, err := flags.NewParser(&opts, flags.Default).Parse(); err != nil {
if opts.Version {
printVersion()
os.Exit(0)
}
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}
func main() {
parseArg()
mux := http.NewServeMux()
mux.HandleFunc("/", send)
server := &http.Server{
Addr: ":4000",
Handler: mux,
}
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
if err := server.Shutdown(context.Background()); err != nil {
log.Fatal("Shutdown server:", err)
}
}()
log.Info("Starting HTTP server on http://%s", opts.Addr)
if err := server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
log.Info("Server closed under request")
} else {
log.Fatal("Server closed unexpected")
}
}
}

1
version Normal file
View File

@ -0,0 +1 @@
v0.0.1