Compare commits

..

No commits in common. "master" and "v0.1.24" have entirely different histories.

15 changed files with 154 additions and 277 deletions

View File

@ -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.Criticalln("ERROR PATH") 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.Criticalln(err.Error) starlog.StdPrintln(0, starlog.RED, err.Error)
} else { } else {
fmt.Println("完成") fmt.Println("完成")
} }

View File

@ -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.Criticalln("参数不足,请输入文件地址或字符串") 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.Criticalln(err) starlog.StdPrintln(0, starlog.RED, err)
return return
} }
}, },

View File

@ -4,13 +4,10 @@ package tools
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"b612.me/starainrt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,8 +18,7 @@ var cdcmd = &cobra.Command{
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
fileInfo, _ := os.Stdin.Stat() fileInfo, _ := os.Stdin.Stat()
if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe { if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe {
fmt.Println(args) if len(args) != 1 {
if len(args) != 0 {
os.Exit(1) os.Exit(1)
} else { } else {
exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run() exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(args[0])).Run()
@ -31,12 +27,7 @@ var cdcmd = &cobra.Command{
} }
s := bufio.NewScanner(os.Stdin) s := bufio.NewScanner(os.Stdin)
for s.Scan() { for s.Scan() {
dir := s.Text() exec.Command("cmd.exe", "/c", "explorer "+filepath.Dir(s.Text())).Run()
if starainrt.IsFile(dir) {
exec.Command("cmd.exe", "/c", "explorer /n,/select,"+dir).Run()
} else {
exec.Command("cmd.exe", "/c", "explorer "+dir).Run()
}
return return
} }
}, },

View File

@ -4,7 +4,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version string = "0.1.25" var Version string = "0.1.24"
var Maincmd = &cobra.Command{ var Maincmd = &cobra.Command{
Use: "", Use: "",

View File

@ -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.Criticalln("ERROR PATH") 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.Criticalln(err.Error) starlog.StdPrintln(0, starlog.RED, err.Error)
} else { } else {
fmt.Println("完成") fmt.Println("完成")
} }

View File

@ -2,8 +2,6 @@ package tools
import ( import (
"fmt" "fmt"
"os"
"time"
"b612.me/starainrt" "b612.me/starainrt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -16,38 +14,26 @@ var gencmd = &cobra.Command{
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
sum, _ := this.Flags().GetInt("sum") sum, _ := this.Flags().GetInt("sum")
num, _ := this.Flags().GetInt("num") num, _ := this.Flags().GetInt("num")
cap, _ := this.Flags().GetInt("cap")
if len(args) != 1 { if len(args) != 1 {
this.Help() this.Help()
return return
} }
if num <= 0 { err := starainrt.FillWithRandom(args[0], num, 1024*1024, sum, func(pect float64) {
fmt.Println("num不合法不应该小于1") if pect == 100 {
os.Exit(2) fmt.Println("文件已处理100.000000%")
} } else {
if sum <= 0 { fmt.Printf("文件已处理:%f%%\r", pect)
fmt.Println("sum不合法不应该小于1") }
os.Exit(2)
}
if cap <= 0 {
fmt.Println("cap不合法不应该小于1")
os.Exit(2)
}
err := starainrt.FillWithRandom(args[0], num, cap, sum, func(pect float64) {
fmt.Printf("文件已处理:%f%%\r", pect)
}) })
if err != nil { if err != nil {
fmt.Println("err:" + err.Error()) fmt.Println("err:" + err.Error())
} }
fmt.Println("文件已处理100.0000000%")
time.Sleep(time.Millisecond * 10)
}, },
} }
func init() { func init() {
gencmd.Flags().IntP("sum", "s", 3, "随机的种子组数") gencmd.Flags().IntP("sum", "s", 3, "随机的种子组数")
gencmd.Flags().IntP("num", "n", 1024, "生成的文件大小") gencmd.Flags().IntP("num", "n", 1024, "生成的文件大小")
gencmd.Flags().IntP("cap", "c", 1048576, "bufcap大小")
gencmd.MarkFlagRequired("num") gencmd.MarkFlagRequired("num")
Maincmd.AddCommand(gencmd) Maincmd.AddCommand(gencmd)
} }

View File

@ -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.Criticalln("错误:" + err.Error()) 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])

View File

