]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
Forbid any later GNU GPL versions autousage
[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-2019 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Simple secure, DPI/censorship-resistant free software VPN daemon client.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "os/signal"
27         "time"
28
29         "cypherpunks.ru/govpn"
30         "cypherpunks.ru/govpn/client"
31 )
32
33 func main() {
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", "TUN/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 TUN/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                 protocol    client.Protocol
57                 err         error
58         )
59
60         flag.Parse()
61         if *warranty {
62                 fmt.Println(govpn.Warranty)
63                 return
64         }
65         if *version {
66                 fmt.Println(govpn.VersionGet())
67                 return
68         }
69         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
70
71         if *egdPath != "" {
72                 log.Println("Using", *egdPath, "EGD")
73                 govpn.EGDInit(*egdPath)
74         }
75
76         switch *proto {
77         case "udp":
78                 protocol = client.ProtocolUDP
79         case "tcp":
80                 protocol = client.ProtocolTCP
81         default:
82                 log.Fatalln("Unknown protocol specified")
83         }
84
85         if *proxyAddr != "" && protocol == client.ProtocolUDP {
86                 log.Fatalln("HTTP proxy is supported only in TCP mode")
87         }
88
89         if *verifierRaw == "" {
90                 log.Fatalln("-verifier is required")
91         }
92         verifier, err := govpn.VerifierFromString(*verifierRaw)
93         if err != nil {
94                 log.Fatalln("Invalid -verifier:", err)
95         }
96         key, err := govpn.KeyRead(*keyPath)
97         if err != nil {
98                 log.Fatalln("Invalid -key:", err)
99         }
100         priv := verifier.PasswordApply(key)
101         if *encless {
102                 if protocol != client.ProtocolTCP {
103                         log.Fatalln("Currently encryptionless mode works only with TCP")
104                 }
105                 *noisy = true
106         }
107         conf := client.Configuration{
108                 PrivateKey: priv,
109                 Peer: &govpn.PeerConf{
110                         ID:       verifier.ID,
111                         Iface:    *ifaceName,
112                         MTU:      *mtu,
113                         Timeout:  time.Second * time.Duration(*timeoutP),
114                         TimeSync: *timeSync,
115                         Noise:    *noisy,
116                         CPR:      *cpr,
117                         Encless:  *encless,
118                         Verifier: verifier,
119                         DSAPriv:  priv,
120                 },
121                 Protocol:            protocol,
122                 InterfaceName:       *ifaceName,
123                 ProxyAddress:        *proxyAddr,
124                 ProxyAuthentication: *proxyAuth,
125                 RemoteAddress:       *remoteAddr,
126                 UpPath:              *upPath,
127                 DownPath:            *downPath,
128                 StatsAddress:        *stats,
129                 NoReconnect:         *noreconnect,
130                 MTU:                 *mtu,
131         }
132         if err = conf.Validate(); err != nil {
133                 log.Fatalln("Invalid settings:", err)
134         }
135
136         log.Println(govpn.VersionGet())
137
138         if *syslog {
139                 govpn.SyslogEnable()
140         }
141
142         termSignal := make(chan os.Signal, 1)
143         signal.Notify(termSignal, os.Interrupt, os.Kill)
144         c := client.NewClient(conf, verifier, termSignal)
145         go c.MainCycle()
146         if err = <-c.Error; err != nil {
147                 log.Fatalln(err)
148         }
149 }