Skip to content

Commit

Permalink
Add --interface flag for choosing which network interface to listen on (
Browse files Browse the repository at this point in the history
#81)

* Add --interface flag

* Fix default networkInterface
  • Loading branch information
bradleyjkemp authored Mar 3, 2020
1 parent eff327c commit 2efa4b2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## Unreleased changes
## [v0.2.5](https://github.com/bradleyjkemp/grpc-tools/releases/tag/v0.2.5)
* Added grpc-proxy options `WithServerOptions` and `WithDialOptions`, deprecated `WithOptions` [#71](https://github.com/bradleyjkemp/grpc-tools/pull/71).
* Added a new command-line option, `--interface` to allow choosing which network interface `grpc-proxy` listens on [#81](https://github.com/bradleyjkemp/grpc-tools/pull/81).

## [v0.2.4](https://github.com/bradleyjkemp/grpc-tools/releases/tag/v0.2.4)
* Added a new command-line option, `--tls_secrets_file`, to write the [TLS master secrets file](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format) to the specified path. This can be used later with different application, such as Wireshark, to decrypt the gRPC traffic [#63](https://github.com/bradleyjkemp/grpc-tools/pull/63).
Expand Down
3 changes: 3 additions & 0 deletions grpc-proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func WithDialer(dialer ContextDialer) Configurator {
}

var (
fNetworkInterface string
fPort int
fCertFile string
fKeyFile string
Expand All @@ -84,6 +85,7 @@ var (

// Must be called before flag.Parse() if using the DefaultFlags option
func RegisterDefaultFlags() {
flag.StringVar(&fNetworkInterface, "interface", "localhost", "Network interface to listen on. By default listens on the localhost interface.")
flag.IntVar(&fPort, "port", 0, "Port to listen on.")
flag.StringVar(&fCertFile, "cert", "", "Certificate file to use for serving using TLS. By default the current directory will be scanned for mkcert certificates to use.")
flag.StringVar(&fKeyFile, "key", "", "Key file to use for serving using TLS. By default the current directory will be scanned for mkcert keys to use.")
Expand All @@ -96,6 +98,7 @@ func RegisterDefaultFlags() {
// This must be used after a call to flag.Parse()
func DefaultFlags() Configurator {
return func(s *server) {
s.networkInterface = fNetworkInterface
s.port = fPort
s.certFile = fCertFile
s.keyFile = fKeyFile
Expand Down
20 changes: 11 additions & 9 deletions grpc-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ type server struct {
grpcServer *grpc.Server
logger logrus.FieldLogger

port int
certFile string
keyFile string
x509Cert *x509.Certificate
tlsCert tls.Certificate
networkInterface string
port int
certFile string
keyFile string
x509Cert *x509.Certificate
tlsCert tls.Certificate

destination string
connPool *internal.ConnPool
Expand All @@ -53,8 +54,9 @@ type server struct {
func New(configurators ...Configurator) (*server, error) {
logger := logrus.New()
s := &server{
logger: logger,
dialer: proxydialer.NewProxyDialer(httpproxy.FromEnvironment().ProxyFunc()),
logger: logger,
dialer: proxydialer.NewProxyDialer(httpproxy.FromEnvironment().ProxyFunc()),
networkInterface: "localhost", // default to just localhost if no other interface is chosen
}
s.serverOptions = []grpc.ServerOption{
grpc.CustomCodec(codec.NoopCodec{}), // Allows for passing raw []byte messages around
Expand Down Expand Up @@ -103,9 +105,9 @@ func New(configurators ...Configurator) (*server, error) {

func (s *server) Start() error {
var err error
s.listener, err = net.Listen("tcp", fmt.Sprintf("localhost:%d", s.port))
s.listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", s.networkInterface, s.port))
if err != nil {
return fmt.Errorf("failed to listen on port (%d): %v", s.port, err)
return fmt.Errorf("failed to listen on interface (%s:%d): %v", s.networkInterface, s.port, err)
}
s.logger.Infof("Listening on %s", s.listener.Addr())
if s.x509Cert != nil {
Expand Down

0 comments on commit 2efa4b2

Please sign in to comment.