支持断点续传

master v0.1.24
兔子 5 years ago
parent 874012fe56
commit 4b69d2b8bf

@ -24,14 +24,14 @@ var attachcmd = &cobra.Command{
out, _ = this.Flags().GetString("out") out, _ = this.Flags().GetString("out")
} }
if src == "" || dst == "" { if src == "" || dst == "" {
starlog.Println("ERROR PATH", "red", "b") starlog.StdPrintln(0, starlog.RED, "ERROR PATH")
this.Help() this.Help()
return return
} }
cryp := new(starainrt.StarCrypto) cryp := new(starainrt.StarCrypto)
err := cryp.Attach(src, dst, out) err := cryp.Attach(src, dst, out)
if err != nil { if err != nil {
starlog.Println(err.Error, "red", "b") starlog.StdPrintln(0, starlog.RED, err.Error)
} else { } else {
fmt.Println("完成") fmt.Println("完成")
} }

@ -17,7 +17,7 @@ var b64cmd = &cobra.Command{
ok, _ := this.Flags().GetBool("file") ok, _ := this.Flags().GetBool("file")
de, _ := this.Flags().GetBool("decode") de, _ := this.Flags().GetBool("decode")
if len(args) != 1 { if len(args) != 1 {
starlog.Println("参数不足,请输入文件地址或字符串", "red", "b") starlog.StdPrintln(0, starlog.RED, "参数不足,请输入文件地址或字符串")
this.Help() this.Help()
return return
} }
@ -47,7 +47,7 @@ var b64cmd = &cobra.Command{
} }
} }
if err != nil { if err != nil {
starlog.Println(err, "red", "b") starlog.StdPrintln(0, starlog.RED, err)
return return
} }
}, },

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version string = "0.1.20" var Version string = "0.1.24"
var Maincmd = &cobra.Command{ var Maincmd = &cobra.Command{
Use: "", Use: "",
@ -14,5 +14,6 @@ var Maincmd = &cobra.Command{
func init() { func init() {
cobra.MousetrapHelpText = "" cobra.MousetrapHelpText = ""
Maincmd.Flags().BoolP("version", "v", false, "查看版本号")
Maincmd.Version = Version Maincmd.Version = Version
} }

@ -73,7 +73,7 @@ var curlcmd = &cobra.Command{
if h != "" { if h != "" {
head := strings.Split(h, ",") head := strings.Split(h, ",")
for _, v := range head { for _, v := range head {
hp := strings.Split(v, "=") hp := strings.Split(v, ":")
if len(hp) != 2 { if len(hp) != 2 {
continue continue
} }

@ -25,14 +25,14 @@ var detachcmd = &cobra.Command{
} }
num, _ := this.Flags().GetInt("num") num, _ := this.Flags().GetInt("num")
if src == "" || dst == "" { if src == "" || dst == "" {
starlog.Println("ERROR PATH", "red", "b") starlog.StdPrintln(0, starlog.RED, "ERROR PATH")
this.Help() this.Help()
return return
} }
cryp := new(starainrt.StarCrypto) cryp := new(starainrt.StarCrypto)
err := cryp.Detach(src, num, dst, out) err := cryp.Detach(src, num, dst, out)
if err != nil { if err != nil {
starlog.Println(err.Error, "red", "b") starlog.StdPrintln(0, starlog.RED, err.Error)
} else { } else {
fmt.Println("完成") fmt.Println("完成")
} }

@ -49,7 +49,7 @@ var hashcmd = &cobra.Command{
result, err = crypto.SumAll([]byte(args[0]), method) result, err = crypto.SumAll([]byte(args[0]), method)
} }
if err != nil { if err != nil {
starlog.Println("错误:"+err.Error(), "red", "") starlog.StdPrintln(0, starlog.RED, "错误:"+err.Error())
} }
for _, v := range method { for _, v := range method {
fmt.Printf("%s%s\n", v, result[v]) fmt.Printf("%s%s\n", v, result[v])

@ -39,7 +39,7 @@ var httpcmd = &cobra.Command{
} }
err := http.ListenAndServe(ip+":"+port, nil) err := http.ListenAndServe(ip+":"+port, nil)
if err != 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) { func httplisten(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Vicorique") w.Header().Set("Server", "Vicorique")
w.Header().Set("Powered", "B612.ME")
p := r.URL.Path p := r.URL.Path
cmd := r.URL.Query()["cmd"] cmd := r.URL.Query()["cmd"]
fmt.Println("Get " + p + " " + r.RemoteAddr) fmt.Println("Get " + p + " " + r.RemoteAddr)
@ -84,7 +85,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
if up { if up {
if p == "/vtqeupload1127/web" { if p == "/vtqeupload1127/web" {
w.Write([]byte(`<html><body><form id= "uploadForm" action= "../vtqeupload1127" method= "post" enctype ="multipart/form-data"> w.Write([]byte(`<html><body><form id= "uploadForm" action= "../vtqeupload1127" method= "post" enctype ="multipart/form-data">
<h1 >Victorique's File Upload Page </h1> <h1 >B612 File Upload Page </h1>
<p > <input type ="file" name="victorique" /></p> <p > <input type ="file" name="victorique" /></p>
<input type ="submit" value="上传"/> <input type ="submit" value="上传"/>
</form> </form>
@ -137,11 +138,60 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/download") w.Header().Set("Content-Type", "application/download")
w.Header().Set("Content-Disposition", "attachment;filename="+name) 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("Content-Transfer-Encoding", "binary")
w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Length", strconv.Itoa(int(fpinfo.Size()))) w.Header().Set("ETag", etag)
w.WriteHeader(200) 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
}
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() 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 { for {
buf := make([]byte, 1048576) buf := make([]byte, 1048576)
n, err := fpdst.Read(buf) n, err := fpdst.Read(buf)
@ -151,7 +201,22 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
} }
return return
} }
if rangeEnd == -1 {
w.Write(buf[0:n]) 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)
}
}
}
fmt.Println(fpinfo.Name(), "客户端下载已结束")
} }
} }
@ -162,7 +227,7 @@ func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot
w.WriteHeader(403) w.WriteHeader(403)
w.Write([]byte("<h1>May Cannot Access!</h1>")) w.Write([]byte("<h1>May Cannot Access!</h1>"))
} }
w.Write([]byte("<html>\n<style>\np{margin: 2px auto}\n</style>\n<h1>Victorique Http Server - " + Version + "</h1>")) w.Write([]byte("<html>\n<style>\np{margin: 2px auto}\n</style>\n<h1>B612 Http Server - " + Version + "</h1>"))
if up { if up {
w.Write([]byte("<a href=/vtqeupload1127/web>Upload Web Page Is Openned!</a><br /><br />")) w.Write([]byte("<a href=/vtqeupload1127/web>Upload Web Page Is Openned!</a><br /><br />"))
} }

@ -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
}

@ -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("任务完成!")
},
}

