Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

9 changed files with 155 additions and 262 deletions

View File

@ -3,68 +3,79 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
const UPSTREAM_URL = "https://keyserver.freifunk-franken.de/v2/"
func run() error {
func main() {
var root []root
err := os.MkdirAll("hoods", 0777)
err := os.Mkdir("hoods", 0777)
if err != nil {
return err
log.Panic(err)
}
res, err := http.Get(UPSTREAM_URL + "hoods.php")
req, err := http.NewRequest(http.MethodGet, "https://keyserver.freifunk-franken.de/v2/hoods.php", nil)
if err != nil {
return err
log.Panic(err)
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&root)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
log.Panic(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Panic(err)
}
err = json.Unmarshal(body, &root)
if err != nil {
log.Panic(err)
}
for _, h := range root {
res, err := http.Get(fmt.Sprintf("%s/?hoodid=%d", UPSTREAM_URL, h.Id))
if err != nil {
return err
}
defer res.Body.Close()
var hood Hood
err = json.NewDecoder(res.Body).Decode(&hood)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://keyserver.freifunk-franken.de/v2/?hoodid=%d", h.Id), nil)
if err != nil {
return err
log.Panic(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Panic(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Panic(err)
}
err = json.Unmarshal(body, &hood)
if err != nil {
log.Panic(err)
}
if len(h.Polygons) == 0 {
hood.Location = []GeoCoordinate{{Lat: h.Lat, Long: h.Long}}
hood.Location = []HoodCoordinate{{Lat: h.Lat, Long: h.Long}}
} else {
hood.Location = h.Polygons[0]
hood.Location = []HoodCoordinate{}
for _, p := range h.Polygons[0] {
hood.Location = append(hood.Location, HoodCoordinate{Lat: p.Lat, Long: p.Long})
}
}
b, err := json.MarshalIndent(hood, "", " ")
if err != nil {
return err
log.Panic(err)
}
err = os.WriteFile("hoods/"+hood.HoodInfo.Name+".json", b, 0666)
if err != nil {
return err
log.Panic(err)
}
log.Printf("%s\n", hood.HoodInfo.Name)
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

View File

@ -1,16 +1,16 @@
package main
type root struct {
Id uint `json:"id"`
Active uint `json:"active"`
Name string `json:"name"`
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
Polygons [][]GeoCoordinate `json:"polygons"`
Id uint `json:"id"`
Active uint `json:"active"`
Name string `json:"name"`
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
Polygons [][]InputCoordinate `json:"polygons"`
Hood Hood
}
type GeoCoordinate struct {
type InputCoordinate struct {
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
}
@ -24,7 +24,7 @@ type Hood struct {
// one coordinate: voronoi
// two coordinates: invalid
// three or more coordinates: polygon
Location []GeoCoordinate `json:"location,omitempty"`
Location []HoodCoordinate `json:"location,omitempty"`
}
type HoodNetwork struct {
@ -58,3 +58,8 @@ type HoodInfo struct {
NtpIp string `json:"ntp_ip"`
Timestamp uint64 `json:"timestamp"`
}
type HoodCoordinate struct {
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
}

View File

@ -1,15 +1,11 @@
package main
import (
"cmp"
"encoding/json"
"flag"
"fmt"
"log"
"math"
"net/http"
"os"
"slices"
"strconv"
"strings"
"time"
@ -17,29 +13,10 @@ import (
geo "github.com/kellydunn/golang-geo"
)
const EARTH_RADIUS = 6371
var hoodDir string = "/home/fbl/Desktop/keyserver/hoods"
var hoods []Hood
func deg2rad(deg float64) float64 {
return deg / 180 * math.Pi
}
func distanceHaversine(lat1, lon1, lat2, lon2 float64) float64 {
lat1 = deg2rad(lat1)
lat2 = deg2rad(lat2)
lon1 = deg2rad(lon1)
lon2 = deg2rad(lon2)
alpha := (lat1 - lat2) * 0.5
beta := (lon1 - lon2) * 0.5
a := math.Sin(alpha)*math.Sin(alpha) + math.Cos(lat1)*math.Cos(lat2)*math.Sin(beta)*math.Sin(beta)
c := math.Asin(math.Min(1, math.Sqrt(a)))
distance := 2 * EARTH_RADIUS * c
return distance
}
func hoodVoronoi(hoods []Hood, lat, long float64) *Hood {
func hoodVoronoi(lat, long float64) *Hood {
var best *Hood
var bestDist float64 = math.MaxFloat64
@ -48,7 +25,8 @@ func hoodVoronoi(hoods []Hood, lat, long float64) *Hood {
continue
}
dist := distanceHaversine(lat, long, hood.Location[0].Lat, hood.Location[0].Long)
dist := math.Pow(lat-hood.Location[0].Lat, 2) + math.Pow(long-hood.Location[0].Long, 2)
dist = math.Sqrt(dist)
if dist < bestDist {
best = &hoods[i]
@ -59,7 +37,7 @@ func hoodVoronoi(hoods []Hood, lat, long float64) *Hood {
return best
}
func hoodPoly(hoods []Hood, lat, long float64) *Hood {
func hoodPoly(lat, long float64) *Hood {
var matches []*Hood
var best *Hood
@ -99,7 +77,7 @@ func hoodPoly(hoods []Hood, lat, long float64) *Hood {
return best
}
func hoodId(hoods []Hood, id uint64) *Hood {
func hoodId(id uint64) *Hood {
for _, hood := range hoods {
if hood.HoodInfo.Id == id {
return &hood
@ -109,17 +87,13 @@ func hoodId(hoods []Hood, id uint64) *Hood {
return nil
}
func logRequestDuration(h http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
func keyserverV2Time(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
keyserverV2(w, r)
duration := time.Since(start)
log.Printf("Processed request in %s", duration)
},
)
duration := time.Since(start)
log.Printf("Processed request in %s", duration)
}
func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
@ -127,134 +101,91 @@ func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
w.WriteHeader(statusCode)
}
func keyserverV2Handler(hoods []Hood) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
hoodid := r.URL.Query().Get("hoodid")
lat := r.URL.Query().Get("lat")
long := r.URL.Query().Get("long")
lon := r.URL.Query().Get("lon")
var hood *Hood = hoodId(hoods, 0)
func keyserverV2(w http.ResponseWriter, r *http.Request) {
hoodid := r.URL.Query().Get("hoodid")
lat := r.URL.Query().Get("lat")
long := r.URL.Query().Get("long")
lon := r.URL.Query().Get("lon")
var hood *Hood = hoodId(0)
if lon != "" && long != "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
if lon != "" {
long = lon
}
if hoodid != "" {
id, err := strconv.ParseUint(hoodid, 10, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
hood = hoodId(hoods, id)
}
if lat != "" {
if long == "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
// parse numbers
lat := strings.Replace(lat, ",", ".", -1)
long := strings.Replace(long, ",", ".", -1)
latF, err := strconv.ParseFloat(lat, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
longF, err := strconv.ParseFloat(long, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
hoodP := hoodPoly(hoods, latF, longF)
hood = hoodVoronoi(hoods, latF, longF)
if hoodP != nil {
hood = hoodP
}
if hood == nil {
httpHeader(w, r, http.StatusInternalServerError)
return
}
} else if long != "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
if hood == nil {
log.Print("No hood found")
httpHeader(w, r, http.StatusNotFound)
return
}
b, err := json.MarshalIndent(hood, "", " ")
if err != nil {
log.Printf("Marshaling error: %s", err)
httpHeader(w, r, http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.Write(b)
},
)
}
func keyserverV2Hoods(hoods []Hood) http.Handler {
var resp []HoodsResponseHood
for _, h := range hoods {
hi := HoodsResponseHood{
Id: h.HoodInfo.Id,
Active: 1,
Name: h.HoodInfo.Name,
Essid: h.HoodInfo.Essid,
}
if len(h.Location) == 1 {
hi.Lat = h.Location[0].Lat
hi.Lon = h.Location[0].Long
}
if len(h.Location) > 1 {
hi.Polygons = append(hi.Polygons, h.Location)
}
resp = append(resp, hi)
if lon != "" && long != "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
slices.SortFunc(resp, func(a, b HoodsResponseHood) int { return cmp.Compare(a.Id, b.Id) })
if lon != "" {
long = lon
}
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
b, err := json.MarshalIndent(resp, "", " ")
if err != nil {
log.Printf("Marshaling error: %s", err)
httpHeader(w, r, http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.Write(b)
},
)
if hoodid != "" {
id, err := strconv.ParseUint(hoodid, 10, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
hood = hoodId(id)
}
if lat != "" {
if long == "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
// parse numbers
lat := strings.Replace(lat, ",", ".", -1)
long := strings.Replace(long, ",", ".", -1)
latF, err := strconv.ParseFloat(lat, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
longF, err := strconv.ParseFloat(long, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
hoodP := hoodPoly(latF, longF)
hood = hoodVoronoi(latF, longF)
if hoodP != nil {
hood = hoodP
}
if hood == nil {
httpHeader(w, r, http.StatusInternalServerError)
return
}
} else if long != "" {
httpHeader(w, r, http.StatusBadRequest)
return
}
if hood == nil {
log.Print("No hood found")
httpHeader(w, r, http.StatusNotFound)
return
}
b, err := json.MarshalIndent(hood, "", " ")
if err != nil {
log.Printf("Marshaling error: %s", err)
httpHeader(w, r, http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.Write(b)
}
func parseHoods(hoodDir string) ([]Hood, error) {
func parseHoods() {
var newHoods []Hood
var err error
items, err := os.ReadDir(hoodDir)
if err != nil {
return nil, fmt.Errorf(`Error opening dir ("%s"): %w`, hoodDir, err)
log.Fatalf(`Error opening dir ("%s"): %s`, hoodDir, err)
}
for _, item := range items {
var tmp Hood
@ -280,31 +211,17 @@ func parseHoods(hoodDir string) ([]Hood, error) {
newHoods = append(newHoods, tmp)
}
return newHoods, nil
hoods = newHoods
}
func blank(w http.ResponseWriter, r *http.Request) {
}
func run() error {
hoodDir := flag.String("hoods", "./hoods", "Directory of hood json files")
addr := flag.String("addr", ":8080", "HTTP listen addr")
flag.Parse()
hoods, err := parseHoods(*hoodDir)
if err != nil {
return err
}
http.Handle("/v2/", logRequestDuration(keyserverV2Handler(hoods)))
http.Handle("/v2/hoods.php", logRequestDuration(keyserverV2Hoods(hoods)))
log.Printf("Starting HTTP Server on http://%s", *addr)
return http.ListenAndServe(*addr, nil)
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
parseHoods()
http.HandleFunc("/v2/", keyserverV2Time)
http.HandleFunc("/v2/hoods.php", blank)
log.Fatal(http.ListenAndServe(":8080", nil))
}

View File

@ -20,9 +20,9 @@ type HoodVpnEndpoint struct {
Name string `json:"name"`
Protocol string `json:"protocol"`
Address string `json:"address"`
Port uint16 `json:"port,omitempty"`
Key string `json:"key,omitempty"`
Contact string `json:"contact,omitempty"`
Port uint16 `json:"port"`
Key string `json:"key"`
Contact string `json:"contact"`
}
type HoodInfo struct {
@ -48,13 +48,3 @@ type HoodCoordinate struct {
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
}
type HoodsResponseHood struct {
Id uint64 `json:"id"`
Active uint `json:"active"`
Name string `json:"name"`
Essid string `json:"essid_ap"`
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
Polygons [][]HoodCoordinate `json:"polygons,omitempty"`
}

View File

@ -19,12 +19,6 @@
"port": 10022,
"key": "c43a060aa9eca8f798fe3f9003bac0b2b53ca263e44bb0aa933807063429f98e",
"contact": "freifunk@beibecks.de"
},
{
"name": "rl-fff1",
"protocol": "vxlan",
"address": "rl-fff1.fff.community",
"contact": "rlanghammer@web.de"
}
],
"hood": {
@ -51,4 +45,4 @@
"lon": 10.9608003
}
]
}
}

View File

@ -19,12 +19,6 @@
"port": 10023,
"key": "eb0bb080203559fb8aa569ff6ab3013a3879fa381af8134c9d2d27f423478d8a",
"contact": "freifunk@beibecks.de"
},
{
"name": "rl-fff1",
"protocol": "vxlan",
"address": "rl-fff1.fff.community",
"contact": "rlanghammer@web.de"
}
],
"hood": {
@ -51,4 +45,4 @@
"lon": 10.80041885
}
]
}
}

View File

@ -19,12 +19,6 @@
"port": 10005,
"key": "9aa3592b23b3056d41dcca2d14c0b9781344cd3eed4a7e7a01e192eddb46933a",
"contact": "rlanghammer@web.de"
},
{
"name": "rl-fff1",
"protocol": "vxlan",
"address": "rl-fff1.fff.community",
"contact": "rlanghammer@web.de"
}
],
"hood": {
@ -51,4 +45,4 @@
"lon": 10.347808
}
]
}
}

View File

@ -19,12 +19,6 @@
"port": 10002,
"key": "a58ff2aae73eaff03c74d7360630aff60d1ae997941e0fc7545d15127134f925",
"contact": "rlanghammer@web.de"
},
{
"name": "rl-fff1",
"protocol": "vxlan",
"address": "rl-fff1.fff.community",
"contact": "rlanghammer@web.de"
}
],
"hood": {
@ -51,4 +45,4 @@
"lon": 10.506658
}
]
}
}

View File

@ -19,12 +19,6 @@
"port": 10024,
"key": "d9c9718e169a39e5e01b6930506075e396dae4532c61854b26fa88f79720a218",
"contact": "freifunk@beibecks.de"
},
{
"name": "rl-fff1",
"protocol": "vxlan",
"address": "rl-fff1.fff.community",
"contact": "rlanghammer@web.de"
}
],
"hood": {
@ -51,4 +45,4 @@
"lon": 10.23419
}
]
}
}