Skip to content

Commit

Permalink
Merge branch 'feat/change-dns-41' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cad committed Sep 3, 2017
2 parents 9a5dc2a + 27773c9 commit ee82e38
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 81 deletions.
11 changes: 10 additions & 1 deletion api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (s *VPNService) Status(ctx context.Context, req *pb.VPNStatusRequest) (*pb.
Net: server.GetNet(),
Mask: server.GetMask(),
CreatedAt: server.GetCreatedAt(),
DNS: server.GetDNS(),
}
return &response, nil
}
Expand All @@ -203,12 +204,20 @@ func (s *VPNService) Init(ctx context.Context, req *pb.VPNInitRequest) (*pb.VPNI
proto = ovpm.UDPProto
}

if err := ovpm.Init(req.Hostname, req.Port, proto, req.IPBlock); err != nil {
if err := ovpm.Init(req.Hostname, req.Port, proto, req.IPBlock, req.DNS); err != nil {
logrus.Errorf("server can not be created: %v", err)
}
return &pb.VPNInitResponse{}, nil
}

func (s *VPNService) Update(ctx context.Context, req *pb.VPNUpdateRequest) (*pb.VPNUpdateResponse, error) {
logrus.Debugf("rpc call: vpn update")
if err := ovpm.Update(req.IPBlock, req.DNS); err != nil {
logrus.Errorf("server can not be updated: %v", err)
}
return &pb.VPNUpdateResponse{}, nil
}

type NetworkService struct{}

func (s *NetworkService) List(ctx context.Context, req *pb.NetworkListRequest) (*pb.NetworkListResponse, error) {
Expand Down
6 changes: 3 additions & 3 deletions bindata/bindata.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cmd/ovpm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
Subcommands: []cli.Command{
vpnStatusCommand,
vpnInitCommand,
vpnUpdateCommand,
},
},
{
Expand Down
68 changes: 67 additions & 1 deletion cmd/ovpm/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var vpnStatusCommand = cli.Command{
table.Append([]string{"Network", res.Net})
table.Append([]string{"Netmask", res.Mask})
table.Append([]string{"Created At", res.CreatedAt})
table.Append([]string{"DNS", res.DNS})
table.Render()

return nil
Expand Down Expand Up @@ -65,6 +66,10 @@ var vpnInitCommand = cli.Command{
Name: "net, n",
Usage: fmt.Sprintf("VPN network to give clients IP addresses from, in the CIDR form (default: %s)", ovpm.DefaultVPNNetwork),
},
cli.StringFlag{
Name: "dns, d",
Usage: fmt.Sprintf("DNS server to push to clients (default: %s)", ovpm.DefaultVPNDNS),
},
},
Action: func(c *cli.Context) error {
action = "vpn:init"
Expand Down Expand Up @@ -96,6 +101,14 @@ var vpnInitCommand = cli.Command{
os.Exit(1)
}

dns := c.String("dns")
if dns != "" && !govalidator.IsIPv4(dns) {
fmt.Println("--dns takes an IPv4 address. e.g. 8.8.8.8")
fmt.Println()
fmt.Println(cli.ShowSubcommandHelp(c))
os.Exit(1)
}

conn := getConn(c.GlobalString("daemon-port"))
defer conn.Close()
vpnSvc := pb.NewVPNServiceClient(conn)
Expand All @@ -115,7 +128,7 @@ var vpnInitCommand = cli.Command{
okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
nokayResponses := []string{"n", "N", "no", "No", "NO"}
if stringInSlice(response, okayResponses) {
if _, err := vpnSvc.Init(context.Background(), &pb.VPNInitRequest{Hostname: hostname, Port: port, Protopref: proto, IPBlock: ipblock}); err != nil {
if _, err := vpnSvc.Init(context.Background(), &pb.VPNInitRequest{Hostname: hostname, Port: port, Protopref: proto, IPBlock: ipblock, DNS: dns}); err != nil {
logrus.Errorf("server can not be initialized: %v", err)
os.Exit(1)
return err
Expand All @@ -130,3 +143,56 @@ var vpnInitCommand = cli.Command{
return nil
},
}

var vpnUpdateCommand = cli.Command{
Name: "update",
Usage: "Update VPN server.",
Aliases: []string{"i"},
Flags: []cli.Flag{
cli.StringFlag{
Name: "net, n",
Usage: fmt.Sprintf("VPN network to give clients IP addresses from, in the CIDR form (default: %s)", ovpm.DefaultVPNNetwork),
},
cli.StringFlag{
Name: "dns, d",
Usage: fmt.Sprintf("DNS server to push to clients (default: %s)", ovpm.DefaultVPNDNS),
},
},
Action: func(c *cli.Context) error {
action = "vpn:update"

ipblock := c.String("net")
if ipblock != "" && !govalidator.IsCIDR(ipblock) {
fmt.Println("--net takes an ip network in the CIDR form. e.g. 10.9.0.0/24")
fmt.Println()
fmt.Println(cli.ShowSubcommandHelp(c))
os.Exit(1)
}

dns := c.String("dns")
if dns != "" && !govalidator.IsIPv4(dns) {
fmt.Println("--dns takes an IPv4 address. e.g. 8.8.8.8")
fmt.Println()
fmt.Println(cli.ShowSubcommandHelp(c))
os.Exit(1)
}

if !(ipblock != "" || dns != "") {
fmt.Println()
fmt.Println(cli.ShowSubcommandHelp(c))
os.Exit(1)
}

conn := getConn(c.GlobalString("daemon-port"))
defer conn.Close()
vpnSvc := pb.NewVPNServiceClient(conn)

if _, err := vpnSvc.Update(context.Background(), &pb.VPNUpdateRequest{IPBlock: ipblock, DNS: dns}); err != nil {
logrus.Errorf("server can not be updated: %v", err)
os.Exit(1)
return err
}
logrus.Info("ovpm server updated")
return nil
},
}
3 changes: 3 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
// DefaultVPNNetwork is the default OpenVPN network to use.
DefaultVPNNetwork = "10.9.0.0/24"

// DefaultVPNDNS is the default DNS to push to clients.
DefaultVPNDNS = "8.8.8.8"

etcBasePath = "/etc/ovpm/"
varBasePath = "/var/db/ovpm/"

Expand Down
14 changes: 7 additions & 7 deletions net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestVPNCreateNewNetwork(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestVPNDeleteNetwork(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestVPNGetNetwork(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestVPNGetAllNetworks(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestNetAssociate(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestNetDissociate(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
err := Init("localhost", "", UDPProto, "")
err := Init("localhost", "", UDPProto, "", "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestNetGetAssociatedUsers(t *testing.T) {
setupTestCase()
CreateDB("sqlite3", ":memory:")
defer db.Cease()
Init("localhost", "", UDPProto, "")
Init("localhost", "", UDPProto, "", "")

// Prepare:
// Test:
Expand Down
2 changes: 2 additions & 0 deletions pb/user.pb.go

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

0 comments on commit ee82e38

Please sign in to comment.