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