commit
eb9f7b8031
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Victorique/vtqe/tools"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tools.Maincmd.Execute()
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
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.Println("ERROR PATH", "red", "b")
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
cryp := new(starainrt.StarCrypto)
|
||||
err := cryp.Attach(src, dst, out)
|
||||
if err != nil {
|
||||
starlog.Println(err.Error, "red", "b")
|
||||
} else {
|
||||
fmt.Println("完成")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
attachcmd.Flags().StringP("src", "s", "", "源文件路径")
|
||||
attachcmd.Flags().StringP("dst", "d", "", "目标文件路径")
|
||||
attachcmd.Flags().StringP("out", "o", "", "输出文件路径")
|
||||
Maincmd.AddCommand(attachcmd)
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version string = "0.1.12"
|
||||
|
||||
var Maincmd = &cobra.Command{
|
||||
Use: "",
|
||||
Short: "Victorique's Small Smart Toolkit",
|
||||
Long: "Victorique's Small Smart Toolkit",
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.MousetrapHelpText = ""
|
||||
Maincmd.Version = Version
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
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.Println("ERROR PATH", "red", "b")
|
||||
this.Help()
|
||||
return
|
||||
}
|
||||
cryp := new(starainrt.StarCrypto)
|
||||
err := cryp.Detach(src, num, dst, out)
|
||||
if err != nil {
|
||||
starlog.Println(err.Error, "red", "b")
|
||||
} 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)
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
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 ""
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,149 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
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]")
|
||||
})
|
||||
})
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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")
|
||||
i, _ := this.Flags().GetString("identify")
|
||||
k, _ := this.Flags().GetString("password")
|
||||
p, _ := this.Flags().GetInt("port")
|
||||
b, _ := this.Flags().GetInt("buffer")
|
||||
var user, host string
|
||||
var err error
|
||||
if len(args) != 3 {
|
||||
fmt.Println("sftp <[user@]Host> <Local> <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连接……")
|
||||
client, err := sshd.Connect(user, k, host, i, p, []string{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
fmt.Println("已连接上……")
|
||||
sftp, err := sshd.CreateSftp(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)
|
||||
}
|
||||
}
|
||||
if !d {
|
||||
err = sshd.FtpTransferOutFunc(args[1], args[2], b, shell, sftp)
|
||||
} else {
|
||||
err = sshd.FtpTransferInFunc(args[1], args[2], b, shell, sftp)
|
||||
}
|
||||
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().IntP("port", "p", 22, "登录端口")
|
||||
sftpcmd.Flags().IntP("buffer", "b", 10240, "buffer大小")
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"strconv"
|
||||
|
||||
"victorique/vtqe/tools/ping"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
showVersion bool
|
||||
version string
|
||||
gitCommit string
|
||||
counter int
|
||||
timeout string
|
||||
interval string
|
||||
sigs chan os.Signal
|
||||
|
||||
httpMode bool
|
||||
httpHead bool
|
||||
httpPost bool
|
||||
httpUA string
|
||||
|
||||
dnsServer []string
|
||||
)
|
||||
|
||||
var tcpingcmd = &cobra.Command{
|
||||
Use: "tcping",
|
||||
Short: "tcp ping",
|
||||
Long: "进行Tcping",
|
||||
Example: `
|
||||
1. ping over tcp
|
||||
> tcping google.com
|
||||
2. ping over tcp with custom port
|
||||
> tcping google.com 443
|
||||
3. ping over http
|
||||
> tcping -H google.com
|
||||
4. ping with URI schema
|
||||
> tcping http://hui.lu
|
||||
`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
sigs = make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
if showVersion {
|
||||
fmt.Printf("version: %s\n", version)
|
||||
fmt.Printf("git: %s\n", gitCommit)
|
||||
return
|
||||
}
|
||||
if len(args) != 2 && len(args) != 1 {
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
host := args[0]
|
||||
|
||||
var (
|
||||
err error
|
||||
port int
|
||||
schema string
|
||||
)
|
||||
if len(args) == 2 {
|
||||
port, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
fmt.Println("端口应当为Int类型")
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
schema = ping.TCP.String()
|
||||
} else {
|
||||
var matched bool
|
||||
schema, host, port, matched = ping.CheckURI(host)
|
||||
if !matched {
|
||||
fmt.Println("不是一个合法的URI")
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
}
|
||||
var timeoutDuration time.Duration
|
||||
if res, err := strconv.Atoi(timeout); err == nil {
|
||||
timeoutDuration = time.Duration(res) * time.Millisecond
|
||||
} else {
|
||||
timeoutDuration, err = time.ParseDuration(timeout)
|
||||
if err != nil {
|
||||
fmt.Println("parse timeout failed", err)
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var intervalDuration time.Duration
|
||||
if res, err := strconv.Atoi(interval); err == nil {
|
||||
intervalDuration = time.Duration(res) * time.Millisecond
|
||||
} else {
|
||||
intervalDuration, err = time.ParseDuration(interval)
|
||||
if err != nil {
|
||||
fmt.Println("parse interval failed", err)
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
}
|
||||
var protocol ping.Protocol
|
||||
if httpMode {
|
||||
protocol = ping.HTTP
|
||||
} else {
|
||||
protocol, err = ping.NewProtocol(schema)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(dnsServer) != 0 {
|
||||
ping.UseCustomeDNS(dnsServer)
|
||||
}
|
||||
|
||||
parseHost := ping.FormatIP(host)
|
||||
target := ping.Target{
|
||||
Timeout: timeoutDuration,
|
||||
Interval: intervalDuration,
|
||||
Host: parseHost,
|
||||
Port: port,
|
||||
Counter: counter,
|
||||
Protocol: protocol,
|
||||
}
|
||||
var pinger ping.Pinger
|
||||
switch protocol {
|
||||
case ping.TCP:
|
||||
pinger = ping.NewTCPing()
|
||||
case ping.HTTP, ping.HTTPS:
|
||||
var httpMethod string
|
||||
switch {
|
||||
case httpHead:
|
||||
httpMethod = "HEAD"
|
||||
case httpPost:
|
||||
httpMethod = "POST"
|
||||
default:
|
||||
httpMethod = "GET"
|
||||
}
|
||||
pinger = ping.NewHTTPing(httpMethod)
|
||||
default:
|
||||
fmt.Printf("schema: %s not support\n", schema)
|
||||
cmd.Usage()
|
||||
return
|
||||
}
|
||||
pinger.SetTarget(&target)
|
||||
pingerDone := pinger.Start()
|
||||
select {
|
||||
case <-pingerDone:
|
||||
break
|
||||
case <-sigs:
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Println(pinger.Result())
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
tcpingcmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show the version and exit")
|
||||
tcpingcmd.Flags().IntVarP(&counter, "counter", "c", 4, "ping的次数")
|
||||
tcpingcmd.Flags().StringVarP(&timeout, "timeout", "T", "1s", `超时时间, 单位为 "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
|
||||
tcpingcmd.Flags().StringVarP(&interval, "interval", "I", "1s", `ping间隔时间, 单位为 "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
|
||||
|
||||
tcpingcmd.Flags().BoolVarP(&httpMode, "http", "H", false, `Use "HTTP" mode. will ignore URI Schema, force to http`)
|
||||
tcpingcmd.Flags().BoolVar(&httpHead, "head", false, `使用http head模式`)
|
||||
tcpingcmd.Flags().BoolVar(&httpPost, "post", false, `使用http post模式`)
|
||||
tcpingcmd.Flags().StringVar(&httpUA, "user-agent", "tcping", `自定义UA`)
|
||||
|
||||
tcpingcmd.Flags().StringArrayVarP(&dnsServer, "dns-server", "D", nil, `使用自定义DNS服务器`)
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
Maincmd.AddCommand(tcpingcmd)
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
// +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)
|
||||
}
|
Loading…
Reference in New Issue