mirror of
https://github.com/vx3r/wg-gen-web.git
synced 2025-09-11 12:24:27 +00:00
Initial commit
This commit is contained in:
88
util/tpl.go
Normal file
88
util/tpl.go
Normal file
@ -0,0 +1,88 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"gitlab.127-0-0-1.fr/vx3r/wg-gen-web/model"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
clientTpl = `
|
||||
[Interface]
|
||||
Address = {{.Client.Address}}
|
||||
PrivateKey = {{.Client.PrivateKey}}
|
||||
DNS = {{.Server.Dns}}
|
||||
[Peer]
|
||||
PublicKey = {{.Server.PublicKey}}
|
||||
PresharedKey = {{.Server.PresharedKey}}
|
||||
AllowedIPs = {{.Client.AllowedIPs}}
|
||||
Endpoint = {{.Server.Endpoint}}
|
||||
PersistentKeepalive = {{.Server.PersistentKeepalive}}`
|
||||
|
||||
wgTpl = `
|
||||
# {{.Server.Name}} / Updated: {{.Server.Updated}} / Created: {{.Server.Created}}
|
||||
[Interface]
|
||||
{{range .ServerAdresses}}
|
||||
Address = {{.}}
|
||||
{{end}}
|
||||
ListenPort = {{.Server.ListenPort}}
|
||||
PrivateKey = {{.Server.PrivateKey}}
|
||||
{{$server := .Server}}
|
||||
{{range .Clients}}
|
||||
{{if .Enable}}
|
||||
# {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}}
|
||||
[Peer]
|
||||
PublicKey = {{.PublicKey}}
|
||||
PresharedKey = {{$server.PresharedKey}}
|
||||
AllowedIPs = {{.Address}}
|
||||
{{end}}
|
||||
{{end}}`
|
||||
)
|
||||
|
||||
func DumpClient(client *model.Client, server *model.Server) (bytes.Buffer, error) {
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
t, err := template.New("client").Parse(clientTpl)
|
||||
if err != nil {
|
||||
return tplBuff, err
|
||||
}
|
||||
|
||||
return dump(t, struct {
|
||||
Client *model.Client
|
||||
Server *model.Server
|
||||
}{
|
||||
Client: client,
|
||||
Server: server,
|
||||
})
|
||||
}
|
||||
|
||||
func DumpServerWg(clients []*model.Client, server *model.Server) (bytes.Buffer, error) {
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
t, err := template.New("server").Parse(wgTpl)
|
||||
if err != nil {
|
||||
return tplBuff, err
|
||||
}
|
||||
|
||||
return dump(t, struct {
|
||||
Clients []*model.Client
|
||||
Server *model.Server
|
||||
ServerAdresses []string
|
||||
}{
|
||||
ServerAdresses: strings.Split(server.Address, ","),
|
||||
Clients: clients,
|
||||
Server: server,
|
||||
})
|
||||
}
|
||||
|
||||
func dump(tpl *template.Template , data interface{}) (bytes.Buffer, error) {
|
||||
var tplBuff bytes.Buffer
|
||||
|
||||
err := tpl.Execute(&tplBuff, data)
|
||||
if err != nil {
|
||||
return tplBuff, err
|
||||
}
|
||||
|
||||
return tplBuff, nil
|
||||
}
|
93
util/util.go
Normal file
93
util/util.go
Normal file
@ -0,0 +1,93 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ReadFile(path string) (bytes []byte, err error) {
|
||||
bytes, err = ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func WriteFile(path string, bytes []byte) (err error) {
|
||||
err = ioutil.WriteFile(path, bytes, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func FileExists(name string) bool {
|
||||
info, err := os.Stat(name)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func DirectoryExists(name string) bool {
|
||||
info, err := os.Stat(name)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return info.IsDir()
|
||||
}
|
||||
|
||||
func GetAvailableIp(cidr string, reserved []string) (string, error) {
|
||||
addresses, err := GetAllAddressesFromCidr(cidr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, addresse := range addresses {
|
||||
ok := true
|
||||
for _, r := range reserved {
|
||||
if addresse == r {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
return addresse, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("no more available address from cidr")
|
||||
}
|
||||
|
||||
func GetAllAddressesFromCidr(cidr string) ([]string, error) {
|
||||
ip, ipnet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ips []string
|
||||
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
|
||||
ips = append(ips, ip.String())
|
||||
}
|
||||
// remove network address and broadcast address (and server ip .1)
|
||||
return ips[2 : len(ips)-1], nil
|
||||
}
|
||||
|
||||
// http://play.golang.org/p/m8TNTtygK0
|
||||
func inc(ip net.IP) {
|
||||
for j := len(ip) - 1; j >= 0; j-- {
|
||||
ip[j]++
|
||||
if ip[j] > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func IsIPv6(address string) bool {
|
||||
return strings.Count(address, ":") >= 2
|
||||
}
|
Reference in New Issue
Block a user