mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 02:48:21 +00:00
Merge pull request #9 from speatzle/update-project-structure
Update project structure
This commit is contained in:
commit
68bf3b7d85
47 changed files with 629 additions and 85 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
config.json
|
||||
nftables.conf
|
||||
go.work
|
||||
bin/*
|
||||
nfsense
|
||||
nfsense.exe
|
|
@ -21,4 +21,474 @@ let options = [{ name: 'Vue.js', code: 'vu' },
|
|||
</div>
|
||||
</template>
|
||||
|
||||
<style src="vue-multiselect/dist/vue-multiselect.css"></style>
|
||||
<style>
|
||||
.multiselect, .multiselect * {
|
||||
all: initial;
|
||||
}
|
||||
|
||||
fieldset[disabled] .multiselect {
|
||||
pointer-events: none
|
||||
}
|
||||
|
||||
.multiselect__spinner {
|
||||
position: absolute;
|
||||
right: 1px;
|
||||
top: 1px;
|
||||
width: 48px;
|
||||
height: 35px;
|
||||
background: #fff;
|
||||
display: block
|
||||
}
|
||||
|
||||
.multiselect__spinner:after,
|
||||
.multiselect__spinner:before {
|
||||
position: absolute;
|
||||
content: "";
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
margin: -8px 0 0 -8px;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border-radius: 100%;
|
||||
border: 2px solid transparent;
|
||||
border-top-color: #41b883;
|
||||
box-shadow: 0 0 0 1px transparent
|
||||
}
|
||||
|
||||
.multiselect__spinner:before {
|
||||
animation: spinning 2.4s cubic-bezier(.41, .26, .2, .62);
|
||||
animation-iteration-count: infinite
|
||||
}
|
||||
|
||||
.multiselect__spinner:after {
|
||||
animation: spinning 2.4s cubic-bezier(.51, .09, .21, .8);
|
||||
animation-iteration-count: infinite
|
||||
}
|
||||
|
||||
.multiselect__loading-enter-active,
|
||||
.multiselect__loading-leave-active {
|
||||
transition: opacity .4s ease-in-out;
|
||||
opacity: 1
|
||||
}
|
||||
|
||||
.multiselect__loading-enter,
|
||||
.multiselect__loading-leave-active {
|
||||
opacity: 0
|
||||
}
|
||||
|
||||
.multiselect,
|
||||
.multiselect__input,
|
||||
.multiselect__single {
|
||||
font-family: inherit;
|
||||
font-size: 16px;
|
||||
-ms-touch-action: manipulation;
|
||||
touch-action: manipulation
|
||||
}
|
||||
|
||||
.multiselect {
|
||||
box-sizing: content-box;
|
||||
display: block;
|
||||
position: relative;
|
||||
width: 100%;
|
||||
min-height: 40px;
|
||||
text-align: left;
|
||||
color: #35495e
|
||||
}
|
||||
|
||||
.multiselect * {
|
||||
box-sizing: border-box
|
||||
}
|
||||
|
||||
.multiselect:focus {
|
||||
outline: none
|
||||
}
|
||||
|
||||
.multiselect--disabled {
|
||||
background: #ededed;
|
||||
pointer-events: none;
|
||||
opacity: .6
|
||||
}
|
||||
|
||||
.multiselect--active {
|
||||
z-index: 50
|
||||
}
|
||||
|
||||
.multiselect--active:not(.multiselect--above) .multiselect__current,
|
||||
.multiselect--active:not(.multiselect--above) .multiselect__input,
|
||||
.multiselect--active:not(.multiselect--above) .multiselect__tags {
|
||||
border-bottom-left-radius: 0;
|
||||
border-bottom-right-radius: 0
|
||||
}
|
||||
|
||||
.multiselect--active .multiselect__select {
|
||||
transform: rotate(180deg)
|
||||
}
|
||||
|
||||
.multiselect--above.multiselect--active .multiselect__current,
|
||||
.multiselect--above.multiselect--active .multiselect__input,
|
||||
.multiselect--above.multiselect--active .multiselect__tags {
|
||||
border-top-left-radius: 0;
|
||||
border-top-right-radius: 0
|
||||
}
|
||||
|
||||
.multiselect__input,
|
||||
.multiselect__single {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
min-height: 20px;
|
||||
line-height: 20px;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
background: #fff;
|
||||
padding: 0 0 0 5px;
|
||||
width: 100%;
|
||||
transition: border .1s ease;
|
||||
box-sizing: border-box;
|
||||
margin-bottom: 8px;
|
||||
vertical-align: top
|
||||
}
|
||||
|
||||
.multiselect__input:-ms-input-placeholder {
|
||||
color: #35495e
|
||||
}
|
||||
|
||||
.multiselect__input::placeholder {
|
||||
color: #35495e
|
||||
}
|
||||
|
||||
.multiselect__tag~.multiselect__input,
|
||||
.multiselect__tag~.multiselect__single {
|
||||
width: auto
|
||||
}
|
||||
|
||||
.multiselect__input:hover,
|
||||
.multiselect__single:hover {
|
||||
border-color: #cfcfcf
|
||||
}
|
||||
|
||||
.multiselect__input:focus,
|
||||
.multiselect__single:focus {
|
||||
border-color: #a8a8a8;
|
||||
outline: none
|
||||
}
|
||||
|
||||
.multiselect__single {
|
||||
padding-left: 5px;
|
||||
margin-bottom: 8px
|
||||
}
|
||||
|
||||
.multiselect__tags-wrap {
|
||||
display: inline
|
||||
}
|
||||
|
||||
.multiselect__tags {
|
||||
min-height: 40px;
|
||||
display: block;
|
||||
padding: 8px 40px 0 8px;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #e8e8e8;
|
||||
background: #fff;
|
||||
font-size: 14px
|
||||
}
|
||||
|
||||
.multiselect__tag {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
padding: 4px 26px 4px 10px;
|
||||
border-radius: 5px;
|
||||
margin-right: 10px;
|
||||
color: #fff;
|
||||
line-height: 1;
|
||||
background: #41b883;
|
||||
margin-bottom: 5px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
max-width: 100%;
|
||||
text-overflow: ellipsis
|
||||
}
|
||||
|
||||
.multiselect__tag-icon {
|
||||
cursor: pointer;
|
||||
margin-left: 7px;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
font-weight: 700;
|
||||
font-style: normal;
|
||||
width: 22px;
|
||||
text-align: center;
|
||||
line-height: 22px;
|
||||
transition: all .2s ease;
|
||||
border-radius: 5px
|
||||
}
|
||||
|
||||
.multiselect__tag-icon:after {
|
||||
content: "\D7";
|
||||
color: #266d4d;
|
||||
font-size: 14px
|
||||
}
|
||||
|
||||
.multiselect__tag-icon:focus,
|
||||
.multiselect__tag-icon:hover {
|
||||
background: #369a6e
|
||||
}
|
||||
|
||||
.multiselect__tag-icon:focus:after,
|
||||
.multiselect__tag-icon:hover:after {
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__current {
|
||||
min-height: 40px;
|
||||
overflow: hidden;
|
||||
padding: 8px 30px 0 12px;
|
||||
white-space: nowrap;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #e8e8e8
|
||||
}
|
||||
|
||||
.multiselect__current,
|
||||
.multiselect__select {
|
||||
line-height: 16px;
|
||||
box-sizing: border-box;
|
||||
display: block;
|
||||
margin: 0;
|
||||
text-decoration: none;
|
||||
cursor: pointer
|
||||
}
|
||||
|
||||
.multiselect__select {
|
||||
position: absolute;
|
||||
width: 40px;
|
||||
height: 38px;
|
||||
right: 1px;
|
||||
top: 1px;
|
||||
padding: 4px 8px;
|
||||
text-align: center;
|
||||
transition: transform .2s ease
|
||||
}
|
||||
|
||||
.multiselect__select:before {
|
||||
position: relative;
|
||||
right: 0;
|
||||
top: 65%;
|
||||
color: #999;
|
||||
margin-top: 4px;
|
||||
border-color: #999 transparent transparent;
|
||||
border-style: solid;
|
||||
border-width: 5px 5px 0;
|
||||
content: ""
|
||||
}
|
||||
|
||||
.multiselect__placeholder {
|
||||
color: #adadad;
|
||||
display: inline-block;
|
||||
margin-bottom: 10px;
|
||||
padding-top: 2px
|
||||
}
|
||||
|
||||
.multiselect--active .multiselect__placeholder {
|
||||
display: none
|
||||
}
|
||||
|
||||
.multiselect__content-wrapper {
|
||||
position: absolute;
|
||||
display: block;
|
||||
background: #fff;
|
||||
width: 100%;
|
||||
max-height: 240px;
|
||||
overflow: auto;
|
||||
border: 1px solid #e8e8e8;
|
||||
border-top: none;
|
||||
border-bottom-left-radius: 5px;
|
||||
border-bottom-right-radius: 5px;
|
||||
z-index: 50;
|
||||
-webkit-overflow-scrolling: touch
|
||||
}
|
||||
|
||||
.multiselect__content {
|
||||
list-style: none;
|
||||
display: inline-block;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
min-width: 100%;
|
||||
vertical-align: top
|
||||
}
|
||||
|
||||
.multiselect--above .multiselect__content-wrapper {
|
||||
bottom: 100%;
|
||||
border-bottom-left-radius: 0;
|
||||
border-bottom-right-radius: 0;
|
||||
border-top-left-radius: 5px;
|
||||
border-top-right-radius: 5px;
|
||||
border-bottom: none;
|
||||
border-top: 1px solid #e8e8e8
|
||||
}
|
||||
|
||||
.multiselect__content::webkit-scrollbar {
|
||||
display: none
|
||||
}
|
||||
|
||||
.multiselect__element {
|
||||
display: block
|
||||
}
|
||||
|
||||
.multiselect__option {
|
||||
display: block;
|
||||
padding: 12px;
|
||||
min-height: 40px;
|
||||
line-height: 16px;
|
||||
text-decoration: none;
|
||||
text-transform: none;
|
||||
vertical-align: middle;
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
white-space: nowrap
|
||||
}
|
||||
|
||||
.multiselect__option:after {
|
||||
top: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
line-height: 40px;
|
||||
padding-right: 12px;
|
||||
padding-left: 20px;
|
||||
font-size: 13px
|
||||
}
|
||||
|
||||
.multiselect__option--highlight {
|
||||
background: #41b883;
|
||||
outline: none;
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__option--highlight:after {
|
||||
content: attr(data-select);
|
||||
background: #41b883;
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__option--selected {
|
||||
background: #f3f3f3;
|
||||
color: #35495e;
|
||||
font-weight: 700
|
||||
}
|
||||
|
||||
.multiselect__option--selected:after {
|
||||
content: attr(data-selected);
|
||||
color: silver
|
||||
}
|
||||
|
||||
.multiselect__option--selected.multiselect__option--highlight {
|
||||
background: #ff6a6a;
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__option--selected.multiselect__option--highlight:after {
|
||||
background: #ff6a6a;
|
||||
content: attr(data-deselect);
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect--disabled .multiselect__current,
|
||||
.multiselect--disabled .multiselect__select {
|
||||
background: #ededed;
|
||||
color: #a6a6a6
|
||||
}
|
||||
|
||||
.multiselect__option--disabled {
|
||||
background: #ededed !important;
|
||||
color: #a6a6a6 !important;
|
||||
cursor: text;
|
||||
pointer-events: none
|
||||
}
|
||||
|
||||
.multiselect__option--group {
|
||||
background: #ededed;
|
||||
color: #35495e
|
||||
}
|
||||
|
||||
.multiselect__option--group.multiselect__option--highlight {
|
||||
background: #35495e;
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__option--group.multiselect__option--highlight:after {
|
||||
background: #35495e
|
||||
}
|
||||
|
||||
.multiselect__option--disabled.multiselect__option--highlight {
|
||||
background: #dedede
|
||||
}
|
||||
|
||||
.multiselect__option--group-selected.multiselect__option--highlight {
|
||||
background: #ff6a6a;
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect__option--group-selected.multiselect__option--highlight:after {
|
||||
background: #ff6a6a;
|
||||
content: attr(data-deselect);
|
||||
color: #fff
|
||||
}
|
||||
|
||||
.multiselect-enter-active,
|
||||
.multiselect-leave-active {
|
||||
transition: all .15s ease
|
||||
}
|
||||
|
||||
.multiselect-enter,
|
||||
.multiselect-leave-active {
|
||||
opacity: 0
|
||||
}
|
||||
|
||||
.multiselect__strong {
|
||||
margin-bottom: 8px;
|
||||
line-height: 20px;
|
||||
display: inline-block;
|
||||
vertical-align: top
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect {
|
||||
text-align: right
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__select {
|
||||
right: auto;
|
||||
left: 1px
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__tags {
|
||||
padding: 8px 8px 0 40px
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__content {
|
||||
text-align: right
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__option:after {
|
||||
right: auto;
|
||||
left: 0
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__clear {
|
||||
right: auto;
|
||||
left: 12px
|
||||
}
|
||||
|
||||
[dir=rtl] .multiselect__spinner {
|
||||
right: auto;
|
||||
left: 1px
|
||||
}
|
||||
|
||||
@keyframes spinning {
|
||||
0% {
|
||||
transform: rotate(0)
|
||||
}
|
||||
|
||||
to {
|
||||
transform: rotate(2turn)
|
||||
}
|
||||
}
|
||||
</style>
|
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 (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetAddressesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
|||
import (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetDestinationNATRulesParameters struct {
|
|
@ -1,7 +1,7 @@
|
|||
package firewall
|
||||
|
||||
import (
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type Firewall struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
|||
import (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetForwardRulesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
|||
import (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetServicesParameters struct {
|
|
@ -3,7 +3,7 @@ package firewall
|
|||
import (
|
||||
"context"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetSourceNATRulesParameters struct {
|
|
@ -10,7 +10,7 @@ import (
|
|||
"runtime/debug"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/session"
|
||||
"nfsense.net/nfsense/internal/session"
|
||||
)
|
||||
|
||||
type Handler struct {
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
func GenerateNfTablesFile(conf definitions.Config) (string, error) {
|
|
@ -3,8 +3,8 @@ package nftables
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/pkg/util"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/util"
|
||||
)
|
||||
|
||||
func GenerateMatcher(services map[string]definitions.Service, addresses map[string]definitions.Address, match definitions.Match) (string, error) {
|
|
@ -8,7 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/session"
|
||||
"nfsense.net/nfsense/internal/session"
|
||||
)
|
||||
|
||||
func HandleAPI(w http.ResponseWriter, r *http.Request) {
|
|
@ -8,9 +8,9 @@ import (
|
|||
|
||||
"golang.org/x/exp/slog"
|
||||
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/pkg/jsonrpc"
|
||||
"nfsense.net/nfsense/pkg/session"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/jsonrpc"
|
||||
"nfsense.net/nfsense/internal/session"
|
||||
)
|
||||
|
||||
var server http.Server
|
|
@ -7,7 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/session"
|
||||
"nfsense.net/nfsense/internal/session"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
|
@ -9,7 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/session"
|
||||
"nfsense.net/nfsense/internal/session"
|
||||
"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
|
||||
|
||||
import "nfsense.net/nfsense/pkg/definitions"
|
||||
import "nfsense.net/nfsense/internal/definitions"
|
||||
|
||||
// ResolveBaseAddresses Resolves all groups to their base Addresses
|
||||
func ResolveBaseAddresses(allAddresses map[string]definitions.Address, addressNames []string) []definitions.Address {
|
|
@ -1,6 +1,6 @@
|
|||
package util
|
||||
|
||||
import "nfsense.net/nfsense/pkg/definitions"
|
||||
import "nfsense.net/nfsense/internal/definitions"
|
||||
|
||||
// ResolveBaseServices Resolves all groups to their base Services
|
||||
func ResolveBaseServices(allServices map[string]definitions.Service, serviceNames []string) []definitions.Service {
|
|
@ -2,16 +2,20 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
"nfsense.net/nfsense/pkg/definitions"
|
||||
"nfsense.net/nfsense/pkg/jsonrpc"
|
||||
"nfsense.net/nfsense/pkg/server"
|
||||
"nfsense.net/nfsense/internal/api/firewall"
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
"nfsense.net/nfsense/internal/jsonrpc"
|
||||
"nfsense.net/nfsense/internal/nftables"
|
||||
"nfsense.net/nfsense/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -69,3 +73,38 @@ func main() {
|
|||
|
||||
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 SessionCookieName string = "session"
|
||||
const SessionLiveTime = 15
|
||||
|
||||
type Session struct {
|
||||
Username string
|
||||
|
@ -42,7 +43,7 @@ func ExtendSession(s *Session) {
|
|||
sessionsSync.Lock()
|
||||
defer sessionsSync.Unlock()
|
||||
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) {
|
||||
id := uuid.New().String()
|
||||
expires := time.Now().Add(time.Minute * 5)
|
||||
expires := time.Now().Add(time.Minute * SessionLiveTime)
|
||||
sessionsSync.Lock()
|
||||
defer sessionsSync.Unlock()
|
||||
sessions[id] = &Session{
|
||||
|
|
Loading…
Add table
Reference in a new issue