Files
go-easy/main.go

198 lines
5.3 KiB
Go

package main
import (
"database/sql"
"fmt"
"log"
"net"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
)
type WgEasyClient struct {
ID int `json:"id"`
UserID int `json:"user_id"`
InterfaceID string `json:"interface_id"`
Name string `json:"name"`
IPv4Address string `json:"ipv4_address"`
IPv6Address string `json:"ipv6_address"`
PreUp string `json:"pre_up"`
PostUp string `json:"post_up"`
PreDown string `json:"pre_down"`
PostDown string `json:"post_down"`
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
PreSharedKey string `json:"pre_shared_key"`
ExpiresAt string `json:"expires_at,omitempty"`
AllowedIps []string `json:"allowed_ips,omitempty"`
ServerAllowedIps []string `json:"server_allowed_ips"`
PersistentKeepalive int `json:"persistent_keepalive"`
MTU int `json:"mtu"`
JC int `json:"j_c"`
JMin int `json:"j_min"`
JMax int `json:"j_max"`
I1 string `json:"i1,omitempty"`
I2 string `json:"i2,omitempty"`
I3 string `json:"i3,omitempty"`
I4 string `json:"i4,omitempty"`
I5 string `json:"i5,omitempty"`
DNS []string `json:"dns,omitempty"`
ServerEndpoint string `json:"server_endpoint"`
Enabled bool `json:"enabled"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type WgEasyConfig struct {
ID int `json:"id"`
SetupStep int `json:"setup_step"`
SessionPassword string `json:"session_password"`
SessionTimeout int `json:"session_timeout"`
MetricsPrometheus int `json:"metrics_prometheus"`
MetricsJSON int `json:"metrics_json"`
MetricsPassword *string `json:"metrics_password,omitempty"` // Pointer to handle NULL values
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type WgEasyInterfaceConfig struct {
Name string `json:"name"`
Device string `json:"device"`
Port int `json:"port"`
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
IPv4CIDR string `json:"ipv4_cidr"`
IPv6CIDR string `json:"ipv6_cidr"`
MTU int `json:"mtu"`
Enabled int `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// getEnv gets an environment variable or returns a default value.
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func insertNewUser(client WgEasyClient) {
db_file := getEnv("DB_FILE", "wg-easy.db")
db, err := sql.Open("sqlite3", db_file)
if err != nil {
fmt.Println(err)
return
}
insertStmt := `INSERT INTO clients_table (userId, interfaceId, name, ipv4Address, ipv6Address, preUp, postUp, preDown, postDown, privateKey, publicKey, preSharedKey, expiresAt, allowedIps, serverAllowedIps, persistentKeepalive, mtu, jC, jMin, jMax, i1, i2, i3, i4, i5, dns, serverEndpoint, enabled) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
_, err = db.Exec(insertStmt,
// Replace these values with the actual data you want to insert
client.UserID,
client.InterfaceID,
client.Name,
client.IPv4Address,
client.IPv6Address,
client.PreUp,
client.PostUp,
client.PreDown,
client.PostDown,
client.PrivateKey,
client.PublicKey,
client.PreSharedKey,
client.ExpiresAt,
client.AllowedIps,
client.ServerAllowedIps,
client.PersistentKeepalive,
client.MTU,
client.JC,
client.JMin,
client.JMax,
client.I1,
client.I2,
client.I3,
client.I4,
client.I5,
client.DNS,
client.ServerEndpoint,
client.Enabled,
client.CreatedAt,
client.UpdatedAt,
)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted new row successfully")
}
func getLatestClient(rowName string) WgEasyClient {
var client WgEasyClient
db_file := getEnv("DB_FILE", "wg-easy.db")
db, err := sql.Open("sqlite3", db_file)
if err != nil {
fmt.Println(err)
}
queryString := "SELECT MAX(id), ipv4_address FROM clients_table"
rows, err := db.Query(queryString, rowName, rowName)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&client.ID, &client.IPv4Address)
if err != nil {
log.Fatal(err)
}
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return client
}
func getNextIp(ip net.IP) net.IP {
ip = ip.To4()
if ip == nil {
log.Fatal("non ipv4 address")
}
ip[3]++
return net.IP(ip)
}
func main() {
db_file := getEnv("DB_FILE", "wg-easy.db")
db, err := sql.Open("sqlite3", db_file)
if err != nil {
fmt.Println(err)
return
}
defer db.Close()
fmt.Println("Connected to the SQLite database successfully.")
latestClient := getLatestClient("user_id")
fmt.Printf("Next Client ID: %d\n", latestClient.ID)
lastIp := net.ParseIP(latestClient.IPv4Address)
if lastIp == nil {
fmt.Println("Invalid IP address")
} else {
fmt.Printf("Next Valid IP: %v\n", getNextIp(lastIp))
}
}