CurlServer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package server
  2. import (
  3. curl "cfTest/curl"
  4. "context"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "runtime"
  10. "time"
  11. )
  12. type CurlServer struct {
  13. curl.UnimplementedCurlWithResolveServer
  14. }
  15. func (s *CurlServer) CurlWithResolveParam(ctx context.Context, requestCurlObject *curl.CurlRequest) (*curl.CurlReply, error) {
  16. log.Printf("url Received: %v", requestCurlObject.GetUrl())
  17. log.Printf("ip Received: %v", requestCurlObject.GetIp())
  18. log.Printf("UA Received: %v", requestCurlObject.GetUA())
  19. log.Printf("UseHeadMethod Received: %v", requestCurlObject.GetUseHeadMethod())
  20. // UA
  21. var UA = ""
  22. if len(requestCurlObject.GetUA()) == 0 {
  23. UA = generateDefaultUserAgent()
  24. } else {
  25. UA = requestCurlObject.GetUA()
  26. }
  27. // Server
  28. // 创建一个自定义的 Transport
  29. transport := &CustomTransport{
  30. Transport: &http.Transport{
  31. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  32. dialer := &net.Dialer{
  33. Timeout: 30 * time.Second,
  34. KeepAlive: 30 * time.Second,
  35. DualStack: true,
  36. }
  37. // 修改addr -> 指向IP地址
  38. addr = requestCurlObject.GetIp() + ":443"
  39. log.Println("addr = ", addr)
  40. conn, err := dialer.DialContext(ctx, network, addr)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return conn, nil
  45. },
  46. },
  47. // 指定UA
  48. UserAgent: UA,
  49. UseHeadMethod: requestCurlObject.GetUseHeadMethod(),
  50. }
  51. // 创建一个使用自定义 Transport 的 HTTP 客户端
  52. client := &http.Client{
  53. Transport: transport,
  54. }
  55. // 发起 HTTP 请求
  56. resp, err := client.Get(requestCurlObject.GetUrl())
  57. if err != nil {
  58. return nil, err
  59. }
  60. defer resp.Body.Close()
  61. // 解析响应结果
  62. log.Println("Code = ", resp.StatusCode)
  63. body, err := io.ReadAll(resp.Body)
  64. if err != nil {
  65. return nil, err
  66. }
  67. log.Println("Body = ", string(body))
  68. log.Println("header = ", resp.Header)
  69. return &curl.CurlReply{
  70. Code: int32(resp.StatusCode),
  71. Body: string(body),
  72. }, nil
  73. }
  74. type CustomTransport struct {
  75. Transport http.RoundTripper
  76. UserAgent string
  77. UseHeadMethod bool
  78. }
  79. func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  80. req.Header.Set("User-Agent", t.UserAgent)
  81. if t.UseHeadMethod {
  82. req.Method = http.MethodHead
  83. }
  84. return t.Transport.RoundTrip(req)
  85. }
  86. func generateDefaultUserAgent() string {
  87. // 获取操作系统名称和版本号
  88. osInfo := runtime.GOOS
  89. // 获取编程语言和版本号
  90. langInfo := "Go/" + runtime.Version()
  91. // 获取应用程序运行时的信息
  92. appInfo := "Executor/1.0"
  93. return appInfo + " (" + osInfo + "; " + langInfo + ")"
  94. }