Skip to content

Single file config #18

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

Open
wants to merge 6 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
24 changes: 24 additions & 0 deletions conf/openvpn-client-config.ovpn.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
dev tun
persist-tun
persist-key
client
resolv-retry infinite
remote {{ .ServerAddress }} {{ .Port }} {{ .Proto }}
lport 0

cipher {{ .Cipher }}
keysize {{ .Keysize }}
auth {{ .Auth }}
tls-client

<ca>
{{ .Ca }}
</ca>
<cert>
{{ .Cert }}
</cert>
<key>
{{ .Key }}
</key>

comp-lzo
49 changes: 49 additions & 0 deletions controllers/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -33,6 +34,21 @@ func (c *CertificatesController) NestPrepare() {
}
}

// @router /certificates/single-config/:key [get]
func (c *CertificatesController) DownloadSingleConfig() {
name := c.GetString(":key")
filename := fmt.Sprintf("%s.ovpn", name)

c.Ctx.Output.Header("Content-Type", "text/plain")
c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))

keysPath := models.GlobalCfg.OVConfigPath + "keys/"
if cfgPath, err := saveClientSingleConfig(name, keysPath); err == nil {
c.Ctx.Output.Download(cfgPath, filename);
}

}

// @router /certificates/:key [get]
func (c *CertificatesController) Download() {
name := c.GetString(":key")
Expand Down Expand Up @@ -158,3 +174,36 @@ func saveClientConfig(name string) (string, error) {

return destPath, nil
}

func saveClientSingleConfig(name string, pathString string) (string, error) {
cfg := config.New()
cfg.ServerAddress = models.GlobalCfg.ServerAddress
cfg.Cert = readCert(pathString + name + ".crt");
cfg.Key = readCert(pathString + name + ".key");
cfg.Ca = readCert(pathString + "ca.crt");
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

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

return destPath, nil
}

func readCert(path string) (string) {
buff, err := ioutil.ReadFile(path) // just pass the file name
if err != nil {
beego.Error(err)
return "";
}
return string(buff);
}
7 changes: 7 additions & 0 deletions routers/commentsRouter_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func init() {
AllowHTTPMethods: []string{"get"},
Params: nil})

beego.GlobalControllerRouter["github.com/adamwalach/openvpn-web-ui/controllers:CertificatesController"] = append(beego.GlobalControllerRouter["github.com/adamwalach/openvpn-web-ui/controllers:CertificatesController"],
beego.ControllerComments{
Method: "DownloadSingleConfig",
Router: `/certificates/single-config/:key`,
AllowHTTPMethods: []string{"get"},
Params: nil})

beego.GlobalControllerRouter["github.com/adamwalach/openvpn-web-ui/controllers:CertificatesController"] = append(beego.GlobalControllerRouter["github.com/adamwalach/openvpn-web-ui/controllers:CertificatesController"],
beego.ControllerComments{
Method: "Get",
Expand Down

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

5 changes: 4 additions & 1 deletion views/certificates.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ <h3 class="box-title">Clients certificates</h3>
<tr>
<td>
<a href="{{urlfor "CertificatesController.Download" ":key" .Details.Name}}">
{{ .Details.Name }}
{{ .Details.Name }}.zip
</a>
<a href="{{urlfor "CertificatesController.DownloadSingleConfig" ":key" .Details.Name}}">
{{ .Details.Name }}.ovpn
</a>
</td>
<td>{{ .EntryType }}</td>
Expand Down