kpl / internal /app /kpl.go
aurorax-neo's picture
init
a4468f1
package app
import (
"context"
"fmt"
"github.com/aurorax-neo/tls_client_httpi"
"github.com/aurorax-neo/tls_client_httpi/tls_client"
"github.com/bogdanfinn/tls-client/profiles"
"golang.org/x/crypto/ssh"
"kpl/pkg/logx"
"time"
)
func AsyncTimingTask(nanosecond time.Duration, fun func()) {
go func() {
timerChan := time.After(nanosecond)
// 使用for循环阻塞等待定时器的信号
for {
// 通过select语句监听定时器通道和其他事件
select {
case <-timerChan:
fun()
// 重新设置定时器,以便下一次执行
timerChan = time.After(nanosecond)
}
time.Sleep(time.Millisecond * 100)
}
}()
}
// DoGetRequest 定义一个函数,用于发送GET请求
func DoGetRequest(ctx context.Context, rawUrl string, proxy string) {
opts := tls_client.NewClientOptions(5, profiles.Chrome_124)
client := tls_client.NewClient(opts)
_ = client.SetProxy(proxy)
res, err := client.Request(tls_client_httpi.GET, rawUrl, nil, nil, nil)
if err != nil {
logx.WithContext(ctx).Error(err)
return
}
logx.WithContext(ctx).Info(fmt.Sprint("GET ", rawUrl, " ", res.Status))
}
// KplServ00 serv00
func KplServ00(ctx context.Context, user string, password string, host string, port int, cmd string) {
// SSH 连接配置
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
answers = make([]string, len(questions))
for i := range questions {
answers[i] = password
}
return answers, nil
}),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// 连接到远程服务器
client, err := ssh.Dial("tcp", fmt.Sprint(host, ":", port), sshConfig)
if err != nil {
logx.WithContext(ctx).Error(err)
return
}
defer func(client *ssh.Client) {
_ = client.Close()
}(client)
// 创建一个会话
session1, err := client.NewSession()
if err != nil {
logx.WithContext(ctx).Error(err)
return
}
// 关闭会话
defer func(session *ssh.Session) {
_ = session.Close()
}(session1)
// 执行命令
output1, err := session1.CombinedOutput(cmd)
if err != nil {
errMsg := fmt.Sprintf("%s@%s:%d - 执行命令失败: \n%s", user, host, port, err)
logx.WithContext(ctx).Error(errMsg)
} else {
msg := fmt.Sprintf("%s@%s:%d - 执行命令成功: \n%s", user, host, port, output1)
logx.WithContext(ctx).Info(msg)
}
}