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