0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-12-18 00:13:23 +00:00

issue #23 peer client preshared key, update dependencies

This commit is contained in:
vx3r 2020-03-18 15:57:15 +09:00
parent 38a284c7c8
commit 200e47b708
12 changed files with 484 additions and 348 deletions

View File

@ -142,7 +142,7 @@ func configClient(c *gin.Context) {
return
}
// return config as png qrcode
png, err := qrcode.Encode(string(configData), qrcode.Medium, 220)
png, err := qrcode.Encode(string(configData), qrcode.Medium, 250)
if err != nil {
log.WithFields(log.Fields{
"err": err,

View File

@ -42,6 +42,12 @@ func CreateClient(client *model.Client) (*model.Client, error) {
client.PrivateKey = key.String()
client.PublicKey = key.PublicKey().String()

presharedKey, err := wgtypes.GenerateKey()
if err != nil {
return nil, err
}
client.PresharedKey = presharedKey.String()

reserverIps, err := GetAllReservedIps()
if err != nil {
return nil, err

View File

@ -15,7 +15,7 @@ import (
)

// Migrate all changes, current struct fields change
func Migrate() error {
func MigrateInitialStructChange() error {
clients, err := readClients()
if err != nil {
return err
@ -98,7 +98,7 @@ func Migrate() error {
server.ListenPort = int(s["listenPort"].(float64))
server.PrivateKey = s["privateKey"].(string)
server.PublicKey = s["publicKey"].(string)
server.PresharedKey = s["presharedKey"].(string)
//server.PresharedKey = s["presharedKey"].(string)
server.Endpoint = s["endpoint"].(string)
server.PersistentKeepalive = int(s["persistentKeepalive"].(float64))
server.Dns = make([]string, 0)
@ -144,6 +144,122 @@ func Migrate() error {
return nil
}

// Migrate presharedKey issue #23
func MigratePresharedKey() error {
clients, err := readClients()
if err != nil {
return err
}

s, err := deserialize("server.json")
if err != nil {
return err
}

for _, client := range clients {
if _, ok := client["presharedKey"]; ok {
log.Infof("client %s has been already migrated for preshared key", client["id"])
continue
}

c := &model.Client{}
c.Id = client["id"].(string)
c.Name = client["name"].(string)
c.Email = client["email"].(string)
c.Enable = client["enable"].(bool)
c.IgnorePersistentKeepalive = client["ignorePersistentKeepalive"].(bool)
c.PresharedKey = s["presharedKey"].(string)
c.AllowedIPs = make([]string, 0)
for _, address := range client["allowedIPs"].([]interface{}) {
c.AllowedIPs = append(c.AllowedIPs, address.(string))
}
c.Address = make([]string, 0)
for _, address := range client["address"].([]interface{}) {
c.Address = append(c.Address, address.(string))
}
c.PrivateKey = client["privateKey"].(string)
c.PublicKey = client["publicKey"].(string)
created, err := time.Parse(time.RFC3339, client["created"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
continue
}
c.Created = created
updated, err := time.Parse(time.RFC3339, client["updated"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
continue
}
c.Updated = updated

err = storage.Serialize(c.Id, c)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to Serialize client")
}
}

if _, ok := s["presharedKey"]; ok {
server := &model.Server{}

server.Address = make([]string, 0)
server.Address = make([]string, 0)
for _, address := range s["address"].([]interface{}) {
server.Address = append(server.Address, address.(string))
}
server.ListenPort = int(s["listenPort"].(float64))
server.PrivateKey = s["privateKey"].(string)
server.PublicKey = s["publicKey"].(string)
server.Endpoint = s["endpoint"].(string)
server.PersistentKeepalive = int(s["persistentKeepalive"].(float64))
server.Dns = make([]string, 0)
for _, address := range s["dns"].([]interface{}) {
server.Dns = append(server.Dns, address.(string))
}
if val, ok := s["preUp"]; ok {
server.PreUp = val.(string)
}
if val, ok := s["postUp"]; ok {
server.PostUp = val.(string)
}
if val, ok := s["preDown"]; ok {
server.PreDown = val.(string)
}
if val, ok := s["postDown"]; ok {
server.PostDown = val.(string)
}
created, err := time.Parse(time.RFC3339, s["created"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
}
server.Created = created
updated, err := time.Parse(time.RFC3339, s["updated"].(string))
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to parse time")
}
server.Updated = updated

err = storage.Serialize("server.json", server)
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("failed to Serialize server")
}

}

return nil
}

func readClients() ([]map[string]interface{}, error) {
clients := make([]map[string]interface{}, 0)


View File

@ -25,12 +25,6 @@ func ReadServer() (*model.Server, error) {
server.PrivateKey = key.String()
server.PublicKey = key.PublicKey().String()

presharedKey, err := wgtypes.GenerateKey()
if err != nil {
return nil, err
}
server.PresharedKey = presharedKey.String()

server.Endpoint = "wireguard.example.com:123"
server.ListenPort = 51820

@ -91,7 +85,7 @@ func UpdateServer(server *model.Server) (*model.Server, error) {

server.PrivateKey = current.(*model.Server).PrivateKey
server.PublicKey = current.(*model.Server).PublicKey
server.PresharedKey = current.(*model.Server).PresharedKey
//server.PresharedKey = current.(*model.Server).PresharedKey
server.Updated = time.Now().UTC()

err = storage.Serialize("server.json", server)

15
go.mod
View File

@ -4,14 +4,27 @@ go 1.13

require (
github.com/danielkov/gin-helmet v0.0.0-20171108135313-1387e224435e
github.com/gin-contrib/cors v1.3.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2
github.com/gin-gonic/gin v1.5.0
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/golang/protobuf v1.3.5 // indirect
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.4.2
github.com/skip2/go-qrcode v0.0.0-20191027152451-9434209cb086
golang.org/x/crypto v0.0.0-20200317142112-1b76d66859c6 // indirect
golang.org/x/sys v0.0.0-20200317113312-5766fd39f98d // indirect
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200205215550-e35592f146e4
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v2 v2.2.8 // indirect
)

29
main.go
View File

@ -29,7 +29,7 @@ func main() {
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to initialize env")
}).Fatal("failed to load .env file")
}

// check directories or create it
@ -39,11 +39,11 @@ func main() {
log.WithFields(log.Fields{
"err": err,
"dir": filepath.Join(os.Getenv("WG_CONF_DIR")),
}).Fatal("failed to mkdir")
}).Fatal("failed to create directory")
}
}

// check if server.json exists otherwise create it
// check if server.json exists otherwise create it with default values
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
_, err = core.ReadServer()
if err != nil {
@ -66,23 +66,36 @@ func main() {
}

// migrate
err = core.Migrate()
err = core.MigrateInitialStructChange()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to migrate")
}).Fatal("failed to migrate initial struct changes")
}
err = core.MigratePresharedKey()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to migrate preshared key struct changes")
}

// dump wg config file
err = core.UpdateServerConfigWg()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("failed to dump wg config file")
}

// creates a gin router with default middleware: logger and recovery (crash-free) middleware
app := gin.Default()

// same as
// cors middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true
app.Use(cors.New(config))
//app.Use(cors.Default())

// protection
// protection middleware
app.Use(helmet.Default())

// no route redirect to frontend app

View File

@ -13,6 +13,7 @@ type Client struct {
Email string `json:"email"`
Enable bool `json:"enable"`
IgnorePersistentKeepalive bool `json:"ignorePersistentKeepalive"`
PresharedKey string `json:"presharedKey"`
AllowedIPs []string `json:"allowedIPs"`
Address []string `json:"address"`
PrivateKey string `json:"privateKey"`

View File

@ -13,7 +13,6 @@ type Server struct {
Mtu int `json:"mtu"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
PresharedKey string `json:"presharedKey"`
Endpoint string `json:"endpoint"`
PersistentKeepalive int `json:"persistentKeepalive"`
Dns []string `json:"dns"`

View File

@ -208,7 +208,7 @@ MTU = {{.Server.Mtu}}
{{- end}}
[Peer]
PublicKey = {{ .Server.PublicKey }}
PresharedKey = {{ .Server.PresharedKey }}
PresharedKey = {{ .Client.PresharedKey }}
AllowedIPs = {{ StringsJoin .Client.AllowedIPs ", " }}
Endpoint = {{ .Server.Endpoint }}
{{ if and (ne .Server.PersistentKeepalive 0) (not .Client.IgnorePersistentKeepalive) -}}
@ -230,13 +230,12 @@ PreUp = {{ .Server.PreUp }}
PostUp = {{ .Server.PostUp }}
PreDown = {{ .Server.PreDown }}
PostDown = {{ .Server.PostDown }}
{{ $server := .Server }}
{{- range .Clients }}
{{ if .Enable -}}
# {{.Name}} / {{.Email}} / Updated: {{.Updated}} / Created: {{.Created}}
[Peer]
PublicKey = {{ .PublicKey }}
PresharedKey = {{ $server.PresharedKey }}
PresharedKey = {{ .PresharedKey }}
AllowedIPs = {{ StringsJoin .Address ", " }}
{{- end }}
{{ end }}`

638
ui/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
"vue": "^2.6.10",
"vue-moment": "^4.1.0",
"vue-router": "^3.1.6",
"vuetify": "^2.2.17"
"vuetify": "^2.2.18"
},
"devDependencies": {
"@vue/cli-plugin-router": "^4.2.3",

View File

@ -15,11 +15,6 @@
label="Public key"
disabled
/>
<v-text-field
v-model="server.presharedKey"
label="Preshared key"
disabled
/>
<v-text-field
v-model="server.listenPort"
type="number"