This repository has been archived on 2024-09-28. You can view files and clone it, but cannot push or open issues or pull requests.
nftables-http-api-go/utils.go
Georg Pfuetzenreuter f2821a9293
Add POST functionality to add set elements
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
2024-08-30 20:35:58 +02:00

107 lines
2.8 KiB
Go

/*
* This file is part of nftables-http-api.
* Copyright (C) 2024 Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
*
* The nftables-http-api program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"errors"
"golang.org/x/crypto/bcrypt"
"log"
"net"
"net/http"
)
type Response struct {
RError string `json:"error,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) {
var response any
if status == http.StatusOK || status == http.StatusCreated {
response = Response{RResult: text}
} 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 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 {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(token))
if err == nil {
return true
} else {
log.Printf("Token check failed: %s", err)
return false
}
}
func parseIPAddress(straddress string) (net.IP, string) {
parsedaddress := net.ParseIP(straddress)
var address net.IP
family, err := getIPAddressFamily(straddress)
if err == nil {
if family == "ipv4" {
address = parsedaddress.To4()
} else if family == "ipv6" {
address = parsedaddress.To16()
} else {
log.Println("unknown family, this should not happen")
return nil, family
}
return address, family
}
log.Println("address parsing failed:", err)
return nil, ""
}
func getIPAddressFamily(ip string) (string, error) {
if net.ParseIP(ip) == nil {
return "", errors.New("Not an IP address")
}
for i := 0; i < len(ip); i++ {
switch ip[i] {
case '.':
return "ipv4", nil
case ':':
return "ipv6", nil
}
}
return "", errors.New("unknown error")
}