]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
Ability to disable client reconnection
[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         noreconnect = flag.Bool("noreconnect", false, "Disable reconnection after timeout")
49         noisy       = flag.Bool("noise", false, "Enable noise appending")
50         encless     = flag.Bool("encless", false, "Encryptionless mode")
51         cpr         = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
52         egdPath     = flag.String("egd", "", "Optional path to EGD socket")
53         syslog      = flag.Bool("syslog", false, "Enable logging to syslog")
54         warranty    = flag.Bool("warranty", false, "Print warranty information")
55
56         conf        *govpn.PeerConf
57         tap         *govpn.TAP
58         timeout     int
59         firstUpCall bool = true
60         knownPeers  govpn.KnownPeers
61         idsCache    *govpn.MACCache
62 )
63
64 func main() {
65         flag.Parse()
66         if *warranty {
67                 fmt.Println(govpn.Warranty)
68                 return
69         }
70         timeout = *timeoutP
71         var err error
72         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
73
74         if *mtu > govpn.MTUMax {
75                 log.Fatalln("Maximum allowable MTU is", govpn.MTUMax)
76         }
77         if *egdPath != "" {
78                 log.Println("Using", *egdPath, "EGD")
79                 govpn.EGDInit(*egdPath)
80         }
81
82         if *proxyAddr != "" {
83                 *proto = "tcp"
84         }
85         if !(*proto == "udp" || *proto == "tcp") {
86                 log.Fatalln("Unknown protocol specified")
87         }
88         if *verifierRaw == "" {
89                 log.Fatalln("No verifier specified")
90         }
91         verifier, err := govpn.VerifierFromString(*verifierRaw)
92         if err != nil {
93                 log.Fatalln(err)
94         }
95         key, err := govpn.KeyRead(*keyPath)
96         if err != nil {
97                 log.Fatalln("Unable to read the key", err)
98         }
99         priv := verifier.PasswordApply(key)
100         if *encless {
101                 if *proto != "tcp" {
102                         log.Fatalln("Currently encryptionless mode works only with TCP")
103                 }
104                 *noisy = true
105         }
106         conf = &govpn.PeerConf{
107                 Id:       verifier.Id,
108                 Iface:    *ifaceName,
109                 MTU:      *mtu,
110                 Timeout:  time.Second * time.Duration(timeout),
111                 TimeSync: *timeSync,
112                 Noise:    *noisy,
113                 CPR:      *cpr,
114                 Encless:  *encless,
115                 Verifier: verifier,
116                 DSAPriv:  priv,
117         }
118         idsCache = govpn.NewMACCache()
119         confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf}
120         idsCache.Update(&confs)
121         log.Println(govpn.VersionGet())
122
123         tap, err = govpn.TAPListen(*ifaceName, *mtu)
124         if err != nil {
125                 log.Fatalln("Can not listen on TAP interface:", err)
126         }
127
128         if *stats != "" {
129                 log.Println("Stats are going to listen on", *stats)
130                 statsPort, err := net.Listen("tcp", *stats)
131                 if err != nil {
132                         log.Fatalln("Can not listen on stats port:", err)
133                 }
134                 go govpn.StatsProcessor(statsPort, &knownPeers)
135         }
136
137         if *syslog {
138                 govpn.SyslogEnable()
139         }
140
141         termSignal := make(chan os.Signal, 1)
142         signal.Notify(termSignal, os.Interrupt, os.Kill)
143
144 MainCycle:
145         for {
146                 timeouted := make(chan struct{})
147                 rehandshaking := make(chan struct{})
148                 termination := make(chan struct{})
149                 switch *proto {
150                 case "udp":
151                         go startUDP(timeouted, rehandshaking, termination)
152                 case "tcp":
153                         if *proxyAddr != "" {
154                                 go proxyTCP(timeouted, rehandshaking, termination)
155                         } else {
156                                 go startTCP(timeouted, rehandshaking, termination)
157                         }
158                 }
159                 select {
160                 case <-termSignal:
161                         govpn.BothPrintf(`[finish remote="%s"]`, *remoteAddr)
162                         termination <- struct{}{}
163                         break MainCycle
164                 case <-timeouted:
165                         if *noreconnect {
166                                 break MainCycle
167                         }
168                         govpn.BothPrintf(`[sleep seconds="%d"]`, timeout)
169                         time.Sleep(time.Second * time.Duration(timeout))
170                 case <-rehandshaking:
171                 }
172                 close(timeouted)
173                 close(rehandshaking)
174                 close(termination)
175         }
176         govpn.ScriptCall(*downPath, *ifaceName, *remoteAddr)
177 }