@ -1,7 +1,6 @@
package tools package tools
import ( import (
"encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -20,48 +19,27 @@ import (
var port, ip, path string var port, ip, path string
var up bool var up bool
var basicAuth, certKey string
type TraceHandler struct { type TraceHandler struct {
h http.Handler h http.Handler
} }
func init() {
httpcmd.Flags().StringVarP(&port, "port", "p", "80", "监听端口")
httpcmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听ip")
httpcmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
httpcmd.Flags().BoolVarP(&up, "upload", "u", false, "是否开启文件上传")
httpcmd.Flags().StringVarP(&basicAuth, "auth", "a", "", "HTTP BASIC AUTH认证(用户名:密码)")
httpcmd.Flags().StringVarP(&certKey, "cert", "c", "", "TLS证书路径用:分割证书与密钥")
Maincmd.AddCommand(httpcmd)
}
// httpCmd represents the http command // httpCmd represents the http command
var httpcmd = &cobra.Command{ var httpcmd = &cobra.Command{
Use: "http", Use: "http",
Short: "HTTP文件服务器", Short: "HTTP文件服务器",
Long: `HTTP文件服务器`, Long: `HTTP文件服务器`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var err error
http.HandleFunc("/", httplisten) http.HandleFunc("/", httplisten)
path, _ = filepath.Abs(path) path, _ = filepath.Abs(path)
starlog.Infoln("Listening On Port:" + port) fmt.Println("Listening On Port:" + port)
if up { if up {
starlog.Infoln("upload is openned,path is /vtqeupload1127") fmt.Println("upload is openned,path is /vtqeupload1127")
http.HandleFunc("/vtqeupload1127", uploadfile) http.HandleFunc("/vtqeupload1127", uploadfile)
} }
if certKey == "" { err := http.ListenAndServe(ip+":"+port, nil)
err = http.ListenAndServe(ip+":"+port, nil)
} else {
certs := strings.Split(certKey, ":")
if len(certs) != 2 {
starlog.Criticalln("证书不正确!")
return
}
err = http.ListenAndServeTLS(ip+":"+port, certs[0], certs[1], nil)
}
if err != nil { if err != nil {
starlog.Criticalln("Error:" + err.Error()) starlog.StdPrintln(0, starlog.RED, "Error:"+err.Error())
} }
}, },
} }
@ -74,18 +52,18 @@ func uploadfile(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10485760) r.ParseMultipartForm(10485760)
file, handler, err := r.FormFile("victorique") file, handler, err := r.FormFile("victorique")
if err != nil { if err != nil {
starlog.Errorln(err) fmt.Println(err)
w.WriteHeader(502) w.WriteHeader(502)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
defer file.Close() defer file.Close()
starlog.Noticef("Upload %s From %s\n", handler.Filename, r.RemoteAddr) fmt.Printf("Upload %s From %s\n", handler.Filename, r.RemoteAddr)
fmt.Fprintf(w, `<html><body><p>%v</p><h2><a href="./vtqeupload1127/web">Return To Web Page</a></h2></body></html>`, handler.Header) fmt.Fprintf(w, `<html><body><p>%v</p><h2><a href="./vtqeupload1127/web">Return To Web Page</a></h2></body></html>`, handler.Header)
os.Mkdir("./vtqeupload1127", 0755) os.Mkdir("./vtqeupload1127", 0755)
f, err := os.OpenFile("./vtqeupload1127/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0755) f, err := os.OpenFile("./vtqeupload1127/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil { if err != nil {
starlog.Errorln(err) fmt.Println(err)
return return
} }
defer f.Close() defer f.Close()
@ -93,40 +71,11 @@ func uploadfile(w http.ResponseWriter, r *http.Request) {
} }
func httplisten(w http.ResponseWriter, r *http.Request) { func httplisten(w http.ResponseWriter, r *http.Request) {
log := starlog.Std.NewFlag()
w.Header().Set("Server", "Vicorique") w.Header().Set("Server", "Vicorique")
w.Header().Set("Powered", "B612.ME") w.Header().Set("Powered", "B612.ME")
write401 := func() {
w.Header().Set("WWW-Authenticate", ` Basic realm="Please Enter Passwd"`)
w.WriteHeader(401)
w.Write([]byte(`
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>B612 HTTP SERVER</center>
</body>
</html>`))
}
if basicAuth != "" {
authHeader := strings.TrimSpace(r.Header.Get("Authorization"))
if len(authHeader) == 0 {
log.Noticeln("No Authed! Get Path is", r.URL.Path, r.RemoteAddr)
write401()
return
} else {
userAuth := base64.StdEncoding.EncodeToString([]byte(basicAuth))
authStr := strings.Split(authHeader, " ")
if strings.TrimSpace(authStr[1]) != userAuth {
log.Noticeln("Auth Failed! Get Path is", r.URL.Path, r.RemoteAddr, "pwd enter is", authHeader)
write401()
return
}
}
}
p := r.URL.Path p := r.URL.Path
cmd := r.URL.Query()["cmd"] cmd := r.URL.Query()["cmd"]
log.Noticeln("Get " + p + " " + r.RemoteAddr) fmt.Println("Get " + p + " " + r.RemoteAddr)
fullpath, _ := filepath.Abs(path + p) fullpath, _ := filepath.Abs(path + p)
if p == "/" { if p == "/" {
@ -164,7 +113,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
} }
fpdst, err := os.Open(fullpath) fpdst, err := os.Open(fullpath)
if err != nil { if err != nil {
log.Errorln(err) fmt.Println(err)
w.WriteHeader(403) w.WriteHeader(403)
w.Write([]byte("<h1>403 NO ACCESS</h1>")) w.Write([]byte("<h1>403 NO ACCESS</h1>"))
return return
@ -193,7 +142,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
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("ETag", etag) w.Header().Set("ETag", etag)
w.Header().Set("Last-Modified", strings.ReplaceAll(fpinfo.ModTime().UTC().Format("Mon, 2 Jan 2006 15:04:05 MST"), "UTC", "GMT")) w.Header().Set("Last-Modified", fpinfo.ModTime().Format("Mon,2 Jan 2006 15:04:05 MST"))
isRange := false isRange := false
var rangeStart, rangeEnd int64 var rangeStart, rangeEnd int64
rangeEnd = -1 rangeEnd = -1
@ -217,26 +166,19 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
} }
} }
defer fpdst.Close() defer fpdst.Close()
var transferData int
if !isRange { if !isRange {
w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10)) w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10))
w.WriteHeader(200) w.WriteHeader(200)
for { for {
buf := make([]byte, 1048576) buf := make([]byte, 1048576)
n, err := fpdst.Read(buf) n, err := fpdst.Read(buf)
if n != 0 {
ns, err := w.Write(buf[0:n])
transferData += ns
if err != nil {
starlog.Errorln("Transfer Error:", err)
}
}
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
break break
} }
break return
} }
w.Write(buf[0:n])
} }
} else { } else {
w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10)) w.Header().Set("Content-Length", strconv.FormatInt(fpinfo.Size(), 10))
@ -260,11 +202,7 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
return return
} }
if rangeEnd == -1 { if rangeEnd == -1 {
ns, err := w.Write(buf[0:n]) w.Write(buf[0:n])
transferData += ns
if err != nil {
starlog.Errorln("Transfer Error:", err)
}
} else { } else {
if count > rangeEnd { if count > rangeEnd {
break break
@ -273,34 +211,19 @@ func httplisten(w http.ResponseWriter, r *http.Request) {
w.Write(buf[0 : rangeEnd-count+1]) w.Write(buf[0 : rangeEnd-count+1])
break break
} else { } else {
ns, err := w.Write(buf[0:n]) w.Write(buf[0:n])
transferData += ns
if err != nil {
starlog.Errorln("Transfer Error:", err)
}
count += int64(n) count += int64(n)
} }
} }
} }
fmt.Println(fpinfo.Name(), "客户端下载已结束")
} }
var tani string
tani = fmt.Sprintf("%v Byte", transferData)
if f64 := float64(transferData) / 1024; f64 > 1 {
tani = fmt.Sprintf("%v KB", f64)
if f64 = float64(f64) / 1024; f64 > 1 {
tani = fmt.Sprintf("%v MB", f64)
if f64 = float64(f64) / 1024; f64 > 1 {
tani = fmt.Sprintf("%v GB", f64)
}
}
}
log.Infoln(fpinfo.Name(), "客户端下载已结束,共传输大小:"+tani)
} }
func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot bool) { func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot bool) {
dir, err := ioutil.ReadDir(fullpath) dir, err := ioutil.ReadDir(fullpath)
if err != nil { if err != nil {
starlog.Errorln(err) fmt.Println(err)
w.WriteHeader(403) w.WriteHeader(403)
w.Write([]byte("<h1>May Cannot Access!</h1>")) w.Write([]byte("<h1>May Cannot Access!</h1>"))
} }
@ -329,3 +252,10 @@ func ReadFolder(w http.ResponseWriter, r *http.Request, fullpath string, isroot
w.Write([]byte("</pre>\n</html>")) w.Write([]byte("</pre>\n</html>"))
return return
} }
func init() {
httpcmd.Flags().StringVarP(&port, "port", "p", "80", "监听端口")
httpcmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听ip")
httpcmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
httpcmd.Flags().BoolVarP(&up, "upload", "u", false, "是否开启文件上传")
Maincmd.AddCommand(httpcmd)
}

