Skip to content

Commit

Permalink
Fix isAuthed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Oct 29, 2024
1 parent 75a9633 commit 1c713a2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 26 deletions.
6 changes: 1 addition & 5 deletions internal/router/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,6 @@ func (f *Firewall) IsAuthed(address string) bool {

func (f *Firewall) isAuthed(addr netip.Addr) bool {

ok := f.userIsLocked[addr.String()]
if !ok {
return false
}

device, ok := f.addressToDevice[addr]
if !ok {
return false
Expand All @@ -449,6 +444,7 @@ func (f *Firewall) isAuthed(addr netip.Addr) bool {

// If the device has been inactive
if device.lastPacketTime.Add(f.inactivityTimeout).Before(time.Now()) {

return false
}

Expand Down
16 changes: 1 addition & 15 deletions internal/router/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAddNewDevices(t *testing.T) {

for address, device := range testFw.addressToDevice {

if device.lastPacketTime.IsZero() || device.sessionExpiry.IsZero() {
if !device.lastPacketTime.IsZero() || !device.sessionExpiry.IsZero() {
t.Fatal("timers were not 0 immediately after device add")
}
found[address.String()] = true
Expand Down Expand Up @@ -787,9 +787,6 @@ func TestPortRestrictions(t *testing.T) {
for _, rule := range rules {

for _, policy := range rule.Values {
if policy.Is(routetypes.STOP) {
break
}

// If we've got an any single port rule e.g 55/any, make sure that the proto is something that has ports otherwise the test fails
successProto := policy.Proto
Expand Down Expand Up @@ -895,9 +892,6 @@ func TestAgnosticRuleOrdering(t *testing.T) {
for _, rule := range rules {

for _, policy := range rule.Values {
if policy.Is(routetypes.STOP) {
break
}

// If we've got an any single port rule e.g 55/any, make sure that the proto is something that has ports otherwise the test fails
successProto := policy.Proto
Expand Down Expand Up @@ -985,10 +979,6 @@ func TestLookupDifferentKeyTypesInMap(t *testing.T) {
t.Fatal("policy was not marked as allow all despite having no rules defined")
}

if !policies[1].Is(routetypes.STOP) {
t.Fatal("policy should only contain one any/any rule")
}

k = routetypes.Key{
IP: []byte{3, 3, 3, 3},
Prefixlen: 32,
Expand All @@ -1009,10 +999,6 @@ func TestLookupDifferentKeyTypesInMap(t *testing.T) {
t.Fatal("policy had incorrect proto and port defintions")
}

if !policies[1].Is(routetypes.STOP) {
t.Fatal("policy should only contain one any/any rule")
}

}

func addDevices(fw *Firewall) error {
Expand Down
8 changes: 8 additions & 0 deletions internal/router/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/netip"
"time"

"github.com/NHAS/wag/internal/config"
"github.com/NHAS/wag/internal/data"
Expand Down Expand Up @@ -36,6 +37,13 @@ func newFw(testing, iptables bool, testDev tun.Device) (*Firewall, error) {
hasIptables: iptables,
}

inactivityTimeoutInt, err := data.GetSessionInactivityTimeoutMinutes()
if err != nil {
return nil, fmt.Errorf("failed to get session inactivity timeout: %s", err)
}

fw.inactivityTimeout = time.Duration(inactivityTimeoutInt) * time.Minute

fw.nodeID = data.GetServerID()
fw.deviceName = config.Values.Wireguard.DevName

Expand Down
7 changes: 1 addition & 6 deletions internal/routetypes/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
type PolicyType uint16

const (
ANY = 0
STOP = 0 // Special directive, stop searching through the array, this is the end
ANY = 0

PUBLIC = 1 << PolicyType(iota)
RANGE
Expand Down Expand Up @@ -80,10 +79,6 @@ func (r Policy) String() string {
restrictionType = "deny"
}

if r.Is(STOP) {
return "stop"
}

if r.Is(SINGLE) {
port := fmt.Sprintf("%d", r.LowerPort)
if r.LowerPort == 0 {
Expand Down

0 comments on commit 1c713a2

Please sign in to comment.