]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
Reconnect client after timeout, do not exit
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-client / main.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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, DPI/censorship-resistant free software VPN daemon client.
20 package main
21
22 import (
23         "flag"
24         "fmt"
25         "log"
26         "net"
27         "os"
28         "os/signal"
29         "time"
30
31         "cypherpunks.ru/govpn"
32 )
33
34 var (
35         remoteAddr  = flag.String("remote", "", "Remote server address")
36         proto       = flag.String("proto", "udp", "Protocol to use: udp or tcp")
37         ifaceName   = flag.String("iface", "tap0", "TAP network interface")
38         verifierRaw = flag.String("verifier", "", "Verifier")
39         keyPath     = flag.String("key", "", "Path to passphrase file")
40         upPath      = flag.String("up", "", "Path to up-script")
41         downPath    = flag.String("down", "", "Path to down-script")
42         stats       = flag.String("stats", "", "Enable stats retrieving on host:port")
43         proxyAddr   = flag.String("proxy", "", "Use HTTP proxy on host:port")
44         proxyAuth   = flag.String("proxy-auth", "", "user:password Basic proxy auth")
45         mtu         = flag.Int("mtu", govpn.MTUDefault, "MTU of TAP interface")
46         timeoutP    = flag.Int("timeout", 60, "Timeout seconds")
47         timeSync    = flag.Int("timesync", 0, "Time synchronization requirement")
48         noisy       = flag.Bool("noise", false, "Enable noise appending")
49         encless     = flag.Bool("encless", false, "Encryptionless mode")
50         cpr         = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
51         egdPath     = flag.String("egd", "", "Optional path to EGD socket")
52         syslog      = flag.Bool("syslog", false, "Enable logging to syslog")
53         warranty    = flag.Bool("warranty", false, "Print warranty information")
54
55         conf        *govpn.PeerConf
56         tap         *govpn.TAP
57         timeout     int
58         firstUpCall bool = true
59         knownPeers  govpn.KnownPeers
60         idsCache    *govpn.MACCache
61 )
62
63 func main() {
64         flag.Parse()
65         if *warranty {
66                 fmt.Println(govpn.Warranty)
67                 return
68         }
69         timeout = *timeoutP
70         var err error
71         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
72
73         if *mtu > govpn.MTUMax {
74                 log.Fatalln("Maximum allowable MTU is", govpn.MTUMax)
75         }
76         if *egdPath != "" {
77                 log.Println("Using", *egdPath, "EGD")
78                 govpn.EGDInit(*egdPath)
79         }
80
81         if *proxyAddr != "" {
82                 *proto = "tcp"
83         }
84         if !(*proto == "udp" || *proto == "tcp") {
85                 log.Fatalln("Unknown protocol specified")
86         }
87         if *verifierRaw == "" {
88                 log.Fatalln("No verifier specified")
89         }
90         verifier, err := govpn.VerifierFromString(*verifierRaw)
91         if err != nil {
92                 log.Fatalln(err)
93         }
94         key, err := govpn.KeyRead(*keyPath)
95         if err != nil {
96                 log.Fatalln("Unable to read the key", err)
97         }
98         priv := verifier.PasswordApply(key)
99         if *encless {
100                 if *proto != "tcp" {
101                         log.Fatalln("Currently encryptionless mode works only with TCP")
102                 }
103                 *noisy = true
104         }
105         conf = &govpn.PeerConf{
106                 Id:       verifier.Id,
107                 Iface:    *ifaceName,
108                 MTU:      *mtu,
109                 Timeout:  time.Second * time.Duration(timeout),
110                 TimeSync: *timeSync,
111                 Noise:    *noisy,
112                 CPR:      *cpr,
113                 Encless:  *encless,
114                 Verifier: verifier,
115                 DSAPriv:  priv,
116         }
117         idsCache = govpn.NewMACCache()
118         confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf}
119         idsCache.Update(&confs)
120         log.Println(govpn.VersionGet())
121
122         tap, err = govpn.TAPListen(*ifaceName, *mtu)
123         if err != nil {
124                 log.Fatalln("Can not listen on TAP interface:", err)
125         }
126
127         if *stats != "" {
128                 log.Println("Stats are going to listen on", *stats)
129                 statsPort, err := net.Listen("tcp", *stats)
130                 if err != nil {
131                         log.Fatalln("Can not listen on stats port:", err)
132                 }
133                 go govpn.StatsProcessor(statsPort, &knownPeers)
134         }
135
136         if *syslog {
137                 govpn.SyslogEnable()
138         }
139
140         termSignal := make(chan os.Signal, 1)
141         signal.Notify(termSignal, os.Interrupt, os.Kill)
142
143 MainCycle:
144         for {
145                 timeouted := make(chan struct{})
146                 rehandshaking := make(chan struct{})
147                 termination := make(chan struct{})
148                 switch *proto {
149                 case "udp":
150                         go startUDP(timeouted, rehandshaking, termination)
151                 case "tcp":
152                         if *proxyAddr != "" {
153                                 go proxyTCP(timeouted, rehandshaking, termination)
154                         } else {
155                                 go startTCP(timeouted, rehandshaking, termination)
156                         }
157                 }
158                 select {
159                 case <-termSignal:
160                         govpn.BothPrintf(`[finish remote="%s"]`, *remoteAddr)
161                         termination <- struct{}{}
162                         break MainCycle
163                 case <-timeouted:
164                         govpn.BothPrintf(`[sleep seconds="%d"]`, timeout)
165                         time.Sleep(time.Second * time.Duration(timeout))
166                 case <-rehandshaking:
167                 }
168                 close(timeouted)
169                 close(rehandshaking)
170                 close(termination)
171         }
172         govpn.ScriptCall(*downPath, *ifaceName, *remoteAddr)
173 }