Skip to content

Commit

Permalink
Merge branch 'release-v0.1.13'
Browse files Browse the repository at this point in the history
  • Loading branch information
cad committed Sep 3, 2017
2 parents 870dbc0 + 558e332 commit c897bb5
Show file tree
Hide file tree
Showing 24 changed files with 821 additions and 315 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log

## [v0.1.12](https://github.com/cad/ovpm/tree/v0.1.12) (2017-09-02)
## [v0.1.13](https://github.com/cad/ovpm/tree/v0.1.13) (2017-09-03)
[Full Changelog](https://github.com/cad/ovpm/compare/v0.1.12...v0.1.13)

**Implemented enhancements:**

- change dns to push to clients [\#41](https://github.com/cad/ovpm/issues/41)

## [v0.1.12](https://github.com/cad/ovpm/tree/v0.1.12) (2017-09-01)
[Full Changelog](https://github.com/cad/ovpm/compare/v0.1.11...v0.1.12)

**Implemented enhancements:**
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ With OVPM you can create and run an OpenVPN server, add/remove VPN users, genera

*This software is not stable yet. We recommend against using it for anything serious until, version 1.0 is released.*

**Roadmap**

- [x] OpenVPN management functionality
- [x] User management functionality
- [x] Network management functionality
- [x] Command Line Interface (CLI)
- [ ] Web User Interface (WebUI)
- [ ] Import/Export/Backup OVPM config
- [ ] Effortless client profile (.ovpn file) delivery over Web

## Installation
**from RPM (CentOS/Fedora):**

Expand Down Expand Up @@ -71,6 +81,12 @@ It complains about an error due to server not being initialized, it's completely

## Usage

**Demo**
Here is a little demo of what it looks on terminal to init the server, create a vpn user and generate **.ovpn** file for the created user.

[![asciicast](https://asciinema.org/a/136016.png)](https://asciinema.org/a/136016)


### Init Server
If you just installed the ovpm from scratch you have started the **ovpm server** (ovpmd) then now you need to initialize the server.

Expand Down
30 changes: 19 additions & 11 deletions api/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"os"
"time"

"google.golang.org/grpc"

Expand Down Expand Up @@ -178,16 +177,17 @@ func (s *VPNService) Status(ctx context.Context, req *pb.VPNStatusRequest) (*pb.
}

response := pb.VPNStatusResponse{
Name: server.Name,
SerialNumber: server.SerialNumber,
Hostname: server.Hostname,
Port: server.Port,
Name: server.GetServerName(),
SerialNumber: server.GetSerialNumber(),
Hostname: server.GetHostname(),
Port: server.GetPort(),
Proto: server.GetProto(),
Cert: server.Cert,
CACert: server.CACert,
Net: server.Net,
Mask: server.Mask,
CreatedAt: server.CreatedAt.Format(time.UnixDate),
Cert: server.GetCert(),
CACert: server.GetCACert(),
Net: server.GetNet(),
Mask: server.GetMask(),
CreatedAt: server.GetCreatedAt(),
DNS: server.GetDNS(),
}
return &response, nil
}
Expand All @@ -204,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.

4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ cp $DIR/contrib/yumrepo.repo $RELEASEDIR/rpm/ovpm.repo
cp $DIR/contrib/deb-repo-config $RELEASEDIR/deb/conf/distributions

#package
fpm -s dir -t rpm -n ovpm --version `git name-rev --tags --name-only $(git rev-parse HEAD) | cut -d 'v' -f 2` --iteration $RELEASEVER --depends openvpn --description "OVPM makes all aspects of OpenVPN server administration a breeze." --after-install $DIR/contrib/afterinstall.sh --before-remove $DIR/contrib/beforeremove.sh -p $RELEASEDIR/rpm -C $RELEASEDIR/build .
fpm -s dir -t rpm -n ovpm --version `git name-rev --tags --name-only $(git rev-parse HEAD) | cut -d 'v' -f 2` --iteration $RELEASEVER --depends openvpn --description "OVPM makes all aspects of OpenVPN server administration a breeze." --after-install $DIR/contrib/afterinstall.sh --before-remove $DIR/contrib/beforeremove.sh --after-upgrade $DIR/contrib/afterupgrade.sh -p $RELEASEDIR/rpm -C $RELEASEDIR/build .

fpm -s dir -t deb -n ovpm --version `git name-rev --tags --name-only $(git rev-parse HEAD) | cut -d 'v' -f 2` --iteration $RELEASEVER --depends openvpn --description "OVPM makes all aspects of OpenVPN server administration a breeze." --after-install $DIR/contrib/afterinstall.sh --before-remove $DIR/contrib/beforeremove.sh -p $RELEASEDIR/deb -C $RELEASEDIR/build .
fpm -s dir -t deb -n ovpm --version `git name-rev --tags --name-only $(git rev-parse HEAD) | cut -d 'v' -f 2` --iteration $RELEASEVER --depends openvpn --description "OVPM makes all aspects of OpenVPN server administration a breeze." --after-install $DIR/contrib/afterinstall.sh --before-remove $DIR/contrib/beforeremove.sh --after-upgrade $DIR/contrib/afterupgrade.sh -p $RELEASEDIR/deb -C $RELEASEDIR/build .

#create rpm repo
createrepo --database $RELEASEDIR/rpm
Expand Down
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
},
}
5 changes: 3 additions & 2 deletions cmd/ovpmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
)

var action string
var db *ovpm.DB

func main() {
app := cli.NewApp()
Expand All @@ -45,11 +46,11 @@ func main() {
if c.GlobalBool("verbose") {
logrus.SetLevel(logrus.DebugLevel)
}
ovpm.SetupDB("sqlite3", "")
db = ovpm.CreateDB("sqlite3", "")
return nil
}
app.After = func(c *cli.Context) error {
ovpm.CeaseDB()
db.Cease()
return nil
}
app.Action = func(c *cli.Context) error {
Expand Down
5 changes: 4 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ovpm

const (
// Version defines the version of ovpm.
Version = "0.1.12"
Version = "0.1.13"

// DefaultVPNPort is the default OpenVPN port to listen.
DefaultVPNPort = "1197"
Expand All @@ -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
4 changes: 4 additions & 0 deletions contrib/afterupgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if [ "`systemctl is-active ovpmd`" != "active" ]
then
systemctl restart ovpmd
fi
32 changes: 21 additions & 11 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,40 @@ import (
_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var db *gorm.DB
var db *DB

// SetupDB prepares database for use.
// DB represents a persistent storage.
type DB struct {
*gorm.DB
}

// CreateDB prepares and returns new storage.
//
// It should be run at the start of the program.
func SetupDB(dialect string, args ...interface{}) {
func CreateDB(dialect string, args ...interface{}) *DB {
if len(args) > 0 && args[0] == "" {
args[0] = _DefaultDBPath
}
var err error
db, err = gorm.Open(dialect, args...)

dbase, err := gorm.Open(dialect, args...)
if err != nil {
logrus.Fatalf("couldn't open sqlite database %v: %v", args, err)
}

db.AutoMigrate(&DBUser{})
db.AutoMigrate(&DBServer{})
db.AutoMigrate(&DBRevoked{})
db.AutoMigrate(&DBNetwork{})
dbase.AutoMigrate(&dbUserModel{})
dbase.AutoMigrate(&dbServerModel{})
dbase.AutoMigrate(&dbRevokedModel{})
dbase.AutoMigrate(&dbNetworkModel{})

dbPTR := &DB{DB: dbase}
db = dbPTR
return dbPTR
}

// CeaseDB closes the database.
// Cease closes the database.
//
// It should be run at the exit of the program.
func CeaseDB() {
db.Close()
func (db *DB) Cease() {
db.DB.Close()
}
10 changes: 5 additions & 5 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestDBSetup(t *testing.T) {
// Test:

// Create database.
SetupDB("sqlite3", ":memory:")
CreateDB("sqlite3", ":memory:")

// Is database created?
if db == nil {
Expand All @@ -23,15 +23,15 @@ func TestDBCease(t *testing.T) {
Testing = true

// Prepare:
SetupDB("sqlite3", ":memory:")
user := DBUser{Username: "testUser"}
CreateDB("sqlite3", ":memory:")
user := dbUserModel{Username: "testUser"}
db.Save(&user)

// Test:
// Close database.
CeaseDB()
db.Cease()

var users []DBUser
var users []dbUserModel
db.Find(&users)

// Is length zero?
Expand Down
Loading

0 comments on commit c897bb5

Please sign in to comment.