Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate proper .ovpn. resolve #16 , resolve #20 #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# pack project by beego
# cd ../ && bee pack -be GOOS=linux

set -e

PKGFILE=openvpn-web-ui.tar.gz
Expand Down
22 changes: 22 additions & 0 deletions conf/openvpn-client-ovpn.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
client
remote {{ .ServerAddress }} {{ .Port }}
proto {{ .Proto }}
dev tun
remote-cert-tls server
comp-lzo
;auth-user-pass
persist-key
persist-tun
nobind
resolv-retry infinite
verb 3
mute 10
<ca>
{{ .Ca }}
</ca>
<cert>
{{ .Cert }}
</cert>
<key>
{{ .Key }}
</key>
27 changes: 16 additions & 11 deletions conf/openvpn-server-config.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
management {{ .Management }}
verb 3

port {{ .Port }}
proto {{ .Proto }}

dev tun

ca {{ .Ca }}
cert {{ .Cert }}
key {{ .Key }}
Expand All @@ -14,21 +13,27 @@ keysize {{ .Keysize }}
auth {{ .Auth }}
dh {{ .Dh }}

server 10.8.0.0 255.255.255.0
ifconfig-pool-persist {{ .IfconfigPoolPersist }}
push "route 10.8.0.0 255.255.255.0"
server 192.168.255.0 255.255.255.0
### Route Configurations Below
route 192.168.254.0 255.255.255.0

### Push Configurations Below
push "block-outside-dns"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
push "comp-lzo no"

dev tun
key-direction 0
keepalive {{ .Keepalive }}

comp-lzo
max-clients {{ .MaxClients }}

persist-key
persist-tun
user nobody
group nogroup
comp-lzo no
mute 10

log openvpn.log
verb 3
max-clients {{ .MaxClients }}

mute 10
log openvpn.log
64 changes: 64 additions & 0 deletions controllers/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package controllers

import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"time"

"github.com/adamwalach/go-openvpn/client/config"
Expand Down Expand Up @@ -47,6 +50,10 @@ func (c *CertificatesController) Download() {
if cfgPath, err := saveClientConfig(name); err == nil {
addFileToZip(zw, cfgPath)
}
if ovpnPath, err := saveClientOvpn(name); err == nil {
addFileToZip(zw, ovpnPath)
}

addFileToZip(zw, keysPath+"ca.crt")
addFileToZip(zw, keysPath+name+".crt")
addFileToZip(zw, keysPath+name+".key")
Expand Down Expand Up @@ -158,3 +165,60 @@ func saveClientConfig(name string) (string, error) {

return destPath, nil
}

func saveClientOvpn(name string) (string, error) {
cfg := config.New()
cfg.ServerAddress = models.GlobalCfg.ServerAddress
serverConfig := models.OVConfig{Profile: "default"}
serverConfig.Read("Profile")
cfg.Port = serverConfig.Port
cfg.Proto = serverConfig.Proto
cfg.Auth = serverConfig.Auth
cfg.Cipher = serverConfig.Cipher
cfg.Keysize = serverConfig.Keysize

keysPath := models.GlobalCfg.OVConfigPath + "keys/"
caFilePath := keysPath + "ca.crt"
certFilePath := keysPath + name + ".crt"
keyFilePath := keysPath + name + ".key"

if caByte, err := ioutil.ReadFile(caFilePath); err == nil {
cfg.Ca = string(caByte)
}
if certByte, err := ioutil.ReadFile(certFilePath); err == nil {
cfg.Cert = string(certByte)
}
if keyByte, err := ioutil.ReadFile(keyFilePath); err == nil {
cfg.Key = string(keyByte)
}

destPath := models.GlobalCfg.OVConfigPath + "keys/" + name + ".ovpn"
if err := saveToFile("conf/openvpn-client-ovpn.tpl",
cfg, destPath); err != nil {
beego.Error(err)
return "", err
}

return destPath, nil
}

//SaveToFile reads teamplate and writes result to destination file with text/template
func saveToFile(tplPath string, c config.Config, destPath string) error {
templateByte, err := ioutil.ReadFile(tplPath)
if err != nil {
return err
}

t := template.New("config")
temp, err := t.Parse(string(templateByte))
if err != nil {
return err
}

buf := new(bytes.Buffer)
temp.Execute(buf, c)

str := buf.String()
fmt.Printf(str)
return ioutil.WriteFile(destPath, []byte(str), 0644)
}