new gen
parent
f8bcc2c171
commit
f89188761d
@ -1,9 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Victorique/vtqe/tools"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tools.Maincmd.Execute()
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Vtqe is my own toolbox
|
||||
|
||||
var cmdMain = &cobra.Command{
|
||||
Short: "Victorique Wisdom ToolBox",
|
||||
Long: "中二的工具箱:未来を照らし出せ!",
|
||||
Version: "v1.0.0 NG",
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmdMain.Execute()
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"b612.me/starainrt"
|
||||
"b612.me/starlog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var attachcmd = &cobra.Command{
|
||||
Use: "attach",
|
||||
Short: "合并两个文件",
|
||||
Long: "合并两个文件",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
var src, dst, out string
|
||||
if len(args) == 3 {
|
||||
src = args[0]
|
||||
dst = args[1]
|
||||
out = args[2]
|
||||
} else {
|
||||
src, _ = this.Flags().GetString("src")
|
||||
dst, _ = this.Flags().GetString("dst")
|
||||
out, _ = this.Flags().GetString("out")
|
||||
}
|
||||
if src == "" || dst == "" {
|
||||
starlog.Criticalln("ERROR PATH")
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
cryp := new(starainrt.StarCrypto)
|
||||
err := cryp.Attach(src, dst, out)
|
||||
if err != nil {
|
||||
starlog.Criticalln(err.Error)
|
||||
} else {
|
||||
fmt.Println("完成")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
attachcmd.Flags().StringP("src", "s", "", "源文件路径")
|
||||
attachcmd.Flags().StringP("dst", "d", "", "目标文件路径")
|
||||
attachcmd.Flags().StringP("out", "o", "", "输出文件路径")
|
||||
Maincmd.AddCommand(attachcmd)
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"b612.me/starainrt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cdcmd = &cobra.Command{
|
||||
Use: "cd",
|
||||
Short: "便捷进入文件夹",
|
||||
Long: "使用stdin便捷进入文件夹",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
fileInfo, _ := os.Stdin.Stat()
|
||||
if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe {
|
||||
fmt.Println(args)
|
||||
if len(args) != 0 {
|
||||
os.Exit(1)
|
||||
} else {
|
||||
exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run()
|
||||
return
|
||||
}
|
||||
}
|
||||
s := bufio.NewScanner(os.Stdin)
|
||||
for s.Scan() {
|
||||
dir := s.Text()
|
||||
if starainrt.IsFile(dir) {
|
||||
exec.Command("cmd.exe", "/c", "explorer /n,/select,"+dir).Run()
|
||||
} else {
|
||||
exec.Command("cmd.exe", "/c", "explorer "+dir).Run()
|
||||
}
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(cdcmd)
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version string = "0.1.25"
|
||||
|
||||
var Maincmd = &cobra.Command{
|
||||
Use: "",
|
||||
Short: "Victorique's Small Smart Toolkit",
|
||||
Long: "Victorique's Small Smart Toolkit",
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.MousetrapHelpText = ""
|
||||
Maincmd.Flags().BoolP("version", "v", false, "查看版本号")
|
||||
Maincmd.Version = Version
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"b612.me/starainrt"
|
||||
"b612.me/starlog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var detachcmd = &cobra.Command{
|
||||
Use: "detach",
|
||||
Short: "分离两个文件",
|
||||
Long: "分离两个文件",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
var src, dst, out string
|
||||
if len(args) == 3 {
|
||||
src = args[0]
|
||||
dst = args[1]
|
||||
out = args[2]
|
||||
} else {
|
||||
src, _ = this.Flags().GetString("src")
|
||||
dst, _ = this.Flags().GetString("dst")
|
||||
out, _ = this.Flags().GetString("out")
|
||||
}
|
||||
num, _ := this.Flags().GetInt("num")
|
||||
if src == "" || dst == "" {
|
||||
starlog.Criticalln("ERROR PATH")
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
cryp := new(starainrt.StarCrypto)
|
||||
err := cryp.Detach(src, num, dst, out)
|
||||
if err != nil {
|
||||
starlog.Criticalln(err.Error)
|
||||
} else {
|
||||
fmt.Println("完成")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
detachcmd.Flags().StringP("src", "s", "", "源文件路径")
|
||||
detachcmd.Flags().StringP("dst", "d", "", "目标文件路径1")
|
||||
detachcmd.Flags().StringP("out", "o", "", "目标文件路径2")
|
||||
detachcmd.Flags().IntP("num", "n", 0, "分割开始字节")
|
||||
Maincmd.AddCommand(detachcmd)
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
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
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
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.Errorln("请指定需要转换的图像!")
|
||||
return
|
||||
}
|
||||
for _, v := range args {
|
||||
img, err := OpenImage(v)
|
||||
if err != nil {
|
||||
starlog.Errorln(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.Errorln(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.Errorln("请指定需要转换的图像!")
|
||||
return
|
||||
}
|
||||
for _, v := range args {
|
||||
img, err := OpenImage(v)
|
||||
if err != nil {
|
||||
starlog.Errorln(err, v)
|
||||
continue
|
||||
}
|
||||
img = SetAlpha(img, 4)
|
||||
if err := SavePhoto(v, img); err != nil {
|
||||
starlog.Errorln(err, v)
|
||||
continue
|
||||
} else {
|
||||
fmt.Println(v, "转换已完成!")
|
||||
}
|
||||
}
|
||||
fmt.Println("任务完成!")
|
||||
},
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package ping
|
||||
|
||||
import "net"
|
||||
|
||||
// GetIP ...
|
||||
func GetIP(hostname string) string {
|
||||
addrs, err := net.LookupIP(hostname)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
if ipv4 := addr.To4(); ipv4 != nil {
|
||||
return ipv4.String()
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTPing ...
|
||||
type HTTPing struct {
|
||||
target *Target
|
||||
done chan struct{}
|
||||
result *Result
|
||||
Method string
|
||||
}
|
||||
|
||||
var _ Pinger = (*HTTPing)(nil)
|
||||
|
||||
// NewHTTPing return new HTTPing
|
||||
func NewHTTPing(method string) *HTTPing {
|
||||
return &HTTPing{
|
||||
done: make(chan struct{}),
|
||||
Method: method,
|
||||
}
|
||||
}
|
||||
|
||||
// SetTarget ...
|
||||
func (ping *HTTPing) SetTarget(target *Target) {
|
||||
ping.target = target
|
||||
if ping.result == nil {
|
||||
ping.result = &Result{Target: target}
|
||||
}
|
||||
}
|
||||
|
||||
// Start ping
|
||||
func (ping *HTTPing) Start() <-chan struct{} {
|
||||
go func() {
|
||||
t := time.NewTicker(ping.target.Interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
if ping.result.Counter >= ping.target.Counter && ping.target.Counter != 0 {
|
||||
ping.Stop()
|
||||
return
|
||||
}
|
||||
duration, resp, remoteAddr, err := ping.ping()
|
||||
ping.result.Counter++
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Ping %s - failed: %s\n", ping.target, err)
|
||||
} else {
|
||||
defer resp.Body.Close()
|
||||
length, _ := io.Copy(ioutil.Discard, resp.Body)
|
||||
fmt.Printf("Ping %s(%s) - %s is open - time=%s method=%s status=%d bytes=%d\n", ping.target, remoteAddr, ping.target.Protocol, duration, ping.Method, resp.StatusCode, length)
|
||||
if ping.result.MinDuration == 0 {
|
||||
ping.result.MinDuration = duration
|
||||
}
|
||||
if ping.result.MaxDuration == 0 {
|
||||
ping.result.MaxDuration = duration
|
||||
}
|
||||
ping.result.SuccessCounter++
|
||||
if duration > ping.result.MaxDuration {
|
||||
ping.result.MaxDuration = duration
|
||||
} else if duration < ping.result.MinDuration {
|
||||
ping.result.MinDuration = duration
|
||||
}
|
||||
ping.result.TotalDuration += duration
|
||||
}
|
||||
case <-ping.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return ping.done
|
||||
}
|
||||
|
||||
// Result return ping result
|
||||
func (ping *HTTPing) Result() *Result {
|
||||
return ping.result
|
||||
}
|
||||
|
||||
// Stop the tcping
|
||||
func (ping *HTTPing) Stop() {
|
||||
ping.done <- struct{}{}
|
||||
}
|
||||
|
||||
func (ping HTTPing) ping() (time.Duration, *http.Response, string, error) {
|
||||
var resp *http.Response
|
||||
var body io.Reader
|
||||
if ping.Method == "POST" {
|
||||
body = bytes.NewBufferString("{}")
|
||||
}
|
||||
req, err := http.NewRequest(ping.Method, ping.target.String(), body)
|
||||
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), "tcping")
|
||||
if err != nil {
|
||||
return 0, nil, "", err
|
||||
}
|
||||
var remoteAddr string
|
||||
trace := &httptrace.ClientTrace{
|
||||
ConnectStart: func(network, addr string) {
|
||||
remoteAddr = addr
|
||||
},
|
||||
}
|
||||
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
||||
duration, errIfce := timeIt(func() interface{} {
|
||||
client := http.Client{Timeout: ping.target.Timeout}
|
||||
resp, err = client.Do(req)
|
||||
return err
|
||||
})
|
||||
if errIfce != nil {
|
||||
err := errIfce.(error)
|
||||
return 0, nil, "", err
|
||||
}
|
||||
return time.Duration(duration), resp, remoteAddr, nil
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Protocol ...
|
||||
type Protocol int
|
||||
|
||||
func (protocol Protocol) String() string {
|
||||
switch protocol {
|
||||
case TCP:
|
||||
return "tcp"
|
||||
case HTTP:
|
||||
return "http"
|
||||
case HTTPS:
|
||||
return "https"
|
||||
}
|
||||
return "unkown"
|
||||
}
|
||||
|
||||
const (
|
||||
// TCP is tcp protocol
|
||||
TCP Protocol = iota
|
||||
// HTTP is http protocol
|
||||
HTTP
|
||||
// HTTPS is https protocol
|
||||
HTTPS
|
||||
)
|
||||
|
||||
// NewProtocol convert protocol stirng to Protocol
|
||||
func NewProtocol(protocol string) (Protocol, error) {
|
||||
switch strings.ToLower(protocol) {
|
||||
case TCP.String():
|
||||
return TCP, nil
|
||||
case HTTP.String():
|
||||
return HTTP, nil
|
||||
case HTTPS.String():
|
||||
return HTTPS, nil
|
||||
}
|
||||
return 0, fmt.Errorf("protocol %s not support", protocol)
|
||||
}
|
||||
|
||||
// Target is a ping
|
||||
type Target struct {
|
||||
Protocol Protocol
|
||||
Host string
|
||||
Port int
|
||||
|
||||
Counter int
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func (target Target) String() string {
|
||||
return fmt.Sprintf("%s://%s:%d", target.Protocol, target.Host, target.Port)
|
||||
}
|
||||
|
||||
// Pinger is a ping interface
|
||||
type Pinger interface {
|
||||
Start() <-chan struct{}
|
||||
Stop()
|
||||
Result() *Result
|
||||
SetTarget(target *Target)
|
||||
}
|
||||
|
||||
// Ping is a ping interface
|
||||
type Ping interface {
|
||||
Start() <-chan struct{}
|
||||
|
||||
Host() string
|
||||
Port() int
|
||||
Protocol() Protocol
|
||||
Counter() int
|
||||
|
||||
Stop()
|
||||
|
||||
Result() Result
|
||||
}
|
||||
|
||||
// Result ...
|
||||
type Result struct {
|
||||
Counter int
|
||||
SuccessCounter int
|
||||
Target *Target
|
||||
|
||||
MinDuration time.Duration
|
||||
MaxDuration time.Duration
|
||||
TotalDuration time.Duration
|
||||
}
|
||||
|
||||
// Avg return the average time of ping
|
||||
func (result Result) Avg() time.Duration {
|
||||
if result.SuccessCounter == 0 {
|
||||
return 0
|
||||
}
|
||||
return result.TotalDuration / time.Duration(result.SuccessCounter)
|
||||
}
|
||||
|
||||
// Failed return failed counter
|
||||
func (result Result) Failed() int {
|
||||
return result.Counter - result.SuccessCounter
|
||||
}
|
||||
|
||||
func (result Result) String() string {
|
||||
const resultTpl = `
|
||||
Ping statistics {{.Target}}
|
||||
{{.Counter}} probes sent.
|
||||
{{.SuccessCounter}} successful, {{.Failed}} failed.
|
||||
Approximate trip times:
|
||||
Minimum = {{.MinDuration}}, Maximum = {{.MaxDuration}}, Average = {{.Avg}}`
|
||||
t := template.Must(template.New("result").Parse(resultTpl))
|
||||
res := bytes.NewBufferString("")
|
||||
t.Execute(res, result)
|
||||
return res.String()
|
||||
}
|
||||
|
||||
// CheckURI check uri
|
||||
func CheckURI(uri string) (schema, host string, port int, matched bool) {
|
||||
const reExp = `^((?P<schema>((ht|f)tp(s?))|tcp)\://)?((([a-zA-Z0-9_\-]+\.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)((\.?\d)\.)){4})|(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))(:([0-9]+))?(/[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]*)?$`
|
||||
pattern := regexp.MustCompile(reExp)
|
||||
res := pattern.FindStringSubmatch(uri)
|
||||
if len(res) == 0 {
|
||||
return
|
||||
}
|
||||
matched = true
|
||||
schema = res[2]
|
||||
if schema == "" {
|
||||
schema = "tcp"
|
||||
}
|
||||
host = res[6]
|
||||
if res[17] == "" {
|
||||
if schema == HTTPS.String() {
|
||||
port = 443
|
||||
} else {
|
||||
port = 80
|
||||
}
|
||||
} else {
|
||||
port, _ = strconv.Atoi(res[17])
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TCPing ...
|
||||
type TCPing struct {
|
||||
target *Target
|
||||
done chan struct{}
|
||||
result *Result
|
||||
}
|
||||
|
||||
var _ Pinger = (*TCPing)(nil)
|
||||
|
||||
// NewTCPing return a new TCPing
|
||||
func NewTCPing() *TCPing {
|
||||
tcping := TCPing{
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
return &tcping
|
||||
}
|
||||
|
||||
// SetTarget set target for TCPing
|
||||
func (tcping *TCPing) SetTarget(target *Target) {
|
||||
tcping.target = target
|
||||
if tcping.result == nil {
|
||||
tcping.result = &Result{Target: target}
|
||||
}
|
||||
}
|
||||
|
||||
// Result return the result
|
||||
func (tcping TCPing) Result() *Result {
|
||||
return tcping.result
|
||||
}
|
||||
|
||||
// Start a tcping
|
||||
func (tcping TCPing) Start() <-chan struct{} {
|
||||
go func() {
|
||||
t := time.NewTicker(tcping.target.Interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
if tcping.result.Counter >= tcping.target.Counter && tcping.target.Counter != 0 {
|
||||
tcping.Stop()
|
||||
return
|
||||
}
|
||||
duration, remoteAddr, err := tcping.ping()
|
||||
tcping.result.Counter++
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Ping %s - failed: %s\n", tcping.target, err)
|
||||
} else {
|
||||
fmt.Printf("Ping %s(%s) - Connected - time=%s\n", tcping.target, remoteAddr, duration)
|
||||
|
||||
if tcping.result.MinDuration == 0 {
|
||||
tcping.result.MinDuration = duration
|
||||
}
|
||||
if tcping.result.MaxDuration == 0 {
|
||||
tcping.result.MaxDuration = duration
|
||||
}
|
||||
tcping.result.SuccessCounter++
|
||||
if duration > tcping.result.MaxDuration {
|
||||
tcping.result.MaxDuration = duration
|
||||
} else if duration < tcping.result.MinDuration {
|
||||
tcping.result.MinDuration = duration
|
||||
}
|
||||
tcping.result.TotalDuration += duration
|
||||
}
|
||||
case <-tcping.done:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return tcping.done
|
||||
}
|
||||
|
||||
// Stop the tcping
|
||||
func (tcping *TCPing) Stop() {
|
||||
tcping.done <- struct{}{}
|
||||
}
|
||||
|
||||
func (tcping TCPing) ping() (time.Duration, net.Addr, error) {
|
||||
var remoteAddr net.Addr
|
||||
duration, errIfce := timeIt(func() interface{} {
|
||||
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", tcping.target.Host, tcping.target.Port), tcping.target.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
remoteAddr = conn.RemoteAddr()
|
||||
conn.Close()
|
||||
return nil
|
||||
})
|
||||
if errIfce != nil {
|
||||
err := errIfce.(error)
|
||||
return 0, remoteAddr, err
|
||||
}
|
||||
return time.Duration(duration), remoteAddr, nil
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeIt(f func() interface{}) (int64, interface{}) {
|
||||
startAt := time.Now()
|
||||
res := f()
|
||||
endAt := time.Now()
|
||||
return endAt.UnixNano() - startAt.UnixNano(), res
|
||||
}
|
||||
|
||||
// UseCustomeDNS will set the dns to default DNS resolver for global
|
||||
func UseCustomeDNS(dns []string) {
|
||||
resolver := net.Resolver{
|
||||
PreferGo: true,
|
||||
Dial: func(ctx context.Context, network, address string) (conn net.Conn, err error) {
|
||||
for _, addr := range dns {
|
||||
if conn, err = net.Dial("udp", addr+":53"); err != nil {
|
||||
continue
|
||||
} else {
|
||||
return conn, nil
|
||||
}
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
net.DefaultResolver = &resolver
|
||||
}
|
||||
|
||||
// FormatIP - trim spaces and format IP
|
||||
//
|
||||
// IP - the provided IP
|
||||
//
|
||||
// string - return "" if the input is neither valid IPv4 nor valid IPv6
|
||||
// return IPv4 in format like "192.168.9.1"
|
||||
// return IPv6 in format like "[2002:ac1f:91c5:1::bd59]"
|
||||
func FormatIP(IP string) string {
|
||||
|
||||
host := strings.Trim(IP, "[ ]")
|
||||
if parseIP := net.ParseIP(host); parseIP != nil && parseIP.To4() == nil {
|
||||
host = fmt.Sprintf("[%s]", host)
|
||||
}
|
||||
|
||||
return host
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package ping
|
||||
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestFormatIP(t *testing.T) {
|
||||
|
||||
Convey("IP", t, func() {
|
||||
Convey("for v4 success", func() {
|
||||
rc := FormatIP("192.168.0.1")
|
||||
So(rc, ShouldEqual, "192.168.0.1")
|
||||
})
|
||||
|
||||
Convey("for v4 failure", func() {
|
||||
rc := FormatIP("192.0.1")
|
||||
So(rc, ShouldEqual, "")
|
||||
})
|
||||
|
||||
Convey("for v4 format", func() {
|
||||
rc := FormatIP("[192.0.1.1] ")
|
||||
So(rc, ShouldEqual, "192.0.1.1")
|
||||
})
|
||||
|
||||
Convey("for v6 success", func() {
|
||||
rc := FormatIP("[2002:ac1f:91c5:1::bd59]")
|
||||
So(rc, ShouldEqual, "[2002:ac1f:91c5:1::bd59]")
|
||||
})
|
||||
|
||||
Convey("for v6 failure", func() {
|
||||
rc := FormatIP("2002:ac1f:91c5:1:")
|
||||
So(rc, ShouldEqual, "")
|
||||
})
|
||||
|
||||
Convey("for v6 format", func() {
|
||||
rc := FormatIP("2002:ac1f:91c5:1::bd59 ")
|
||||
So(rc, ShouldEqual, "[2002:ac1f:91c5:1::bd59]")
|
||||
})
|
||||
})
|
||||
}
|
@ -1,173 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"b612.me/starainrt"
|
||||
|
||||
"b612.me/sshd"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var sftpcmd = &cobra.Command{
|
||||
Use: "sftp",
|
||||
Short: "sftp上传下载",
|
||||
Long: "sftp上传下载",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
d, _ := this.Flags().GetBool("download")
|
||||
s, _ := this.Flags().GetString("src")
|
||||
r, _ := this.Flags().GetString("dst")
|
||||
i, _ := this.Flags().GetString("identify")
|
||||
k, _ := this.Flags().GetString("password")
|
||||
p, _ := this.Flags().GetInt("port")
|
||||
b, _ := this.Flags().GetInt("buffer")
|
||||
g, _ := this.Flags().GetString("regexp")
|
||||
var user, host string
|
||||
var err error
|
||||
if len(args) != 1 {
|
||||
fmt.Println("sftp <[user@]Host> -s <Local> -d <Remote>")
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
hosts := strings.Split(args[0], "@")
|
||||
if len(hosts) == 1 {
|
||||
host = hosts[0]
|
||||
user = "root"
|
||||
} else {
|
||||
user = hosts[0]
|
||||
host = hosts[1]
|
||||
}
|
||||
fmt.Println("进行SSH连接……")
|
||||
myssh := new(sshd.StarSSH)
|
||||
err = myssh.Connect(user, k, host, i, p)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer myssh.Close()
|
||||
fmt.Println("已连接上……")
|
||||
sftp, err := sshd.CreateSftp(myssh.Client)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer sftp.Close()
|
||||
fmt.Println("已建立SFTP……")
|
||||
|
||||
shell := func(pect float64) {
|
||||
if pect != 100.0 {
|
||||
fmt.Printf("传输已完成:%f%%\r", pect)
|
||||
} else {
|
||||
fmt.Printf("传输已完成:%f%%\n", pect)
|
||||
}
|
||||
}
|
||||
var UploadDir func(string, string)
|
||||
UploadDir = func(fs, remote string) {
|
||||
if runtime.GOOS == "windows" {
|
||||
fs = strings.Replace(fs, "/", "\\", -1)
|
||||
}
|
||||
sftp.MkdirAll(remote)
|
||||
abspath, _ := filepath.Abs(fs)
|
||||
dir, err := ioutil.ReadDir(fs)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for _, v := range dir {
|
||||
if v.IsDir() {
|
||||
if g != "" {
|
||||
continue
|
||||
}
|
||||
UploadDir(abspath+string(os.PathSeparator)+v.Name(), remote+"/"+v.Name())
|
||||
} else {
|
||||
if ok, _ := regexp.MatchString(g, v.Name()); !ok {
|
||||
continue
|
||||
}
|
||||
fmt.Println("上传:" + abspath + string(os.PathSeparator) + v.Name())
|
||||
err = sshd.FtpTransferOutFunc(abspath+string(os.PathSeparator)+v.Name(), remote+"/"+v.Name(), b, shell, sftp)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var DownloadDir func(string, string)
|
||||
DownloadDir = func(fs, remote string) {
|
||||
abspath, _ := filepath.Abs(remote)
|
||||
os.MkdirAll(abspath, 0755)
|
||||
dir, err := sftp.ReadDir(fs)
|
||||
if err != nil {
|
||||
fmt.Println("读取错误", err)
|
||||
return
|
||||
}
|
||||
for _, v := range dir {
|
||||
if v.IsDir() {
|
||||
if g != "" {
|
||||
continue
|
||||
}
|
||||
DownloadDir(fs+"/"+v.Name(), abspath+string(os.PathSeparator)+v.Name())
|
||||
} else {
|
||||
if ok, _ := regexp.MatchString(g, v.Name()); !ok {
|
||||
continue
|
||||
}
|
||||
fmt.Println("下载:" + fs + "/" + v.Name())
|
||||
err = sshd.FtpTransferInFunc(fs+"/"+v.Name(), abspath+string(os.PathSeparator)+v.Name(), b, shell, sftp)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !d {
|
||||
if !starainrt.Exists(s) {
|
||||
fmt.Println("本地文件或文件夹:" + s + "不存在")
|
||||
return
|
||||
}
|
||||
if starainrt.IsFile(s) {
|
||||
err = sshd.FtpTransferOutFunc(s, r, b, shell, sftp)
|
||||
} else {
|
||||
UploadDir(s, r)
|
||||
}
|
||||
} else {
|
||||
if !myssh.Exists(s) {
|
||||
fmt.Println("远端文件或文件夹:" + s + "不存在")
|
||||
return
|
||||
}
|
||||
stat, err := sftp.Stat(s)
|
||||
if err != nil {
|
||||
fmt.Println("错误:", err)
|
||||
return
|
||||
}
|
||||
if !stat.IsDir() {
|
||||
err = sshd.FtpTransferInFunc(s, r, b, shell, sftp)
|
||||
} else {
|
||||
DownloadDir(s, r)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(sftpcmd)
|
||||
sftpcmd.Flags().BoolP("download", "D", false, "进行下载")
|
||||
sftpcmd.Flags().StringP("identify", "i", "", "RSA登录密钥")
|
||||
sftpcmd.Flags().StringP("password", "k", "", "登录密码")
|
||||
sftpcmd.Flags().StringP("src", "s", "", "本机路径/若为下载则相反")
|
||||
sftpcmd.Flags().StringP("dst", "d", "", "远程路径/若为下载则相反")
|
||||
sftpcmd.Flags().IntP("port", "p", 22, "登录端口")
|
||||
sftpcmd.Flags().StringP("regexp", "r", "", "正则表达式")
|
||||
sftpcmd.Flags().IntP("buffer", "b", 10240, "buffer大小")
|
||||
}
|
@ -1,443 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"b612.me/starainrt"
|
||||
"b612.me/starlog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var tcpcmd = &cobra.Command{
|
||||
Use: "tcp",
|
||||
Short: "发送并监听tcp数据包",
|
||||
Long: "发送并监听tcp数据包",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
r, _ := this.Flags().GetBool("recvonly")
|
||||
if len(args) != 1 && !r {
|
||||
fmt.Println("请指定远程tcp地址")
|
||||
return
|
||||
}
|
||||
l, _ := this.Flags().GetString("port")
|
||||
a, _ := this.Flags().GetString("addr")
|
||||
s, _ := this.Flags().GetBool("local")
|
||||
b, _ := this.Flags().GetBool("byte")
|
||||
laddr, err := net.ResolveTCPAddr("tcp", a+":"+l)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if s {
|
||||
tcplisten, err := net.ListenTCP("tcp", laddr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("监听已建立")
|
||||
defer tcplisten.Close()
|
||||
go func() {
|
||||
for {
|
||||
conn, err := tcplisten.AcceptTCP()
|
||||
if err != nil {
|
||||
fmt.Printf("Error Connect From %s : %s\n", conn.RemoteAddr(), err.Error())
|
||||
continue
|
||||
}
|
||||
starlog.Infof("Accept Connect From %s\n", conn.RemoteAddr())
|
||||
go func(conns *net.TCPConn) {
|
||||
for {
|
||||
buf := make([]byte, 204800)
|
||||
n, err := conns.Read(buf)
|
||||
if err != nil {
|
||||
starlog.Infof("Error from %s Where Message=%s\n", conns.RemoteAddr(), err.Error())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
starlog.Infof("Receive Msg From %s : %s\n", conns.RemoteAddr(), string(buf[0:n]))
|
||||
if b {
|
||||
starlog.Infof("%#v", buf[0:n])
|
||||
}
|
||||
}
|
||||
|
||||
}(conn)
|
||||
}
|
||||
}()
|
||||
}
|
||||
if !r {
|
||||
mytcp, err := net.DialTimeout("tcp", args[0], time.Second*15)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if s {
|
||||
for {
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
defer mytcp.Close()
|
||||
fmt.Println("TCP连接已建立")
|
||||
go func() {
|
||||
var err error
|
||||
for {
|
||||
txt := starainrt.MessageBox("", "")
|
||||
if txt == "" {
|
||||
continue
|
||||
}
|
||||
if !b {
|
||||
_, err = mytcp.Write([]byte(txt))
|
||||
} else {
|
||||
var sendbyte []byte
|
||||
bytes := strings.Split(txt, ",")
|
||||
for _, v := range bytes {
|
||||
ints, _ := strconv.Atoi(v)
|
||||
if ints < 0 || ints > 255 {
|
||||
continue
|
||||
}
|
||||
sendbyte = append(sendbyte, byte(ints))
|
||||
}
|
||||
_, err = mytcp.Write(sendbyte)
|
||||
}
|
||||
if err != nil {
|
||||
starlog.Errorf("Error from %s Where Message=%s\n", mytcp.RemoteAddr().String(), err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
for {
|
||||
buf := make([]byte, 204800)
|
||||
n, err := mytcp.Read(buf)
|
||||
if err != nil {
|
||||
starlog.Errorf("Error from %s Where Message=%s\n", mytcp.RemoteAddr().String(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
starlog.Infof("Receive Msg From %s : %s\n", mytcp.RemoteAddr().String(), string(buf[0:n]))
|
||||
if b {
|
||||
starlog.Infof("%#v", buf[0:n])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(tcpcmd)
|
||||
tcpcmd.Flags().BoolP("byte", "b", false, "发送二进制数据")
|
||||
tcpcmd.Flags().StringP("port", "p", "1127", "本地监听端口")
|
||||
tcpcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip")
|
||||
tcpcmd.Flags().BoolP("local", "s", false, "启动本地监听")
|
||||
tcpsendcmd.Flags().StringP("port", "p", "1127", "远程连接端口")
|
||||
tcpsendcmd.Flags().StringP("addr", "a", "0.0.0.0", "远程监听ip")
|
||||
tcpsendcmd.Flags().StringP("regexp", "r", "", "正则匹配字符串")
|
||||
tcprecvcmd.Flags().StringP("port", "p", "1127", "本地监听端口")
|
||||
tcprecvcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip")
|
||||
tcpcmd.Flags().BoolP("recvonly", "r", false, "仅接收udp包")
|
||||
tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd)
|
||||
}
|
||||
|
||||
var cansend, backsck, foldercreate bool = false, false, false
|
||||
var tcpsendcmd = &cobra.Command{
|
||||
Use: "send",
|
||||
Short: "通过tcp发送文件",
|
||||
Long: "通过tcp接受文件",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
stop := false
|
||||
p, _ := this.Flags().GetString("port")
|
||||
a, _ := this.Flags().GetString("addr")
|
||||
r, _ := this.Flags().GetString("regexp")
|
||||
if len(args) != 1 {
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
mytcp, err := net.DialTimeout("tcp", a+":"+p, time.Second*15)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("TCP连接已建立")
|
||||
defer mytcp.Close()
|
||||
go func() {
|
||||
for {
|
||||
buf := make([]byte, 1048576)
|
||||
n, err := mytcp.Read(buf)
|
||||
if err != nil {
|
||||
if !stop {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
continue
|
||||
}
|
||||
data := string(buf[0:n])
|
||||
if data == "oksend!" {
|
||||
cansend = true
|
||||
} else if data == "backsck" {
|
||||
backsck = true
|
||||
} else if data == "folderok" {
|
||||
foldercreate = true
|
||||
} else {
|
||||
fmt.Println(data)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}()
|
||||
file := args[0]
|
||||
if file[len(file)-1:] == "\\" || file[len(file)-1:] == "/" {
|
||||
file = file[0 : len(file)-1]
|
||||
}
|
||||
file, _ = filepath.Abs(file)
|
||||
Scan(file, "", mytcp, r)
|
||||
stop = true
|
||||
mytcp.Write([]byte("jane&0&0"))
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func tcpupload(file, prefix string, mytcp net.Conn) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
file = strings.Replace(file, "/", "\\", -1)
|
||||
prefix = strings.Replace(prefix, "/", "\\", -1)
|
||||
}
|
||||
fmt.Println(file)
|
||||
fpsrc, err := os.Open(file)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
stat, _ := os.Stat(file)
|
||||
filebig := float64(stat.Size())
|
||||
_, err = mytcp.Write([]byte("victorique&" + stat.Name() + "&" + prefix))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
var sakura int = 0
|
||||
for !cansend {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
sakura++
|
||||
if sakura > 50 {
|
||||
mytcp.Write([]byte{1, 9, 9, 6, 1, 1, 2, 7})
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
mytcp.Write([]byte("victorique&" + stat.Name() + "&" + prefix))
|
||||
}
|
||||
}
|
||||
cansend = false
|
||||
sum := 0
|
||||
for {
|
||||
buf := make([]byte, 524288)
|
||||
n, err := fpsrc.Read(buf)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
mytcp.Write([]byte{1, 9, 9, 6, 1, 1, 2, 7})
|
||||
for !backsck {
|
||||
time.Sleep(time.Millisecond * 150)
|
||||
}
|
||||
backsck = false
|
||||
break
|
||||
}
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
_, err = mytcp.Write(buf[0:n])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
sum += n
|
||||
fmt.Printf("当前已传输:%f\r", float64(sum)/filebig*100)
|
||||
}
|
||||
fmt.Printf("当前已传输:%f\n\n", 100.000000)
|
||||
fpsrc.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func Scan(path, prefix string, mytcp net.Conn, reg string) {
|
||||
var err error
|
||||
var regp *regexp.Regexp
|
||||
if prefix != "" {
|
||||
prefix += "/"
|
||||
}
|
||||
if reg != "" {
|
||||
regp, err = regexp.Compile(reg)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
path = strings.Replace(path, "/", "\\", -1)
|
||||
prefix = strings.Replace(prefix, "/", "\\", -1)
|
||||
}
|
||||
if starainrt.IsFile(path) {
|
||||
if reg != "" {
|
||||
if regp.MatchString(path) {
|
||||
tcpupload(path, prefix, mytcp)
|
||||
}
|
||||
} else {
|
||||
tcpupload(path, prefix, mytcp)
|
||||
}
|
||||
} else if starainrt.IsFolder(path) {
|
||||
dir, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
for _, v := range dir {
|
||||
if v.IsDir() && (v.Name() != "." || v.Name() != "..") {
|
||||
mytcp.Write([]byte("b612&" + prefix + v.Name() + "&0"))
|
||||
count := 0
|
||||
for !foldercreate {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
count++
|
||||
if count%30 == 0 {
|
||||
mytcp.Write([]byte("b612&" + prefix + v.Name() + "&0"))
|
||||
}
|
||||
}
|
||||
foldercreate = false
|
||||
Scan(path+"/"+v.Name(), prefix+v.Name(), mytcp, reg)
|
||||
} else {
|
||||
if reg != "" {
|
||||
if regp.MatchString(path + "/" + v.Name()) {
|
||||
err = tcpupload(path+"/"+v.Name(), prefix, mytcp)
|
||||
}
|
||||
} else {
|
||||
err = tcpupload(path+"/"+v.Name(), prefix, mytcp)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var tcprecvcmd = &cobra.Command{
|
||||
Use: "recv",
|
||||
Short: "通过tcp接收文件",
|
||||
Long: "通过tcp接收文件",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
p, _ := this.Flags().GetString("port")
|
||||
a, _ := this.Flags().GetString("addr")
|
||||
laddr, err := net.ResolveTCPAddr("tcp", a+":"+p)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
tcplisten, err := net.ListenTCP("tcp", laddr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("TCP监听已建立")
|
||||
defer tcplisten.Close()
|
||||
for {
|
||||
conn, err := tcplisten.AcceptTCP()
|
||||
if err != nil {
|
||||
fmt.Printf("Error Connect From %s : %s\n", conn.RemoteAddr(), err.Error())
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Accept Connect From %s\n", conn.RemoteAddr())
|
||||
go func(conns *net.TCPConn) {
|
||||
canrecv := false
|
||||
name := ""
|
||||
var fpdst *os.File
|
||||
defer conns.Close()
|
||||
var sum int64
|
||||
for {
|
||||
buf := make([]byte, 393216)
|
||||
n, err := conns.Read(buf)
|
||||
|
||||
if !canrecv {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
data := string(buf[0:n])
|
||||
str := strings.Split(data, "&")
|
||||
if len(str) != 3 {
|
||||
continue
|
||||
}
|
||||
if str[0] == "victorique" {
|
||||
canrecv = true
|
||||
name = str[1]
|
||||
prefix := str[2]
|
||||
if runtime.GOOS != "windows" {
|
||||
prefix = strings.Replace(prefix, "\\", "/", -1)
|
||||
}
|
||||
if prefix == "" {
|
||||
fpdst, err = os.Create("./" + name)
|
||||
} else {
|
||||
fpdst, err = os.Create(prefix + name)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
conns.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
conns.Write([]byte("oksend!"))
|
||||
} else if str[0] == "b612" {
|
||||
str[1] = ".\\" + str[1]
|
||||
if runtime.GOOS != "windows" {
|
||||
str[1] = strings.Replace(str[1], "\\", "/", -1)
|
||||
}
|
||||
fmt.Println("进入文件夹:" + str[1] + "\n")
|
||||
os.MkdirAll(str[1], 0644)
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
conns.Write([]byte("folderok"))
|
||||
} else if str[0] == "jane" {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if buf[n-1] == byte(7) && buf[n-2] == byte(2) && buf[n-3] == byte(1) {
|
||||
ok := func() bool {
|
||||
for k, v := range []byte{1, 9, 9, 6, 1, 1, 2, 7} {
|
||||
if buf[n-8+k] != v {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}()
|
||||
if ok {
|
||||
if n > 8 {
|
||||
fpdst.Write(buf[0 : n-8])
|
||||
sum += int64(n - 8)
|
||||
}
|
||||
fmt.Printf("已写入:%d 字节\n", sum)
|
||||
fmt.Println(name + "接收成功\n")
|
||||
fpdst.Close()
|
||||
canrecv = false
|
||||
sum = 0
|
||||
conns.Write([]byte("backsck"))
|
||||
continue
|
||||
}
|
||||
}
|
||||
fpdst.Write(buf[0:n])
|
||||
sum += int64(n)
|
||||
fmt.Printf("已写入:%d 字节\r", sum)
|
||||
}
|
||||
}
|
||||
}(conn)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
// +build windows
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"b612.me/wincmd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var uaccmd = &cobra.Command{
|
||||
Use: "uac",
|
||||
Short: "Windows 使用uac权限打开文件",
|
||||
Long: "Windows 使用uac权限打开文件",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
return
|
||||
}
|
||||
cmdLine := ""
|
||||
if len(args) > 1 {
|
||||
for _, v := range args[1:] {
|
||||
cmdLine += v + " "
|
||||
}
|
||||
}
|
||||
pwd, _ := os.Getwd()
|
||||
wincmd.StartProcess(args[0], cmdLine, pwd, true, 1)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(uaccmd)
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"b612.me/starainrt"
|
||||
"b612.me/starlog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var udpcmd = &cobra.Command{
|
||||
Use: "udp",
|
||||
Short: "发送并监听udp数据包",
|
||||
Long: "发送并监听udp数据包",
|
||||
Run: func(this *cobra.Command, args []string) {
|
||||
var raddr *net.UDPAddr
|
||||
r, _ := this.Flags().GetBool("recvonly")
|
||||
if len(args) != 1 && !r {
|
||||
fmt.Println("请指定远程udp地址")
|
||||
return
|
||||
}
|
||||
l, _ := this.Flags().GetString("port")
|
||||
a, _ := this.Flags().GetString("addr")
|
||||
s, _ := this.Flags().GetBool("local")
|
||||
b, _ := this.Flags().GetBool("byte")
|
||||
laddr, err := net.ResolveUDPAddr("udp", a+":"+l)
|
||||
if !r {
|
||||
raddr, err = net.ResolveUDPAddr("udp", args[0])
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if s {
|
||||
udplisten, err := net.ListenUDP("udp", laddr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("监听已建立")
|
||||
go func() {
|
||||
for {
|
||||
buf := make([]byte, 204800)
|
||||
n, addr, err := udplisten.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
starlog.Errorln("Error from %s Where Message=%s\n", addr.String(), err.Error())
|
||||
continue
|
||||
}
|
||||
starlog.Infof("Receive Msg From %s : %s\n", addr.String(), string(buf[0:n]))
|
||||
if b {
|
||||
starlog.Infof("%#v\n", buf[0:n])
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
if !r {
|
||||
myudp, err := net.DialUDP("udp", nil, raddr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("UDP虚拟连接已建立")
|
||||
go func() {
|
||||
var err error
|
||||
for {
|
||||
txt := starainrt.MessageBox("", "")
|
||||
if txt == "" {
|
||||
continue
|
||||
}
|
||||
if !b {
|
||||
_, err = myudp.Write([]byte(txt))
|
||||
} else {
|
||||
var sendbyte []byte
|
||||
bytes := strings.Split(txt, ",")
|
||||
for _, v := range bytes {
|
||||
ints, _ := strconv.Atoi(v)
|
||||
if ints < 0 || ints > 255 {
|
||||
continue
|
||||
}
|
||||
sendbyte = append(sendbyte, byte(ints))
|
||||
}
|
||||
_, err = myudp.Write(sendbyte)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("Error from %s Where Message=%s\n", myudp.RemoteAddr().String(), err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
for {
|
||||
buf := make([]byte, 204800)
|
||||
n, err := myudp.Read(buf)
|
||||
if err != nil {
|
||||
starlog.Infof("Error from %s Where Message=%s\n", myudp.RemoteAddr().String(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
starlog.Infof("Receive Msg From %s : %s\n", myudp.RemoteAddr().String(), string(buf[0:n]))
|
||||
if b {
|
||||
starlog.Infof("%#v", buf[0:n])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(udpcmd)
|
||||
udpcmd.Flags().BoolP("byte", "b", false, "发送二进制数据")
|
||||
udpcmd.Flags().StringP("port", "p", "1127", "本地监听端口")
|
||||
udpcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip")
|
||||
udpcmd.Flags().BoolP("local", "s", false, "启动本地监听")
|
||||
udpcmd.Flags().BoolP("recvonly", "r", false, "仅接收udp包")
|
||||
}
|
Loading…
Reference in New Issue