How to get the client's real IP in golang in net/rpc

I have a requirement that requires the rpc server to get the client’s real IP and current connection net.Conn.

I used to get it through the following example method

// example_server.go
type MathService struct{}
type Args struct {
	A, B int
}
type Reply struct {
	Result int
}
func (m *MathService) Multiply(args *Args, reply *Reply) error {
	reply.Result = args.A * args.B
	return nil
}
func main() {
	// Create an instance of the MathService
	mathService := new(MathService)
	// Register MathService for RPC
	rpc.Register(mathService)
	// Create a TCP listener
	listener, err := net.Listen("tcp", "0.0.0.0:1234")
	if err != nil {
		fmt.Println("Error starting server:", err)
		return
	}
	defer listener.Close()
	fmt.Println("Server listening on :1234")
	for {
		// Accept incoming connections
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("Error accepting connection:", err)
			continue
		}
		fmt.Printf("remote ip addr: %s\n", conn.RemoteAddr().String())
		// Serve the connection in a new goroutine
		go rpc.ServeConn(conn)
	}
}

// example_client.go
type Args struct {
	A, B int
}
type Reply struct {
	Result int
}
func main() {
	// Connect to the server
	client, err := rpc.Dial("tcp", "127.0.0.1:1234")
	if err != nil {
		fmt.Println("Error connecting to server:", err)
		return
	}
	defer client.Close()
	// Prepare the arguments for the RPC call
	args := &Args{A: 5, B: 3}
	// Call the Multiply method remotely
	var reply Reply
	err = client.Call("MathService.Multiply", args, &reply)
	if err != nil {
		fmt.Println("Error calling Multiply:", err)
		return
	}
	// Print the result
	fmt.Printf("Result: %d\n", reply.Result)
}

But what the RemoteAddr method gets is not necessarily the real IP of the client.

Therefore, I hope to manually call theReportRealIP function to report the real IP, but I cannot get the current connected net.Conn in the rpc function.

But I searched some information and found no way to achieve my needs. Is there a way to get the two variables I want? Looking forward to your reply

The reason for needing real IP and net.Conn is roughly for two-way communication. I hope to be able to send RPC requests to the client of a specific IP to perform certain operations. For example, issue RPC commands to certain Agent services to execute a certain script.

Backing up a tad:

Are you sure RPC is even the tool for the job here? If two-way communication is all you want, you might be able to use websockets. Benefits include: you are already familiar with stdlib web stuff presumably so this would be much easier to hit the ground running with. Check out this demo I made with gorilla/mux to help a colleague with a proof of concept:

Also this might be a good use case for gRPC and protocol buffers:

You could then use this method to get the IP:

Anyway, just some thoughts. I’m not super familiar with net/rpc. Most of the times when I would need something like this I used gRPC and protobuf.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.