2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
27 "github.com/pkg/errors"
29 "cypherpunks.ru/govpn"
32 func (c *Client) proxyTCP() {
33 proxyAddr, err := net.ResolveTCPAddr("tcp", c.config.ProxyAddress)
35 c.Error <- errors.Wrapf(err, "net.ResolveTCPAddr %s", c.config.ProxyAddress)
38 conn, err := net.DialTCP("tcp", nil, proxyAddr)
40 c.Error <- errors.Wrapf(err, "net.DialTCP %s", proxyAddr.String())
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"
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")
55 resp, err := http.ReadResponse(
56 bufio.NewReader(conn),
57 &http.Request{Method: "CONNECT"},
60 govpn.CloseLog(conn, c.logger, c.LogFields())
61 c.Error <- errors.Wrap(err, "http.ReadResponse CONNECT")
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),
73 "func", logFuncPrefix+"Client.proxyTCP",
76 ).Debug("Proxy connected")