1
1
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-23 02:42:07 +01:00

Merge pull request #111 from ThatAnonyG/feat/stats-api-auth

Token Based Authentication for WG Stats API
This commit is contained in:
vx3r 2022-10-31 21:07:41 +01:00 committed by GitHub
commit c0af160fab
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

@ -193,8 +193,13 @@ Wg Gen Web will only access your profile to get email address and your name, no
Wg Gen Web integrates a [WireGuard API implementation](https://github.com/jamescun/wg-api) to display client stats. Wg Gen Web integrates a [WireGuard API implementation](https://github.com/jamescun/wg-api) to display client stats.
In order to enable the Status API integration, the following settings need to be configured: In order to enable the Status API integration, the following settings need to be configured:
``` ```
# https://github.com/jamescun/wg-api integration, user and password (basic auth) are optional # https://github.com/jamescun/wg-api integration
WG_STATS_API=http://<API_LISTEN_IP>:8182 WG_STATS_API=http://<API_LISTEN_IP>:8182
# Optional: Token Auth
WG_STATS_API_TOKEN=
# Optional: Basic Auth
WG_STATS_API_USER= WG_STATS_API_USER=
WG_STATS_API_PASS= WG_STATS_API_PASS=
``` ```

@ -1,6 +1,8 @@
package client package client
import ( import (
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode" "github.com/skip2/go-qrcode"
@ -8,14 +10,12 @@ import (
"github.com/vx3r/wg-gen-web/core" "github.com/vx3r/wg-gen-web/core"
"github.com/vx3r/wg-gen-web/model" "github.com/vx3r/wg-gen-web/model"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"net/http"
) )
// ApplyRoutes applies router to gin Router // ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) { func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/client") g := r.Group("/client")
{ {
g.POST("", createClient) g.POST("", createClient)
g.GET("/:id", readClient) g.GET("/:id", readClient)
g.PATCH("/:id", updateClient) g.PATCH("/:id", updateClient)

@ -4,7 +4,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "fmt"
"io"
"net/http" "net/http"
"os" "os"
"sort" "sort"
@ -54,7 +55,9 @@ func fetchWireGuardAPI(reqData apiRequest) (*apiResponse, error) {
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Cache-Control", "no-cache")
if os.Getenv("WG_STATS_API_USER") != "" { if os.Getenv("WG_STATS_API_TOKEN") != "" {
req.Header.Set("Authorization", fmt.Sprintf("Token %s", os.Getenv("WG_STATS_API_TOKEN")))
} else if os.Getenv("WG_STATS_API_USER") != "" {
req.SetBasicAuth(os.Getenv("WG_STATS_API_USER"), os.Getenv("WG_STATS_API_PASS")) req.SetBasicAuth(os.Getenv("WG_STATS_API_USER"), os.Getenv("WG_STATS_API_PASS"))
} }
@ -67,7 +70,7 @@ func fetchWireGuardAPI(reqData apiRequest) (*apiResponse, error) {
defer res.Body.Close() defer res.Body.Close()
} }
body, readErr := ioutil.ReadAll(res.Body) body, readErr := io.ReadAll(res.Body)
if readErr != nil { if readErr != nil {
return nil, readErr return nil, readErr
} }