Skip to content

Commit

Permalink
Set hoverfly to listen on localhost only by default and provide flag …
Browse files Browse the repository at this point in the history
…to set specific interface to bind to
  • Loading branch information
tommysitu committed Jan 5, 2018
1 parent 4e665ec commit cc46916
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (this *AdminApi) StartAdminInterface(hoverfly *Hoverfly) {
"AdminPort": hoverfly.Cfg.AdminPort,
}).Info("Admin interface is starting...")

http.ListenAndServe(fmt.Sprintf(":%s", hoverfly.Cfg.AdminPort), n)
http.ListenAndServe(fmt.Sprintf("%s:%s", hoverfly.Cfg.ListenOnHost, hoverfly.Cfg.AdminPort), n)
}

// Will add the handlers to the router.
Expand Down
9 changes: 9 additions & 0 deletions core/cmd/hoverfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var (
middleware = flag.String("middleware", "", "Should proxy use middleware")
proxyPort = flag.String("pp", "", "Proxy port - run proxy on another port (i.e. '-pp 9999' to run proxy on port 9999)")
adminPort = flag.String("ap", "", "Admin port - run admin interface on another port (i.e. '-ap 1234' to run admin UI on port 1234)")
listenOnHost= flag.String("listen-on-host", "", "Specify which network interface to bind to, eg. 0.0.0.0 will bind to all interfaces. By default hoverfly will only bind ports to loopback interface")
metrics = flag.Bool("metrics", false, "Supply -metrics flag to enable metrics logging to stdout")
dev = flag.Bool("dev", false, "Enable CORS headers to allow frontend development")
destination = flag.String("destination", ".", "Destination URI to catch")
Expand Down Expand Up @@ -244,6 +245,14 @@ func main() {
}).Info("Default admin port has been overwritten")
}

if *listenOnHost != "" {
cfg.ListenOnHost = *listenOnHost

log.WithFields(log.Fields{
"host": *listenOnHost,
}).Info("Listen on specific interface")
}

// overriding environment variable (external proxy)
if *upstreamProxy != "" {
cfg.SetUpstreamProxy(*upstreamProxy)
Expand Down
2 changes: 1 addition & 1 deletion core/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (hf *Hoverfly) StartProxy() error {
}).Info("current proxy configuration")

// creating TCP listener
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", hf.Cfg.ProxyPort))
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", hf.Cfg.ListenOnHost, hf.Cfg.ProxyPort))
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions core/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Configuration struct {
AdminPort string
ProxyPort string
ListenOnHost string
Mode string
Destination string
Middleware middleware.Middleware
Expand Down Expand Up @@ -72,6 +73,8 @@ const DefaultPort = "8500"
// DefaultAdminPort - default admin interface port
const DefaultAdminPort = "8888"

const DefaultListenOnHost = "127.0.0.1"

// DefaultDatabasePath - default database name that will be created
// or used by Hoverfly
const DefaultDatabasePath = "requests.db"
Expand Down Expand Up @@ -120,6 +123,8 @@ func InitSettings() *Configuration {
appConfig.ProxyPort = DefaultPort
}

appConfig.ListenOnHost = DefaultListenOnHost

// getting external proxy
if os.Getenv(HoverflyUpstreamProxyPortEV) != "" {
appConfig.UpstreamProxy = os.Getenv(HoverflyUpstreamProxyPortEV)
Expand Down
7 changes: 7 additions & 0 deletions core/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func TestSettingsDefaultProxyPort(t *testing.T) {
Expect(cfg.ProxyPort).To(Equal(DefaultPort))
}

func TestSettingsDefaultListenOnHost(t *testing.T) {
RegisterTestingT(t)

cfg := InitSettings()
Expect(cfg.ListenOnHost).To(Equal("127.0.0.1"))
}

func TestSettingsMiddlewareEnv(t *testing.T) {
RegisterTestingT(t)

Expand Down
3 changes: 2 additions & 1 deletion hoverctl/wrapper/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func runBinary(target *configuration.Target, path string) (*exec.Cmd, error) {
}

func Start(target *configuration.Target) error {
// TODO only check port if is it localhost
err := checkPorts(target.AdminPort, target.ProxyPort)
if err != nil {
return err
Expand Down Expand Up @@ -322,7 +323,7 @@ func doRequest(target configuration.Target, method, url, body string, headers ma

func checkPorts(ports ...int) error {
for _, port := range ports {
server, err := net.Listen("tcp", ":"+strconv.Itoa(port))
server, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
if err != nil {
return fmt.Errorf("Could not start Hoverfly\n\nPort %v was not free", port)
}
Expand Down

0 comments on commit cc46916

Please sign in to comment.