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/>.
25 "github.com/Sirupsen/logrus"
26 "github.com/agl/ed25519"
27 "github.com/pkg/errors"
29 "cypherpunks.ru/govpn"
32 const logFuncPrefix = "govpn/client."
34 // Configuration holds GoVPN client configuration
35 type Configuration struct {
36 PrivateKey *[ed25519.PrivateKeySize]byte
38 Protocol govpn.Protocol
40 ProxyAuthentication string
43 // FileDescriptor allows creating Client from a pre-existing file
44 // descriptor. Required for Android. Requires TCP transport.
48 // Validate returns an error if a configuration is invalid
49 func (c *Configuration) Validate() error {
50 if c.Peer.MTU > govpn.MTUMax {
51 return errors.Errorf("Invalid MTU %d, maximum allowable is %d", c.Peer.MTU, govpn.MTUMax)
53 if len(c.RemoteAddress) == 0 {
54 return errors.New("Missing RemoteAddress")
56 if len(c.Peer.Iface) == 0 && c.Peer.PreUp == nil {
57 return errors.New("Missing InterfaceName or PreUp")
59 if c.Protocol != govpn.ProtocolTCP && c.Protocol != govpn.ProtocolUDP {
60 return errors.Errorf("Invalid protocol %d for client", c.Protocol)
62 if c.FileDescriptor > 0 && c.Protocol != govpn.ProtocolTCP {
64 "Connect with file descriptor requires protocol %s",
65 govpn.ProtocolTCP.String(),
71 // LogFields returns a logrus compatible logging context
72 func (c *Configuration) LogFields() logrus.Fields {
73 const prefix = "client_conf_"
74 f := c.Peer.LogFields(prefix)
75 f[prefix+"protocol"] = c.Protocol.String()
76 f[prefix+"no_reconnect"] = c.NoReconnect
77 if len(c.ProxyAddress) > 0 {
78 f[prefix+"proxy"] = c.ProxyAddress
80 if c.FileDescriptor > 0 {
81 f[prefix+"remote"] = fmt.Sprintf("fd:%d(%s)", c.FileDescriptor, c.RemoteAddress)
83 f[prefix+"remote"] = c.RemoteAddress
88 func (c *Configuration) isProxy() bool {
89 return len(c.ProxyAddress) > 0
92 // Client is a GoVPN client
94 idsCache *govpn.MACCache
96 knownPeers govpn.KnownPeers
97 timeouted chan struct{}
98 rehandshaking chan struct{}
99 termination chan struct{}
101 termSignal chan interface{}
103 logger *logrus.Logger
105 // Error channel receives any kind of routine errors
109 // LogFields returns a logrus compatible logging context
110 func (c *Client) LogFields() logrus.Fields {
111 const prefix = "client_"
113 prefix + "remote": c.config.RemoteAddress,
116 f[prefix+"interface"] = c.tap.Name
118 if c.config.Peer != nil {
119 f[prefix+"id"] = c.config.Peer.ID.String()
124 func (c *Client) postDownAction() error {
125 if c.config.Peer.Down == nil {
128 err := c.config.Peer.Down(govpn.PeerContext{
129 RemoteAddress: c.config.RemoteAddress,
130 Protocol: c.config.Protocol,
131 Config: *c.config.Peer,
133 return errors.Wrap(err, "c.config.Peer.Down")
136 func (c *Client) postUpAction() error {
137 if c.config.Peer.Up == nil {
140 err := c.config.Peer.Up(govpn.PeerContext{
141 RemoteAddress: c.config.RemoteAddress,
142 Protocol: c.config.Protocol,
143 Config: *c.config.Peer,
145 return errors.Wrap(err, "c.config.Peer.Up")
148 // KnownPeers returns GoVPN peers. Always 1. Used to get client statistics.
149 func (c *Client) KnownPeers() *govpn.KnownPeers {
153 // MainCycle main loop of a connecting/connected client
154 func (c *Client) MainCycle() {
156 l := c.logger.WithFields(logrus.Fields{"func": logFuncPrefix + "Client.MainCycle"})
157 l.WithFields(c.LogFields()).WithFields(c.config.LogFields()).Info("Starting...")
159 // if available, run PreUp, it might create interface
160 if c.config.Peer.PreUp != nil {
161 l.Debug("Running PreUp")
162 if c.tap, err = c.config.Peer.PreUp(govpn.PeerContext{
163 RemoteAddress: c.config.RemoteAddress,
164 Protocol: c.config.Protocol,
165 Config: *c.config.Peer,
167 c.Error <- errors.Wrap(err, "c.config.Peer.PreUp")
170 l.Debug("PreUp success")
172 l.Debug("No PreUp to run")
175 // if TAP wasn't set by PreUp, listen here
177 l.WithField("asking", c.config.Peer.Iface).Debug("No interface, try to listen")
178 c.tap, err = govpn.TAPListen(c.config.Peer.Iface, c.config.Peer.MTU)
180 c.Error <- errors.Wrapf(
182 "govpn.TAPListen inteface:%s mtu:%d",
183 c.config.Peer.Iface, c.config.Peer.MTU,
188 c.config.Peer.Iface = c.tap.Name
189 l.WithFields(c.LogFields()).Debug("Got interface, start main loop")
193 c.timeouted = make(chan struct{})
194 c.rehandshaking = make(chan struct{})
195 c.termination = make(chan struct{})
196 switch c.config.Protocol {
197 case govpn.ProtocolUDP:
200 case govpn.ProtocolTCP:
202 if c.config.isProxy() {
210 l.WithFields(c.LogFields()).Debug("Finish")
211 c.termination <- struct{}{}
212 // empty value signals that everything is fine
216 if c.config.NoReconnect {
217 l.Debug("No reconnect, stop")
221 l.WithField("timeout", c.config.Peer.Timeout.String()).Debug("Sleep")
222 time.Sleep(c.config.Peer.Timeout)
223 case <-c.rehandshaking:
226 close(c.rehandshaking)
229 l.WithFields(c.config.LogFields()).Debug("Run post down action")
230 if err = c.postDownAction(); err != nil {
231 c.Error <- errors.Wrap(err, "c.postDownAction")
235 // NewClient returns a configured GoVPN client, to trigger connection
236 // MainCycle must be executed.
237 func NewClient(conf Configuration, logger *logrus.Logger, termSignal chan interface{}) (*Client, error) {
239 idsCache: govpn.NewMACCache(),
242 termSignal: termSignal,
243 Error: make(chan error, 1),
244 knownPeers: govpn.KnownPeers(make(map[string]**govpn.Peer)),
247 govpn.SetLogger(client.logger)
248 confs := map[govpn.PeerID]*govpn.PeerConf{*conf.Peer.ID: conf.Peer}
249 if err := client.idsCache.Update(&confs); err != nil {
250 return nil, errors.Wrap(err, "client.idsCache.Update")