Browse Source

feature: CurlServer增加日志机制、完善脚本

kuanglingwei 1 year ago
parent
commit
97e4237cc9

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 /CloudflareServer
 /logs/
 /release/log/
+/lib/

+ 29 - 0
Curl.go

@@ -0,0 +1,29 @@
+package main
+
+import (
+	"cfTest/curlServer"
+	"cfTest/curlServer/pb"
+	"google.golang.org/grpc"
+	"net"
+)
+
+const (
+	curlPort = ":50053"
+)
+
+func main() {
+	curlServer.InitLogger()
+	logger := curlServer.GetLogger()
+
+	lis, err := net.Listen("tcp", curlPort)
+	if err != nil {
+		logger.Error("failed to listen: ", err)
+	}
+
+	s := grpc.NewServer()
+	pb.RegisterCurlWithResolveServer(s, &curlServer.CurlServer{})
+	logger.Println("server listening at ", lis.Addr())
+	if err := s.Serve(lis); err != nil {
+		logger.Error("failed to serve: ", err)
+	}
+}

+ 0 - 28
curl.go

@@ -1,28 +0,0 @@
-package main
-
-import (
-	"cfTest/curl"
-	"cfTest/server"
-	"google.golang.org/grpc"
-	"log"
-	"net"
-)
-
-const (
-	curlPort = ":50053"
-)
-
-func main() {
-
-	lis, err := net.Listen("tcp", curlPort)
-	if err != nil {
-		log.Fatalf("failed to listen: %v", err)
-	}
-
-	s := grpc.NewServer()
-	curl.RegisterCurlWithResolveServer(s, &server.CurlServer{})
-	log.Printf("server listening at %v", lis.Addr())
-	if err := s.Serve(lis); err != nil {
-		log.Fatalf("failed to serve: %v", err)
-	}
-}

+ 154 - 0
curlServer/CurlServer.go

