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