Skip to content

Commit

Permalink
Merge pull request #111 from ThatAnonyG/feat/stats-api-auth
Browse files Browse the repository at this point in the history
Token Based Authentication for WG Stats API
  • Loading branch information
vx3r authored Oct 31, 2022
2 parents 7031d2d + 4668ba9 commit c0af160
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=
```
Expand Down
4 changes: 2 additions & 2 deletions api/v1/client/client.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package client

import (
"net/http"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
"github.com/vx3r/wg-gen-web/auth"
"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)
Expand Down
9 changes: 6 additions & 3 deletions core/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"fmt"
"io"
"net/http"
"os"
"sort"
Expand Down Expand Up @@ -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"))
}

Expand All @@ -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
}
Expand Down

0 comments on commit c0af160

Please sign in to comment.