Spaces:
Running
Running
aurorax-neo
commited on
Commit
•
46acce2
1
Parent(s):
6ad1de1
update
Browse files- app/app.go +0 -57
- app/http.go +0 -44
- app/kpl.go +0 -92
app/app.go
DELETED
@@ -1,57 +0,0 @@
|
|
1 |
-
package app
|
2 |
-
|
3 |
-
import (
|
4 |
-
"context"
|
5 |
-
"kpl/internal/conf"
|
6 |
-
"kpl/pkg/logx"
|
7 |
-
"kpl/pkg/tools"
|
8 |
-
"os"
|
9 |
-
"time"
|
10 |
-
)
|
11 |
-
|
12 |
-
func Run(ctx context.Context) {
|
13 |
-
cleanUp := tools.Stack{}
|
14 |
-
ctx = logx.TagContext(ctx, "initial")
|
15 |
-
logx.Init(ctx)
|
16 |
-
|
17 |
-
// config
|
18 |
-
conf.Init(ctx)
|
19 |
-
|
20 |
-
// http
|
21 |
-
cleanUp.Push(Start(ctx))
|
22 |
-
|
23 |
-
// 提示 保活列表
|
24 |
-
logx.WithContext(ctx).Info("hgUrls: ")
|
25 |
-
for i, url := range conf.CONF.HgUrls {
|
26 |
-
logx.WithContext(ctx).Info(i, ": ", url)
|
27 |
-
}
|
28 |
-
|
29 |
-
logx.WithContext(ctx).Info("serv00s: ")
|
30 |
-
for i, serv00 := range conf.CONF.Serv00s {
|
31 |
-
logx.WithContext(ctx).Info(i, ": ", serv00.Username, "@", serv00.Host, ":", serv00.Port)
|
32 |
-
}
|
33 |
-
|
34 |
-
AsyncTimingTask(time.Duration(conf.CONF.HgIntervalSec)*time.Second, func() {
|
35 |
-
for _, url := range conf.CONF.HgUrls {
|
36 |
-
go DoGetRequest(ctx, url, conf.CONF.Proxy)
|
37 |
-
}
|
38 |
-
})
|
39 |
-
|
40 |
-
AsyncTimingTask(time.Duration(conf.CONF.Serv00IntervalSec)*time.Second, func() {
|
41 |
-
for _, serv00 := range conf.CONF.Serv00s {
|
42 |
-
go KplServ00(ctx, serv00.Username, serv00.Password, serv00.Host, serv00.Port, serv00.Cmd)
|
43 |
-
}
|
44 |
-
})
|
45 |
-
|
46 |
-
// Handle signals
|
47 |
-
{
|
48 |
-
exitCode := 1
|
49 |
-
exitCode = tools.HandleSignals(exitCode)
|
50 |
-
ctx = logx.TagContext(ctx, "cleanup")
|
51 |
-
for cleanUp.Next() {
|
52 |
-
cleanUp.Pop()(ctx)
|
53 |
-
}
|
54 |
-
time.Sleep(time.Second)
|
55 |
-
os.Exit(exitCode)
|
56 |
-
}
|
57 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/http.go
DELETED
@@ -1,44 +0,0 @@
|
|
1 |
-
package app
|
2 |
-
|
3 |
-
import (
|
4 |
-
"context"
|
5 |
-
"errors"
|
6 |
-
"github.com/gin-gonic/gin"
|
7 |
-
"kpl/pkg/logx"
|
8 |
-
"net/http"
|
9 |
-
"time"
|
10 |
-
)
|
11 |
-
|
12 |
-
func Start(ctx context.Context) func(ctx context.Context) {
|
13 |
-
gin.SetMode(gin.ReleaseMode)
|
14 |
-
|
15 |
-
e := gin.New()
|
16 |
-
|
17 |
-
e.Use(gin.Recovery())
|
18 |
-
|
19 |
-
e.GET("/", func(ctx *gin.Context) {
|
20 |
-
ctx.String(http.StatusOK, "Hello World!")
|
21 |
-
})
|
22 |
-
|
23 |
-
srv := &http.Server{
|
24 |
-
Addr: ":3040",
|
25 |
-
Handler: e,
|
26 |
-
}
|
27 |
-
|
28 |
-
go func() {
|
29 |
-
logx.WithContext(ctx).Infof("http server initialized successfully at \u001B[35m%v\u001B[0m", srv.Addr)
|
30 |
-
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
31 |
-
panic(err)
|
32 |
-
}
|
33 |
-
}()
|
34 |
-
|
35 |
-
return func(ctx context.Context) {
|
36 |
-
logx.WithContext(ctx).Info("http server shutdown.")
|
37 |
-
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
|
38 |
-
srv.SetKeepAlivesEnabled(false)
|
39 |
-
if err := srv.Shutdown(ctx); err != nil {
|
40 |
-
logx.WithContext(ctx).Error(err.Error())
|
41 |
-
}
|
42 |
-
cancel()
|
43 |
-
}
|
44 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/kpl.go
DELETED
@@ -1,92 +0,0 @@
|
|
1 |
-
package app
|
2 |
-
|
3 |
-
import (
|
4 |
-
"context"
|
5 |
-
"fmt"
|
6 |
-
"github.com/aurorax-neo/tls_client_httpi"
|
7 |
-
"github.com/aurorax-neo/tls_client_httpi/tls_client"
|
8 |
-
"github.com/bogdanfinn/tls-client/profiles"
|
9 |
-
"golang.org/x/crypto/ssh"
|
10 |
-
"kpl/pkg/logx"
|
11 |
-
"time"
|
12 |
-
)
|
13 |
-
|
14 |
-
func AsyncTimingTask(nanosecond time.Duration, fun func()) {
|
15 |
-
go func() {
|
16 |
-
timerChan := time.After(nanosecond)
|
17 |
-
// 使用for循环阻塞等待定时器的信号
|
18 |
-
for {
|
19 |
-
// 通过select语句监听定时器通道和其他事件
|
20 |
-
select {
|
21 |
-
case <-timerChan:
|
22 |
-
fun()
|
23 |
-
// 重新设置定时器,以便下一次执行
|
24 |
-
timerChan = time.After(nanosecond)
|
25 |
-
}
|
26 |
-
time.Sleep(time.Millisecond * 100)
|
27 |
-
}
|
28 |
-
}()
|
29 |
-
}
|
30 |
-
|
31 |
-
// DoGetRequest 定义一个函数,用于发送GET请求
|
32 |
-
func DoGetRequest(ctx context.Context, rawUrl string, proxy string) {
|
33 |
-
opts := tls_client.NewClientOptions(5, profiles.Chrome_124)
|
34 |
-
client := tls_client.NewClient(opts)
|
35 |
-
_ = client.SetProxy(proxy)
|
36 |
-
res, err := client.Request(tls_client_httpi.GET, rawUrl, nil, nil, nil)
|
37 |
-
if err != nil {
|
38 |
-
logx.WithContext(ctx).Error(err)
|
39 |
-
return
|
40 |
-
}
|
41 |
-
logx.WithContext(ctx).Info(fmt.Sprint("GET ", rawUrl, " ", res.Status))
|
42 |
-
}
|
43 |
-
|
44 |
-
// KplServ00 serv00
|
45 |
-
func KplServ00(ctx context.Context, user string, password string, host string, port int, cmd string) {
|
46 |
-
// SSH 连接配置
|
47 |
-
sshConfig := &ssh.ClientConfig{
|
48 |
-
User: user,
|
49 |
-
Auth: []ssh.AuthMethod{
|
50 |
-
ssh.Password(password),
|
51 |
-
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
|
52 |
-
answers = make([]string, len(questions))
|
53 |
-
for i := range questions {
|
54 |
-
answers[i] = password
|
55 |
-
}
|
56 |
-
return answers, nil
|
57 |
-
}),
|
58 |
-
},
|
59 |
-
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
60 |
-
}
|
61 |
-
|
62 |
-
// 连接到远程服务器
|
63 |
-
client, err := ssh.Dial("tcp", fmt.Sprint(host, ":", port), sshConfig)
|
64 |
-
if err != nil {
|
65 |
-
logx.WithContext(ctx).Error(err)
|
66 |
-
return
|
67 |
-
}
|
68 |
-
defer func(client *ssh.Client) {
|
69 |
-
_ = client.Close()
|
70 |
-
}(client)
|
71 |
-
|
72 |
-
// 创建一个会话
|
73 |
-
session1, err := client.NewSession()
|
74 |
-
if err != nil {
|
75 |
-
logx.WithContext(ctx).Error(err)
|
76 |
-
return
|
77 |
-
}
|
78 |
-
// 关闭会话
|
79 |
-
defer func(session *ssh.Session) {
|
80 |
-
_ = session.Close()
|
81 |
-
}(session1)
|
82 |
-
|
83 |
-
// 执行命令
|
84 |
-
output1, err := session1.CombinedOutput(cmd)
|
85 |
-
if err != nil {
|
86 |
-
errMsg := fmt.Sprintf("%s@%s:%d - 执行命令失败: \n%s", user, host, port, err)
|
87 |
-
logx.WithContext(ctx).Error(errMsg)
|
88 |
-
} else {
|
89 |
-
msg := fmt.Sprintf("%s@%s:%d - 执行命令成功: \n%s", user, host, port, output1)
|
90 |
-
logx.WithContext(ctx).Info(msg)
|
91 |
-
}
|
92 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|