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