]> Cypherpunks.ru repositories - govpn.git/blob - client/proxy.go
GnuPG without the
[govpn.git] / client / proxy.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package client
19
20 import (
21         "bufio"
22         "encoding/base64"
23         "fmt"
24         "net"
25         "net/http"
26
27         "go.cypherpunks.ru/govpn/v7"
28 )
29
30 func (c *Client) proxyTCP() {
31         proxyAddr, err := net.ResolveTCPAddr("tcp", c.config.ProxyAddress)
32         if err != nil {
33                 c.Error <- err
34                 return
35         }
36         conn, err := net.DialTCP("tcp", nil, proxyAddr)
37         if err != nil {
38                 c.Error <- err
39                 return
40         }
41         req := "CONNECT " + c.config.ProxyAddress + " HTTP/1.1\n"
42         req += "Host: " + c.config.ProxyAddress + "\n"
43         if c.config.ProxyAuthentication != "" {
44                 req += "Proxy-Authorization: Basic "
45                 req += base64.StdEncoding.EncodeToString([]byte(c.config.ProxyAuthentication)) + "\n"
46         }
47         req += "\n"
48         conn.Write([]byte(req))
49         resp, err := http.ReadResponse(
50                 bufio.NewReader(conn),
51                 &http.Request{Method: "CONNECT"},
52         )
53         if err != nil || resp.StatusCode != http.StatusOK {
54                 c.Error <- fmt.Errorf("Unexpected response from proxy: %s", err.Error())
55                 return
56         }
57         govpn.Printf(`[proxy-connected remote="%s" addr="%s"]`, c.config.RemoteAddress, (*proxyAddr).String())
58         go c.handleTCP(conn)
59 }