-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
215 lines (183 loc) · 4.36 KB
/
session.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package netExtender
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
)
type session struct {
hostname string
routes []string
nameservers []string
sessionID string
client *http.Client
tlsConfig *tls.Config
shutdown chan struct{}
}
type Session interface {
Connect(username, password, domain string) error
Disconnect() error
}
const (
hdrUserAgent = "User-Agent"
hdrContentType = "Content-Type"
hdrContentTypeForm = "application/x-www-form-urlencoded"
hdrUserAgentName = "SonicWALL NetExtender for Linux 8.6.801"
hdrProxyAuth = "Proxy-Authorization"
hdrNetExtenderPDA = "X-Ne-Pda"
)
func New(hostname string) (Session, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
return &session{
hostname: hostname,
client: &http.Client{
Jar: cookieJar,
},
shutdown: make(chan struct{}),
}, nil
}
func (s *session) Connect(username, password, domain string) error {
log.Print("Logging in...")
err := s.login(username, password, domain)
if err != nil {
return fmt.Errorf("login %s", err)
}
defer func() {
err = s.logout()
if err != nil {
log.Printf("logout %s", err)
}
}()
err = s.getEpcVersion()
if err != nil {
return fmt.Errorf("login %s", err)
}
log.Print("Starting session...")
err = s.getSession()
if err != nil {
return fmt.Errorf("session %s", err)
}
time.Sleep(time.Second)
log.Print("Dialing up tunnel...")
err = s.dialTunnel()
if err != nil {
return fmt.Errorf("Dial %s", err)
}
return nil
}
func (s *session) Disconnect() error {
s.shutdown <- struct{}{}
return nil
}
func (s *session) login(username, password, domain string) error {
data := url.Values{
"username": []string{username},
"password": []string{password},
"domain": []string{domain},
"login": []string{"true"},
}
request, err := s.newRequest("POST", "cgi-bin/userLogin", strings.NewReader(data.Encode()))
if err != nil {
return err
}
request.Header.Add(hdrContentType, hdrContentTypeForm)
request.Header.Add(hdrNetExtenderPDA, "true")
res, err := s.client.Do(request)
if err != nil {
return err
}
defer res.Body.Close()
fail, err := strconv.ParseInt(res.Header.Get("X-Ne-Tfresult"), 10, 64)
if err != nil {
return fmt.Errorf("Unexpected X-Ne-Tfresult value")
}
if fail > 0 {
msg := res.Header.Get("X-Ne-Message")
if len(msg) > 0 {
return errors.New(msg)
}
return errors.New("Login failed - User login denied - Account already in use and uniqueness enable.")
}
return nil
}
func (s *session) getEpcVersion() error {
request, err := s.newRequest("GET", "cgi-bin/sslvpnclient?epcversionquery=nxx", nil)
if err != nil {
return err
}
res, err := s.client.Do(request)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
func (s *session) getSession() error {
request, err := s.newRequest("GET", "cgi-bin/sslvpnclient?launchplatform=mac&neProto=3&supportipv6=yes", nil)
if err != nil {
return err
}
request.Header.Add(hdrNetExtenderPDA, "true")
res, err := s.client.Do(request)
if err != nil {
return err
}
defer res.Body.Close()
scanner := bufio.NewScanner(res.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "=") {
line = strings.Trim(line, ";")
kv := strings.Split(line, "=")
log.Print(line)
name := strings.TrimSpace(kv[0])
value := strings.TrimSpace(kv[1])
switch name {
case "SessionId":
case "Route":
s.routes = append(s.routes, value)
case "dns1", "dns2":
s.nameservers = append(s.nameservers, value)
}
}
}
for _, cookie := range res.Cookies() {
if cookie.Name == "swap" {
s.sessionID = cookie.Value
}
}
return nil
}
func (s *session) logout() error {
request, err := s.newRequest("POST", "cgi-bin/userLogout", strings.NewReader(" "))
if err != nil {
return err
}
request.Header.Add(hdrContentType, hdrContentTypeForm)
request.Header.Add(hdrNetExtenderPDA, "true")
res, err := s.client.Do(request)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
func (s *session) newRequest(method, path string, body io.Reader) (*http.Request, error) {
request, err := http.NewRequest(method, fmt.Sprintf("https://%s/%s", s.hostname, path), body)
if err != nil {
return nil, err
}
request.Header.Add(hdrUserAgent, hdrUserAgentName)
return request, nil
}