]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/main.go
09b06ca44c753c894357f5b6ebf27fb27e6e5500
[govpn.git] / src / govpn / cmd / govpn-client / main.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 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         "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", 1452, "MTU for outgoing packets")
45         timeoutP    = flag.Int("timeout", 60, "Timeout seconds")
46         noisy       = flag.Bool("noise", false, "Enable noise appending")
47         cpr         = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
48         egdPath     = flag.String("egd", "", "Optional path to EGD socket")
49
50         conf        *govpn.PeerConf
51         tap         *govpn.TAP
52         timeout     int
53         firstUpCall bool = true
54         knownPeers  govpn.KnownPeers
55         idsCache    govpn.CipherCache
56 )
57
58 func main() {
59         flag.Parse()
60         timeout = *timeoutP
61         var err error
62         log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
63
64         govpn.MTU = *mtu
65
66         if *egdPath != "" {
67                 log.Println("Using", *egdPath, "EGD")
68                 govpn.EGDInit(*egdPath)
69         }
70
71         verifier, err := govpn.VerifierFromString(*verifierRaw)
72         if err != nil {
73                 log.Fatalln(err)
74         }
75         priv := verifier.PasswordApply(govpn.StringFromFile(*keyPath))
76         conf = &govpn.PeerConf{
77                 Id:       verifier.Id,
78                 Timeout:  time.Second * time.Duration(timeout),
79                 Noise:    *noisy,
80                 CPR:      *cpr,
81                 Verifier: verifier,
82                 DSAPriv:  priv,
83         }
84         idsCache = govpn.NewCipherCache([]govpn.PeerId{*verifier.Id})
85         log.Println(govpn.VersionGet())
86
87         tap, err = govpn.TAPListen(*ifaceName)
88         if err != nil {
89                 log.Fatalln("Can not listen on TAP interface:", err)
90         }
91
92         log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
93         if *stats != "" {
94                 log.Println("Stats are going to listen on", *stats)
95                 statsPort, err := net.Listen("tcp", *stats)
96                 if err != nil {
97                         log.Fatalln("Can not listen on stats port:", err)
98                 }
99                 go govpn.StatsProcessor(statsPort, &knownPeers)
100         }
101
102         termSignal := make(chan os.Signal, 1)
103         signal.Notify(termSignal, os.Interrupt, os.Kill)
104
105 MainCycle:
106         for {
107                 timeouted := make(chan struct{})
108                 rehandshaking := make(chan struct{})
109                 termination := make(chan struct{})
110                 if *proxyAddr != "" {
111                         *proto = "tcp"
112                 }
113                 switch *proto {
114                 case "udp":
115                         go startUDP(timeouted, rehandshaking, termination)
116                 case "tcp":
117                         if *proxyAddr != "" {
118                                 go proxyTCP(timeouted, rehandshaking, termination)
119                         } else {
120                                 go startTCP(timeouted, rehandshaking, termination)
121                         }
122                 default:
123                         log.Fatalln("Unknown protocol specified")
124                 }
125                 select {
126                 case <-termSignal:
127                         log.Fatalln("Finishing")
128                         termination <- struct{}{}
129                         break MainCycle
130                 case <-timeouted:
131                         break MainCycle
132                 case <-rehandshaking:
133                 }
134                 close(timeouted)
135                 close(rehandshaking)
136                 close(termination)
137         }
138         govpn.ScriptCall(*downPath, *ifaceName)
139 }