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