View File

@ -28,13 +28,13 @@ var imgMirrorCmd = &cobra.Command{
Long: "图像镜像翻转<水平>", Long: "图像镜像翻转<水平>",
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
starlog.Errorln("请指定需要转换的图像!") starlog.StdPrintln(0, starlog.RED, "请指定需要转换的图像!")
return return
} }
for _, v := range args { for _, v := range args {
img, err := OpenImage(v) img, err := OpenImage(v)
if err != nil { if err != nil {
starlog.Errorln(err, v) starlog.StdPrintln(0, starlog.RED, err, v)
continue continue
} }
size := img.Bounds() size := img.Bounds()
@ -45,7 +45,7 @@ var imgMirrorCmd = &cobra.Command{
} }
} }
if err := SavePhoto(v, nimg); err != nil { if err := SavePhoto(v, nimg); err != nil {
starlog.Errorln(err, v) starlog.StdPrintln(0, starlog.RED, err, v)
continue continue
} else { } else {
fmt.Println(v, "转换已完成!") fmt.Println(v, "转换已完成!")
@ -61,18 +61,18 @@ var imgAlpha = &cobra.Command{
Long: "设置alpha通道透明度", Long: "设置alpha通道透明度",
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
starlog.Errorln("请指定需要转换的图像!") starlog.StdPrintln(0, starlog.RED, "请指定需要转换的图像!")
return return
} }
for _, v := range args { for _, v := range args {
img, err := OpenImage(v) img, err := OpenImage(v)
if err != nil { if err != nil {
starlog.Errorln(err, v) starlog.StdPrintln(0, starlog.RED, err, v)
continue continue
} }
img = SetAlpha(img, 4) img = SetAlpha(img, 4)
if err := SavePhoto(v, img); err != nil { if err := SavePhoto(v, img); err != nil {
starlog.Errorln(err, v) starlog.StdPrintln(0, starlog.RED, err, v)
continue continue
} else { } else {
fmt.Println(v, "转换已完成!") fmt.Println(v, "转换已完成!")

View File

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

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"net" "net"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -669,23 +668,21 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
}() }()
} }
return return
} else { }
isRun := false }()
go func() {
if isconn {
buf := make([]byte, 7) buf := make([]byte, 7)
if strings.Split(conn.RemoteAddr().String(), ":")[0] == strings.Split(trueconn.RemoteAddr().String(), ":")[0] { n, err := conn.Read(buf)
isRun = true if n != 7 || err != nil {
n, err := conn.Read(buf) conn.Close()
if n != 7 || err != nil { return
conn.Close()
return
}
} }
if string(buf) == "v%2^f&K" { if string(buf) == "v%2^f&K" {
fmt.Println("穿透客户端已建立新连接") fmt.Println("穿透客户端已建立新连接")
if len(waitconn) != 0 { if len(waitconn) != 0 {
if waitconn[0].Msg != nil { conn.Write(waitconn[0].Msg)
conn.Write(waitconn[0].Msg)
}
waitconn[0].Msg = []byte{} 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)
@ -699,12 +696,7 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
} }
} else { } else {
fmt.Println("链接已加入等待列表") fmt.Println("链接已加入等待列表")
var tcpnats tcpnat tcpnats := tcpnat{Msg: buf, Conn: conn, Date: time.Now().Unix()}
if isRun {
tcpnats = tcpnat{Msg: buf, Conn: conn, Date: time.Now().Unix()}
} else {
tcpnats = tcpnat{Msg: nil, Conn: conn, Date: time.Now().Unix()}
}
waitconn = append(waitconn, tcpnats) waitconn = append(waitconn, tcpnats)
if trueconn == nil { if trueconn == nil {
isconn = false isconn = false

View File

@ -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.Errorln("源文件不存在") starlog.StdPrintln(0, starlog.RED, "源文件不存在")
this.Help() this.Help()
return return
} }
if num == 0 { if num == 0 {
starlog.Errorln("参数num不合法", "red") 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.Errorln(err.Error) starlog.StdPrintln(0, starlog.RED, err.Error)
} }
}, },

View File

@ -14,7 +14,6 @@ import (
"time" "time"
"b612.me/starainrt" "b612.me/starainrt"
"b612.me/starlog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -24,8 +23,7 @@ var tcpcmd = &cobra.Command{
Short: "发送并监听tcp数据包", Short: "发送并监听tcp数据包",
Long: "发送并监听tcp数据包", Long: "发送并监听tcp数据包",
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
r, _ := this.Flags().GetBool("recvonly") if len(args) != 1 {
if len(args) != 1 && !r {
fmt.Println("请指定远程tcp地址") fmt.Println("请指定远程tcp地址")
return return
} }
@ -53,19 +51,19 @@ var tcpcmd = &cobra.Command{
fmt.Printf("Error Connect From %s : %s\n", conn.RemoteAddr(), err.Error()) fmt.Printf("Error Connect From %s : %s\n", conn.RemoteAddr(), err.Error())
continue continue
} }
starlog.Infof("Accept Connect From %s\n", conn.RemoteAddr()) fmt.Printf("Accept Connect From %s\n", conn.RemoteAddr())
go func(conns *net.TCPConn) { go func(conns *net.TCPConn) {
for { for {
buf := make([]byte, 204800) buf := make([]byte, 204800)
n, err := conns.Read(buf) n, err := conns.Read(buf)
if err != nil { if err != nil {
starlog.Infof("Error from %s Where Message=%s\n", conns.RemoteAddr(), err.Error()) fmt.Printf("Error from %s Where Message=%s\n", conns.RemoteAddr(), err.Error())
conn.Close() conn.Close()
return return
} }
starlog.Infof("Receive Msg From %s : %s\n", conns.RemoteAddr(), string(buf[0:n])) fmt.Printf("Receive Msg From %s : %s\n", conns.RemoteAddr(), string(buf[0:n]))
if b { if b {
starlog.Infof("%#v", buf[0:n]) fmt.Println(buf[0:n])
} }
} }
@ -73,62 +71,56 @@ var tcpcmd = &cobra.Command{
} }
}() }()
} }
if !r { mytcp, err := net.DialTimeout("tcp", args[0], time.Second*15)
mytcp, err := net.DialTimeout("tcp", args[0], time.Second*15) if err != nil {
if err != nil { fmt.Println(err)
fmt.Println(err) if s {
if s {
for {
time.Sleep(time.Second * 10)
}
}
return
}
defer mytcp.Close()
fmt.Println("TCP连接已建立")
go func() {
var err error
for { for {
txt := starainrt.MessageBox("", "") time.Sleep(time.Second * 10)
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
}
} }
}() }
return
}
defer mytcp.Close()
fmt.Println("TCP连接已建立")
go func() {
var err error
for { for {
buf := make([]byte, 204800) txt := starainrt.MessageBox("", "")
n, err := mytcp.Read(buf) 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 { if err != nil {
starlog.Errorf("Error from %s Where Message=%s\n", mytcp.RemoteAddr().String(), err.Error()) fmt.Printf("Error from %s Where Message=%s\n", mytcp.RemoteAddr().String(), err.Error())
return 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 { for {
time.Sleep(time.Second) buf := make([]byte, 204800)
n, err := mytcp.Read(buf)
if err != nil {
fmt.Printf("Error from %s Where Message=%s\n", mytcp.RemoteAddr().String(), err.Error())
return
}
fmt.Printf("Receive Msg From %s : %s\n", mytcp.RemoteAddr().String(), string(buf[0:n]))
if b {
fmt.Println(buf[0:n])
} }
} }
}, },
@ -149,7 +141,6 @@ func init() {
tcpsendcmd.Flags().StringP("regexp", "r", "", "正则匹配字符串") tcpsendcmd.Flags().StringP("regexp", "r", "", "正则匹配字符串")
tcprecvcmd.Flags().StringP("port", "p", "1127", "本地监听端口") tcprecvcmd.Flags().StringP("port", "p", "1127", "本地监听端口")
tcprecvcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip") tcprecvcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip")
tcpcmd.Flags().BoolP("recvonly", "r", false, "仅接收udp包")
tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd) tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd)
} }

