V2.7 增加agent执行器

This commit is contained in:
georgehao
2019-07-03 22:31:27 +08:00
parent c3a89e9243
commit 37fb659c4e
48 changed files with 2832 additions and 513 deletions

15
libs/convert.go Normal file
View File

@@ -0,0 +1,15 @@
/************************************************************
** @Description: convert
** @Author: george hao
** @Date: 2019-06-28 09:34
** @Last Modified by: george hao
** @Last Modified time: 2019-06-28 09:34
*************************************************************/
package libs
import "fmt"
//查看数据类型
func DataType(i interface{}) string {
return fmt.Sprintf("%T", i)
}

View File

@@ -9,10 +9,10 @@ package libs
import (
"github.com/pkg/errors"
"io/ioutil"
"strings"
"net/http"
"io"
"io/ioutil"
"net/http"
"strings"
)
func HttpGet(url string, param map[string]string) (string, error) {

52
libs/ip.go Normal file
View File

@@ -0,0 +1,52 @@
/************************************************************
** @Description: ip
** @Author: george hao
** @Date: 2019-06-27 09:20
** @Last Modified by: george hao
** @Last Modified time: 2019-06-27 09:20
*************************************************************/
package libs
import (
"io/ioutil"
"net"
"net/http"
)
func GetHostIp(IpType int) string {
if IpType == 0 {
return LocalIp()
} else {
return PublicIp()
}
}
func LocalIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func PublicIp() string {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return ""
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
//buf := new(bytes.Buffer)
//buf.ReadFrom(resp.Body)
//s := buf.String()
return string(content)
}

150
libs/server.go Normal file
View File

@@ -0,0 +1,150 @@
/************************************************************
** @Description: server
** @Author: george hao
** @Date: 2019-07-02 11:16
** @Last Modified by: george hao
** @Last Modified time: 2019-07-02 11:16
*************************************************************/
package libs
import (
"fmt"
"github.com/george518/PPGo_Job/common"
"github.com/george518/PPGo_Job/models"
"github.com/linxiaozhi/go-telnet"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"strconv"
"strings"
"time"
)
func RemoteCommandByTelnetPassword(servers *models.TaskServer) error {
addr := fmt.Sprintf("%s:%d", servers.ServerIp, servers.Port)
conn, err := gote.DialTimeout("tcp", addr, time.Second*10)
if err != nil {
return err
}
defer conn.Close()
buf := make([]byte, 4096)
_, err = conn.Read(buf)
if err != nil {
return err
}
_, err = conn.Write([]byte(servers.ServerAccount + "\r\n"))
if err != nil {
return err
}
_, err = conn.Read(buf)
if err != nil {
return err
}
_, err = conn.Write([]byte(servers.Password + "\r\n"))
if err != nil {
return err
}
_, err = conn.Read(buf)
if err != nil {
return err
}
str := GbkAsUtf8(string(buf[:]))
if strings.Contains(str, ">") {
return nil
}
return errors.Errorf("连接失败!")
}
func RemoteCommandByPassword(servers *models.TaskServer) error {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
)
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(servers.Password))
clientConfig = &ssh.ClientConfig{
User: servers.ServerAccount,
Auth: auth,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 5 * time.Second,
}
addr = fmt.Sprintf("%s:%d", servers.ServerIp, servers.Port)
client, err := ssh.Dial("tcp", addr, clientConfig)
if err == nil {
defer client.Close()
}
return err
}
func RemoteCommandByKey(servers *models.TaskServer) error {
key, err := ioutil.ReadFile(servers.PrivateKeySrc)
if err != nil {
return err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return err
}
addr := fmt.Sprintf("%s:%d", servers.ServerIp, servers.Port)
config := &ssh.ClientConfig{
User: servers.ServerAccount,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
//HostKeyCallback: ssh.FixedHostKey(hostKey),
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 5 * time.Second,
}
client, err := ssh.Dial("tcp", addr, config)
if err == nil {
client.Close()
}
return err
}
func RemoteAgent(servers *models.TaskServer) error {
conn, err := net.Dial("tcp", servers.ServerIp+":"+strconv.Itoa(servers.Port))
if err != nil {
return err
}
defer conn.Close()
client := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn))
var reply *common.RpcResult
defer client.Close()
ping := "ping"
err = client.Call("RpcTask.HeartBeat", ping, &reply)
if err != nil {
return err
}
if reply.Status == 200 {
return nil
} else {
return fmt.Errorf("链接错误:%v", reply.Message)
}
}

View File

@@ -11,6 +11,7 @@ package libs
import (
"crypto/md5"
"fmt"
"github.com/axgle/mahonia"
"math/rand"
"regexp"
"time"
@@ -67,3 +68,16 @@ func GetRandomString(lens int) string {
}
return string(result)
}
func GbkAsUtf8(str string) string {
srcDecoder := mahonia.NewDecoder("gbk")
desDecoder := mahonia.NewDecoder("utf-8")
resStr := srcDecoder.ConvertString(str)
_, resBytes, _ := desDecoder.Translate([]byte(resStr), true)
return string(resBytes)
}
//任务识别码
func JobKey(taskId, serverId int) int {
return taskId*10000000 + serverId
}