2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "github.com/agl/ed25519"
30 "go.cypherpunks.ru/govpn/v7"
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
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 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
107 c.timeouted = make(chan struct{})
108 c.rehandshaking = make(chan struct{})
109 c.termination = make(chan struct{})
110 switch c.config.Protocol {
114 if c.config.isProxy() {
122 govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
123 c.termination <- struct{}{}
124 // empty value signals that everything is fine
128 if c.config.NoReconnect {
131 govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout/time.Second)
132 time.Sleep(c.config.Peer.Timeout)
133 case <-c.rehandshaking:
136 close(c.rehandshaking)
139 if _, err = govpn.ScriptCall(
141 c.config.InterfaceName,
142 c.config.RemoteAddress,
148 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
150 idsCache: govpn.NewMACCache(),
153 termSignal: termSignal,
154 Error: make(chan error, 1),
156 confs := map[govpn.PeerID]*govpn.PeerConf{*verifier.ID: conf.Peer}
157 client.idsCache.Update(&confs)