]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
Merge branch 'develop'
[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         if *verifierRaw == "" {
75                 log.Fatalln("No verifier specified")
76         }
77         verifier, err := govpn.VerifierFromString(*verifierRaw)
78         if err != nil {
79                 log.Fatalln(err)
80         }
81         key, err := govpn.KeyRead(*keyPath)
82         if err != nil {
83                 log.Fatalln("Unable to read the key", err)
84         }
85         priv := verifier.PasswordApply(key)
86         if *encless {
87                 if *proto != "tcp" {
88                         log.Fatalln("Currently encryptionless mode works only with TCP")
89                 }
90                 *noisy = true
91         }
92         conf = &govpn.PeerConf{
93                 Id:       verifier.Id,
94                 Iface:    *ifaceName,
95                 MTU:      *mtu,
96                 Timeout:  time.Second * time.Duration(timeout),
97                 TimeSync: *timeSync,
98                 Noise:    *noisy,
99                 CPR:      *cpr,
100                 Encless:  *encless,
101                 Verifier: verifier,
102                 DSAPriv:  priv,
103         }
104         idsCache = govpn.NewCipherCache()
105         confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf}
106         idsCache.Update(&confs)
107         log.Println(govpn.VersionGet())
108
109         tap, err = govpn.TAPListen(*ifaceName, *mtu)
110         if err != nil {
111                 log.Fatalln("Can not listen on TAP interface:", err)
112         }
113
114         if *stats != "" {
115                 log.Println("Stats are going to listen on", *stats)
116                 statsPort, err := net.Listen("tcp", *stats)
117                 if err != nil {
118                         log.Fatalln("Can not listen on stats port:", err)
119                 }
120                 go govpn.StatsProcessor(statsPort, &knownPeers)
121         }
122
123         termSignal := make(chan os.Signal, 1)
124         signal.Notify(termSignal, os.Interrupt, os.Kill)
125
126 MainCycle:
127         for {
128                 timeouted := make(chan struct{})
129                 rehandshaking := make(chan struct{})
130                 termination := make(chan struct{})
131                 if *proxyAddr != "" {
132                         *proto = "tcp"
133                 }
134                 switch *proto {
135                 case "udp":
136                         go startUDP(timeouted, rehandshaking, termination)
137                 case "tcp":
138                         if *proxyAddr != "" {
139                                 go proxyTCP(timeouted, rehandshaking, termination)
140                         } else {
141                                 go startTCP(timeouted, rehandshaking, termination)
142                         }
143                 default:
144                         log.Fatalln("Unknown protocol specified")
145                 }
146                 select {
147                 case <-termSignal:
148                         log.Fatalln("Finishing")
149                         termination <- struct{}{}
150                         break MainCycle
151                 case <-timeouted:
152                         break MainCycle
153                 case <-rehandshaking:
154                 }
155                 close(timeouted)
156                 close(rehandshaking)
157                 close(termination)
158         }
159         govpn.ScriptCall(*downPath, *ifaceName)
160 }