]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/tcp.go
Ability to use HTTP proxies for accessing server
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 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 main
20
21 import (
22         "encoding/binary"
23         "io"
24         "log"
25         "net"
26
27         "govpn"
28 )
29
30 // TCPSender prepends size prefix to each outgoing packet.
31 type TCPSender struct {
32         conn *net.TCPConn
33 }
34
35 func (c TCPSender) Write(data []byte) (int, error) {
36         size := make([]byte, 2)
37         binary.BigEndian.PutUint16(size, uint16(len(data)))
38         return c.conn.Write(append(size, data...))
39 }
40
41 func startTCP() (io.Writer, chan []byte, chan struct{}) {
42         remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
43         if err != nil {
44                 log.Fatalln("Can not resolve remote address:", err)
45         }
46         c, err := net.DialTCP("tcp", nil, remote)
47         conn := TCPSender{c}
48         if err != nil {
49                 log.Fatalln("Can not connect TCP:", err)
50         }
51         sink := make(chan []byte)
52         ready := make(chan struct{})
53         go handleTCP(c, sink, ready)
54         go func() { ready <- struct{}{} }()
55         return conn, sink, ready
56 }
57
58 func handleTCP(conn *net.TCPConn, sink chan []byte, ready chan struct{}) {
59         var err error
60         var n int
61         var sizeNbuf int
62         sizeBuf := make([]byte, 2)
63         var sizeNeed uint16
64         var bufN uint16
65         buf := make([]byte, govpn.MTU)
66         for {
67                 <-ready
68                 if sizeNbuf != 2 {
69                         n, err = conn.Read(sizeBuf[sizeNbuf:2])
70                         if err != nil {
71                                 break
72                         }
73                         sizeNbuf += n
74                         if sizeNbuf != 2 {
75                                 sink <- nil
76                                 continue
77                         }
78                         sizeNeed = binary.BigEndian.Uint16(sizeBuf)
79                         if int(sizeNeed) > govpn.MTU-2 {
80                                 log.Println("Invalid TCP size, skipping")
81                                 sizeNbuf = 0
82                                 sink <- nil
83                                 continue
84                         }
85                         bufN = 0
86                 }
87         ReadMore:
88                 if sizeNeed != bufN {
89                         n, err = conn.Read(buf[bufN:sizeNeed])
90                         if err != nil {
91                                 break
92                         }
93                         bufN += uint16(n)
94                         goto ReadMore
95                 }
96                 sizeNbuf = 0
97                 sink <- buf[:sizeNeed]
98         }
99 }