]> Cypherpunks.ru repositories - govpn.git/blob - cmd/govpn-client/main.go
Texinfo documentation, client ID, simultaneous clients
[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         mtu        = flag.Int("mtu", 1500, "MTU")
40         nonceDiff  = flag.Int("noncediff", 1, "Allow nonce difference")
41         timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
42 )
43
44 func main() {
45         flag.Parse()
46         timeout := *timeoutP
47         var err error
48         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
49
50         govpn.MTU = *mtu
51         govpn.Timeout = timeout
52         govpn.Noncediff = *nonceDiff
53
54         id := govpn.IDDecode(*IDRaw)
55         key := govpn.KeyRead(*keyPath)
56         if id == nil {
57                 panic("ID is not specified")
58         }
59
60         bind, err := net.ResolveUDPAddr("udp", "0.0.0.0:0")
61         if err != nil {
62                 panic(err)
63         }
64         conn, err := net.ListenUDP("udp", bind)
65         if err != nil {
66                 panic(err)
67         }
68         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
69         if err != nil {
70                 panic(err)
71         }
72
73         tap, ethSink, ethReady, _, err := govpn.TAPListen(*ifaceName)
74         if err != nil {
75                 panic(err)
76         }
77         udpSink, udpBuf, udpReady := govpn.ConnListen(conn)
78
79         timeouts := 0
80         firstUpCall := true
81         var peer *govpn.Peer
82         var ethPkt []byte
83         var udpPkt *govpn.UDPPkt
84         var udpPktData []byte
85
86         termSignal := make(chan os.Signal, 1)
87         signal.Notify(termSignal, os.Interrupt, os.Kill)
88
89         log.Println("Client version", govpn.Version)
90         log.Println("Starting handshake")
91         handshake := govpn.HandshakeStart(conn, remote, id, key)
92
93 MainCycle:
94         for {
95                 if peer != nil && peer.Bytes > govpn.MaxBytesPerKey {
96                         peer = nil
97                         handshake = govpn.HandshakeStart(conn, remote, id, key)
98                         log.Println("Rehandshaking")
99                 }
100                 select {
101                 case <-termSignal:
102                         break MainCycle
103                 case ethPkt = <-ethSink:
104                         if peer == nil {
105                                 ethReady <- struct{}{}
106                                 continue
107                         }
108                         peer.EthProcess(ethPkt, conn, ethReady)
109                 case udpPkt = <-udpSink:
110                         timeouts++
111                         if timeouts >= timeout {
112                                 break MainCycle
113                         }
114                         if udpPkt == nil {
115                                 udpReady <- struct{}{}
116                                 continue
117                         }
118
119                         udpPktData = udpBuf[:udpPkt.Size]
120                         if govpn.IsValidHandshakePkt(udpPktData) {
121                                 if udpPkt.Addr.String() != remote.String() {
122                                         udpReady <- struct{}{}
123                                         log.Println("Unknown handshake message")
124                                         continue
125                                 }
126                                 if p := handshake.Client(conn, key, udpPktData); p != nil {
127                                         log.Println("Handshake completed")
128                                         if firstUpCall {
129                                                 go govpn.ScriptCall(*upPath, *ifaceName)
130                                                 firstUpCall = false
131                                         }
132                                         peer = p
133                                         handshake = nil
134                                 }
135                                 udpReady <- struct{}{}
136                                 continue
137                         }
138                         if peer == nil {
139                                 udpReady <- struct{}{}
140                                 continue
141                         }
142                         if peer.UDPProcess(udpPktData, tap, udpReady) {
143                                 timeouts = 0
144                         }
145                 }
146         }
147         govpn.ScriptCall(*downPath, *ifaceName)
148 }