@@ -0,0 +1,154 @@
+package curlServer
+
+import (
+	curl2 "cfTest/curlServer/pb"
+	"context"
+	"github.com/sirupsen/logrus"
+	"gopkg.in/natefinch/lumberjack.v2"
+	"io"
+	"net"
+	"net/http"
+	"runtime"
+	"time"
+)
+
+var logger *logrus.Logger
+
+type CurlServer struct {
+	curl2.UnimplementedCurlWithResolveServer
+}
+
+func (s *CurlServer) CurlWithResolveParam(ctx context.Context, requestCurlObject *curl2.CurlRequest) (*curl2.CurlReply, error) {
+	logger.Println("url Received: ", requestCurlObject.GetUrl())
+	logger.Println("ip Received: ", requestCurlObject.GetIp())
+	logger.Println("UA Received: ", requestCurlObject.GetUA())
+	logger.Println("UseHeadMethod Received: ", requestCurlObject.GetUseHeadMethod())
+
+	// UA
+	var UA = ""
+	if len(requestCurlObject.GetUA()) == 0 {
+		UA = generateDefaultUserAgent()
+	} else {
+		UA = requestCurlObject.GetUA()
+	}
+
+	// Server
+	// 创建一个自定义的 Transport
+	transport := &CustomTransport{
+		Transport: &http.Transport{
+			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
+				dialer := &net.Dialer{
+					Timeout:   30 * time.Second,
+					KeepAlive: 30 * time.Second,
+					DualStack: true,
+				}
+
+				// 修改addr -> 指向IP地址
+				addr = requestCurlObject.GetIp() + ":443"
+				logger.Println("addr = ", addr)
+				conn, err := dialer.DialContext(ctx, network, addr)
+				if err != nil {
+					logger.Error(err.Error())
+					return nil, err
+				}
+				return conn, nil
+			},
+		},
+		// 指定UA
+		UserAgent:     UA,
+		UseHeadMethod: requestCurlObject.GetUseHeadMethod(),
+	}
+
+	// 创建一个使用自定义 Transport 的 HTTP 客户端
+	client := &http.Client{
+		Transport: transport,
+	}
+
+	// 发起 HTTP 请求
+	resp, err := client.Get(requestCurlObject.GetUrl())
+	if err != nil {
+		logger.Error(err.Error())
+		return nil, err
+	}
+	defer resp.Body.Close()
+
+	// 解析响应结果
+	logger.Println("Code = ", resp.StatusCode)
+
+	_, err = io.ReadAll(resp.Body)
+	if err != nil {
+		logger.Error(err.Error())
+		return nil, err
+	}
+	logger.Println("header = ", resp.Header)
+
+	return &curl2.CurlReply{
+		Code:          int32(resp.StatusCode),
+		ContentLength: resp.ContentLength,
+	}, nil
+}
+
+type CustomTransport struct {
+	Transport     http.RoundTripper
+	UserAgent     string
+	UseHeadMethod bool
+}
+
+func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+	req.Header.Set("User-Agent", t.UserAgent)
+	if t.UseHeadMethod {
+		req.Method = http.MethodHead
+	}
+	return t.Transport.RoundTrip(req)
+}
+
+func generateDefaultUserAgent() string {
+	// 获取操作系统名称和版本号
+	osInfo := runtime.GOOS
+	// 获取编程语言和版本号
+	langInfo := "Go/" + runtime.Version()
+	// 获取应用程序运行时的信息
+	appInfo := "Executor/1.0"
+
+	return appInfo + " (" + osInfo + "; " + langInfo + ")"
+}
+
+func InitLogger() {
+	setupLogger()
+	// 在初始化函数中对包级别的变量进行初始化
+	logger = logrus.StandardLogger()
+	logger.Println("启动CurlServer, 初始化日志框架...")
+}
+
+func setupLogger() {
+	// 创建一个新的logrus实例
+	log := logrus.New()
+
+	// 设置日志输出为文件,并使用lumberjack库来实现每天生成一个新的日志文件,并保存旧日志文件
+	log.SetOutput(&lumberjack.Logger{
+		Filename:   "../../logs/CurlServer/" + generateLogFileName(), // 日志文件路径,使用日期占位符%Y-%m-%d
+		MaxSize:    20,                                               // 每个日志文件的最大尺寸,单位:MB
+		MaxBackups: 3,                                                // 最多保留的旧日志文件数
+		MaxAge:     30,                                               // 最多保留的旧日志文件天数(这里设置为30天)
+		LocalTime:  true,                                             // 使用本地时间(默认为UTC时间)
+		Compress:   true,                                             // 是否压缩旧日志文件
+	})
+
+	// 设置日志格式为JSON格式
+	log.SetFormatter(&logrus.JSONFormatter{})
+
+	// 设置日志级别为Debug
+	log.SetLevel(logrus.DebugLevel)
+
+	// 将logrus实例设置为全局的默认日志记录器
+	logrus.StandardLogger().SetOutput(log.Out)
+}
+
+func generateLogFileName() string {
+	currentDate := time.Now().Format("2006-01-02")
+	return "curl_" + currentDate + ".log"
+}
+
+func GetLogger() *logrus.Logger {
+	return logger
+}

+ 21 - 20
curl/curl.pb.go

@@ -4,7 +4,7 @@
 // 	protoc        v4.23.3
 // source: curl.proto
 
