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

go fmt / badges

This commit is contained in:
vx3r 2020-01-30 16:21:45 +09:00
parent a1828bfcd8
commit 38c86f3271

View File

@ -16,9 +16,7 @@ import (
"time" "time"
) )


/* // CreateClient client with all necessary data
* CreateClient client with all necessary data
*/
func CreateClient(client *model.Client) (*model.Client, error) { func CreateClient(client *model.Client) (*model.Client, error) {
u := uuid.NewV4() u := uuid.NewV4()
client.Id = u.String() client.Id = u.String()
@ -85,9 +83,7 @@ func CreateClient(client *model.Client) (*model.Client, error) {
return client, nil return client, nil
} }


/* // ReadClient client by id
* ReadClient client by id
*/
func ReadClient(id string) (*model.Client, error) { func ReadClient(id string) (*model.Client, error) {
v, err := deserialize(id) v, err := deserialize(id)
if err != nil { if err != nil {
@ -98,9 +94,7 @@ func ReadClient(id string) (*model.Client, error) {
return client, nil return client, nil
} }


/* // ReadClientConfig in wg format
* ReadClientConfig in wg format
*/
func ReadClientConfig(id string) ([]byte, error) { func ReadClientConfig(id string) ([]byte, error) {
client, err := ReadClient(id) client, err := ReadClient(id)
if err != nil { if err != nil {
@ -120,9 +114,7 @@ func ReadClientConfig(id string) ([]byte, error) {
return configDataWg.Bytes(), nil return configDataWg.Bytes(), nil
} }


/* // UpdateClient preserve keys
* UpdateClient preserve keys
*/
func UpdateClient(Id string, client *model.Client) (*model.Client, error) { func UpdateClient(Id string, client *model.Client) (*model.Client, error) {
v, err := deserialize(Id) v, err := deserialize(Id)
if err != nil { if err != nil {
@ -153,9 +145,7 @@ func UpdateClient(Id string, client *model.Client) (*model.Client, error) {
return client, nil return client, nil
} }


/* // DeleteClient from disk
* DeleteClient from disk
*/
func DeleteClient(id string) error { func DeleteClient(id string) error {
path := filepath.Join(os.Getenv("WG_CONF_DIR"), id) path := filepath.Join(os.Getenv("WG_CONF_DIR"), id)
err := os.Remove(path) err := os.Remove(path)
@ -167,9 +157,7 @@ func DeleteClient(id string) error {
return generateWgConfig() return generateWgConfig()
} }


/* // ReadClients all clients
* ReadClients all clients
*/
func ReadClients() ([]*model.Client, error) { func ReadClients() ([]*model.Client, error) {
clients := make([]*model.Client, 0) clients := make([]*model.Client, 0)


@ -201,9 +189,7 @@ func ReadClients() ([]*model.Client, error) {
return clients, nil return clients, nil
} }


/* // ReadServer object, create default one
* ReadServer object, create default one
*/
func ReadServer() (*model.Server, error) { func ReadServer() (*model.Server, error) {
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) { if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
server := &model.Server{} server := &model.Server{}
@ -244,9 +230,7 @@ func ReadServer() (*model.Server, error) {
return c.(*model.Server), nil return c.(*model.Server), nil
} }


/* // UpdateServer keep private values from existing one
* UpdateServer keep private values from existing one
*/
func UpdateServer(server *model.Server) (*model.Server, error) { func UpdateServer(server *model.Server) (*model.Server, error) {
current, err := deserialize("server.json") current, err := deserialize("server.json")
if err != nil { if err != nil {
@ -272,9 +256,7 @@ func UpdateServer(server *model.Server) (*model.Server, error) {
return server, nil return server, nil
} }


/* // Write object to disk
* Write object to disk
*/
func serialize(id string, c interface{}) error { func serialize(id string, c interface{}) error {
b, err := json.Marshal(c) b, err := json.Marshal(c)
if err != nil { if err != nil {
@ -290,9 +272,7 @@ func serialize(id string, c interface{}) error {
return generateWgConfig() return generateWgConfig()
} }


/* // Read client from disc
* Read client from disc
*/
func deserializeClient(data []byte) (*model.Client, error) { func deserializeClient(data []byte) (*model.Client, error) {
var c *model.Client var c *model.Client
err := json.Unmarshal(data, &c) err := json.Unmarshal(data, &c)
@ -303,9 +283,7 @@ func deserializeClient(data []byte) (*model.Client, error) {
return c, nil return c, nil
} }


/* // Read server from disc
* Read server from disc
*/
func deserializeServer(data []byte) (*model.Server, error) { func deserializeServer(data []byte) (*model.Server, error) {
var c *model.Server var c *model.Server
err := json.Unmarshal(data, &c) err := json.Unmarshal(data, &c)
@ -329,9 +307,7 @@ func deserialize(id string) (interface{}, error) {
return deserializeClient(b) return deserializeClient(b)
} }


/* // Generate Wireguard interface configuration
* Generate Wireguard interface configuration
*/
func generateWgConfig() error { func generateWgConfig() error {
clients, err := ReadClients() clients, err := ReadClients()
if err != nil { if err != nil {