diff --git a/vtqe/tools/attach.go b/vtqe/tools/attach.go index 4a23174..16cd7f4 100644 --- a/vtqe/tools/attach.go +++ b/vtqe/tools/attach.go @@ -24,14 +24,14 @@ var attachcmd = &cobra.Command{ out, _ = this.Flags().GetString("out") } if src == "" || dst == "" { - starlog.Println("ERROR PATH", "red", "b") + starlog.StdPrintln(0, starlog.RED, "ERROR PATH") this.Help() return } cryp := new(starainrt.StarCrypto) err := cryp.Attach(src, dst, out) if err != nil { - starlog.Println(err.Error, "red", "b") + starlog.StdPrintln(0, starlog.RED, err.Error) } else { fmt.Println("完成") } diff --git a/vtqe/tools/base64.go b/vtqe/tools/base64.go index c9f54c7..9836ff5 100644 --- a/vtqe/tools/base64.go +++ b/vtqe/tools/base64.go @@ -17,7 +17,7 @@ var b64cmd = &cobra.Command{ ok, _ := this.Flags().GetBool("file") de, _ := this.Flags().GetBool("decode") if len(args) != 1 { - starlog.Println("参数不足,请输入文件地址或字符串", "red", "b") + starlog.StdPrintln(0, starlog.RED, "参数不足,请输入文件地址或字符串") this.Help() return } @@ -47,7 +47,7 @@ var b64cmd = &cobra.Command{ } } if err != nil { - starlog.Println(err, "red", "b") + starlog.StdPrintln(0, starlog.RED, err) return } }, diff --git a/vtqe/tools/cmd.go b/vtqe/tools/cmd.go index ea90a04..2df9bee 100644 --- a/vtqe/tools/cmd.go +++ b/vtqe/tools/cmd.go @@ -4,7 +4,7 @@ import ( "github.com/spf13/cobra" ) -var Version string = "0.1.20" +var Version string = "0.1.24" var Maincmd = &cobra.Command{ Use: "", @@ -14,5 +14,6 @@ var Maincmd = &cobra.Command{ func init() { cobra.MousetrapHelpText = "" + Maincmd.Flags().BoolP("version", "v", false, "查看版本号") Maincmd.Version = Version } diff --git a/vtqe/tools/curl.go b/vtqe/tools/curl.go index 2551c7c..715df07 100644 --- a/vtqe/tools/curl.go +++ b/vtqe/tools/curl.go @@ -73,7 +73,7 @@ var curlcmd = &cobra.Command{ if h != "" { head := strings.Split(h, ",") for _, v := range head { - hp := strings.Split(v, "=") + hp := strings.Split(v, ":") if len(hp) != 2 { continue } diff --git a/vtqe/tools/detach.go b/vtqe/tools/detach.go index 26f0aa2..fa9c808 100644 --- a/vtqe/tools/detach.go +++ b/vtqe/tools/detach.go @@ -25,14 +25,14 @@ var detachcmd = &cobra.Command{ } num, _ := this.Flags().GetInt("num") if src == "" || dst == "" { - starlog.Println("ERROR PATH", "red", "b") + starlog.StdPrintln(0, starlog.RED, "ERROR PATH") this.Help() return } cryp := new(starainrt.StarCrypto) err := cryp.Detach(src, num, dst, out) if err != nil { - starlog.Println(err.Error, "red", "b") + starlog.StdPrintln(0, starlog.RED, err.Error) } else { fmt.Println("完成") } diff --git a/vtqe/tools/hash.go b/vtqe/tools/hash.go index 117bde9..2350f5a 100644 --- a/vtqe/tools/hash.go +++ b/vtqe/tools/hash.go @@ -49,7 +49,7 @@ var hashcmd = &cobra.Command{ result, err = crypto.SumAll([]byte(args[0]), method) } if err != nil { - starlog.Println("错误:"+err.Error(), "red", "") + starlog.StdPrintln(0, starlog.RED, "错误:"+err.Error()) } for _, v := range method { fmt.Printf("%s:%s\n", v, result[v]) diff --git a/vtqe/tools/http.go b/vtqe/tools/http.go index 4479b98..75efbb4 100644 --- a/vtqe/tools/http.go +++ b/vtqe/tools/http.go @@ -39,7 +39,7 @@ var httpcmd = &cobra.Command{ } err := http.ListenAndServe(ip+":"+port, nil) if err != nil { - starlog.Println("Error:"+err.Error(), "red", "") + starlog.StdPrintln(0, starlog.RED, "Error:"+err.Error()) } }, } @@ -72,6 +72,7 @@ func uploadfile(w http.ResponseWriter, r *http.Request) { func httplisten(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", "Vicorique") + w.Header().Set("Powered", "B612.ME") p := r.URL.Path cmd := r.URL.Query()["cmd"] fmt.Println("Get " + p + " " + r.RemoteAddr) @@ -84,7 +85,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) { if up { if p == "/vtqeupload1127/web" { w.Write([]byte(`
-

Victorique's File Upload Page

+

B612 File Upload Page

上传文件:

@@ -137,21 +138,85 @@ func httplisten(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/download") w.Header().Set("Content-Disposition", "attachment;filename="+name) } + etag := new(starainrt.StarCrypto).MD5([]byte(fpinfo.ModTime().String())) w.Header().Set("Content-Transfer-Encoding", "binary") w.Header().Set("Accept-Ranges", "bytes") - w.Header().Set("Content-Length", strconv.Itoa(int(fpinfo.Size()))) - w.WriteHeader(200) - defer fpdst.Close() - for { - buf := make([]byte, 1048576) - n, err := fpdst.Read(buf) - if err != nil { - if err == io.EOF { + w.Header().Set("ETag", etag) + w.Header().Set("Last-Modified", fpinfo.ModTime().Format("Mon,2 Jan 2006 15:04:05 MST")) + isRange := false + var rangeStart, rangeEnd int64 + rangeEnd = -1 + for k, v := range r.Header { + if strings.ToLower(k) == "range" { + if strings.Index(v[0], "bytes=") < 0 { break } - return + v[0] = strings.Replace(v[0], "bytes=", "", -1) + data := strings.Split(v[0], "-") + if len(data) == 0 { + break + } + rangeStart, _ = strconv.ParseInt(data[0], 10, 64) + if len(data) == 2 { + rangeEnd, _ = strconv.ParseInt(data[1], 10, 64) + } + //w.WriteHeader(206) //206 支持断点续传 + isRange = true + break + } + } + defer fpdst.Close() + if !isRange { + w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10)) + w.WriteHeader(200) + for { + buf := make([]byte, 1048576) + n, err := fpdst.Read(buf) + if err != nil { + if err == io.EOF { + break + } + return + } + w.Write(buf[0:n]) + } + } else { + w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10)) + if rangeEnd == -1 { + w.Header().Set("Content-Range", `bytes `+strconv.FormatInt(rangeStart, 10)+"-"+strconv.FormatInt(fpinfo.Size(), 10)+"/"+strconv.FormatInt(fpinfo.Size(), 10)) + //w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size()-rangeStart, 10)) + } else { + w.Header().Set("Content-Range", `bytes `+strconv.FormatInt(rangeStart, 10)+"-"+strconv.FormatInt(rangeEnd, 10)+"/"+strconv.FormatInt(fpinfo.Size(), 10)) + //w.Header().Set("Content-Length", strconv.FormatInt(1+rangeEnd-rangeStart, 10)) + } + w.WriteHeader(206) + fpdst.Seek(int64(rangeStart), 0) + count := rangeStart + for { + buf := make([]byte, 1048576) + n, err := fpdst.Read(buf) + if err != nil { + if err == io.EOF { + break + } + return + } + if rangeEnd == -1 { + w.Write(buf[0:n]) + } else { + if count > rangeEnd { + break + } + if count+int64(n) > rangeEnd { + w.Write(buf[0 : rangeEnd-count+1]) + break + } else { + w.Write(buf[0:n]) + count += int64(n) + } + } } - w.Write(buf[0:n]) + fmt.Println(fpinfo.Name(), "客户端下载已结束") } } @@ -162,7 +227,7 @@ func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot w.WriteHeader(403) w.Write([]byte("

May Cannot Access!

")) } - w.Write([]byte("\n\n

Victorique Http Server - " + Version + "

")) + w.Write([]byte("\n\n

B612 Http Server - " + Version + "

")) if up { w.Write([]byte("Upload Web Page Is Openned!

")) } diff --git a/vtqe/tools/image-basic.go b/vtqe/tools/image-basic.go new file mode 100644 index 0000000..93764fa --- /dev/null +++ b/vtqe/tools/image-basic.go @@ -0,0 +1,194 @@ +package tools + +import ( + "errors" + "image" + "io/ioutil" + "os" + + "github.com/golang/freetype" + + "github.com/nfnt/resize" + + "image/color" + "image/draw" + _ "image/gif" + _ "image/jpeg" + "image/png" + _ "image/png" + + "b612.me/starainrt" +) + +func OpenImage(name string) (image.Image, error) { + if !starainrt.Exists(name) { + return nil, errors.New("File Not Exists") + } + fso, err := os.Open(name) + if err != nil { + return nil, err + } + img, _, err := image.Decode(fso) + if err != nil { + return nil, err + } + return img, nil +} + +func MergePhoto(big, small image.Image, bigsize, smallsize uint, x, y int) image.Image { + big = resize.Resize(bigsize, bigsize, big, resize.Lanczos3) + small = resize.Resize(smallsize, smallsize, small, resize.Lanczos3) + offset := image.Pt(x, y) + b := big.Bounds() + nimg := image.NewRGBA(b) + draw.Draw(nimg, b, big, image.ZP, draw.Src) + draw.Draw(nimg, small.Bounds(), small, offset, draw.Over) + return nimg +} + +func SavePhoto(path string, img image.Image) error { + imgf, err := os.Create(path) + if err != nil { + return err + } + defer imgf.Close() + return png.Encode(imgf, img) +} + +func SetAlpha(img image.Image, alpha uint8) image.Image { + size := img.Bounds() + nimg := image.NewRGBA(size) + for x := 0; x < size.Dx(); x++ { + for y := 0; y < size.Dy(); y++ { + r, g, b, a := img.At(x, y).RGBA() + r = r >> 8 + g = g >> 8 + b = b >> 8 + a = a >> 8 + nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), alpha}) + //nimg.Set(x, y, color.Alpha{alpha}) + //nimg.Set(x, y, img.At(x, y)) + } + } + return nimg +} + +func AddText(text string, img image.Image, x, y int, dpi, size float64, colors color.RGBA, ttf string) (image.Image, error) { + if !starainrt.Exists(ttf) { + return nil, errors.New("File Not Exists") + } + fontbyte, err := ioutil.ReadFile(ttf) + if err != nil { + return nil, err + } + font, err := freetype.ParseFont(fontbyte) + if err != nil { + return nil, err + } + nimg, ok := img.(draw.Image) + if !ok { + size := img.Bounds() + nimg = image.NewRGBA(img.Bounds()) + for x := 0; x < size.Dx(); x++ { + for y := 0; y < size.Dy(); y++ { + r, g, b, a := img.At(x, y).RGBA() + nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) + } + } + } + f := freetype.NewContext() + f.SetDPI(dpi) + f.SetFontSize(size) + f.SetFont(font) + f.SetClip(nimg.Bounds()) + f.SetDst(nimg) + f.SetSrc(image.NewUniform(colors)) + _, err = f.DrawString(text, freetype.Pt(x, y)) + if err != nil { + return nil, err + } + return nimg, nil +} + +type TextImg struct { + Text string + X int + Y int + Dpi float64 + Size float64 + Color color.NRGBA +} + +type TextList struct { + List []TextImg + TTF []byte +} + +func AddListTests(list TextList, img image.Image) (image.Image, error) { + font, err := freetype.ParseFont(list.TTF) + if err != nil { + return nil, err + } + nimg, ok := img.(draw.Image) + if !ok { + size := img.Bounds() + nimg = image.NewRGBA(img.Bounds()) + for x := 0; x < size.Dx(); x++ { + for y := 0; y < size.Dy(); y++ { + r, g, b, a := img.At(x, y).RGBA() + nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) + } + } + } + for _, v := range list.List { + f := freetype.NewContext() + f.SetDPI(v.Dpi) + f.SetFontSize(v.Size) + f.SetFont(font) + f.SetClip(nimg.Bounds()) + f.SetDst(nimg) + f.SetSrc(image.NewUniform(v.Color)) + _, err = f.DrawString(v.Text, freetype.Pt(v.X, v.Y)) + if err != nil { + return nil, err + } + } + return nimg, nil +} + +func AddTexts(text string, img image.Image, x, y int, dpi, size float64, colors color.NRGBA, ttf string) (image.Image, error) { + if !starainrt.Exists(ttf) { + return nil, errors.New("File Not Exists") + } + fontbyte, err := ioutil.ReadFile(ttf) + if err != nil { + return nil, err + } + font, err := freetype.ParseFont(fontbyte) + if err != nil { + return nil, err + } + nimg, ok := img.(draw.Image) + if !ok { + size := img.Bounds() + nimg = image.NewRGBA(img.Bounds()) + for x := 0; x < size.Dx(); x++ { + for y := 0; y < size.Dy(); y++ { + r, g, b, a := img.At(x, y).RGBA() + nimg.Set(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)}) + } + } + } + f := freetype.NewContext() + f.SetDPI(dpi) + f.SetFontSize(size) + f.SetFont(font) + f.SetClip(nimg.Bounds()) + f.SetDst(nimg) + f.SetSrc(image.NewUniform(colors)) + _, err = f.DrawString(text, freetype.Pt(x, y)) + if err != nil { + return nil, err + } + return nimg, nil +} diff --git a/vtqe/tools/image.go b/vtqe/tools/image.go new file mode 100644 index 0000000..de46e8f --- /dev/null +++ b/vtqe/tools/image.go @@ -0,0 +1,83 @@ +package tools + +import ( + "fmt" + "image" + + "b612.me/starlog" + "github.com/spf13/cobra" +) + +func init() { + imageCmd.AddCommand(imgMirrorCmd) + Maincmd.AddCommand(imageCmd) +} + +var imageCmd = &cobra.Command{ + Use: "image", + Short: "图像处理", + Long: "简单的图像处理工具", + Run: func(this *cobra.Command, args []string) { + this.Help() + }, +} + +var imgMirrorCmd = &cobra.Command{ + Use: "mirror", + Short: "图像镜像翻转", + Long: "图像镜像翻转<水平>", + Run: func(this *cobra.Command, args []string) { + if len(args) == 0 { + starlog.StdPrintln(0, starlog.RED, "请指定需要转换的图像!") + return + } + for _, v := range args { + img, err := OpenImage(v) + if err != nil { + starlog.StdPrintln(0, starlog.RED, err, v) + continue + } + size := img.Bounds() + nimg := image.NewRGBA(size) + for x := 0; x < size.Dx(); x++ { + for y := 0; y < size.Dy(); y++ { + nimg.Set(size.Dx()-x, y, img.At(x, y)) + } + } + if err := SavePhoto(v, nimg); err != nil { + starlog.StdPrintln(0, starlog.RED, err, v) + continue + } else { + fmt.Println(v, "转换已完成!") + } + } + fmt.Println("任务完成!") + }, +} + +var imgAlpha = &cobra.Command{ + Use: "alpha", + Short: "设置透明度", + Long: "设置alpha通道透明度", + Run: func(this *cobra.Command, args []string) { + if len(args) == 0 { + starlog.StdPrintln(0, starlog.RED, "请指定需要转换的图像!") + return + } + for _, v := range args { + img, err := OpenImage(v) + if err != nil { + starlog.StdPrintln(0, starlog.RED, err, v) + continue + } + img = SetAlpha(img, 4) + if err := SavePhoto(v, img); err != nil { + starlog.StdPrintln(0, starlog.RED, err, v) + continue + } else { + fmt.Println(v, "转换已完成!") + } + } + fmt.Println("任务完成!") + }, +} diff --git a/vtqe/tools/merge.go b/vtqe/tools/merge.go index d783c28..e0d9207 100644 --- a/vtqe/tools/merge.go +++ b/vtqe/tools/merge.go @@ -35,7 +35,7 @@ var mergecmd = &cobra.Command{ } }) if err != nil { - starlog.Println(err.Error, "red", "") + starlog.StdPrintln(0, starlog.RED, err.Error) } }, diff --git a/vtqe/tools/net.go b/vtqe/tools/net.go index 123a216..07ba985 100644 --- a/vtqe/tools/net.go +++ b/vtqe/tools/net.go @@ -683,6 +683,7 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) { fmt.Println("穿透客户端已建立新连接") if len(waitconn) != 0 { conn.Write(waitconn[0].Msg) + waitconn[0].Msg = []byte{} go TcpCopy(waitconn[0].Conn, conn) go TcpCopy(conn, waitconn[0].Conn) if len(waitconn) > 1 { diff --git a/vtqe/tools/split.go b/vtqe/tools/split.go index 631b227..979e6d8 100644 --- a/vtqe/tools/split.go +++ b/vtqe/tools/split.go @@ -27,12 +27,12 @@ var splitcmd = &cobra.Command{ num, _ = this.Flags().GetInt("num") } if !starainrt.Exists(src) { - starlog.Println("源文件不存在", "red", "b") + starlog.StdPrintln(0, starlog.RED, "源文件不存在") this.Help() return } if num == 0 { - starlog.Println("参数num不合法", "red", "b") + starlog.StdPrintln(0, starlog.RED, "参数num不合法", "red") this.Help() return } @@ -46,7 +46,7 @@ var splitcmd = &cobra.Command{ } }) if err != nil { - starlog.Println(err.Error, "red", "") + starlog.StdPrintln(0, starlog.RED, err.Error) } }, diff --git a/vtqe/tools/vic.go b/vtqe/tools/vic.go index 9721be9..a3f3d0a 100644 --- a/vtqe/tools/vic.go +++ b/vtqe/tools/vic.go @@ -22,7 +22,7 @@ var viccmd = &cobra.Command{ rep, _ := this.Flags().GetBool("replace") ext, _ := this.Flags().GetBool("extension") if len(args) != 2 || args[1] != "sakura" { - starlog.Println("ヴィクトリカだけが使えるよ", "red", "b") + starlog.StdPrintln(0, starlog.RED, "ヴィクトリカだけが使えるよ") return } shell := func(pect float64) { @@ -77,7 +77,7 @@ var viccmd = &cobra.Command{ } } if err != nil { - starlog.Println(err, "red", "b") + starlog.StdPrintln(0, starlog.RED, err) return } },