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