|
| 1 | +package unifi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// GetFirewallPolicies returns firewall policies for all provided sites. |
| 9 | +// Uses the v2 API endpoint: GET /proxy/network/v2/api/site/{site}/firewall-policies |
| 10 | +func (u *Unifi) GetFirewallPolicies(sites []*Site) ([]*FirewallPolicy, error) { |
| 11 | + policies := make([]*FirewallPolicy, 0) |
| 12 | + |
| 13 | + for _, site := range sites { |
| 14 | + path := fmt.Sprintf(APIFirewallPoliciesPath, site.Name) |
| 15 | + |
| 16 | + body, err := u.GetJSON(path) |
| 17 | + if err != nil { |
| 18 | + return nil, fmt.Errorf("failed to fetch firewall policies for site %s: %w", site.SiteName, err) |
| 19 | + } |
| 20 | + |
| 21 | + var raw []*FirewallPolicy |
| 22 | + if err := json.Unmarshal(body, &raw); err != nil { |
| 23 | + return nil, fmt.Errorf("failed to parse firewall policies for site %s: %w", site.SiteName, err) |
| 24 | + } |
| 25 | + |
| 26 | + for _, policy := range raw { |
| 27 | + if policy == nil { |
| 28 | + continue |
| 29 | + } |
| 30 | + |
| 31 | + policy.SiteName = site.SiteName |
| 32 | + policy.SourceName = u.URL |
| 33 | + policies = append(policies, policy) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return policies, nil |
| 38 | +} |
| 39 | + |
| 40 | +// FirewallPolicyEndpoint represents the source or destination of a firewall rule. |
| 41 | +type FirewallPolicyEndpoint struct { |
| 42 | + MatchOppositePorts bool `json:"match_opposite_ports"` |
| 43 | + MatchingTarget string `json:"matching_target"` |
| 44 | + PortMatchingType string `json:"port_matching_type"` |
| 45 | + ZoneID string `json:"zone_id"` |
| 46 | +} |
| 47 | + |
| 48 | +// FirewallPolicySchedule represents when a firewall rule is active. |
| 49 | +type FirewallPolicySchedule struct { |
| 50 | + Mode string `json:"mode"` |
| 51 | +} |
| 52 | + |
| 53 | +// FirewallPolicy represents a firewall policy rule from the UniFi controller. |
| 54 | +type FirewallPolicy struct { |
| 55 | + ID string `json:"_id"` |
| 56 | + Action string `json:"action"` // ALLOW, BLOCK, REJECT |
| 57 | + ConnectionStateType string `json:"connection_state_type"` // ALL, CUSTOM |
| 58 | + ConnectionStates []string `json:"connection_states"` |
| 59 | + CreateAllowRespond FlexBool `json:"create_allow_respond"` |
| 60 | + Destination FirewallPolicyEndpoint `json:"destination"` |
| 61 | + Enabled FlexBool `json:"enabled"` |
| 62 | + ICMPTypename string `json:"icmp_typename"` |
| 63 | + ICMPv6Typename string `json:"icmp_v6_typename"` |
| 64 | + Index FlexInt `json:"index"` |
| 65 | + IPVersion string `json:"ip_version"` // BOTH, IPV4, IPV6 |
| 66 | + Logging FlexBool `json:"logging"` |
| 67 | + MatchIPSec FlexBool `json:"match_ip_sec"` |
| 68 | + Name string `json:"name"` |
| 69 | + Predefined FlexBool `json:"predefined"` |
| 70 | + Protocol string `json:"protocol"` // all, tcp, udp, icmp, etc. |
| 71 | + Schedule FirewallPolicySchedule `json:"schedule"` |
| 72 | + Source FirewallPolicyEndpoint `json:"source"` |
| 73 | + |
| 74 | + SiteName string `json:"-"` |
| 75 | + SourceName string `json:"-"` |
| 76 | +} |
0 commit comments