Skip to content

Commit

Permalink
fix(api): Listen on localhost for API and RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 28, 2019
1 parent d6e5787 commit 04a4500
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (

var log = golog.Logger("qriapi")

// LocalHostIP is the IP address for localhost
const LocalHostIP = "127.0.0.1"

func init() {
// We don't use the log package, and the net/rpc package spits out some complaints b/c
// a few methods don't conform to the proper signature (comment this out & run 'qri connect' to see errors)
Expand Down Expand Up @@ -127,7 +130,7 @@ func (s *Server) ServeRPC() {
return
}

listener, err := net.Listen("tcp", fmt.Sprintf(":%d", s.cfg.RPC.Port))
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", LocalHostIP, s.cfg.RPC.Port))
if err != nil {
log.Infof("RPC listen on port %d error: %s", s.cfg.RPC.Port, err)
return
Expand Down
6 changes: 5 additions & 1 deletion api/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
// that should work just fine on the raw internet (ie not behind a proxy like nginx etc)
// it'll also redirect http traffic to it's https route counterpart if port 80 is open
func StartServer(c *config.API, s *http.Server) error {
s.Addr = fmt.Sprintf(fmt.Sprintf(":%d", c.Port))
if c.ServeRemoteTraffic {
s.Addr = fmt.Sprintf(":%d", c.Port)
} else {
s.Addr = fmt.Sprintf("%s:%d", LocalHostIP, c.Port)
}
if !c.Enabled || c.Port == 0 {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type API struct {
ProxyForceHTTPS bool `json:"proxyforcehttps"`
// support CORS signing from a list of origins
AllowedOrigins []string `json:"allowedorigins"`
// whether to allow requests from addresses other than localhost
ServeRemoteTraffic bool `json:"serveremotetraffic"`
}

// Validate validates all fields of api returning all errors found.
Expand Down

0 comments on commit 04a4500

Please sign in to comment.