Parcourir la source

feature:Go实现带Resolve参数Curl指令

kuanglingwei il y a 1 an
Parent
commit
ca3f0e51f0
4 fichiers modifiés avec 509 ajouts et 0 suppressions
  1. 28 0
      curl.go
  2. 261 0
      curl/curl.pb.go
  3. 111 0
      curl/curl_grpc.pb.go
  4. 109 0
      server/CurlServer.go

+ 28 - 0
curl.go

@@ -0,0 +1,28 @@
+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)
+	}
+}

+ 261 - 0
curl/curl.pb.go

@@ -0,0 +1,261 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// versions:
+// 	protoc-gen-go v1.30.0
+// 	protoc        v4.23.3
+// source: curl.proto
+
+package curl
+
+import (
+	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
+	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
+	reflect "reflect"
+	sync "sync"
+)
+
+const (
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
+)
+
+// request
+type CurlRequest struct {
+	state         protoimpl.MessageState
+	sizeCache     protoimpl.SizeCache
+	unknownFields protoimpl.UnknownFields
+
+	Url           string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`
+	Ip            string `protobuf:"bytes,2,opt,name=ip,proto3" json:"ip,omitempty"`
+	UA            string `protobuf:"bytes,3,opt,name=UA,proto3" json:"UA,omitempty"`
+	UseHeadMethod bool   `protobuf:"varint,4,opt,name=UseHeadMethod,proto3" json:"UseHeadMethod,omitempty"`
+}
+
+func (x *CurlRequest) Reset() {
+	*x = CurlRequest{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_curl_proto_msgTypes[0]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *CurlRequest) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*CurlRequest) ProtoMessage() {}
+
+func (x *CurlRequest) ProtoReflect() protoreflect.Message {
+	mi := &file_curl_proto_msgTypes[0]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use CurlRequest.ProtoReflect.Descriptor instead.
+func (*CurlRequest) Descriptor() ([]byte, []int) {
+	return file_curl_proto_rawDescGZIP(), []int{0}
+}
+
+func (x *CurlRequest) GetUrl() string {
+	if x != nil {
+		return x.Url
+	}
+	return ""
+}
+
+func (x *CurlRequest) GetIp() string {
+	if x != nil {
+		return x.Ip
+	}
+	return ""
+}
+
+func (x *CurlRequest) GetUA() string {
+	if x != nil {
+		return x.UA
+	}
+	return ""
+}
+
+func (x *CurlRequest) GetUseHeadMethod() bool {
+	if x != nil {
+		return x.UseHeadMethod
+	}
+	return false
+}
+
+// response
+type CurlReply struct {
+	state         protoimpl.MessageState
+	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"`
+}
+
+func (x *CurlReply) Reset() {
+	*x = CurlReply{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_curl_proto_msgTypes[1]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *CurlReply) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*CurlReply) ProtoMessage() {}
+
+func (x *CurlReply) ProtoReflect() protoreflect.Message {
+	mi := &file_curl_proto_msgTypes[1]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use CurlReply.ProtoReflect.Descriptor instead.
+func (*CurlReply) Descriptor() ([]byte, []int) {
+	return file_curl_proto_rawDescGZIP(), []int{1}
+}
+
+func (x *CurlReply) GetCode() int32 {
+	if x != nil {
+		return x.Code
+	}
+	return 0
+}
+
+func (x *CurlReply) GetBody() string {
+	if x != nil {
+		return x.Body
+	}
+	return ""
+}
+
+func (x *CurlReply) GetError() string {
+	if x != nil {
+		return x.Error
+	}
+	return ""
+}
+
+var File_curl_proto protoreflect.FileDescriptor
+
+var file_curl_proto_rawDesc = []byte{
+	0x0a, 0x0a, 0x63, 0x75, 0x72, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x63, 0x75,
+	0x72, 0x6c, 0x22, 0x65, 0x0a, 0x0b, 0x43, 0x75, 0x72, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
+	0x74, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03,
+	0x75, 0x72, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
+	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,
+	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,
+}
+
+var (
+	file_curl_proto_rawDescOnce sync.Once
+	file_curl_proto_rawDescData = file_curl_proto_rawDesc
+)
+
+func file_curl_proto_rawDescGZIP() []byte {
+	file_curl_proto_rawDescOnce.Do(func() {
+		file_curl_proto_rawDescData = protoimpl.X.CompressGZIP(file_curl_proto_rawDescData)
+	})
+	return file_curl_proto_rawDescData
+}
+
+var file_curl_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
+var file_curl_proto_goTypes = []interface{}{
+	(*CurlRequest)(nil), // 0: curl.CurlRequest
+	(*CurlReply)(nil),   // 1: curl.CurlReply
+}
+var file_curl_proto_depIdxs = []int32{
+	0, // 0: curl.CurlWithResolve.CurlWithResolveParam:input_type -> curl.CurlRequest
+	1, // 1: curl.CurlWithResolve.CurlWithResolveParam:output_type -> curl.CurlReply
+	1, // [1:2] is the sub-list for method output_type
+	0, // [0:1] is the sub-list for method input_type
+	0, // [0:0] is the sub-list for extension type_name
+	0, // [0:0] is the sub-list for extension extendee
+	0, // [0:0] is the sub-list for field type_name
+}
+
+func init() { file_curl_proto_init() }
+func file_curl_proto_init() {
+	if File_curl_proto != nil {
+		return
+	}
+	if !protoimpl.UnsafeEnabled {
+		file_curl_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*CurlRequest); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
+		file_curl_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*CurlReply); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
+	}
+	type x struct{}
+	out := protoimpl.TypeBuilder{
+		File: protoimpl.DescBuilder{
+			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
+			RawDescriptor: file_curl_proto_rawDesc,
+			NumEnums:      0,
+			NumMessages:   2,
+			NumExtensions: 0,
+			NumServices:   1,
+		},
+		GoTypes:           file_curl_proto_goTypes,
+		DependencyIndexes: file_curl_proto_depIdxs,
+		MessageInfos:      file_curl_proto_msgTypes,
+	}.Build()
+	File_curl_proto = out.File
+	file_curl_proto_rawDesc = nil
+	file_curl_proto_goTypes = nil
+	file_curl_proto_depIdxs = nil
+}