@ -35,7 +35,7 @@ var mergecmd = &cobra.Command{
} }
}) })
if err != nil { if err != nil {
starlog.Println(err.Error, "red", "") starlog.StdPrintln(0, starlog.RED, err.Error)
} }
}, },

@ -683,6 +683,7 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
fmt.Println("穿透客户端已建立新连接") fmt.Println("穿透客户端已建立新连接")
if len(waitconn) != 0 { if len(waitconn) != 0 {
conn.Write(waitconn[0].Msg) conn.Write(waitconn[0].Msg)
waitconn[0].Msg = []byte{}
go TcpCopy(waitconn[0].Conn, conn) go TcpCopy(waitconn[0].Conn, conn)
go TcpCopy(conn, waitconn[0].Conn) go TcpCopy(conn, waitconn[0].Conn)
if len(waitconn) > 1 { if len(waitconn) > 1 {

@ -27,12 +27,12 @@ var splitcmd = &cobra.Command{
num, _ = this.Flags().GetInt("num") num, _ = this.Flags().GetInt("num")
} }
if !starainrt.Exists(src) { if !starainrt.Exists(src) {
starlog.Println("源文件不存在", "red", "b") starlog.StdPrintln(0, starlog.RED, "源文件不存在")
this.Help() this.Help()
return return
} }
if num == 0 { if num == 0 {
starlog.Println("参数num不合法", "red", "b") starlog.StdPrintln(0, starlog.RED, "参数num不合法", "red")
this.Help() this.Help()
return return
} }
@ -46,7 +46,7 @@ var splitcmd = &cobra.Command{
} }
}) })
if err != nil { if err != nil {
starlog.Println(err.Error, "red", "") starlog.StdPrintln(0, starlog.RED, err.Error)
} }
}, },

@ -22,7 +22,7 @@ var viccmd = &cobra.Command{
rep, _ := this.Flags().GetBool("replace") rep, _ := this.Flags().GetBool("replace")
ext, _ := this.Flags().GetBool("extension") ext, _ := this.Flags().GetBool("extension")
if len(args) != 2 || args[1] != "sakura" { if len(args) != 2 || args[1] != "sakura" {
starlog.Println("ヴィクトリカだけが使えるよ", "red", "b") starlog.StdPrintln(0, starlog.RED, "ヴィクトリカだけが使えるよ")
return return
} }
shell := func(pect float64) { shell := func(pect float64) {
@ -77,7 +77,7 @@ var viccmd = &cobra.Command{
} }
} }
if err != nil { if err != nil {
starlog.Println(err, "red", "b") starlog.StdPrintln(0, starlog.RED, err)
return return
} }
}, },

Loading…
Cancel
Save