package curlServer import ( curl2 "cfTest/curlServer/pb" "context" "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" "net" "net/http" "runtime" "strings" "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("TCP连接异常: " + err.Error()) return nil, err } return conn, nil }, }, // 指定UA UserAgent: ua, UseHeadMethod: requestCurlObject.GetUseHeadMethod(), } // 创建一个使用自定义 Transport 的 HTTP 客户端 client := &http.Client{ Transport: transport, } // 创建一个GET请求, 将转义字符重新转换 originalUrl := strings.Replace(requestCurlObject.GetUrl(), "%5B", "[", -1) originalUrl = strings.Replace(originalUrl, "%5D", "]", -1) originalUrl = strings.Replace(originalUrl, "%20", " ", -1) originalUrl = strings.Replace(originalUrl, "%7B", "{", -1) originalUrl = strings.Replace(originalUrl, "%7D", "}", -1) originalUrl = strings.Replace(originalUrl, "%22", "\"", -1) originalUrl = strings.Replace(originalUrl, "%26", "&", -1) logger.Println(originalUrl) // 发起 HTTP 请求 resp, err := client.Get(originalUrl) if err != nil { logger.Error("发送HTTP请求出错, e:" + err.Error() + ", url: " + requestCurlObject.GetUrl()) return nil, err } defer resp.Body.Close() // 解析响应结果 logger.Println("Code = ", resp.StatusCode) 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("01-02-2006") return "curl_" + currentDate + ".log" } func GetLogger() *logrus.Logger { return logger }