Compare commits

...

14 Commits

Author SHA1 Message Date
42434bf718 omitempty for NetworkIDs in destination 2025-04-18 11:17:01 +02:00
abe6ce09b2 omitempty for NetworkIDs in destination 2025-04-18 11:12:52 +02:00
d008cac359 typo 2025-04-17 11:57:05 +02:00
96cf5ac987 follow casing conventions 2025-04-17 10:35:49 +02:00
d83c354874 Missing field 2025-04-17 10:20:27 +02:00
861a985dd0 Remove some anonymous structs 2025-04-17 10:08:03 +02:00
e5b81b217f Create firewall policy 2025-04-17 02:11:29 +02:00
0e9e9cb8a3 Merge pull request #15 from vegardengen/14-support-firewall-zones
Support firewall zones
2025-04-14 12:39:39 +02:00
42d7f4131b Support firewall zones 2025-04-14 12:38:46 +02:00
be515d2e6f Merge pull request #13 from vegardengen/12-support-v2-network-api
Add do_versioned and make do wrap do_versioned
2025-04-14 12:35:44 +02:00
ec43083868 Add do_versioned and make do wrap do_versioned 2025-04-14 12:32:13 +02:00
babe9757a6 Merge pull request #11 from vegardengen/10-fix-module-name
Switch to my branch
2025-04-14 12:19:20 +02:00
225c79703f Switch to my branch 2025-04-14 12:17:32 +02:00
b0d58766fc Merge pull request #9 from vegardengen/8-fix-ipsec-lifetimes
Change ipsec lifetimes to int
2025-04-14 12:08:32 +02:00
6 changed files with 205 additions and 4 deletions

2
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/ubiquiti-community/go-unifi
module github.com/vegardengen/go-unifi
go 1.21

View File

