]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/client/proxy.go
Copyright and stylistic changes
[govpn.git] / src / cypherpunks.ru / govpn / client / proxy.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package client
20
21 import (
22         "bufio"
23         "encoding/base64"
24         "net"
25         "net/http"
26
27         "github.com/pkg/errors"
28
29         "cypherpunks.ru/govpn"
30 )
31
32 func (c *Client) proxyTCP() {
33         proxyAddr, err := net.ResolveTCPAddr("tcp", c.config.ProxyAddress)
34         if err != nil {
35                 c.Error <- errors.Wrapf(err, "net.ResolveTCPAddr %s", c.config.ProxyAddress)
36                 return
37         }
38         conn, err := net.DialTCP("tcp", nil, proxyAddr)
39         if err != nil {
40                 c.Error <- errors.Wrapf(err, "net.DialTCP %s", proxyAddr.String())
41                 return
42         }
43         req := "CONNECT " + c.config.ProxyAddress + " HTTP/1.1\n"
44         req += "Host: " + c.config.ProxyAddress + "\n"
45         if c.config.ProxyAuthentication != "" {
46                 req += "Proxy-Authorization: Basic "
47                 req += base64.StdEncoding.EncodeToString([]byte(c.config.ProxyAuthentication)) + "\n"
48         }
49         req += "\n"
50         if _, err = conn.Write([]byte(req)); err != nil {
51                 govpn.CloseLog(conn, c.logger, c.LogFields())
52                 c.Error <- errors.Wrap(err, "conn.Write")
53                 return
54         }
55         resp, err := http.ReadResponse(
56                 bufio.NewReader(conn),
57                 &http.Request{Method: "CONNECT"},
58         )
59         if err != nil {
60                 govpn.CloseLog(conn, c.logger, c.LogFields())
61                 c.Error <- errors.Wrap(err, "http.ReadResponse CONNECT")
62                 return
63         }
64         if resp.StatusCode != http.StatusOK {
65                 govpn.CloseLog(conn, c.logger, c.LogFields())
66                 c.Error <- errors.Errorf(
67                         "Unexpected response from proxy: %s",
68                         http.StatusText(resp.StatusCode),
69                 )
70                 return
71         }
72         c.logger.WithField(
73                 "func", logFuncPrefix+"Client.proxyTCP",
74         ).WithFields(
75                 c.config.LogFields(),
76         ).Debug("Proxy connected")
77         go c.handleTCP(conn)
78 }