How to get net.Conn in rpc function in net/rpc

110 views
Skip to first unread message

York gao

unread,
Dec 28, 2023, 10:31:02 AM12/28/23
to golang-nuts
I have a requirement that requires the rpc server to get the client’s **real IP** and current connection `net.Conn`.

 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.

I can get the remoteip through the following RemoteAddr method, but this is not necessarily the real IP of the client.
```go
// 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)
}
```

Therefore, I hope to manually call the`ReportRealIP` 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

Brian Candler

unread,
Dec 28, 2023, 1:06:38 PM12/28/23
to golang-nuts
What do you mean by "Real IP" and in particular how does it differ from the TCP source address? If the client is behind a NAT, then (a) you won't know the client's real IP unless they tell it to you (and you trust what they say), and (b) you won't be able to connect to that address anyway.

If your actual requirement is to issue RPC calls in the 'reverse' direction down the same TCP connection, then you can look at how it's done here:

Christian Stewart

unread,
Dec 28, 2023, 4:15:12 PM12/28/23
to Brian Candler, golang-nuts
I agree with what Brian said and would like to suggest the following two way RPC library with multiplexing over a single connection of any type:


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1734f6df-b8c9-4466-8e53-2243a88bb991n%40googlegroups.com.

Dan Kortschak

unread,
Dec 28, 2023, 11:15:42 PM12/28/23
to golan...@googlegroups.com
On Thu, 2023-12-28 at 13:14 -0800, 'Christian Stewart' via golang-nuts
wrote:
> I agree with what Brian said and would like to suggest the following
> two way RPC library with multiplexing over a single connection of any
> type:
>
> https://github.com/aperturerobotics/starpc
>

There is also
https://pkg.go.dev/golang.org/x/tools/internal/jsonrpc2_v2 which is
unfortunately internal, but is extracted mechanically to
https://pkg.go.dev/github.com/kortschak/jsonrpc2

Dan

Reply all
Reply to author
Forward
0 new messages