Skip to content

A simple gRPC server and client to (forward) proxy TCP/UDP connections

License

Notifications You must be signed in to change notification settings

xaionaro-go/grpcproxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Go Reference Go Report Card

This is a gRPC server and a client that allows to proxy network connections via the server.

How to use

Server-side

Let's say you already have a gRPC server written in Go, something like this:

	grpcServer := grpc.NewServer()
	myFancyService := myfancyservice.New()
	myFancyService_grpc.RegisterMyFancyServiceServer(grpcServer, myFancyService)

To add the proxy capability you just need to register the proxy service as well:

import "github.com/xaionaro-go/grpcproxy/grpcproxyserver"
import "github.com/xaionaro-go/grpcproxy/protobuf/go/proxy_grpc"

	grpcServer := grpc.NewServer()
	myFancyService := myfancyservice.New()
	myfancyservice_grpc.RegisterMyFancyServiceServer(grpcServer, myFancyService)

	proxyServer := grpcproxyserver.New()
	proxy_grpc.RegisterNetworkProxyServer(grpcServer, proxyServer)

Client-side

On the client side you may replace a direct connection:

conn, err := net.Dial("tcp", addr)

with proxied connection:

conn, err := grpchttpproxy.NewDialer(myGRPCClient).DialContext(ctx, "tcp", addr)

Or if you need to proxy an HTTP request, you may make an HTTP client:

	httpClient := &http.Client{
		Transport: &http.Transport{
			DialContext: grpchttpproxy.NewDialer(myGRPCClient).DialContext,
		},
	}

And then perform the HTTP requests via this client