@@ -5,7 +5,7 @@ import (
"testing"
"github.com/tj/assert"
"github.com/ubiquiti-community/go-unifi/unifi"
"github.com/vegardengen/go-unifi/unifi"
)
func TestAccountMarshalJSON(t *testing.T) {

141
unifi/firewall_policy.go Normal file
View File

@@ -0,0 +1,141 @@
package unifi
import (
"context"
"fmt"
)
type FirewallDestination struct {
IPGroupID string `json:"ip_group_id"`
IPs []string `json:"ips"`
MatchOppositeIPs bool `json:"match_opposite_ips"`
MatchOppositePorts bool `json:"match_opposite_ports"`
MatchingTarget string `json:"matching_target"`
MatchingTargetType string `json:"matching_target_type"`
NetworkIDs [] string `json:"network_ids,omitempty"`
Port string `json:"port"`
PortGroupID string `json:"port_group_id"`
PortMatchingType string `json:"port_matching_type"`
Regions []string `json:"regions,omitempty"`
ZoneID string `json:"zone_id"`
}
type FirewallSource struct {
ClientMacs []string `json:"client_macs"`
IPs []string `json:"ips"`
MatchMac bool `json:"match_mac"`
MatchOppositeIPs bool `json:"match_opposite_ips"`
MatchOppositeNetworks bool `json:"match_opposite_networks"`
MatchOppositePorts bool `json:"match_opposite_ports"`
MatchingTarget string `json:"matching_target"`
MatchingTargetType string `json:"matching_target_type"`
NetworkIDs []string `json:"network_ids,omitempty"`
Port string `json:"port"`
PortMatchingType string `json:"port_matching_type"`
ZoneID string `json:"zone_id"`
}
type FirewallSchedule struct {
Mode string `json:"mode"`
DateStart string `json:"date_start"`
DateEnd string `json:"date_end"`
RepeatOnDays []string `json:"repeat_on_days"`
TimeAllDay bool `json:"time_all_day"`
TimeRangeStart string `json:"time_range_start"`
TimeRangeEnd string `json:"time_range_end"`
}
type FirewallPolicy struct {
ID string `json:"_id,omitempty"`
// Hidden bool `json:"attr_hidden,omitempty"`
// HiddenID string `json:"attr_hidden_id,omitempty"`
// NoDelete bool `json:"attr_no_delete,omitempty"`
// NoEdit bool `json:"attr_no_edit,omitempty"`
Action string `json:"action"`
ConnectionStateType string `json:"connection_state_type"`
ConnectionStates []string `json:"connection_states"`
CreateAllowRespond bool `json:"create_allow_respond"`
Description string `json:"description"`
Destination FirewallDestination `json:"destination"`
Enabled bool `json:"enabled"`
ICMPTypename string `json:"icmp_typename"`
ICMPV6Typename string `json:"icmp_v6_typename"`
Index int64 `json:"index"`
IPVersion string `json:"ip_version"`
Logging bool `json:"logging"`
MatchIPSec bool `json:"match_ip_sec"`
MatchIPSecType string `json:"match_ip_sec_type"`
MatchOppositeProtocol bool `json:"match_opposite_protocol"`
Name string `json:"name"`
OriginID string `json:"origin_id"`
OriginType string `json:"origin_type"`
Predefined bool `json:"predefined"`
Protocol string `json:"protocol"`
Schedule FirewallSchedule `json:"schedule"`
Source FirewallSource `json:"source"`
// Role string `json:"role"`
}
func (c *Client) ListFirewallPolicy(ctx context.Context, site string) ([]FirewallPolicy, error) {
var respBody []FirewallPolicy
err := c.do_versioned(ctx, "V2", "GET", fmt.Sprintf("site/%s/firewall-policies", site), nil, &respBody)
if err != nil {
return nil, err
}
return respBody, nil
}
func (c *Client) GetFirewallPolicy(ctx context.Context, site, id string) (*FirewallPolicy, error) {
var respBody FirewallPolicy
err := c.do_versioned(ctx, "V2", "GET", fmt.Sprintf("site/%s/firewall-policies/%s", site,id), nil, &respBody)
if err != nil {
return nil, &NotFoundError{}
}
new := respBody
return &new, nil
}
func (c *Client) DeleteFirewallPolicy(ctx context.Context, site, id string) error {
var respBody FirewallPolicy
err := c.do_versioned(ctx, "V2", "DELETE", fmt.Sprintf("site/%s/firewall-policies/%s", site, id), nil, &respBody)
if err != nil {
return err
}
return nil
}
func (c *Client) CreateFirewallPolicy(ctx context.Context, site string, d *FirewallPolicy) (*FirewallPolicy, error) {
var respBody FirewallPolicy
err := c.do_versioned(ctx, "V2", "POST", fmt.Sprintf("site/%s/firewall-policies", site), d, &respBody)
if err != nil {
return nil, err
}
new := respBody
return &new, nil
}
func (c *Client) UpdateFirewallPolicy(ctx context.Context, site string, d *FirewallPolicy) (*FirewallPolicy, error) {
var respBody FirewallPolicy
err := c.do_versioned(ctx, "V2", "PUT", fmt.Sprintf("site/%s/firewall-policies/%s", site, d.ID), d, &respBody)
if err != nil {
return nil, err
}
new := respBody
return &new, nil
}

48
unifi/firewall_zone.go Normal file
View File

@@ -0,0 +1,48 @@
package unifi
import (
"context"
"fmt"
)
type FirewallZone struct {
ID string `json:"_id,omitempty"`
// Hidden bool `json:"attr_hidden,omitempty"`
// HiddenID string `json:"attr_hidden_id,omitempty"`
// NoDelete bool `json:"attr_no_delete,omitempty"`
// NoEdit bool `json:"attr_no_edit,omitempty"`
Name string `json:"name"`
DefaultZone bool `json:"default_zone,omitempty"`
NetworkIDs []string `json:"network_ids,omitempty"`
ZoneKey string `json:"zone_key,omitempty"`
// Role string `json:"role"`
}
func (c *Client) ListFirewallZones(ctx context.Context, site string) ([]FirewallZone, error) {
var respBody []FirewallZone
err := c.do_versioned(ctx, "V2", "GET", fmt.Sprintf("site/%s/firewall/zone", site), nil, &respBody)
if err != nil {
return nil, err
}
return respBody, nil
}
func (c *Client) GetFirewallZone(ctx context.Context, site, id string) (*FirewallZone, error) {
firewallzones, err := c.ListFirewallZones(ctx, site)
if err != nil {
return nil, err
}
for _, z := range firewallzones {
if z.ID == id {
return &z, nil
}
}
return nil, &NotFoundError{}
}

View File

@@ -5,7 +5,7 @@ import (
"reflect"
"testing"
"github.com/ubiquiti-community/go-unifi/unifi"
"github.com/vegardengen/go-unifi/unifi"
)
func TestNetworkUnmarshalJSON(t *testing.T) {

View File

@@ -191,10 +191,22 @@ func (c *Client) Login(ctx context.Context, user, pass string) error {
}
func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody any, respBody any) error {
return c.do_versioned(ctx, "V1", method, relativeURL, reqBody, respBody)
}
func (c *Client) do_versioned(ctx context.Context, version, method, relativeURL string, reqBody any, respBody any) error {
// single threading requests, this is mostly to assist in CSRF token propagation
c.Lock()
defer c.Unlock()
var apiPath string
if version == "V2" {
apiPath = c.apiV2Path
} else {
apiPath = c.apiPath
}
var (
reqReader io.Reader
err error
@@ -213,7 +225,7 @@ func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody any
return fmt.Errorf("unable to parse URL: %s %s %w", method, relativeURL, err)
}
if !strings.HasPrefix(relativeURL, "/") && !reqURL.IsAbs() {
reqURL.Path = path.Join(c.apiPath, reqURL.Path)
reqURL.Path = path.Join(apiPath, reqURL.Path)
}
url := c.baseURL.ResolveReference(reqURL)