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