Skip to content

Commit

Permalink
Merge branch 'feat/lzo-choice-update' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cad committed Jul 27, 2020
2 parents 7d1951e + 133e350 commit 2d66a25
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 61 deletions.
141 changes: 95 additions & 46 deletions api/pb/vpn.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions api/pb/vpn.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ enum VPNProto {
TCP = 2;
}

enum VPNLZOPref {
USE_LZO_NOPREF = 0;
USE_LZO_ENABLE = 1;
USE_LZO_DISABLE= 3;
}

message VPNStatusRequest {}
message VPNInitRequest {
string hostname = 1;
Expand All @@ -25,6 +31,7 @@ message VPNInitRequest {
message VPNUpdateRequest {
string ip_block = 1;
string dns = 2;
VPNLZOPref lzo_pref = 3;
}
message VPNRestartRequest {}

Expand Down Expand Up @@ -68,6 +75,7 @@ message VPNStatusResponse {
string dns = 11;
string expires_at = 12;
string ca_expires_at = 13;
bool use_lzo = 14;
}
message VPNInitResponse {}
message VPNUpdateResponse {}
Expand Down
13 changes: 11 additions & 2 deletions api/rpc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"go.uber.org/thriftrw/ptr"
"os"
"time"

Expand Down Expand Up @@ -132,7 +133,7 @@ func (s *UserService) Create(ctx context.Context, req *pb.UserCreateRequest) (*p
NoGw: user.IsNoGW(),
HostId: user.GetHostID(),
IsAdmin: user.IsAdmin(),
Description: user.GetDescription(),
Description: user.GetDescription(),
}
ut = append(ut, &pbUser)

Expand Down Expand Up @@ -354,6 +355,7 @@ func (s *VPNService) Status(ctx context.Context, req *pb.VPNStatusRequest) (*pb.
Dns: server.GetDNS(),
ExpiresAt: server.ExpiresAt().UTC().Format(time.RFC3339),
CaExpiresAt: server.CAExpiresAt().UTC().Format(time.RFC3339),
UseLzo: server.IsUseLZO(),
}
return &response, nil
}
Expand Down Expand Up @@ -396,7 +398,14 @@ func (s *VPNService) Update(ctx context.Context, req *pb.VPNUpdateRequest) (*pb.
return nil, grpc.Errorf(codes.PermissionDenied, "ovpm.UpdateVPNPerm is required for this operation.")
}

if err := ovpm.TheServer().Update(req.IpBlock, req.Dns); err != nil {
var useLzo *bool
switch req.LzoPref {
case pb.VPNLZOPref_USE_LZO_ENABLE:
useLzo = ptr.Bool(true)
case pb.VPNLZOPref_USE_LZO_DISABLE:
useLzo = ptr.Bool(false)
}
if err := ovpm.TheServer().Update(req.IpBlock, req.Dns, useLzo); err != nil {
logrus.Errorf("server can not be updated: %v", err)
}
return &pb.VPNUpdateResponse{}, nil
Expand Down
25 changes: 21 additions & 4 deletions cmd/ovpm/action_vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func vpnStatusAction(rpcServURLStr string) error {
table.Append([]string{"DNS", vpnStatusResp.Dns})
table.Append([]string{"Cert Exp", vpnStatusResp.ExpiresAt})
table.Append([]string{"CA Cert Exp", vpnStatusResp.CaExpiresAt})
table.Append([]string{"Use LZO", fmt.Sprintf("%t", vpnStatusResp.UseLzo)})

table.Render()

return nil
Expand Down Expand Up @@ -117,7 +119,7 @@ func vpnInitAction(params vpnInitParams) error {
return nil
}

func vpnUpdateAction(rpcServURLStr string, netCIDR *string, dnsAddr *string) error {
func vpnUpdateAction(rpcServURLStr string, netCIDR *string, dnsAddr *string, useLzo *bool) error {
// Parse RPC Server's URL.
rpcSrvURL, err := url.Parse(rpcServURLStr)
if err != nil {
Expand Down Expand Up @@ -171,13 +173,27 @@ func vpnUpdateAction(rpcServURLStr string, netCIDR *string, dnsAddr *string) err
targetDNSAddr = *dnsAddr
}

// Set USE-LZO preference if provided.
var targetLZOPref pb.VPNLZOPref
if useLzo == nil {
targetLZOPref = pb.VPNLZOPref_USE_LZO_NOPREF
} else {
if *useLzo == true {
targetLZOPref = pb.VPNLZOPref_USE_LZO_ENABLE
}
if *useLzo == false {
targetLZOPref = pb.VPNLZOPref_USE_LZO_DISABLE
}
}

// Prepare service caller.
var vpnSvc = pb.NewVPNServiceClient(rpcConn)

// Request update request from vpn service.
_, err = vpnSvc.Update(context.Background(), &pb.VPNUpdateRequest{
IpBlock: targetNetCIDR,
Dns: targetDNSAddr,
LzoPref: targetLZOPref,
})
if err != nil {
err := errors.UnknownGRPCError(err)
Expand All @@ -186,9 +202,10 @@ func vpnUpdateAction(rpcServURLStr string, netCIDR *string, dnsAddr *string) err
}

logrus.WithFields(logrus.Fields{
"SERVER": "OpenVPN",
"CIDR": targetNetCIDR,
"DNS": targetDNSAddr,
"SERVER": "OpenVPN",
"CIDR": targetNetCIDR,
"DNS": targetDNSAddr,
"USE_LZO": targetLZOPref.String(),
}).Infoln("changes applied")

return nil
Expand Down
26 changes: 24 additions & 2 deletions cmd/ovpm/cmd_vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package main

import (
"fmt"

"github.com/asaskevich/govalidator"
"github.com/cad/ovpm"
"github.com/cad/ovpm/api/pb"
"github.com/cad/ovpm/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.uber.org/thriftrw/ptr"
)

var vpnStatusCommand = cli.Command{
Expand Down Expand Up @@ -204,6 +204,14 @@ var vpnUpdateCommand = cli.Command{
Name: "dns, d",
Usage: fmt.Sprintf("DNS server to push to clients (default: %s)", ovpm.DefaultVPNDNS),
},
cli.BoolFlag{
Name: "enable-use-lzo",
Usage: fmt.Sprintf("Enable use of the deprecated lzo compression algorithm to support older clients."),
},
cli.BoolFlag{
Name: "disable-use-lzo",
Usage: fmt.Sprintf("Disable use of the deprecated lzo compression algorithm to support older clients."),
},
},
Action: func(c *cli.Context) error {
action = "vpn:update"
Expand All @@ -223,12 +231,26 @@ var vpnUpdateCommand = cli.Command{
dnsAddr = &dns
}

var useLzo *bool
if c.Bool("enable-use-lzo") && c.Bool("disable-use-lzo") {
e := fmt.Errorf("can not use --enable-use-lzo and --disable-use-lzo together")
fmt.Println(e.Error())
exit(1)
return e
}
if enableLzo := c.Bool("enable-use-lzo"); enableLzo {
useLzo = ptr.Bool(true)
}
if disableLzo := c.Bool("disable-use-lzo"); disableLzo {
useLzo = ptr.Bool(false)
}

// If dry run, then don't call the action, just preprocess.
if c.GlobalBool("dry-run") {
return nil
}

return vpnUpdateAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), netCIDR, dnsAddr)
return vpnUpdateAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), netCIDR, dnsAddr, useLzo)
},
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/stretchr/testify v1.5.1
github.com/urfave/cli v1.22.3
go.mongodb.org/mongo-driver v1.3.1 // indirect
go.uber.org/thriftrw v1.24.0
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect
golang.org/x/net v0.0.0-20200319234117-63522dbf7eec
golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d // indirect
Expand Down
Loading

0 comments on commit 2d66a25

Please sign in to comment.