From 83675dfe743b3780904939c564575a428b4e1ca0 Mon Sep 17 00:00:00 2001 From: yqsas Date: Sun, 24 Mar 2019 12:34:47 +0800 Subject: [PATCH 1/2] generate proper .ovpn. resolve #16 , resolve #20 --- build/build.sh | 3 ++ conf/openvpn-client-ovpn.tpl | 22 +++++++++++++ controllers/certificates.go | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 conf/openvpn-client-ovpn.tpl diff --git a/build/build.sh b/build/build.sh index d0465faf..4efe8607 100755 --- a/build/build.sh +++ b/build/build.sh @@ -1,5 +1,8 @@ #!/bin/bash +# pack project by beego +# cd ../ && bee pack -be GOOS=linux + set -e PKGFILE=openvpn-web-ui.tar.gz diff --git a/conf/openvpn-client-ovpn.tpl b/conf/openvpn-client-ovpn.tpl new file mode 100644 index 00000000..941c9b38 --- /dev/null +++ b/conf/openvpn-client-ovpn.tpl @@ -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 }} + + +{{ .Cert }} + + +{{ .Key }} + \ No newline at end of file diff --git a/controllers/certificates.go b/controllers/certificates.go index 09deb48a..85519fb4 100644 --- a/controllers/certificates.go +++ b/controllers/certificates.go @@ -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" @@ -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") @@ -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) +} From c358d7eb6194e096775eb74ae2df6434bbc002d8 Mon Sep 17 00:00:00 2001 From: yqsas Date: Fri, 29 Mar 2019 15:42:30 +0800 Subject: [PATCH 2/2] Update openvpn-server-config.tpl --- conf/openvpn-server-config.tpl | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/conf/openvpn-server-config.tpl b/conf/openvpn-server-config.tpl index a6346b44..1f082311 100644 --- a/conf/openvpn-server-config.tpl +++ b/conf/openvpn-server-config.tpl @@ -1,10 +1,9 @@ management {{ .Management }} +verb 3 port {{ .Port }} proto {{ .Proto }} -dev tun - ca {{ .Ca }} cert {{ .Cert }} key {{ .Key }} @@ -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