diff --git a/cmd/version.go b/cmd/version.go index 81067f6..e80f2ca 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -45,7 +45,7 @@ var versionCmd = &cobra.Command{ Long: ` Print version of jwt`, Run: func(cmd *cobra.Command, args []string) { - banner, _ := base64.StdEncoding.DecodeString(bannerBase64) + banner, _ := base64.StdEncoding.DecodeString(metadata.BannerBase64) fmt.Printf(versionTpl, banner, metadata.Version, metadata.BuildTime, metadata.GitCommit, metadata.GoVersion) }, } diff --git a/metadata/metadata.go b/metadata/metadata.go index 50c5b31..d58d830 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -26,7 +26,7 @@ var ( GitCommit string GoVersion string - bannerBase64 = "DQogICBfXyAgICAgX18gICAgIF9fICAgICBfX19fX18gIA0KICAvXCBcICAgL1wgXCAgXyBcIFwgICAvXF9fICBfXCANCiBfXF9cIFwgIFwgXCBcLyAiLlwgXCAgXC9fL1wgXC8gDQovXF9fX19fXCAgXCBcX18vIi5+XF9cICAgIFwgXF9cIA0KXC9fX19fXy8gICBcL18vICAgXC9fLyAgICAgXC9fLyANCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgDQo=" + BannerBase64 = "DQogICAgX18gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgX18gDQogICAvIC9fICBfX18gIF8gIF9fICAgICAgX19fX19fX19fICBfX19fIF8gICBfX19fXyAgX19fX18vIC9fDQogIC8gX18gXC8gXyBcfCB8L18vX19fX18vIF9fXy8gX18gXC8gX18gXCB8IC8gLyBfIFwvIF9fXy8gX18vDQogLyAvIC8gLyAgX18vPiAgPC9fX19fXy8gL19fLyAvXy8gLyAvIC8gLyB8LyAvICBfXy8gLyAgLyAvXyAgDQovXy8gL18vXF9fXy9fL3xffCAgICAgIFxfX18vXF9fX18vXy8gL18vfF9fXy9cX19fL18vICAgXF9fLyAgDQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgDQo=" ) var TEN_TO_ANY map[int]string = map[int]string{0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 16: "g", 17: "h", 18: "i", 19: "j", 20: "k", 21: "l", 22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s", 29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z", 36: ":", 37: ";", 38: "<", 39: "=", 40: ">", 41: "?", 42: "@", 43: "[", 44: "]", 45: "^", 46: "_", 47: "{", 48: "|", 49: "}", 50: "A", 51: "B", 52: "C", 53: "D", 54: "E", 55: "F", 56: "G", 57: "H", 58: "I", 59: "J", 60: "K", 61: "L", 62: "M", 63: "N", 64: "O", 65: "P", 66: "Q", 67: "R", 68: "S", 69: "T", 70: "U", 71: "V", 72: "W", 73: "X", 74: "Y", 75: "Z"} diff --git a/server/server.go b/server/server.go index 2ec45e2..c1bba5e 100644 --- a/server/server.go +++ b/server/server.go @@ -1,48 +1,119 @@ -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/ +// Copyright © 2018 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. -VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/cmd -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_GATEWAY_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)' -LD_FLAGS := "$(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_GATEWAY_VERSION) -w -s" +package server -.PHONY : build release clean install upx +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strconv" + "strings" -build: -ifneq ($(shell type gox >/dev/null 2>&1;echo $$?), 0) - @echo "Can't find gox command, will start installation..." - GO111MODULE=off go get -v -u github.com/mitchellh/gox -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}}" + "github.com/ehlxr/hex-convert/gen" -clean: - rm -rf $(DIST_DIR)* + "github.com/ehlxr/hex-convert/converter" +) -install: - go install -ldflags $(LD_FLAGS) +func decimal(w http.ResponseWriter, req *http.Request) { + scale, _ := strconv.Atoi(req.FormValue("scale")) + data := req.FormValue("data") -# 压缩。需要安装 https://github.com/upx/upx -upx: - upx $(DIST_DIR)** + result, err := converter.ToDecimal(scale, data) + if err != nil { + fmt.Println(err) + os.Exit(1) + } -release: build upx -ifneq ($(shell type ghr >/dev/null 2>&1;echo $$?), 0) - @echo "Can't find ghr command, will start installation..." - GO111MODULE=off go get -v -u github.com/tcnksm/ghr -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) + log.Println(result) + _, _ = fmt.Fprintf(w, "首页
%s", strconv.Itoa(result)) +} -# this tells 'make' to export all variables to child processes by default. -.EXPORT_ALL_VARIABLES: +func binary(w http.ResponseWriter, req *http.Request) { + scale, _ := strconv.Atoi(req.FormValue("scale")) + data := req.FormValue("data") -GO111MODULE = on -GOPROXY = https://goproxy.cn,direct -GOSUMDB = sum.golang.google.cn + result, err := converter.ToBinary(scale, data) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + log.Println(result) + _, _ = fmt.Fprintf(w, "首页
%s", result) +} + +func submit(w http.ResponseWriter, req *http.Request) { + scale, _ := strconv.Atoi(req.FormValue("scale")) + data := req.FormValue("data") + + var bd, od, dd, hd string + if scale != 2 && scale != 8 && scale != 10 && scale != 16 { + bd, od, dd, hd = "", "", "", "" + } else { + bd, _ = converter.ToBinary(scale, data) + d, err := converter.ToDecimal(scale, data) + if err == nil { + dd = strconv.Itoa(d) + } + + od, _ = converter.ToOctal(scale, data) + hd, _ = converter.ToHex(scale, data) + } + + log.Printf("%s %s %s %s", bd, od, dd, hd) + _, _ = fmt.Fprintf(w, "首页
二进制:%s 八进制:%s 十进制:%s 十六进制:%s", bd, od, dd, hd) +} + +func index(w http.ResponseWriter, r *http.Request) { + f, err := gen.Assets.Open("/index.tpl") + if err != nil { + log.Fatalln(err) + } + defer f.Close() + + fd, err := ioutil.ReadAll(f) + if err != nil { + panic(err) + } + + _, _ = fmt.Fprintf(w, string(fd)) +} + +func Start(host string, port int) error { + http.HandleFunc("/", index) + http.HandleFunc("/d", decimal) + http.HandleFunc("/b", binary) + http.HandleFunc("/s", submit) + + addr := fmt.Sprintf("%s:%d", host, port) + if strings.Contains(addr, "0.0.0.0") { + addr = strings.Replace(addr, "0.0.0.0", "", 1) + host = strings.Replace(host, "0.0.0.0", "127.0.0.1", 1) + } + fmt.Printf("server start on: %s\n", fmt.Sprintf("http://%s:%d", host, port)) + + if err := http.ListenAndServe(addr, nil); err != nil { + return fmt.Errorf("ListenAndServe: %v", err) + } + return nil +} diff --git a/vesion b/version similarity index 100% rename from vesion rename to version