View File

@ -5,10 +5,8 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
"time"
"b612.me/starainrt" "b612.me/starainrt"
"b612.me/starlog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -18,9 +16,7 @@ var udpcmd = &cobra.Command{
Short: "发送并监听udp数据包", Short: "发送并监听udp数据包",
Long: "发送并监听udp数据包", Long: "发送并监听udp数据包",
Run: func(this *cobra.Command, args []string) { Run: func(this *cobra.Command, args []string) {
var raddr *net.UDPAddr if len(args) != 1 {
r, _ := this.Flags().GetBool("recvonly")
if len(args) != 1 && !r {
fmt.Println("请指定远程udp地址") fmt.Println("请指定远程udp地址")
return return
} }
@ -29,9 +25,7 @@ var udpcmd = &cobra.Command{
s, _ := this.Flags().GetBool("local") s, _ := this.Flags().GetBool("local")
b, _ := this.Flags().GetBool("byte") b, _ := this.Flags().GetBool("byte")
laddr, err := net.ResolveUDPAddr("udp", a+":"+l) laddr, err := net.ResolveUDPAddr("udp", a+":"+l)
if !r { raddr, err := net.ResolveUDPAddr("udp", args[0])
raddr, err = net.ResolveUDPAddr("udp", args[0])
}
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
@ -48,66 +42,60 @@ var udpcmd = &cobra.Command{
buf := make([]byte, 204800) buf := make([]byte, 204800)
n, addr, err := udplisten.ReadFromUDP(buf) n, addr, err := udplisten.ReadFromUDP(buf)
if err != nil { if err != nil {
starlog.Errorln("Error from %s Where Message=%s\n", addr.String(), err.Error()) fmt.Printf("Error from %s Where Message=%s\n", addr.String(), err.Error())
continue continue
} }
starlog.Infof("Receive Msg From %s : %s\n", addr.String(), string(buf[0:n])) fmt.Printf("Receive Msg From %s : %s\n", addr.String(), string(buf[0:n]))
if b { if b {
starlog.Infof("%#v\n", buf[0:n]) fmt.Println(buf[0:n])
} }
} }
}() }()
} }
if !r { myudp, err := net.DialUDP("udp", nil, raddr)
myudp, err := net.DialUDP("udp", nil, raddr) if err != nil {
if err != nil { fmt.Println(err)
fmt.Println(err) return
return }
} fmt.Println("UDP虚拟连接已建立")
fmt.Println("UDP虚拟连接已建立") go func() {
go func() { var err error
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 { for {
buf := make([]byte, 204800) txt := starainrt.MessageBox("", "")
n, err := myudp.Read(buf) 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 { if err != nil {
starlog.Infof("Error from %s Where Message=%s\n", myudp.RemoteAddr().String(), err.Error()) fmt.Printf("Error from %s Where Message=%s\n", myudp.RemoteAddr().String(), err.Error())
return 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 { for {
time.Sleep(time.Second) buf := make([]byte, 204800)
n, err := myudp.Read(buf)
if err != nil {
fmt.Printf("Error from %s Where Message=%s\n", myudp.RemoteAddr().String(), err.Error())
return
}
fmt.Printf("Receive Msg From %s : %s\n", myudp.RemoteAddr().String(), string(buf[0:n]))
if b {
fmt.Println(buf[0:n])
} }
} }
}, },
@ -123,5 +111,4 @@ func init() {
udpcmd.Flags().StringP("port", "p", "1127", "本地监听端口") udpcmd.Flags().StringP("port", "p", "1127", "本地监听端口")
udpcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip") udpcmd.Flags().StringP("addr", "a", "0.0.0.0", "本地监听ip")
udpcmd.Flags().BoolP("local", "s", false, "启动本地监听") udpcmd.Flags().BoolP("local", "s", false, "启动本地监听")
udpcmd.Flags().BoolP("recvonly", "r", false, "仅接收udp包")
} }

View File

@ -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.Errorln("ヴィクトリカだけが使えるよ") 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.Errorln(err) starlog.StdPrintln(0, starlog.RED, err)
return return
} }
}, },