]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
30ba8e90a76709fdc9556025bb14e7340a18f08b
[govpn.git] / 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.
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 authentication key 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         govpn.PeersInitDummy(id, govpn.PeerConf{
58                 Id:          id,
59                 Timeout:     time.Second * time.Duration(timeout),
60                 Noncediff:   *nonceDiff,
61                 NoiseEnable: *noisy,
62                 CPR:         *cpr,
63         })
64         key := govpn.KeyRead(*keyPath)
65         if id == nil {
66                 panic("ID is not specified")
67         }
68
69         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
70         if err != nil {
71                 panic(err)
72         }
73         conn, err := net.ListenUDP("udp", bind)
74         if err != nil {
75                 panic(err)
76         }
77         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
78         if err != nil {
79                 panic(err)
80         }
81
82         tap, ethSink, ethReady, _, err := govpn.TAPListen(
83                 *ifaceName,
84                 time.Second*time.Duration(timeout),
85                 *cpr,
86         )
87         if err != nil {
88                 panic(err)
89         }
90         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
91
92         timeouts := 0
93         firstUpCall := true
94         var peer *govpn.Peer
95         var ethPkt []byte
96         var udpPkt govpn.UDPPkt
97         var udpPktData []byte
98         knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
99
100         log.Println(govpn.VersionGet())
101         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
102         if *stats != "" {
103                 log.Println("Stats are going to listen on", *stats)
104                 statsPort, err := net.Listen("tcp", *stats)
105                 if err != nil {
106                         panic(err)
107                 }
108                 go govpn.StatsProcessor(statsPort, &knownPeers)
109         }
110
111         termSignal := make(chan os.Signal, 1)
112         signal.Notify(termSignal, os.Interrupt, os.Kill)
113
114         log.Println("Starting handshake")
115         handshake := govpn.HandshakeStart(conn, remote, id, key)
116
117 MainCycle:
118         for {
119                 if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
120                         peer.Zero()
121                         peer = nil
122                         handshake = govpn.HandshakeStart(conn, remote, id, key)
123                         log.Println("Rehandshaking")
124                 }
125                 select {
126                 case <-termSignal:
127                         break MainCycle
128                 case ethPkt = <-ethSink:
129                         if peer == nil {
130                                 if len(ethPkt) > 0 {
131                                         ethReady <- struct{}{}
132                                 }
133                                 continue
134                         }
135                         peer.EthProcess(ethPkt, conn, ethReady)
136                 case udpPkt = <-udpSink:
137                         timeouts++
138                         if timeouts >= timeout {
139                                 break MainCycle
140                         }
141                         if udpPkt.Addr == nil {
142                                 udpReady <- struct{}{}
143                                 continue
144                         }
145
146                         udpPktData = udpBuf[:udpPkt.Size]
147                         if peer == nil {
148                                 if udpPkt.Addr.String() != remote.String() {
149                                         udpReady <- struct{}{}
150                                         log.Println("Unknown handshake message")
151                                         continue
152                                 }
153                                 if govpn.IDsCache.Find(udpPktData) == nil {
154                                         log.Println("Invalid identity in handshake packet")
155                                         udpReady <- struct{}{}
156                                         continue
157                                 }
158                                 if p := handshake.Client(id, conn, key, udpPktData); p != nil {
159                                         log.Println("Handshake completed")
160                                         if firstUpCall {
161                                                 go govpn.ScriptCall(*upPath, *ifaceName)
162                                                 firstUpCall = false
163                                         }
164                                         peer = p
165                                         handshake.Zero()
166                                         handshake = nil
167                                 }
168                                 udpReady <- struct{}{}
169                                 continue
170                         }
171                         if peer == nil {
172                                 udpReady <- struct{}{}
173                                 continue
174                         }
175                         if peer.UDPProcess(udpPktData, tap, udpReady) {
176                                 timeouts = 0
177                         }
178                 }
179         }
180         govpn.ScriptCall(*downPath, *ifaceName)
181 }