]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/client/client.go
go fmt code
[govpn.git] / src / cypherpunks.ru / govpn / client / client.go
1 package client
2
3 import (
4         "errors"
5         "fmt"
6         "net"
7         "os"
8         "time"
9
10         "github.com/agl/ed25519"
11
12         "cypherpunks.ru/govpn"
13 )
14
15 type Protocol uint8
16
17 const (
18         ProtocolUDP Protocol = iota
19         ProtocolTCP
20 )
21
22 type Configuration struct {
23         PrivateKey          *[ed25519.PrivateKeySize]byte
24         Peer                *govpn.PeerConf
25         Protocol            Protocol
26         InterfaceName       string
27         ProxyAddress        string
28         ProxyAuthentication string
29         RemoteAddress       string
30         UpPath, DownPath    string
31         StatsAddress        string
32         NoReconnect         bool
33         MTU                 int
34 }
35
36 func (c *Configuration) Validate() error {
37         if c.MTU > govpn.MTUMax {
38                 return fmt.Errorf("Invalid MTU %d, maximum allowable is %d", c.MTU, govpn.MTUMax)
39         }
40         if len(c.RemoteAddress) == 0 {
41                 return errors.New("Missing RemoteAddress")
42         }
43         if len(c.InterfaceName) == 0 {
44                 return errors.New("Missing InterfaceName")
45         }
46         return nil
47 }
48
49 func (c *Configuration) isProxy() bool {
50         return len(c.ProxyAddress) > 0
51 }
52
53 type Client struct {
54         idsCache      *govpn.MACCache
55         tap           *govpn.TAP
56         knownPeers    govpn.KnownPeers
57         statsPort     net.Listener
58         timeouted     chan struct{}
59         rehandshaking chan struct{}
60         termination   chan struct{}
61         firstUpCall   bool
62         termSignal    chan os.Signal
63         config        Configuration
64
65         // Error receive any error of all routines
66         Error chan error
67 }
68
69 func (c *Client) MainCycle() {
70         var err error
71         c.tap, err = govpn.TAPListen(c.config.InterfaceName, c.config.MTU)
72         if err != nil {
73                 c.Error <- fmt.Errorf("Can not listen on TUN/TAP interface: %s", err.Error())
74                 return
75         }
76
77         if len(c.config.StatsAddress) > 0 {
78                 c.statsPort, err = net.Listen("tcp", c.config.StatsAddress)
79                 if err != nil {
80                         c.Error <- fmt.Errorf("Can't listen on stats port: %s", err.Error())
81                         return
82                 }
83                 c.knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
84                 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
85         }
86
87 MainCycle:
88         for {
89                 c.timeouted = make(chan struct{})
90                 c.rehandshaking = make(chan struct{})
91                 c.termination = make(chan struct{})
92                 switch c.config.Protocol {
93                 case ProtocolUDP:
94                         go c.startUDP()
95                 case ProtocolTCP:
96                         if c.config.isProxy() {
97                                 go c.proxyTCP()
98                         } else {
99                                 go c.startTCP()
100                         }
101                 }
102                 select {
103                 case <-c.termSignal:
104                         govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
105                         c.termination <- struct{}{}
106                         // send a non-error to let know everything went fine
107                         c.Error <- nil
108                         break MainCycle
109                 case <-c.timeouted:
110                         if c.config.NoReconnect {
111                                 break MainCycle
112                         }
113                         govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout)
114                         time.Sleep(c.config.Peer.Timeout)
115                 case <-c.rehandshaking:
116                 }
117                 close(c.timeouted)
118                 close(c.rehandshaking)
119                 close(c.termination)
120         }
121         if _, err = govpn.ScriptCall(c.config.DownPath, c.config.InterfaceName, c.config.RemoteAddress); err != nil {
122                 c.Error <- err
123         }
124 }
125
126 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
127         client := &Client{
128                 idsCache:    govpn.NewMACCache(),
129                 firstUpCall: true,
130                 config:      conf,
131                 termSignal:  termSignal,
132                 Error:       make(chan error, 1),
133         }
134         confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf.Peer}
135         client.idsCache.Update(&confs)
136         return client
137 }