-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.go
170 lines (145 loc) · 3.98 KB
/
client.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"C"
"encoding/json"
"strings"
"../localAgent"
"../vpnPing"
)
var currentConnection *localAgent.AgentConnection
var currentClient *WindowsClient
type WindowsClient struct {
localAgent.NativeClient
eventChannel chan *Event
}
type ConnectionDetailsType struct {
DeviceIp string
DeviceCountry string
ServerIpv4 string
ServerIpv6 string
}
type Event struct {
EventType string
Log string
State string
Code int
Desc string
ConnectionDetails *ConnectionDetailsType
FeaturesStatistics string
}
func (c *WindowsClient) Log(log string) {
c.eventChannel <- &Event{EventType: "log", Log: log}
}
func (c *WindowsClient) OnState(s localAgent.State) {
c.eventChannel <- &Event{EventType: "state", State: s}
}
func (c *WindowsClient) OnError(code int, desc string) {
c.eventChannel <- &Event{EventType: "error", Code: code, Desc: desc}
}
func (c *WindowsClient) OnStatusUpdate(status *localAgent.StatusMessage) {
var connectionDetails *ConnectionDetailsType
if status.ConnectionDetails != nil {
connectionDetails = &ConnectionDetailsType {
DeviceIp: status.ConnectionDetails.DeviceIp,
DeviceCountry: status.ConnectionDetails.DeviceCountry,
ServerIpv4: status.ConnectionDetails.ServerIpv4,
ServerIpv6: status.ConnectionDetails.ServerIpv6,
}
}
c.eventChannel <- &Event{EventType: "status", ConnectionDetails: connectionDetails}
var featuresStatistics []byte
if status.FeaturesStatistics != nil {
featuresStatistics, _ = status.FeaturesStatistics.MarshalJSON()
}
if featuresStatistics != nil {
c.eventChannel <- &Event{EventType: "stats", FeaturesStatistics: string(featuresStatistics)}
}
}
func (c *WindowsClient) OnTlsSessionStarted() {
}
func (c *WindowsClient) OnTlsSessionEnded() {
}
//export Connect
func Connect(
clientCertPEM,
clientKeyPEM,
serverCAsPEM,
host,
certServerName,
featuresJson string,
connectivity bool,
) []byte {
var clientCertPEMCopy = deepCopy(clientCertPEM)
var clientKeyPEMCopy = deepCopy(clientKeyPEM)
var serverCAsPEMCOpy = deepCopy(serverCAsPEM)
var hostCopy = deepCopy(host)
var certServerNameCopy = deepCopy(certServerName)
var featuresJsonCopy = deepCopy(featuresJson)
var features *localAgent.Features
var err error
if len(featuresJsonCopy) > 0 {
err := json.Unmarshal([]byte(featuresJsonCopy), &features)
if err != nil {
return []byte(err.Error())
}
}
currentClient = new(WindowsClient)
currentClient.eventChannel = make(chan *Event, 10)
currentConnection, err = localAgent.NewAgentConnection(
clientCertPEMCopy, clientKeyPEMCopy, serverCAsPEMCOpy, hostCopy, certServerNameCopy, currentClient,
features, connectivity)
if err != nil {
currentConnection = nil
return []byte(err.Error())
}
return []byte("")
}
//export GetEvent
func GetEvent() []byte {
client := currentClient
if client == nil {
return []byte("")
}
event := <-client.eventChannel
if event == nil {
return []byte("")
}
result, _ := json.Marshal(event)
return []byte(string(result))
}
//export GetStatus
func GetStatus() []byte {
result, _ := json.Marshal(currentConnection.Status)
return []byte(string(result))
}
//export SendGetStatus
func SendGetStatus(withStatistics bool) {
currentConnection.SendGetStatus(withStatistics)
}
//export SetFeatures
func SetFeatures(featuresJson string) {
features := new(localAgent.Features)
json.Unmarshal([]byte(featuresJson), &features)
currentConnection.SetFeatures(features)
}
//export SetConnectivity
func SetConnectivity(connectivity bool) {
currentConnection.SetConnectivity(connectivity)
}
//export Close
func Close() {
currentConnection.Close()
currentConnection = nil
close(currentClient.eventChannel)
currentClient = nil
}
//export Ping
func Ping(ip string, port int, serverKeyBase64 string, timeoutSeconds int) bool {
return vpnPing.PingSync(ip, port, serverKeyBase64, timeoutSeconds)
}
func deepCopy(s string) string {
var sb strings.Builder
sb.WriteString(s)
return sb.String()
}
func main() {}