CurlServer.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package curlServer
  2. import (
  3. curl2 "cfTest/curlServer/pb"
  4. "context"
  5. "github.com/sirupsen/logrus"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "io"
  8. "net"
  9. "net/http"
  10. "runtime"
  11. "time"
  12. )
  13. var logger *logrus.Logger
  14. type CurlServer struct {
  15. curl2.UnimplementedCurlWithResolveServer
  16. }
  17. func (s *CurlServer) CurlWithResolveParam(ctx context.Context, requestCurlObject *curl2.CurlRequest) (*curl2.CurlReply, error) {
  18. logger.Println("url Received: ", requestCurlObject.GetUrl())
  19. logger.Println("ip Received: ", requestCurlObject.GetIp())
  20. logger.Println("ua Received: ", requestCurlObject.GetUa())
  21. logger.Println("UseHeadMethod Received: ", requestCurlObject.GetUseHeadMethod())
  22. // ua
  23. var ua = ""
  24. if len(requestCurlObject.GetUa()) == 0 {
  25. ua = generateDefaultUserAgent()
  26. } else {
  27. ua = requestCurlObject.GetUa()
  28. }
  29. // Server
  30. // 创建一个自定义的 Transport
  31. transport := &CustomTransport{
  32. Transport: &http.Transport{
  33. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  34. dialer := &net.Dialer{
  35. Timeout: 30 * time.Second,
  36. KeepAlive: 30 * time.Second,
  37. DualStack: true,
  38. }
  39. // 修改addr -> 指向IP地址
  40. addr = requestCurlObject.GetIp() + ":443"
  41. logger.Println("addr = ", addr)
  42. conn, err := dialer.DialContext(ctx, network, addr)
  43. if err != nil {
  44. logger.Error(err.Error())
  45. return nil, err
  46. }
  47. return conn, nil
  48. },
  49. },
  50. // 指定UA
  51. UserAgent: ua,
  52. UseHeadMethod: requestCurlObject.GetUseHeadMethod(),
  53. }
  54. // 创建一个使用自定义 Transport 的 HTTP 客户端
  55. client := &http.Client{
  56. Transport: transport,
  57. }
  58. // 发起 HTTP 请求
  59. resp, err := client.Get(requestCurlObject.GetUrl())
  60. if err != nil {
  61. logger.Error(err.Error())
  62. return nil, err
  63. }
  64. defer resp.Body.Close()
  65. // 解析响应结果
  66. logger.Println("Code = ", resp.StatusCode)
  67. _, err = io.ReadAll(resp.Body)
  68. if err != nil {
  69. logger.Error(err.Error())
  70. return nil, err
  71. }
  72. logger.Println("header = ", resp.Header)
  73. return &curl2.CurlReply{
  74. Code: int32(resp.StatusCode),
  75. ContentLength: resp.ContentLength,
  76. }, nil
  77. }
  78. type CustomTransport struct {
  79. Transport http.RoundTripper
  80. UserAgent string
  81. UseHeadMethod bool
  82. }
  83. func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  84. req.Header.Set("User-Agent", t.UserAgent)
  85. if t.UseHeadMethod {
  86. req.Method = http.MethodHead
  87. }
  88. return t.Transport.RoundTrip(req)
  89. }
  90. func generateDefaultUserAgent() string {
  91. // 获取操作系统名称和版本号
  92. osInfo := runtime.GOOS
  93. // 获取编程语言和版本号
  94. langInfo := "Go/" + runtime.Version()
  95. // 获取应用程序运行时的信息
  96. appInfo := "Executor/1.0"
  97. return appInfo + " (" + osInfo + "; " + langInfo + ")"
  98. }
  99. func InitLogger() {
  100. setupLogger()
  101. // 在初始化函数中对包级别的变量进行初始化
  102. logger = logrus.StandardLogger()
  103. logger.Println("启动CurlServer, 初始化日志框架...")
  104. }
  105. func setupLogger() {
  106. // 创建一个新的logrus实例
  107. log := logrus.New()
  108. // 设置日志输出为文件,并使用lumberjack库来实现每天生成一个新的日志文件,并保存旧日志文件
  109. log.SetOutput(&lumberjack.Logger{
  110. Filename: "../../logs/CurlServer/" + generateLogFileName(), // 日志文件路径,使用日期占位符%Y-%m-%d
  111. MaxSize: 20, // 每个日志文件的最大尺寸,单位:MB
  112. MaxBackups: 3, // 最多保留的旧日志文件数
  113. MaxAge: 30, // 最多保留的旧日志文件天数(这里设置为30天)
  114. LocalTime: true, // 使用本地时间(默认为UTC时间)
  115. Compress: true, // 是否压缩旧日志文件
  116. })
  117. // 设置日志格式为JSON格式
  118. log.SetFormatter(&logrus.JSONFormatter{})
  119. // 设置日志级别为Debug
  120. log.SetLevel(logrus.DebugLevel)
  121. // 将logrus实例设置为全局的默认日志记录器
  122. logrus.StandardLogger().SetOutput(log.Out)
  123. }
  124. func generateLogFileName() string {
  125. currentDate := time.Now().Format("01-02-2006")
  126. return "curl_" + currentDate + ".log"
  127. }
  128. func GetLogger() *logrus.Logger {
  129. return logger
  130. }