0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-04 17:31:17 +00: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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -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.
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

# Optional: Token Auth
WG_STATS_API_TOKEN=

# Optional: Basic Auth
WG_STATS_API_USER=
WG_STATS_API_PASS=
```

View File

@ -1,6 +1,8 @@
package client

import (
"net/http"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
@ -8,14 +10,12 @@ import (
"github.com/vx3r/wg-gen-web/core"
"github.com/vx3r/wg-gen-web/model"
"golang.org/x/oauth2"
"net/http"
)

// ApplyRoutes applies router to gin Router
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/client")
{

g.POST("", createClient)
g.GET("/:id", readClient)
g.PATCH("/:id", updateClient)

View File

@ -4,7 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"fmt"
"io"
"net/http"
"os"
"sort"
@ -54,7 +55,9 @@ func fetchWireGuardAPI(reqData apiRequest) (*apiResponse, error) {
req.Header.Set("Content-Type", "application/json")
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"))
}

@ -67,7 +70,7 @@ func fetchWireGuardAPI(reqData apiRequest) (*apiResponse, error) {
defer res.Body.Close()
}

body, readErr := ioutil.ReadAll(res.Body)
body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}