]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
225dbc82cc574365d00865396146dc954f88a8f9
[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         "os"
27         "os/signal"
28         "time"
29
30         "cypherpunks.ru/govpn"
31         "cypherpunks.ru/govpn/client"
32 )
33
34 func main() {
35         var (
36                 remoteAddr  = flag.String("remote", "", "Remote server address")
37                 proto       = flag.String("proto", "udp", "Protocol to use: udp or tcp")
38                 ifaceName   = flag.String("iface", "tap0", "TUN/TAP network interface")
39                 verifierRaw = flag.String("verifier", "", "Verifier")
40                 keyPath     = flag.String("key", "", "Path to passphrase file")
41                 upPath      = flag.String("up", "", "Path to up-script")
42                 downPath    = flag.String("down", "", "Path to down-script")
43                 stats       = flag.String("stats", "", "Enable stats retrieving on host:port")
44                 proxyAddr   = flag.String("proxy", "", "Use HTTP proxy on host:port")
45                 proxyAuth   = flag.String("proxy-auth", "", "user:password Basic proxy auth")
46                 mtu         = flag.Int("mtu", govpn.MTUDefault, "MTU of TUN/TAP interface")
47                 timeoutP    = flag.Int("timeout", 60, "Timeout seconds")
48                 timeSync    = flag.Int("timesync", 0, "Time synchronization requirement")
49                 noreconnect = flag.Bool("noreconnect", false, "Disable reconnection after timeout")
50                 noisy       = flag.Bool("noise", false, "Enable noise appending")
51                 encless     = flag.Bool("encless", false, "Encryptionless mode")
52                 cpr         = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
53                 egdPath     = flag.String("egd", "", "Optional path to EGD socket")
54                 syslog      = flag.Bool("syslog", false, "Enable logging to syslog")
55                 version     = flag.Bool("version", false, "Print version information")
56                 warranty    = flag.Bool("warranty", false, "Print warranty information")
57                 protocol    client.Protocol
58                 err         error
59         )
60
61         flag.Parse()
62         if *warranty {
63                 fmt.Println(govpn.Warranty)
64                 return
65         }
66         if *version {
67                 fmt.Println(govpn.VersionGet())
68                 return
69         }
70         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
71
72         if *egdPath != "" {
73                 log.Println("Using", *egdPath, "EGD")
74                 govpn.EGDInit(*egdPath)
75         }
76
77         switch *proto {
78         case "udp":
79                 protocol = client.ProtocolUDP
80         case "tcp":
81                 protocol = client.ProtocolTCP
82         default:
83                 log.Fatalln("Unknown protocol specified")
84         }
85
86         if *proxyAddr != "" && protocol == client.ProtocolUDP {
87                 log.Fatalln("HTTP proxy is supported only in TCP mode")
88         }
89
90         if *verifierRaw == "" {
91                 log.Fatalln("-verifier is required")
92         }
93         verifier, err := govpn.VerifierFromString(*verifierRaw)
94         if err != nil {
95                 log.Fatalln("Invalid -verifier:", err)
96         }
97         key, err := govpn.KeyRead(*keyPath)
98         if err != nil {
99                 log.Fatalln("Invalid -key:", err)
100         }
101         priv := verifier.PasswordApply(key)
102         if *encless {
103                 if protocol != client.ProtocolTCP {
104                         log.Fatalln("Currently encryptionless mode works only with TCP")
105                 }
106                 if !*noisy {
107                         log.Println("-encless is on, force -noisy mode")
108                         *noisy = true
109                 }
110         }
111         conf := client.Configuration{
112                 PrivateKey: priv,
113                 Peer: &govpn.PeerConf{
114                         Id:       verifier.Id,
115                         Iface:    *ifaceName,
116                         MTU:      *mtu,
117                         Timeout:  time.Second * time.Duration(*timeoutP),
118                         TimeSync: *timeSync,
119                         Noise:    *noisy,
120                         CPR:      *cpr,
121                         Encless:  *encless,
122                         Verifier: verifier,
123                         DSAPriv:  priv,
124                 },
125                 Protocol:            protocol,
126                 InterfaceName:       *ifaceName,
127                 ProxyAddress:        *proxyAddr,
128                 ProxyAuthentication: *proxyAuth,
129                 RemoteAddress:       *remoteAddr,
130                 UpPath:              *upPath,
131                 DownPath:            *downPath,
132                 StatsAddress:        *stats,
133                 NoReconnect:         *noreconnect,
134                 MTU:                 *mtu,
135         }
136         if err = conf.Validate(); err != nil {
137                 log.Fatalln("Invalid settings:", err)
138         }
139
140         log.Println(govpn.VersionGet())
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         c := client.NewClient(conf, verifier, termSignal)
149         go c.MainCycle()
150         if err := <-c.Error; err != nil {
151                 log.Fatalln(err)
152         } else {
153                 log.Println("Closed VPN tunnel")
154         }
155 }