-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.go
152 lines (131 loc) · 3.12 KB
/
status.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package vpnstatus
import (
"encoding/json"
"github.com/telekom-mms/oc-daemon/pkg/vpnconfig"
)
// TrustedNetwork is the current trusted network state
type TrustedNetwork uint32
// TrustedNetwork states
const (
TrustedNetworkUnknown TrustedNetwork = iota
TrustedNetworkNotTrusted
TrustedNetworkTrusted
)
// Trusted returns whether TrustedNetwork is state "trusted"
func (t TrustedNetwork) Trusted() bool {
return t == TrustedNetworkTrusted
}
// String returns t as string
func (t TrustedNetwork) String() string {
switch t {
case TrustedNetworkUnknown:
return "unknown"
case TrustedNetworkNotTrusted:
return "not trusted"
case TrustedNetworkTrusted:
return "trusted"
}
return ""
}
// ConnectionState is the current connection state
type ConnectionState uint32
// ConnectionState states
const (
ConnectionStateUnknown ConnectionState = iota
ConnectionStateDisconnected
ConnectionStateConnecting
ConnectionStateConnected
ConnectionStateDisconnecting
)
// Connected returns whether ConnectionState is state "connected"
func (c ConnectionState) Connected() bool {
return c == ConnectionStateConnected
}
// String returns ConnectionState as string
func (c ConnectionState) String() string {
switch c {
case ConnectionStateUnknown:
return "unknown"
case ConnectionStateDisconnected:
return "disconnected"
case ConnectionStateConnecting:
return "connecting"
case ConnectionStateConnected:
return "connected"
case ConnectionStateDisconnecting:
return "disconnecting"
}
return ""
}
// OCRunning is the current state of the openconnect client
type OCRunning uint32
// OCRunning states
const (
OCRunningUnknown OCRunning = iota
OCRunningNotRunning
OCRunningRunning
)
// Running returns whether OCRunning is in state "running"
func (o OCRunning) Running() bool {
return o == OCRunningRunning
}
// String returns OCRunning as string
func (o OCRunning) String() string {
switch o {
case OCRunningUnknown:
return "unknown"
case OCRunningNotRunning:
return "not running"
case OCRunningRunning:
return "running"
}
return ""
}
// Status is a VPN status
type Status struct {
TrustedNetwork TrustedNetwork
ConnectionState ConnectionState
IP string
Device string
ConnectedAt int64
Servers []string
OCRunning OCRunning
VPNConfig *vpnconfig.Config
}
// Copy returns a copy of Status
func (s *Status) Copy() *Status {
if s == nil {
return nil
}
return &Status{
TrustedNetwork: s.TrustedNetwork,
ConnectionState: s.ConnectionState,
IP: s.IP,
Device: s.Device,
ConnectedAt: s.ConnectedAt,
Servers: append(s.Servers[:0:0], s.Servers...),
OCRunning: s.OCRunning,
VPNConfig: s.VPNConfig.Copy(),
}
}
// JSON returns the Status as JSON
func (s *Status) JSON() ([]byte, error) {
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
return b, nil
}
// NewFromJSON parses and returns the Status in b
func NewFromJSON(b []byte) (*Status, error) {
s := New()
err := json.Unmarshal(b, s)
if err != nil {
return nil, err
}
return s, nil
}
// New returns a new Status
func New() *Status {
return &Status{}
}