bnhf / openvpn-admin-plus

Docker-based web interface (with golang backend) for monitoring and admin of an OpenVPN TAP/TUN server setup with PiVPN or other OpenVPN server installations. This project has been renamed from pivpn-tap-web-ui, to reflect its new broader scope.
MIT License
141 stars 23 forks source link

Spaces when importing to the name.ovpn file #32

Open karabelnikov opened 1 year ago

karabelnikov commented 1 year ago

Scott, in the files ca.cert, name.crt, name.key and ta.key the last line is always empty. When uploading the name.ovpn file, the file is generated from the openvpn-client-config.ovpn.tpl template.

When uploading the name.ovpn file, this empty string is also passed. It looks like in the screenshot. It is passed for all the above files (ca.cert, name.cert, name.the key and the.key). Can you add to the code so that the last empty line is always discarded?

So that there are no spaces between sections in the file?

Снимок экрана 2022-12-13 023726

bnhf commented 1 year ago

@karabelnikov

Shura - the extra space is added by EasyRSA when the keys and certs are generated, as far as I can tell, not by any of the OpenVPNAdmin Plus code. If I can find a standard Go library function that'll strip blank lines at the end easily, maybe we can do it -- but it'd be more work than it's worth otherwise.

karabelnikov commented 1 year ago

@bnhf

Scott, I agree with you, EasyRsa itself adds an extra empty line to the .crt and .key files. I'm just starting from the work of the script, where an empty line is cut out before the formation of the .ovpn file. I would like to implement this in our project. I understand that it is possible to separate the sections themselves with an empty string, like so:

<ca>
{{.ca}}
</ca>
...
<crt>
{{.crt}}
</crt>
...
<key>
{{.key}}
</key>
...
<ta>
{{.key}}
</ta>

but here's an empty line before the closing section tag, I think it's not needed at all. Yes, these are trifles))) Sometimes such questions require difficult decisions.

karabelnikov commented 1 year ago

@bnhf

Found this solution in Go language:

func Replace(s, old, new string, n int) string Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, giving up to k+1 replacements for the k-rune string. If n < 0, there is no limit on the number of substitutions.

Example:

package main

import (
    "fmt"
    "strings"
)

func main() {

    var s = `line 1
line 2

line 3`

    s = strings.Replace(s, "\n\n", "\n", -1)

    fmt.Println(s)
}

Here's how it works: https://play.golang.org/p/lu5UI74SLo

karabelnikov commented 1 year ago

The more generic approach would be something like this might be.

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {
    s := `

    #### 

    ####

    ####

    ####

    `

    fmt.Println(regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n"))
}

Here's how it works: https://play.golang.org/p/uWyHfUIDw-o