Skip to content

Commit

Permalink
refactor(cmd/ovpmd): force listeners on tcp4 network
Browse files Browse the repository at this point in the history
Closes #108
  • Loading branch information
cad committed Apr 4, 2021
1 parent 9bbfc45 commit ab8b02d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [Unreleased]

- Make sure ovpmd listens on IPv4 interfaces [#108](https://github.com/cad/ovpm/issues/108)

## [v0.2.10](https://github.com/cad/ovpm/tree/v0.2.10)

- Fix ovpmd not starting error on Ubuntu. [#99](https://github.com/cad/ovpm/issues/99)
Expand Down
13 changes: 10 additions & 3 deletions cmd/ovpmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func main() {
type server struct {
grpcPort string
lis net.Listener
restLis net.Listener
grpcServer *grpc.Server
restServer http.Handler
restCancel context.CancelFunc
Expand All @@ -99,14 +100,19 @@ func newServer(port, webPort string) *server {
}()
if !ovpm.Testing {
// NOTE(cad): gRPC endpoint listens on localhost. This is important
// because we don't authanticate requests coming from localhost.
// because we don't authenticate requests coming from localhost.
// So gRPC endpoint should never listen on something else then
// localhost.
lis, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%s", port))
lis, err := net.Listen("tcp4", fmt.Sprintf("127.0.0.1:%s", port))
if err != nil {
logrus.Fatalf("could not listen to port %s: %v", port, err)
}

restLis, err := net.Listen("tcp4", fmt.Sprintf("0.0.0.0:%s", webPort))
if err != nil {
logrus.Fatalf("could not listen to port %s: %v", webPort, err)
}

rpcServer := api.NewRPCServer()
restServer, restCancel, err := api.NewRESTServer(port)
if err != nil {
Expand All @@ -115,6 +121,7 @@ func newServer(port, webPort string) *server {

return &server{
lis: lis,
restLis: restLis,
grpcServer: rpcServer,
restServer: restServer,
restCancel: context.CancelFunc(restCancel),
Expand All @@ -131,7 +138,7 @@ func newServer(port, webPort string) *server {
func (s *server) start() {
logrus.Infof("OVPM %s is running gRPC:%s, REST:%s ...", ovpm.Version, s.grpcPort, s.restPort)
go s.grpcServer.Serve(s.lis)
go http.ListenAndServe(":"+s.restPort, s.restServer)
go http.Serve(s.restLis, s.restServer)
ovpm.TheServer().StartVPNProc()
}

Expand Down

0 comments on commit ab8b02d

Please sign in to comment.