+ 111 - 0
curl/curl_grpc.pb.go

@@ -0,0 +1,111 @@
+// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
+// versions:
+// - protoc-gen-go-grpc v1.3.0
+// - protoc             v4.23.3
+// source: curl.proto
+
+package curl
+
+import (
+	context "context"
+	grpc "google.golang.org/grpc"
+	codes "google.golang.org/grpc/codes"
+	status "google.golang.org/grpc/status"
+)
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+// Requires gRPC-Go v1.32.0 or later.
+const _ = grpc.SupportPackageIsVersion7
+
+const (
+	CurlWithResolve_CurlWithResolveParam_FullMethodName = "/curl.CurlWithResolve/CurlWithResolveParam"
+)
+
+// CurlWithResolveClient is the client API for CurlWithResolve service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
+type CurlWithResolveClient interface {
+	// Sends a greeting
+	CurlWithResolveParam(ctx context.Context, in *CurlRequest, opts ...grpc.CallOption) (*CurlReply, error)
+}
+
+type curlWithResolveClient struct {
+	cc grpc.ClientConnInterface
+}
+
+func NewCurlWithResolveClient(cc grpc.ClientConnInterface) CurlWithResolveClient {
+	return &curlWithResolveClient{cc}
+}
+
+func (c *curlWithResolveClient) CurlWithResolveParam(ctx context.Context, in *CurlRequest, opts ...grpc.CallOption) (*CurlReply, error) {
+	out := new(CurlReply)
+	err := c.cc.Invoke(ctx, CurlWithResolve_CurlWithResolveParam_FullMethodName, in, out, opts...)
+	if err != nil {
+		return nil, err
+	}
+	return out, nil
+}
+
+// CurlWithResolveServer is the server API for CurlWithResolve service.
+// All implementations must embed UnimplementedCurlWithResolveServer
+// for forward compatibility
+type CurlWithResolveServer interface {
+	// Sends a greeting
+	CurlWithResolveParam(context.Context, *CurlRequest) (*CurlReply, error)
+	mustEmbedUnimplementedCurlWithResolveServer()
+}
+
+// UnimplementedCurlWithResolveServer must be embedded to have forward compatible implementations.
+type UnimplementedCurlWithResolveServer struct {
+}
+
+func (UnimplementedCurlWithResolveServer) CurlWithResolveParam(context.Context, *CurlRequest) (*CurlReply, error) {
+	return nil, status.Errorf(codes.Unimplemented, "method CurlWithResolveParam not implemented")
+}
+func (UnimplementedCurlWithResolveServer) mustEmbedUnimplementedCurlWithResolveServer() {}
+
+// UnsafeCurlWithResolveServer may be embedded to opt out of forward compatibility for this service.
+// Use of this interface is not recommended, as added methods to CurlWithResolveServer will
+// result in compilation errors.
+type UnsafeCurlWithResolveServer interface {
+	mustEmbedUnimplementedCurlWithResolveServer()
+}
+
+func RegisterCurlWithResolveServer(s grpc.ServiceRegistrar, srv CurlWithResolveServer) {
+	s.RegisterService(&CurlWithResolve_ServiceDesc, srv)
+}
+
+func _CurlWithResolve_CurlWithResolveParam_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+	in := new(CurlRequest)
+	if err := dec(in); err != nil {
+		return nil, err
+	}
+	if interceptor == nil {
+		return srv.(CurlWithResolveServer).CurlWithResolveParam(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: CurlWithResolve_CurlWithResolveParam_FullMethodName,
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(CurlWithResolveServer).CurlWithResolveParam(ctx, req.(*CurlRequest))
+	}
+	return interceptor(ctx, in, info, handler)
+}
+
+// CurlWithResolve_ServiceDesc is the grpc.ServiceDesc for CurlWithResolve service.
+// It's only intended for direct use with grpc.RegisterService,
+// and not to be introspected or modified (even as a copy)
+var CurlWithResolve_ServiceDesc = grpc.ServiceDesc{
+	ServiceName: "curl.CurlWithResolve",
+	HandlerType: (*CurlWithResolveServer)(nil),
+	Methods: []grpc.MethodDesc{
+		{
+			MethodName: "CurlWithResolveParam",
+			Handler:    _CurlWithResolve_CurlWithResolveParam_Handler,
+		},
+	},
+	Streams:  []grpc.StreamDesc{},
+	Metadata: "curl.proto",
+}

+ 109 - 0
server/CurlServer.go

@@ -0,0 +1,109 @@
+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 + ")"
+}