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