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 allow to create a Client from a pre-existing file descriptor.
44 // Required for Android. requires TCP protocol
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 {
63 return errors.Errorf("Connect with file descriptor requires protocol %s", govpn.ProtocolTCP.String())
68 // LogFields return a logrus compatible logging context
69 func (c *Configuration) LogFields() logrus.Fields {
70 const prefix = "client_conf_"
71 f := c.Peer.LogFields(prefix)
72 f[prefix+"protocol"] = c.Protocol.String()
73 f[prefix+"no_reconnect"] = c.NoReconnect
74 if len(c.ProxyAddress) > 0 {
75 f[prefix+"proxy"] = c.ProxyAddress
77 if c.FileDescriptor > 0 {
78 f[prefix+"remote"] = fmt.Sprintf("fd:%d(%s)", c.FileDescriptor, c.RemoteAddress)
80 f[prefix+"remote"] = c.RemoteAddress
85 func (c *Configuration) isProxy() bool {
86 return len(c.ProxyAddress) > 0
89 // Client is a GoVPN client
91 idsCache *govpn.MACCache
93 knownPeers govpn.KnownPeers
94 timeouted chan struct{}
95 rehandshaking chan struct{}
96 termination chan struct{}
98 termSignal chan interface{}
100 logger *logrus.Logger
102 // Error channel receives any kind of routine errors
106 // LogFields return a logrus compatible logging context
107 func (c *Client) LogFields() logrus.Fields {
108 const prefix = "client_"
110 prefix + "remote": c.config.RemoteAddress,
113 f[prefix+"interface"] = c.tap.Name
115 if c.config.Peer != nil {
116 f[prefix+"id"] = c.config.Peer.ID.String()
121 func (c *Client) postDownAction() error {
122 if c.config.Peer.Down == nil {
125 err := c.config.Peer.Down(govpn.PeerContext{
126 RemoteAddress: c.config.RemoteAddress,
127 Protocol: c.config.Protocol,
128 Config: *c.config.Peer,
130 return errors.Wrap(err, "c.config.Peer.Down")
133 func (c *Client) postUpAction() error {
134 if c.config.Peer.Up == nil {
137 err := c.config.Peer.Up(govpn.PeerContext{
138 RemoteAddress: c.config.RemoteAddress,
139 Protocol: c.config.Protocol,
140 Config: *c.config.Peer,
142 return errors.Wrap(err, "c.config.Peer.Up")
145 // KnownPeers return GoVPN peers. Always 1.
146 // used to get client statistics.
147 func (c *Client) KnownPeers() *govpn.KnownPeers {
151 // MainCycle main loop of a connecting/connected client
152 func (c *Client) MainCycle() {
154 l := c.logger.WithFields(logrus.Fields{"func": logFuncPrefix + "Client.MainCycle"})
155 l.WithFields(c.LogFields()).WithFields(c.config.LogFields()).Info("Starting...")
157 // if available, run PreUp, it might create interface
158 if c.config.Peer.PreUp != nil {
159 l.Debug("Running PreUp")
160 if c.tap, err = c.config.Peer.PreUp(govpn.PeerContext{
161 RemoteAddress: c.config.RemoteAddress,
162 Protocol: c.config.Protocol,
163 Config: *c.config.Peer,
165 c.Error <- errors.Wrap(err, "c.config.Peer.PreUp")
168 l.Debug("PreUp success")
170 l.Debug("No PreUp to run")
173 // if tap wasn't set by PreUp, listen here
175 l.WithField("asking", c.config.Peer.Iface).Debug("No interface, try to listen")
176 c.tap, err = govpn.TAPListen(c.config.Peer.Iface, c.config.Peer.MTU)
178 c.Error <- errors.Wrapf(err, "govpn.TAPListen inteface:%s mtu:%d", c.config.Peer.Iface, c.config.Peer.MTU)
182 c.config.Peer.Iface = c.tap.Name
183 l.WithFields(c.LogFields()).Debug("Got interface, start main loop")
187 c.timeouted = make(chan struct{})
188 c.rehandshaking = make(chan struct{})
189 c.termination = make(chan struct{})
190 switch c.config.Protocol {
191 case govpn.ProtocolUDP:
194 case govpn.ProtocolTCP:
196 if c.config.isProxy() {
204 l.WithFields(c.LogFields()).Debug("Finish")
205 c.termination <- struct{}{}
206 // empty value signals that everything is fine
210 if c.config.NoReconnect {
211 l.Debug("No reconnect, stop")
215 l.WithField("timeout", c.config.Peer.Timeout.String()).Debug("Sleep")
216 time.Sleep(c.config.Peer.Timeout)
217 case <-c.rehandshaking:
220 close(c.rehandshaking)
223 l.WithFields(c.config.LogFields()).Debug("Run post down action")
224 if err = c.postDownAction(); err != nil {
225 c.Error <- errors.Wrap(err, "c.postDownAction")
229 // NewClient returns a configured GoVPN client, to trigger connection
230 // MainCycle must be executed.
231 func NewClient(conf Configuration, logger *logrus.Logger, termSignal chan interface{}) (*Client, error) {
233 idsCache: govpn.NewMACCache(),
236 termSignal: termSignal,
237 Error: make(chan error, 1),
238 knownPeers: govpn.KnownPeers(make(map[string]**govpn.Peer)),
241 govpn.SetLogger(client.logger)
242 confs := map[govpn.PeerID]*govpn.PeerConf{*conf.Peer.ID: conf.Peer}
243 if err := client.idsCache.Update(&confs); err != nil {
244 return nil, errors.Wrap(err, "client.idsCache.Update")