-package curl
+package pb
 
 import (
 	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
@@ -98,9 +98,9 @@ type CurlReply struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	Code  int32  `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
-	Body  string `protobuf:"bytes,2,opt,name=body,proto3" json:"body,omitempty"`
-	Error string `protobuf:"bytes,3,opt,name=error,proto3" json:"error,omitempty"`
+	Code          int32  `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
+	ContentLength int64  `protobuf:"varint,2,opt,name=contentLength,proto3" json:"contentLength,omitempty"`
+	Error         string `protobuf:"bytes,3,opt,name=error,proto3" json:"error,omitempty"`
 }
 
 func (x *CurlReply) Reset() {
@@ -142,11 +142,11 @@ func (x *CurlReply) GetCode() int32 {
 	return 0
 }
 
-func (x *CurlReply) GetBody() string {
+func (x *CurlReply) GetContentLength() int64 {
 	if x != nil {
-		return x.Body
+		return x.ContentLength
 	}
-	return ""
+	return 0
 }
 
 func (x *CurlReply) GetError() string {
@@ -166,20 +166,21 @@ var file_curl_proto_rawDesc = []byte{
 	0x02, 0x69, 0x70, 0x12, 0x0e, 0x0a, 0x02, 0x55, 0x41, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
 	0x02, 0x55, 0x41, 0x12, 0x24, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x48, 0x65, 0x61, 0x64, 0x4d, 0x65,
 	0x74, 0x68, 0x6f, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0d, 0x55, 0x73, 0x65, 0x48,
-	0x65, 0x61, 0x64, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x22, 0x49, 0x0a, 0x09, 0x43, 0x75, 0x72,
+	0x65, 0x61, 0x64, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x22, 0x5b, 0x0a, 0x09, 0x43, 0x75, 0x72,
 	0x6c, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01,
-	0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f,
-	0x64, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x14,
-	0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65,
-	0x72, 0x72, 0x6f, 0x72, 0x32, 0x4f, 0x0a, 0x0f, 0x43, 0x75, 0x72, 0x6c, 0x57, 0x69, 0x74, 0x68,
-	0x52, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x12, 0x3c, 0x0a, 0x14, 0x43, 0x75, 0x72, 0x6c, 0x57,
-	0x69, 0x74, 0x68, 0x52, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x12,
-	0x11, 0x2e, 0x63, 0x75, 0x72, 0x6c, 0x2e, 0x43, 0x75, 0x72, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65,
-	0x73, 0x74, 0x1a, 0x0f, 0x2e, 0x63, 0x75, 0x72, 0x6c, 0x2e, 0x43, 0x75, 0x72, 0x6c, 0x52, 0x65,
-	0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x29, 0x0a, 0x0c, 0x69, 0x6f, 0x2e, 0x67, 0x72, 0x70, 0x63,
-	0x2e, 0x63, 0x75, 0x72, 0x6c, 0x42, 0x0f, 0x63, 0x75, 0x72, 0x6c, 0x57, 0x69, 0x74, 0x68, 0x52,
-	0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x50, 0x01, 0x5a, 0x06, 0x2e, 0x2f, 0x63, 0x75, 0x72, 0x6c,
-	0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x63, 0x6f,
+	0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28,
+	0x03, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68,
+	0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
+	0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x32, 0x4f, 0x0a, 0x0f, 0x43, 0x75, 0x72, 0x6c, 0x57, 0x69,
+	0x74, 0x68, 0x52, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x12, 0x3c, 0x0a, 0x14, 0x43, 0x75, 0x72,
+	0x6c, 0x57, 0x69, 0x74, 0x68, 0x52, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x50, 0x61, 0x72, 0x61,
+	0x6d, 0x12, 0x11, 0x2e, 0x63, 0x75, 0x72, 0x6c, 0x2e, 0x43, 0x75, 0x72, 0x6c, 0x52, 0x65, 0x71,
+	0x75, 0x65, 0x73, 0x74, 0x1a, 0x0f, 0x2e, 0x63, 0x75, 0x72, 0x6c, 0x2e, 0x43, 0x75, 0x72, 0x6c,
+	0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x25, 0x0a, 0x0a, 0x69, 0x6f, 0x2e, 0x67, 0x72,
+	0x70, 0x63, 0x2e, 0x70, 0x62, 0x42, 0x0f, 0x63, 0x75, 0x72, 0x6c, 0x57, 0x69, 0x74, 0x68, 0x52,
+	0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x50, 0x01, 0x5a, 0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06,
+	0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 1 - 1
curl/curl_grpc.pb.go

@@ -4,7 +4,7 @@
 // - protoc             v4.23.3
 // source: curl.proto
 
-package curl
+package pb
 
 import (
 	context "context"

+ 30 - 0
curlServer/proto/curl.proto

@@ -0,0 +1,30 @@
+syntax = "proto3";
+
+package curl;
+
+option go_package = "./pb";
+option java_multiple_files = true;
+option java_package = "io.grpc.pb";
+option java_outer_classname = "curlWithResolve";
+
+service CurlWithResolve {
+  // Sends a greeting
+  rpc CurlWithResolveParam (CurlRequest) returns  (CurlReply) {}
+}
+
+// request
+message CurlRequest {
+  string url = 1;
+  string ip = 2;
+  string UA = 3;
+  bool UseHeadMethod = 4;
+}
+
+// response
+message CurlReply {
+  int32 code = 1;
+  int64 contentLength = 2;
+  string error = 3;
+}
+
+

+ 32 - 1
go.mod

@@ -27,6 +27,7 @@ require (
 	github.com/acomagu/bufpipe v1.0.3 // indirect
 	github.com/alexkohler/prealloc v1.0.0 // indirect
 	github.com/alingse/asasalint v0.0.11 // indirect
+	github.com/armon/go-metrics v0.3.10 // indirect
 	github.com/armon/go-radix v1.0.0 // indirect
 	github.com/ashanbrown/forbidigo v1.3.0 // indirect
 	github.com/ashanbrown/makezero v1.1.1 // indirect
@@ -44,6 +45,8 @@ require (
 	github.com/charithe/durationcheck v0.0.9 // indirect
 	github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4 // indirect
 	github.com/client9/misspell v0.3.4 // indirect
+	github.com/coreos/go-semver v0.3.0 // indirect
+	github.com/coreos/go-systemd/v22 v22.3.2 // indirect
 	github.com/daixiang0/gci v0.5.0 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/denis-tingaikin/go-header v0.4.3 // indirect
@@ -59,6 +62,7 @@ require (
 	github.com/go-git/gcfg v1.5.0 // indirect
 	github.com/go-git/go-billy/v5 v5.3.1 // indirect
 	github.com/go-git/go-git/v5 v5.4.2 // indirect
+	github.com/go-ole/go-ole v1.2.6 // indirect
 	github.com/go-toolsmith/astcast v1.0.0 // indirect
 	github.com/go-toolsmith/astcopy v1.0.0 // indirect
 	github.com/go-toolsmith/astequal v1.0.1 // indirect
@@ -67,8 +71,10 @@ require (
 	github.com/go-toolsmith/strparse v1.0.0 // indirect
 	github.com/go-toolsmith/typep v1.0.2 // indirect
 	github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b // indirect
+	github.com/gobuffalo/here v0.6.0 // indirect
 	github.com/gobwas/glob v0.2.3 // indirect
 	github.com/gofrs/flock v0.8.1 // indirect
+	github.com/gogo/protobuf v1.3.2 // indirect
 	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
 	github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
@@ -89,16 +95,24 @@ require (
 	github.com/gostaticanalysis/comment v1.4.2 // indirect
 	github.com/gostaticanalysis/forcetypeassert v0.1.0 // indirect
 	github.com/gostaticanalysis/nilerr v0.1.1 // indirect
+	github.com/grantae/certinfo v0.0.0-20170412194111-59d56a35515b // indirect
+	github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 // indirect
+	github.com/hashicorp/consul/api v1.12.0 // indirect
 	github.com/hashicorp/errwrap v1.1.0 // indirect
 	github.com/hashicorp/go-changelog v0.0.0-20220419201213-5edfc0d651d8 // indirect
 	github.com/hashicorp/go-checkpoint v0.5.0 // indirect
 	github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
+	github.com/hashicorp/go-hclog v1.2.0 // indirect
+	github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
 	github.com/hashicorp/go-multierror v1.1.1 // indirect
 	github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
+	github.com/hashicorp/go-rootcerts v1.0.2 // indirect
 	github.com/hashicorp/go-uuid v1.0.3 // indirect
 	github.com/hashicorp/go-version v1.6.0 // indirect
+	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/hashicorp/hc-install v0.4.0 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
+	github.com/hashicorp/serf v0.9.7 // indirect
 	github.com/hashicorp/terraform-exec v0.17.2 // indirect
 	github.com/hashicorp/terraform-json v0.14.0 // indirect
 	github.com/hashicorp/terraform-plugin-docs v0.13.0 // indirect
@@ -121,8 +135,10 @@ require (
 	github.com/ldez/tagliatelle v0.3.1 // indirect
 	github.com/leonklingele/grouper v1.1.0 // indirect
 	github.com/lufeee/execinquery v1.2.1 // indirect
+	github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
 	github.com/magiconair/properties v1.8.6 // indirect
 	github.com/maratori/testpackage v1.1.0 // indirect
+	github.com/markbates/pkger v0.17.1 // indirect
 	github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // indirect
 	github.com/mattn/go-colorable v0.1.12 // indirect
 	github.com/mattn/go-isatty v0.0.14 // indirect
@@ -148,6 +164,7 @@ require (
 	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/polyfloyd/go-errorlint v1.0.0 // indirect
 	github.com/posener/complete v1.2.3 // indirect
+	github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
 	github.com/prometheus/client_golang v1.12.1 // indirect
 	github.com/prometheus/client_model v0.2.0 // indirect
 	github.com/prometheus/common v0.32.1 // indirect
@@ -157,6 +174,12 @@ require (
 	github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect
 	github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
 	github.com/rivo/uniseg v0.2.0 // indirect
+	github.com/rookie-ninja/rk-boot v1.4.8 // indirect
+	github.com/rookie-ninja/rk-common v1.2.3 // indirect
+	github.com/rookie-ninja/rk-entry v1.0.11 // indirect
+	github.com/rookie-ninja/rk-grpc v1.2.25 // indirect
+	github.com/rookie-ninja/rk-logger v1.2.10 // indirect
+	github.com/rookie-ninja/rk-query v1.2.10 // indirect
 	github.com/russross/blackfriday v1.6.0 // indirect
 	github.com/ryancurrah/gomodguard v1.2.4 // indirect
 	github.com/ryanrolds/sqlclosecheck v0.3.0 // indirect
@@ -164,6 +187,7 @@ require (
 	github.com/securego/gosec/v2 v2.12.0 // indirect
 	github.com/sergi/go-diff v1.2.0 // indirect
 	github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c // indirect
+	github.com/shirou/gopsutil/v3 v3.22.6 // indirect
 	github.com/shopspring/decimal v1.3.1 // indirect
 	github.com/sirupsen/logrus v1.9.0 // indirect
 	github.com/sivchari/containedctx v1.0.2 // indirect
@@ -186,6 +210,8 @@ require (
 	github.com/tdakkota/asciicheck v0.1.1 // indirect
 	github.com/tetafro/godot v1.4.11 // indirect
 	github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 // indirect
+	github.com/tklauser/go-sysconf v0.3.10 // indirect
+	github.com/tklauser/numcpus v0.4.0 // indirect
 	github.com/tomarrell/wrapcheck/v2 v2.6.2 // indirect
 	github.com/tommy-muehle/go-mnd/v2 v2.5.0 // indirect
 	github.com/ultraware/funlen v0.0.3 // indirect
@@ -194,11 +220,15 @@ require (
 	github.com/xanzy/ssh-agent v0.3.0 // indirect
 	github.com/yagipy/maintidx v1.0.0 // indirect
 	github.com/yeya24/promlinter v0.2.0 // indirect
+	github.com/yusufpapurcu/wmi v1.2.2 // indirect
 	github.com/zclconf/go-cty v1.10.0 // indirect
 	gitlab.com/bosi/decorder v0.2.3 // indirect
+	go.etcd.io/etcd/api/v3 v3.5.4 // indirect
+	go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
+	go.etcd.io/etcd/client/v3 v3.5.4 // indirect
 	go.uber.org/atomic v1.7.0 // indirect
 	go.uber.org/multierr v1.6.0 // indirect
-	go.uber.org/zap v1.17.0 // indirect
+	go.uber.org/zap v1.20.0 // indirect
 	golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
 	golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
 	golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d // indirect
@@ -213,6 +243,7 @@ require (
 	google.golang.org/appengine v1.6.7 // indirect
 	google.golang.org/genproto v0.0.0-20220808131553-a91ffa7f803e // indirect
 	gopkg.in/ini.v1 v1.66.6 // indirect
+	gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
 	gopkg.in/warnings.v0 v0.1.2 // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect

File diff suppressed because it is too large
+ 314 - 0
go.sum


+ 1 - 1
proto

@@ -1 +1 @@
-Subproject commit 15ffd7fb7a6ea24e9a8bf1001c1cd0ff4692ecea
+Subproject commit e1cbc80711aa7c3259731a3642944221e6de6f4f

+ 0 - 109
server/CurlServer.go

@@ -1,109 +0,0 @@
-package server
-
-import (
-	curl "cfTest/curl"
-	"context"
-	"io"
-	"log"
-	"net"
-	"net/http"
-	"runtime"
-	"time"
-)
-
-type CurlServer struct {
-	curl.UnimplementedCurlWithResolveServer
-}
-
-func (s *CurlServer) CurlWithResolveParam(ctx context.Context, requestCurlObject *curl.CurlRequest) (*curl.CurlReply, error) {
-	log.Printf("url Received: %v", requestCurlObject.GetUrl())
-	log.Printf("ip Received: %v", requestCurlObject.GetIp())
-	log.Printf("UA Received: %v", requestCurlObject.GetUA())
-	log.Printf("UseHeadMethod Received: %v", requestCurlObject.GetUseHeadMethod())
-
-	// UA
-	var UA = ""
-	if len(requestCurlObject.GetUA()) == 0 {
-		UA = generateDefaultUserAgent()
-	} else {
-		UA = requestCurlObject.GetUA()
-	}
-
-	// Server
-	// 创建一个自定义的 Transport
-	transport := &CustomTransport{
-		Transport: &http.Transport{
-			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
-				dialer := &net.Dialer{
-					Timeout:   30 * time.Second,
-					KeepAlive: 30 * time.Second,
-					DualStack: true,
-				}
-
-				// 修改addr -> 指向IP地址
-				addr = requestCurlObject.GetIp() + ":443"
-				log.Println("addr = ", addr)
-				conn, err := dialer.DialContext(ctx, network, addr)
-				if err != nil {
-					return nil, err
-				}
-				return conn, nil
-			},
-		},
-		// 指定UA
-		UserAgent:     UA,
-		UseHeadMethod: requestCurlObject.GetUseHeadMethod(),
-	}
-
-	// 创建一个使用自定义 Transport 的 HTTP 客户端
-	client := &http.Client{
-		Transport: transport,
-	}
-
-	// 发起 HTTP 请求
-	resp, err := client.Get(requestCurlObject.GetUrl())
-	if err != nil {
-		return nil, err
-	}
-	defer resp.Body.Close()
-
-	// 解析响应结果
-	log.Println("Code = ", resp.StatusCode)
-
-	body, err := io.ReadAll(resp.Body)
-	if err != nil {
-		return nil, err
-	}
-	log.Println("Body = ", string(body))
-	log.Println("header = ", resp.Header)
-
-	return &curl.CurlReply{
-		Code: int32(resp.StatusCode),
-		Body: string(body),
-	}, nil
-}
-
-type CustomTransport struct {
-	Transport     http.RoundTripper
-	UserAgent     string
-	UseHeadMethod bool
-}
-
-func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
-	req.Header.Set("User-Agent", t.UserAgent)
-	if t.UseHeadMethod {
-		req.Method = http.MethodHead
-	}
-	return t.Transport.RoundTrip(req)
-}
-
-func generateDefaultUserAgent() string {
-	// 获取操作系统名称和版本号
-	osInfo := runtime.GOOS
-	// 获取编程语言和版本号
-	langInfo := "Go/" + runtime.Version()
-	// 获取应用程序运行时的信息
-	appInfo := "Executor/1.0"
-
-	return appInfo + " (" + osInfo + "; " + langInfo + ")"
-}

+ 5 - 0
shell/curlServer/build/build.cmd

@@ -0,0 +1,5 @@
+cd %~dp0\..\..\..\
+SET CGO_ENABLED=0
+SET GOOS=linux
+SET GOARCH=amd64
+go build -o .\lib\CurlServer Curl.go

+ 6 - 0
shell/curlServer/build/build.sh

@@ -0,0 +1,6 @@
+#!/bin/sh
+
+#export CGO_ENABLED=0 GOOS=linux GOARCH=amd64
+export CGO_ENABLED=0
+ROOT=$(cd `dirname $0`/../../../; pwd)
+go build -o $ROOT/lib/CurlServer $ROOT/Curl.go

+ 93 - 0
shell/curlServer/curl-server/curlServer.sh

@@ -0,0 +1,93 @@
+#!/bin/sh
+
+ROOT=$(cd `dirname $0`/../../; pwd)
+APP_NAME=CurlServer
+EXE_NAME=$ROOT/lib/$APP_NAME
+#PID  代表是PID文件
+PID=$ROOT/pid/$APP_NAME\.pid
+#log
+LOG=$ROOT/logs/$APP_NAME\.out
+ERROR_LOG=$ROOT/logs/$APP_NAME_error\.out
+
+usage() {
+    echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
+    exit 1
+}
+
+#检查程序是否在运行
+is_exist(){
+  pid=`ps -ef|grep $EXE_NAME|grep -v grep|awk '{print $2}' `
+  #如果不存在返回1,存在返回0
+  if [ -z "${pid}" ]; then
+   return 1
+  else
+    return 0
+  fi
+}
+
+start(){
+  is_exist
+  if [ $? -eq "0" ]; then
+    echo ">>> ${EXE_NAME} is already running PID=${pid} <<<"
+  else
+	  nohup $EXE_NAME >> $LOG 2 > $ERROR_LOG &
+    echo $! > $PID
+    echo ">>> start $EXE_NAME successed PID=$! <<<"
+  fi
+}
+
+#停止方法
+stop(){
+  #is_exist
+  pidf=$(cat $PID)
+  #echo "$pidf"
+  echo ">>> APP PID = $pidf begin kill $pidf <<<"
+  kill $pidf
+  rm -rf $PID
+  sleep 2
+  is_exist
+  if [ $? -eq "0" ]; then
+    echo ">>> api 2 PID = $pid begin kill -9 $pid  <<<"
+    kill -9  $pid
+    sleep 2
+    echo ">>> $EXE_NAME process stopped <<<"
+  else
+    echo ">>> ${EXE_NAME} is not running <<<"
+  fi
+}
+
+#输出运行状态
+status(){
+  is_exist
+  if [ $? -eq "0" ]; then
+    echo ">>> ${EXE_NAME} is running PID is ${pid} <<<"
+  else
+    echo ">>> ${EXE_NAME} is not running <<<"
+  fi
+}
+
+#重启
+restart(){
+  stop
+  start
+}
+
+#根据输入参数,选择执行对应方法,不输入则执行使用说明
+case "$1" in
+  "start")
+    start
+    ;;
+  "stop")
+    stop
+    ;;
+  "status")
+    status
+    ;;
+  "restart")
+    restart
+    ;;
+  *)
+    usage
+    ;;
+esac
+exit 0

+ 3 - 0
shell/curlServer/curl-server/restart.sh

@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./curlServer.sh restart

+ 2 - 0
shell/curlServer/curl-server/start.sh

@@ -0,0 +1,2 @@
+#!/bin/sh
+./curlServer.sh start

+ 2 - 0
shell/curlServer/curl-server/status.sh

@@ -0,0 +1,2 @@
+#!/bin/sh
+./curlServer.sh status

+ 2 - 0
shell/curlServer/curl-server/stop.sh

@@ -0,0 +1,2 @@
+#!/bin/sh
+./curlServer.sh stop

+ 24 - 0
shell/curlServer/package/packageCurlServer.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+cur_date="`date +%Y-%m-%d`"
+ROOT=$(cd `dirname $0`/../../../; pwd)
+
+mkdir -p $ROOT/release/tmpE
+rm -rf $ROOT/release/tmpE/*
+mkdir -p $ROOT/release/tmpE/curl-server/lib
+mkdir -p $ROOT/release/tmpE/curl-server/shell
+
+cp $ROOT/lib/CurlServer $ROOT/release/tmpE/curl-server/lib
+
+cp -rf $ROOT/shell/curlServer/curl-server $ROOT/release/tmpE/curl-server/shell
+
+mkdir -p $ROOT/release/tmpE/curl-server/logs/CurlServer
+mkdir -p $ROOT/release/tmpE/curl-server/pid
+
+cd $ROOT/release/tmpE
+tar -zcf $ROOT/release/curl-server_${cur_date}.tar.gz curl-server
+cd -
+
+rm -rf $ROOT/release/tmpE/*
+rmdir $ROOT/release/tmpE
+
+echo "打包成功"