-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpn.go
More file actions
129 lines (115 loc) · 4.18 KB
/
Copy pathvpn.go
File metadata and controls
129 lines (115 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package fortimgr
import (
"context"
"fmt"
)
type apiIPSecPhase1 struct {
Name string `json:"name"`
Interface any `json:"interface"`
RemoteGW string `json:"remote-gw"`
Proposal any `json:"proposal"`
DHGroup any `json:"dhgrp"`
Mode any `json:"mode"`
Type any `json:"type"`
Comments string `json:"comments"`
}
type apiIPSecPhase2 struct {
Name string `json:"name"`
Phase1Name any `json:"phase1name"`
Proposal any `json:"proposal"`
SrcSubnet any `json:"src-subnet"`
DstSubnet any `json:"dst-subnet"`
Comments string `json:"comments"`
}
// ListIPSecPhase1 retrieves IPSec Phase 1 interface configurations from an ADOM.
// Pagination is applied transparently; see WithPageSize / WithPageCallback.
func (c *Client) ListIPSecPhase1(ctx context.Context, adom string, opts ...ListOption) ([]IPSecPhase1, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(adom) {
return nil, fmt.Errorf("%w: %q", ErrInvalidName, adom)
}
apiURL := fmt.Sprintf("/pm/config/adom/%s/obj/vpn/ipsec/phase1-interface", adom)
items, err := getPaged[apiIPSecPhase1](ctx, c, apiURL, nil, buildListConfig(opts))
if err != nil {
return nil, err
}
tunnels := make([]IPSecPhase1, len(items))
for i, p := range items {
tunnels[i] = IPSecPhase1{
Name: p.Name,
Interface: toString(p.Interface),
RemoteGW: p.RemoteGW,
Proposal: toString(p.Proposal),
DHGroup: toString(p.DHGroup),
Mode: mapEnum(toString(p.Mode), ipsecModes),
Type: mapEnum(toString(p.Type), ipsecTypes),
Comments: p.Comments,
}
}
return tunnels, nil
}
// IPSecTunnel is a type alias for IPSecPhase1 matching FortiGate GUI
// terminology (phase1-interface → "VPN tunnel"). The field set is
// identical; use ListIPSecTunnels as the preferred entry point.
type IPSecTunnel = IPSecPhase1
// ListIPSecTunnels is a friendlier alias for ListIPSecPhase1. It returns
// IPSec Phase 1 interface configurations (tunnels, in FortiGate GUI terms).
// Shares all logic with ListIPSecPhase1 — IPSecTunnel is a type alias.
// Pagination options pass through to ListIPSecPhase1.
func (c *Client) ListIPSecTunnels(ctx context.Context, adom string, opts ...ListOption) ([]IPSecTunnel, error) {
return c.ListIPSecPhase1(ctx, adom, opts...)
}
// ListIPSecSelectors returns IPSec quick-mode selectors (Phase 2 entries,
// called "VPN selectors" in the FortiGate GUI), each bound to a parent
// tunnel via the Tunnel field. IPSecSelector is a distinct type from
// IPSecPhase2 so the Phase1Name -> Tunnel rename does not break existing
// ListIPSecPhase2 callers. Both share the same API fetch and transformation
// — this function delegates to ListIPSecPhase2 and re-labels the result.
// Pagination options pass through to ListIPSecPhase2.
func (c *Client) ListIPSecSelectors(ctx context.Context, adom string, opts ...ListOption) ([]IPSecSelector, error) {
phases, err := c.ListIPSecPhase2(ctx, adom, opts...)
if err != nil {
return nil, err
}
selectors := make([]IPSecSelector, len(phases))
for i, p := range phases {
selectors[i] = IPSecSelector{
Name: p.Name,
Tunnel: p.Phase1Name,
Proposal: p.Proposal,
SrcSubnet: p.SrcSubnet,
DstSubnet: p.DstSubnet,
Comments: p.Comments,
}
}
return selectors, nil
}
// ListIPSecPhase2 retrieves IPSec Phase 2 interface configurations from an ADOM.
// Pagination is applied transparently; see WithPageSize / WithPageCallback.
func (c *Client) ListIPSecPhase2(ctx context.Context, adom string, opts ...ListOption) ([]IPSecPhase2, error) {
if !c.LoggedIn() {
return nil, ErrNotLoggedIn
}
if !validName(adom) {
return nil, fmt.Errorf("%w: %q", ErrInvalidName, adom)
}
apiURL := fmt.Sprintf("/pm/config/adom/%s/obj/vpn/ipsec/phase2-interface", adom)
items, err := getPaged[apiIPSecPhase2](ctx, c, apiURL, nil, buildListConfig(opts))
if err != nil {
return nil, err
}
tunnels := make([]IPSecPhase2, len(items))
for i, p := range items {
tunnels[i] = IPSecPhase2{
Name: p.Name,
Phase1Name: toString(p.Phase1Name),
Proposal: toString(p.Proposal),
SrcSubnet: formatSubnet(p.SrcSubnet),
DstSubnet: formatSubnet(p.DstSubnet),
Comments: p.Comments,
}
}
return tunnels, nil
}