Force RE to listen to localhost only

80 views
Skip to first unread message

Дмитрий Киселев

unread,
Nov 11, 2015, 6:16:52 AM11/11/15
to RestExpress
Hi, how can I force RestExpress to accept connections only from local addresses?

I know that I could create preprocessor which will drop connections using whitelist,
but maybe somebody already made and test such thing?

Todd Fredrich

unread,
Nov 12, 2015, 4:01:36 PM11/12/15
to RestExpress
Welcome to the forum!

RestExpress currently allows configuration of only the port and listens on *any* local IP address (which ends up being 0.0.0.0). I added Issue #122 (https://github.com/RestExpress/RestExpress/issues/122 ) on your behalf.

Thanks!
--Todd

sushant ravale

unread,
Dec 1, 2015, 6:50:46 AM12/1/15
to RestExpress
I am using following hack through reflection (but this is version dependent) :

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.util.concurrent.EventExecutorGroup;

import java.net.InetSocketAddress;
import java.util.Arrays;

import javax.net.ssl.SSLContext;

import org.restexpress.RestExpress;
import org.restexpress.ServerBootstrapFactory;
import org.restexpress.pipeline.PipelineInitializer;
import org.restexpress.settings.ServerSettings;


public class BindableAddressRestExpress extends RestExpress {
private String bindAddress;
public String getBindAddress() {
return bindAddress;
}

public BindAddressRestExpress setBindAddress(String bindAddress) {
this.bindAddress = bindAddress;
return this;
}

@Override
public Channel bind(int port) {
setPort(port);
ServerBootstrapFactory bootstrapFactory = ReflectionUtils.getFieldInObject(this, "bootstrapFactory", ServerBootstrapFactory.class);

ServerBootstrap bootstrap = bootstrapFactory.newServerBootstrap(getIoThreadCount());
bootstrap.childHandler(new PipelineInitializer()
.setExecutionHandler(ReflectionUtils.callHierarchicalMethodOnObject("initializeExecutorGroup", this, EventExecutorGroup.class,Arrays.asList() ,null))
   .addRequestHandler(buildRequestHandler())
   .setSSLContext(ReflectionUtils.getFieldInObject(this, "sslContext", SSLContext.class))
   .setMaxContentLength(ReflectionUtils.getFieldInObject(this, "serverSettings", ServerSettings.class).getMaxContentSize()));

ReflectionUtils.callHierarchicalMethodOnObject("setBootstrapOptions", this, void.class, Arrays.asList(ServerBootstrap.class),bootstrap);

// Bind and start to accept incoming connections.
if (shouldUseSystemOut())
{
System.out.println(getName() + " server listening on port " + port);
}

Channel channel = bootstrap.bind(new InetSocketAddress(bindAddress, port)).channel();
ReflectionUtils.getFieldInObject(this, "allChannels", ChannelGroup.class).add(channel);

ReflectionUtils.callHierarchicalMethodOnObject("bindPlugins", this, void.class,Arrays.asList(), null);

return channel;
}
}

Todd Fredrich

unread,
Dec 1, 2015, 5:36:59 PM12/1/15
to RestExpress
Thank you very much for your contribution!

When/if you get a chance to try, I created some additional bind() methods on the RestExpress object that enable binding to a particular address:

bind(String hostname, int port)
bind(InetSocketAddress)

as well as being able to call setHostname(String) on your server instance.

This should resolve issue #122 that was created on your behalf. This feature is available in RestExpress 0.11.3-SNAPSHOT

Thanks again!
--Todd

Дмитрий Киселев

unread,
Dec 2, 2015, 12:48:01 AM12/2/15
to RestExpress
Thank you guys!

It takes a lot of pain away!

David Taylor

unread,
Jan 29, 2016, 11:08:36 PM1/29/16
to RestExpress
Thanks for making this change. The new feature to specify the listening address is greatly appreciated!

After seeing the code change in github I went looking for RestExpress-0.11.3-SNAPSHOT, but it seems to be missing in the Maven repos. The artifact was indexed at some point since it shows in repo search results (e.g. http://mavensearch.io/repo/com.strategicgains/RestExpress/0.11.3-SNAPSHOT), but the artifact no longer exists. Was the snapshot removed for some reason?

Thanks,
David

Todd Fredrich

unread,
Feb 8, 2016, 4:04:44 PM2/8/16
to RestExpress
I'm using 0.11.3-SNAPSHOT right now and it's working fine.

Make sure you have this in your POM file:

<profiles>
    <profile>
       <id>allow-snapshots</id>
          <activation><activeByDefault>true</activeByDefault></activation>
       <repositories>
         <repository>
           <id>sonatype-snapshots-repo</id>
           <url>https://oss.sonatype.org/content/repositories/snapshots</url>
           <releases><enabled>false</enabled></releases>
           <snapshots><enabled>true</enabled></snapshots>
         </repository>
       </repositories>
     </profile>
  </profiles>
Reply all
Reply to author
Forward
0 new messages