-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
577 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package router | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"gitlab.com/raspberry.tech/wireguard-manager-and-api/src/db" | ||
) | ||
|
||
type keySetSubJSON struct { | ||
KeyID string `json:"keyID"` | ||
BWLimit int64 `json:"bwLimit"` | ||
SubExpiry string `json:"subExpiry"` | ||
BWReset bool `json:"bwReset"` | ||
} | ||
|
||
func keySetSubscription(res http.ResponseWriter, req *http.Request) { | ||
var incomingJson keySetSubJSON | ||
|
||
err := parseResponse(req, &incomingJson) //parse JSON | ||
if err != nil { | ||
log.Println("Error - Parsing request", err) | ||
sendResponse(res, map[string]string{"response": err.Error()}, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if incomingJson.KeyID == "" { | ||
sendResponse(res, map[string]string{"response": "Bad Request, keyID must be filled"}, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if os.Getenv("AUTH") != "-" { //check AUTH | ||
authHeader := req.Header.Get("Authorization") | ||
if os.Getenv("AUTH") != authHeader { | ||
sendResponse(res, map[string]string{"response": "Authentication key is not valid"}, http.StatusBadRequest) | ||
return | ||
} | ||
} | ||
|
||
boolRes, mapRes := db.SetSubscription(incomingJson.KeyID, incomingJson.BWLimit, incomingJson.SubExpiry, incomingJson.BWReset) //add key to db | ||
if !boolRes { | ||
sendResponse(res, mapRes, http.StatusBadRequest) | ||
} else { | ||
sendResponse(res, mapRes, http.StatusAccepted) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
"gitlab.com/raspberry.tech/wireguard-manager-and-api/src/logger" | ||
"gitlab.com/raspberry.tech/wireguard-manager-and-api/src/manager" | ||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func AddRemovePeers() bool { | ||
getInterfaces, err := manager.GetInterfaces() | ||
if !logger.ErrorHandler("Info - Finding interfaces", err) { | ||
return false | ||
} | ||
|
||
for interfaces := 0; interfaces < len(getInterfaces); interfaces++ { //get interfaces | ||
for peer := 0; peer < len(getInterfaces[interfaces].Peers); peer++ { | ||
currentPeer := getInterfaces[interfaces].Peers[peer] //get the current peer in for loop | ||
interfaceName := getInterfaces[interfaces].Name | ||
updateBW := manager.AddRemovePeer(currentPeer, interfaceName) | ||
if updateBW { | ||
updatePeerBW(currentPeer) | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func BWPeerCheck() bool { | ||
getInterfaces, err := manager.GetInterfaces() | ||
if !logger.ErrorHandler("Info - Finding interfaces", err) { | ||
return false | ||
} | ||
|
||
db := DBSystem | ||
currentTime := time.Now().UTC() | ||
for interfaces := 0; interfaces < len(getInterfaces); interfaces++ { //get interfaces | ||
for peer := 0; peer < len(getInterfaces[interfaces].Peers); peer++ { //get each peer in the wg interface | ||
currentPeer := getInterfaces[interfaces].Peers[peer] //get the current peer in for loop | ||
|
||
publicKey := currentPeer.PublicKey //get public key of client | ||
bwCurrent := currentPeer.TransmitBytes // bandwidth used | ||
pubKeyStr := publicKey.String() | ||
var subStruct Subscription | ||
|
||
resultIP := db.Where("public_key = ?", pubKeyStr).First(&subStruct) //find subscription record | ||
if errors.Is(resultIP.Error, gorm.ErrRecordNotFound) { | ||
log.Println("Could not find public key in database: ", pubKeyStr) | ||
continue | ||
} | ||
|
||
bwStoredUsage := subStruct.BandwidthUsed | ||
bwLimit := subStruct.BandwidthAllotted | ||
subEnd := subStruct.SubscriptionEnd | ||
|
||
subFormatted, subErr := time.Parse("2006-Jan-02 03:04:05 PM", subEnd) | ||
if !logger.ErrorHandler("Error - Parsing stored time ", subErr) { | ||
continue | ||
} | ||
if bwStoredUsage+(bwCurrent/1000000) > bwLimit || currentTime.After(subFormatted) { | ||
keyID := subStruct.KeyID | ||
updatePeerBW(currentPeer) //update bandwidth before disabling | ||
DisableKey(strconv.Itoa(keyID)) //disable key if bandwidth limit reached or subscription end# | ||
log.Println("Info - Disabling key, bw or sub has ended, KeyID: ", keyID) | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func updatePeerBW(currentPeer wgtypes.Peer) { | ||
db := DBSystem | ||
var subStruct Subscription | ||
|
||
pubKey := currentPeer.PublicKey.String() | ||
currentBytes := currentPeer.TransmitBytes | ||
|
||
resultSub := db.Where("public_key = ?", pubKey).First(&subStruct) //find IP not in use | ||
if errors.Is(resultSub.Error, gorm.ErrRecordNotFound) { | ||
log.Println("Error - Subscription not found") | ||
return //continue even on error | ||
} | ||
updatedBW := subStruct.BandwidthUsed + (currentBytes / 1000000) | ||
|
||
db.Model(&Subscription{}).Where("public_key = ?", pubKey).Update("bandwidth_used", updatedBW) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.