]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/main.go
Ability to use HTTP proxies for accessing server
[govpn.git] / src / govpn / cmd / govpn-client / main.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 // Simple secure free software virtual private network daemon client.
20 package main
21
22 import (
23         "flag"
24         "io"
25         "log"
26         "net"
27         "os"
28         "os/signal"
29         "time"
30
31         "govpn"
32 )
33
34 var (
35         remoteAddr = flag.String("remote", "", "Remote server address")
36         proto      = flag.String("proto", "udp", "Protocol to use: udp or tcp")
37         ifaceName  = flag.String("iface", "tap0", "TAP network interface")
38         IDRaw      = flag.String("id", "", "Client identification")
39         keyPath    = flag.String("key", "", "Path to passphrase file")
40         upPath     = flag.String("up", "", "Path to up-script")
41         downPath   = flag.String("down", "", "Path to down-script")
42         stats      = flag.String("stats", "", "Enable stats retrieving on host:port")
43         proxyAddr  = flag.String("proxy", "", "Use HTTP proxy on host:port")
44         proxyAuth  = flag.String("proxy-auth", "", "user:password Basic proxy auth")
45         mtu        = flag.Int("mtu", 1452, "MTU for outgoing packets")
46         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
47         noisy      = flag.Bool("noise", false, "Enable noise appending")
48         cpr        = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
49         egdPath    = flag.String("egd", "", "Optional path to EGD socket")
50 )
51
52 func main() {
53         flag.Parse()
54         timeout := *timeoutP
55         var err error
56         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
57
58         govpn.MTU = *mtu
59
60         id, err := govpn.IDDecode(*IDRaw)
61         if err != nil {
62                 log.Fatalln(err)
63         }
64
65         if *egdPath != "" {
66                 log.Println("Using", *egdPath, "EGD")
67                 govpn.EGDInit(*egdPath)
68         }
69
70         pub, priv := govpn.NewVerifier(id, govpn.StringFromFile(*keyPath))
71         conf := &govpn.PeerConf{
72                 Id:          id,
73                 Timeout:     time.Second * time.Duration(timeout),
74                 NoiseEnable: *noisy,
75                 CPR:         *cpr,
76                 DSAPub:      pub,
77                 DSAPriv:     priv,
78         }
79         govpn.PeersInitDummy(id, conf)
80
81         var conn io.Writer
82         var sink chan []byte
83         var ready chan struct{}
84         switch *proto {
85         case "udp":
86                 conn, sink, ready = startUDP()
87         case "tcp":
88                 if *proxyAddr != "" {
89                         conn, sink, ready = proxyTCP()
90                 } else {
91                         conn, sink, ready = startTCP()
92                 }
93         default:
94                 log.Fatalln("Unknown protocol specified")
95         }
96
97         tap, ethSink, ethReady, _, err := govpn.TAPListen(
98                 *ifaceName,
99                 time.Second*time.Duration(timeout),
100                 *cpr,
101         )
102         if err != nil {
103                 log.Fatalln("Can not listen on TAP interface:", err)
104         }
105
106         timeouts := 0
107         firstUpCall := true
108         var peer *govpn.Peer
109         var ethPkt []byte
110         var pkt []byte
111         knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
112
113         log.Println(govpn.VersionGet())
114         log.Println("Connected to", *proto, *remoteAddr)
115         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
116         if *stats != "" {
117                 log.Println("Stats are going to listen on", *stats)
118                 statsPort, err := net.Listen("tcp", *stats)
119                 if err != nil {
120                         log.Fatalln("Can not listen on stats port:", err)
121                 }
122                 go govpn.StatsProcessor(statsPort, &knownPeers)
123         }
124
125         termSignal := make(chan os.Signal, 1)
126         signal.Notify(termSignal, os.Interrupt, os.Kill)
127
128         log.Println("Starting handshake")
129         handshake := govpn.HandshakeStart(*remoteAddr, conn, conf)
130
131 MainCycle:
132         for {
133                 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
134                         peer.Zero()
135                         peer = nil
136                         handshake = govpn.HandshakeStart(*remoteAddr, conn, conf)
137                         log.Println("Rehandshaking")
138                 }
139                 select {
140                 case <-termSignal:
141                         break MainCycle
142                 case ethPkt = <-ethSink:
143                         if peer == nil {
144                                 if len(ethPkt) > 0 {
145                                         ethReady <- struct{}{}
146                                 }
147                                 continue
148                         }
149                         peer.EthProcess(ethPkt, ethReady)
150                 case pkt = <-sink:
151                         timeouts++
152                         if timeouts >= timeout {
153                                 break MainCycle
154                         }
155                         if pkt == nil {
156                                 ready <- struct{}{}
157                                 continue
158                         }
159
160                         if peer == nil {
161                                 if govpn.IDsCache.Find(pkt) == nil {
162                                         log.Println("Invalid identity in handshake packet")
163                                         ready <- struct{}{}
164                                         continue
165                                 }
166                                 if p := handshake.Client(pkt); p != nil {
167                                         log.Println("Handshake completed")
168                                         if firstUpCall {
169                                                 go govpn.ScriptCall(*upPath, *ifaceName)
170                                                 firstUpCall = false
171                                         }
172                                         peer = p
173                                         handshake.Zero()
174                                         handshake = nil
175                                 }
176                                 ready <- struct{}{}
177                                 continue
178                         }
179                         if peer == nil {
180                                 ready <- struct{}{}
181                                 continue
182                         }
183                         if peer.PktProcess(pkt, tap, ready) {
184                                 timeouts = 0
185                         }
186                 }
187         }
188         govpn.ScriptCall(*downPath, *ifaceName)
189 }