Correctly return set elements on GET
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
parent
f402bce96b
commit
73c788181e
9
nft.go
9
nft.go
@ -14,6 +14,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/google/nftables"
|
"github.com/google/nftables"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type nftError struct {
|
type nftError struct {
|
||||||
@ -79,6 +80,7 @@ func getNftSet(nft *nftables.Conn, setName string) (*nftables.Set, error) {
|
|||||||
log.Printf("Set lookup for %s failed, cannot proceed: %s", setName, err)
|
log.Printf("Set lookup for %s failed, cannot proceed: %s", setName, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
log.Printf("Found set %s", foundSet.Name)
|
||||||
|
|
||||||
return foundSet, nil
|
return foundSet, nil
|
||||||
}
|
}
|
||||||
@ -97,9 +99,10 @@ func getNftSetElements(nft *nftables.Conn, setName string) ([]string, error) {
|
|||||||
|
|
||||||
var returnElements []string
|
var returnElements []string
|
||||||
|
|
||||||
for _, element := range setElements {
|
for i, element := range setElements {
|
||||||
log.Printf("element: %s", element.Key)
|
ip := net.IP(element.Key)
|
||||||
returnElements = append(returnElements, string(element.Key))
|
log.Printf("Element %d: %s", i, ip)
|
||||||
|
returnElements = append(returnElements, ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnElements, nil
|
return returnElements, nil
|
||||||
|
@ -18,7 +18,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
@ -108,7 +107,7 @@ func handleSetRoute(w http.ResponseWriter, r *http.Request) {
|
|||||||
doReturn(w, http.StatusInternalServerError, "nftables failure")
|
doReturn(w, http.StatusInternalServerError, "nftables failure")
|
||||||
}
|
}
|
||||||
if nftResult != nil {
|
if nftResult != nil {
|
||||||
doReturn(w, http.StatusOK, strings.Join(nftResult.([]string), ""))
|
doReturnSet(w, http.StatusOK, "", nftResult.([]string))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
utils.go
20
utils.go
@ -25,6 +25,11 @@ type Response struct {
|
|||||||
RResult string `json:"result,omitempty"`
|
RResult string `json:"result,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ResponseSet struct {
|
||||||
|
RError string `json:"error,omitempty"`
|
||||||
|
RResult []string `json:"result,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func doReturn(w http.ResponseWriter, status int, text string) {
|
func doReturn(w http.ResponseWriter, status int, text string) {
|
||||||
var response any
|
var response any
|
||||||
if status == http.StatusOK {
|
if status == http.StatusOK {
|
||||||
@ -40,6 +45,21 @@ func doReturn(w http.ResponseWriter, status int, text string) {
|
|||||||
w.Write(j)
|
w.Write(j)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doReturnSet(w http.ResponseWriter, status int, text string, elements []string) {
|
||||||
|
var response any
|
||||||
|
if status == http.StatusOK {
|
||||||
|
response = ResponseSet{RResult: elements}
|
||||||
|
} else {
|
||||||
|
response = Response{RError: text}
|
||||||
|
}
|
||||||
|
j, err := json.Marshal(response)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to marshal JSON: %s", err)
|
||||||
|
}
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write(j)
|
||||||
|
}
|
||||||
|
|
||||||
func doCheckToken(token string, hash string) bool {
|
func doCheckToken(token string, hash string) bool {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(token))
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(token))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Reference in New Issue
Block a user