From ee9e97c60d4b0f927eeb9ce7e3b6a1927914f0f2 Mon Sep 17 00:00:00 2001 From: greg stone Date: Sun, 26 Feb 2023 01:22:56 +0000 Subject: [PATCH] adding endpoint. --- pkg/rpc/client.go | 4 ++-- pkg/rpc/dialer.go | 9 ++++++-- pkg/rpc/dialer_options.go | 3 ++- pkg/rpc/endpoint.go | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 pkg/rpc/endpoint.go diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 83eb8a2c..c8e303bd 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -23,8 +23,8 @@ func getNetworkInstance(opts *dialOptions) (net *netstack.Net, err error) { deviceConf := "" + "public_key=" + opts.peerPubKey.HexString() + "\n" + - "endpoint=" + opts.endpoint + "\n" + - "allowed_ip=" + rpcEndpointIp + "/32\n" + + "endpoint=" + opts.endpoint.String() + "\n" + + "allowed_ip=" + opts.rpcEndpoint.Address() + "/32\n" + "persistent_keepalive_interval=" + strconv.Itoa(opts.keepAliveInterval) + "\n" if err = dev.IpcSet(deviceConf); check(err) { diff --git a/pkg/rpc/dialer.go b/pkg/rpc/dialer.go index 63600b2e..7ec8d73c 100644 --- a/pkg/rpc/dialer.go +++ b/pkg/rpc/dialer.go @@ -28,7 +28,12 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * return nil, errors.New("Unsupported protocol. Only unix:// or noise://") } - dialOpts := &dialOptions{peerRPCIP: "192.168.37.2", mtu: 1420, endpoint: strings.TrimPrefix(target, "noise://")} + dialOpts := &dialOptions{ + endpoint: EndpointString(target), + rpcEndpoint: EndpointString("192.168.37.1:80"), + peerRPCIP: "192.168.37.2", + mtu: 1420, + } for _, opt := range opts { opt.apply(dialOpts) @@ -41,7 +46,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * } return grpc.DialContext(ctx, - rpcEndpointIp+":"+rpcEndpointPort, + dialOpts.rpcEndpoint.String(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(ctx context.Context, address string) (net.Conn, error) { return network.DialContext(ctx, "tcp4", address) diff --git a/pkg/rpc/dialer_options.go b/pkg/rpc/dialer_options.go index 9b38a9cf..c02c2e0a 100644 --- a/pkg/rpc/dialer_options.go +++ b/pkg/rpc/dialer_options.go @@ -3,7 +3,8 @@ package rpc // dialOptions configure a Dial call. dialOptions are set by the DialOption // values passed to Dial. type dialOptions struct { - endpoint string + endpoint Endpoint + rpcEndpoint Endpoint key RPCPrivateKey peerPubKey RPCPublicKey peerRPCIP string diff --git a/pkg/rpc/endpoint.go b/pkg/rpc/endpoint.go new file mode 100644 index 00000000..8749dd44 --- /dev/null +++ b/pkg/rpc/endpoint.go @@ -0,0 +1,46 @@ +package rpc + +import "strings" + +type Endpoint string + +func (e Endpoint) Address() string { + + before, _, found := strings.Cut(string(e), ":") + + if !found { + return "" + } + + return before +} + +func (e Endpoint) Port() string { + + _, after, found := strings.Cut(string(e), ":") + + if !found { + return "" + } + + return after +} + +func (e Endpoint) String() string { + return string(e) +} + +func EndpointString(endpoint string) (ep Endpoint) { + + _, after, found := strings.Cut(string(endpoint), "//") + + if !found { + ep = Endpoint(endpoint) + + return + } + + ep = Endpoint(after) + + return +}