mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
restructure project
This commit is contained in:
parent
dd2db438f3
commit
2ca35d4461
46 changed files with 158 additions and 84 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
config.json
|
config.json
|
||||||
nftables.conf
|
nftables.conf
|
||||||
go.work
|
go.work
|
||||||
bin/*
|
nfsense
|
||||||
|
nfsense.exe
|
11
cmd/api.go
11
cmd/api.go
|
@ -1,11 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"nfsense.net/nfsense/api/firewall"
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
|
||||||
"nfsense.net/nfsense/pkg/jsonrpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RegisterAPIMethods(apiHandler *jsonrpc.Handler, conf *definitions.Config) {
|
|
||||||
apiHandler.Register("Firewall", &firewall.Firewall{Conf: conf})
|
|
||||||
}
|
|
23
cmd/apply.go
23
cmd/apply.go
|
@ -1,23 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
|
||||||
"nfsense.net/nfsense/pkg/nftables"
|
|
||||||
)
|
|
||||||
|
|
||||||
func apply(conf *definitions.Config) error {
|
|
||||||
fileContent, err := nftables.GenerateNfTablesFile(*conf)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Generating nftables file %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = nftables.ApplyNfTablesFile(fileContent)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Applying nftables %w", err)
|
|
||||||
}
|
|
||||||
slog.Info("Wrote nftables File!")
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
|
||||||
)
|
|
||||||
|
|
||||||
func LoadConfiguration(file string) (*definitions.Config, error) {
|
|
||||||
var config definitions.Config
|
|
||||||
configFile, err := os.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("opening Config File %w", err)
|
|
||||||
}
|
|
||||||
defer configFile.Close()
|
|
||||||
|
|
||||||
jsonParser := json.NewDecoder(configFile)
|
|
||||||
jsonParser.DisallowUnknownFields()
|
|
||||||
err = jsonParser.Decode(&config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("decoding Config File %w", err)
|
|
||||||
}
|
|
||||||
return &config, nil
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@ package firewall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetAddressesParameters struct {
|
type GetAddressesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetDestinationNATRulesParameters struct {
|
type GetDestinationNATRulesParameters struct {
|
|
@ -1,7 +1,7 @@
|
||||||
package firewall
|
package firewall
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Firewall struct {
|
type Firewall struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetForwardRulesParameters struct {
|
type GetForwardRulesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetServicesParameters struct {
|
type GetServicesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetSourceNATRulesParameters struct {
|
type GetSourceNATRulesParameters struct {
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/pkg/session"
|
"nfsense.net/nfsense/internal/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateNfTablesFile(conf definitions.Config) (string, error) {
|
func GenerateNfTablesFile(conf definitions.Config) (string, error) {
|
|
@ -3,8 +3,8 @@ package nftables
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
"nfsense.net/nfsense/pkg/util"
|
"nfsense.net/nfsense/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateMatcher(services map[string]definitions.Service, addresses map[string]definitions.Address, match definitions.Match) (string, error) {
|
func GenerateMatcher(services map[string]definitions.Service, addresses map[string]definitions.Address, match definitions.Match) (string, error) {
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/pkg/session"
|
"nfsense.net/nfsense/internal/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleAPI(w http.ResponseWriter, r *http.Request) {
|
func HandleAPI(w http.ResponseWriter, r *http.Request) {
|
|
@ -8,9 +8,9 @@ import (
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
|
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
"nfsense.net/nfsense/pkg/jsonrpc"
|
"nfsense.net/nfsense/internal/jsonrpc"
|
||||||
"nfsense.net/nfsense/pkg/session"
|
"nfsense.net/nfsense/internal/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
var server http.Server
|
var server http.Server
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/pkg/session"
|
"nfsense.net/nfsense/internal/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/pkg/session"
|
"nfsense.net/nfsense/internal/session"
|
||||||
"nhooyr.io/websocket"
|
"nhooyr.io/websocket"
|
||||||
)
|
)
|
||||||
|
|
93
internal/session/session.go
Normal file
93
internal/session/session.go
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SessionKeyType string
|
||||||
|
|
||||||
|
const SessionKey SessionKeyType = "session"
|
||||||
|
const SessionCookieName string = "session"
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
Username string
|
||||||
|
Expires time.Time
|
||||||
|
// TODO Add []websocket.Conn pointer to close all active websockets, alternativly do this via context cancelation
|
||||||
|
}
|
||||||
|
|
||||||
|
type SessionResponse struct {
|
||||||
|
CommitHash string `json:"commit_hash"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var sessionsSync sync.Mutex
|
||||||
|
var sessions map[string]*Session = map[string]*Session{}
|
||||||
|
|
||||||
|
var CommitHash = func() string {
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok {
|
||||||
|
for _, setting := range info.Settings {
|
||||||
|
if setting.Key == "vcs.revision" {
|
||||||
|
return setting.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "asd"
|
||||||
|
}()
|
||||||
|
|
||||||
|
func ExtendSession(s *Session) {
|
||||||
|
sessionsSync.Lock()
|
||||||
|
defer sessionsSync.Unlock()
|
||||||
|
if s != nil {
|
||||||
|
s.Expires = time.Now().Add(time.Minute * 5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSession(r *http.Request) (string, *Session) {
|
||||||
|
c, err := r.Cookie("session")
|
||||||
|
if err != nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
s, ok := sessions[c.Value]
|
||||||
|
if ok {
|
||||||
|
return c.Value, s
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateSession(w http.ResponseWriter, username string) {
|
||||||
|
id := uuid.New().String()
|
||||||
|
expires := time.Now().Add(time.Minute * 5)
|
||||||
|
sessionsSync.Lock()
|
||||||
|
defer sessionsSync.Unlock()
|
||||||
|
sessions[id] = &Session{
|
||||||
|
Username: username,
|
||||||
|
Expires: expires,
|
||||||
|
}
|
||||||
|
http.SetCookie(w, &http.Cookie{Name: SessionCookieName, HttpOnly: true, SameSite: http.SameSiteStrictMode, Value: id, Expires: expires})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CleanupSessions(stop chan struct{}) {
|
||||||
|
tick := time.NewTicker(time.Minute)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-tick.C:
|
||||||
|
ids := []string{}
|
||||||
|
sessionsSync.Lock()
|
||||||
|
for id, s := range sessions {
|
||||||
|
if time.Now().After(s.Expires) {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, id := range ids {
|
||||||
|
delete(sessions, id)
|
||||||
|
}
|
||||||
|
sessionsSync.Unlock()
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "nfsense.net/nfsense/pkg/definitions"
|
import "nfsense.net/nfsense/internal/definitions"
|
||||||
|
|
||||||
// ResolveBaseAddresses Resolves all groups to their base Addresses
|
// ResolveBaseAddresses Resolves all groups to their base Addresses
|
||||||
func ResolveBaseAddresses(allAddresses map[string]definitions.Address, addressNames []string) []definitions.Address {
|
func ResolveBaseAddresses(allAddresses map[string]definitions.Address, addressNames []string) []definitions.Address {
|
|
@ -1,6 +1,6 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import "nfsense.net/nfsense/pkg/definitions"
|
import "nfsense.net/nfsense/internal/definitions"
|
||||||
|
|
||||||
// ResolveBaseServices Resolves all groups to their base Services
|
// ResolveBaseServices Resolves all groups to their base Services
|
||||||
func ResolveBaseServices(allServices map[string]definitions.Service, serviceNames []string) []definitions.Service {
|
func ResolveBaseServices(allServices map[string]definitions.Service, serviceNames []string) []definitions.Service {
|
|
@ -2,16 +2,20 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"nfsense.net/nfsense/pkg/definitions"
|
"nfsense.net/nfsense/internal/api/firewall"
|
||||||
"nfsense.net/nfsense/pkg/jsonrpc"
|
"nfsense.net/nfsense/internal/definitions"
|
||||||
"nfsense.net/nfsense/pkg/server"
|
"nfsense.net/nfsense/internal/jsonrpc"
|
||||||
|
"nfsense.net/nfsense/internal/nftables"
|
||||||
|
"nfsense.net/nfsense/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -69,3 +73,38 @@ func main() {
|
||||||
|
|
||||||
slog.Info("Done")
|
slog.Info("Done")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadConfiguration(file string) (*definitions.Config, error) {
|
||||||
|
var config definitions.Config
|
||||||
|
configFile, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("opening Config File %w", err)
|
||||||
|
}
|
||||||
|
defer configFile.Close()
|
||||||
|
|
||||||
|
jsonParser := json.NewDecoder(configFile)
|
||||||
|
jsonParser.DisallowUnknownFields()
|
||||||
|
err = jsonParser.Decode(&config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("decoding Config File %w", err)
|
||||||
|
}
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterAPIMethods(apiHandler *jsonrpc.Handler, conf *definitions.Config) {
|
||||||
|
apiHandler.Register("Firewall", &firewall.Firewall{Conf: conf})
|
||||||
|
}
|
||||||
|
|
||||||
|
func apply(conf *definitions.Config) error {
|
||||||
|
fileContent, err := nftables.GenerateNfTablesFile(*conf)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Generating nftables file %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = nftables.ApplyNfTablesFile(fileContent)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Applying nftables %w", err)
|
||||||
|
}
|
||||||
|
slog.Info("Wrote nftables File!")
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ type SessionKeyType string
|
||||||
|
|
||||||
const SessionKey SessionKeyType = "session"
|
const SessionKey SessionKeyType = "session"
|
||||||
const SessionCookieName string = "session"
|
const SessionCookieName string = "session"
|
||||||
|
const SessionLiveTime = 15
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Username string
|
Username string
|
||||||
|
@ -42,7 +43,7 @@ func ExtendSession(s *Session) {
|
||||||
sessionsSync.Lock()
|
sessionsSync.Lock()
|
||||||
defer sessionsSync.Unlock()
|
defer sessionsSync.Unlock()
|
||||||
if s != nil {
|
if s != nil {
|
||||||
s.Expires = time.Now().Add(time.Minute * 5)
|
s.Expires = time.Now().Add(time.Minute * SessionLiveTime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +61,7 @@ func GetSession(r *http.Request) (string, *Session) {
|
||||||
|
|
||||||
func GenerateSession(w http.ResponseWriter, username string) {
|
func GenerateSession(w http.ResponseWriter, username string) {
|
||||||
id := uuid.New().String()
|
id := uuid.New().String()
|
||||||
expires := time.Now().Add(time.Minute * 5)
|
expires := time.Now().Add(time.Minute * SessionLiveTime)
|
||||||
sessionsSync.Lock()
|
sessionsSync.Lock()
|
||||||
defer sessionsSync.Unlock()
|
defer sessionsSync.Unlock()
|
||||||
sessions[id] = &Session{
|
sessions[id] = &Session{
|
||||||
|
|
Loading…
Add table
Reference in a new issue