2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
28 "github.com/agl/ed25519"
30 "cypherpunks.ru/govpn"
36 ProtocolUDP Protocol = iota
40 type Configuration struct {
41 PrivateKey *[ed25519.PrivateKeySize]byte
46 ProxyAuthentication string
55 func (c *Configuration) Validate() error {
56 if c.MTU > govpn.MTUMax {
57 return fmt.Errorf("Invalid MTU %d, maximum allowable is %d", c.MTU, govpn.MTUMax)
59 if len(c.RemoteAddress) == 0 {
60 return errors.New("Missing RemoteAddress")
62 if len(c.InterfaceName) == 0 {
63 return errors.New("Missing InterfaceName")
68 func (c *Configuration) isProxy() bool {
69 return len(c.ProxyAddress) > 0
73 idsCache *govpn.MACCache
75 knownPeers govpn.KnownPeers
76 statsPort net.Listener
77 timeouted chan struct{}
78 rehandshaking chan struct{}
79 termination chan struct{}
81 termSignal chan os.Signal
84 // Error channel receives any kind of routine errors
88 func (c *Client) MainCycle() {
90 c.tap, err = govpn.TAPListen(c.config.InterfaceName, c.config.MTU)
92 c.Error <- fmt.Errorf("Can not listen on TUN/TAP interface: %s", err.Error())
96 if len(c.config.StatsAddress) > 0 {
97 c.statsPort, err = net.Listen("tcp", c.config.StatsAddress)
99 c.Error <- fmt.Errorf("Can't listen on stats port: %s", err.Error())
102 c.knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
103 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
108 c.timeouted = make(chan struct{})
109 c.rehandshaking = make(chan struct{})
110 c.termination = make(chan struct{})
111 switch c.config.Protocol {
115 if c.config.isProxy() {
123 govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
124 c.termination <- struct{}{}
125 // empty value signals that everything is fine
129 if c.config.NoReconnect {
132 govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout/time.Second)
133 time.Sleep(c.config.Peer.Timeout)
134 case <-c.rehandshaking:
137 close(c.rehandshaking)
140 if _, err = govpn.ScriptCall(
142 c.config.InterfaceName,
143 c.config.RemoteAddress,
149 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
151 idsCache: govpn.NewMACCache(),
154 termSignal: termSignal,
155 Error: make(chan error, 1),
157 confs := map[govpn.PeerID]*govpn.PeerConf{*verifier.ID: conf.Peer}
158 client.idsCache.Update(&confs)