10 "github.com/agl/ed25519"
12 "cypherpunks.ru/govpn"
18 ProtocolUDP Protocol = iota
22 type Configuration struct {
23 PrivateKey *[ed25519.PrivateKeySize]byte
28 ProxyAuthentication string
37 func (c *Configuration) Validate() error {
38 if c.MTU > govpn.MTUMax {
39 return fmt.Errorf("Invalid MTU %d, maximum allowable is %d", c.MTU, govpn.MTUMax)
41 if len(c.RemoteAddress) == 0 {
42 return errors.New("Missing RemoteAddress")
44 if len(c.InterfaceName) == 0 {
45 return errors.New("Missing InterfaceName")
50 func (c *Configuration) isProxy() bool {
51 return len(c.ProxyAddress) > 0
55 idsCache *govpn.MACCache
57 knownPeers govpn.KnownPeers
58 statsPort net.Listener
59 timeouted chan struct{}
60 rehandshaking chan struct{}
61 termination chan struct{}
63 termSignal chan os.Signal
66 // Error channel receives any kind of routine errors
70 func (c *Client) MainCycle() {
72 c.tap, err = govpn.TAPListen(c.config.InterfaceName, c.config.MTU)
74 c.Error <- fmt.Errorf("Can not listen on TUN/TAP interface: %s", err.Error())
78 if len(c.config.StatsAddress) > 0 {
79 c.statsPort, err = net.Listen("tcp", c.config.StatsAddress)
81 c.Error <- fmt.Errorf("Can't listen on stats port: %s", err.Error())
84 c.knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
85 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
90 c.timeouted = make(chan struct{})
91 c.rehandshaking = make(chan struct{})
92 c.termination = make(chan struct{})
93 switch c.config.Protocol {
97 if c.config.isProxy() {
105 govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
106 c.termination <- struct{}{}
107 // empty value signals that everything is fine
111 if c.config.NoReconnect {
114 govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout)
115 time.Sleep(c.config.Peer.Timeout)
116 case <-c.rehandshaking:
119 close(c.rehandshaking)
122 if _, err = govpn.ScriptCall(
124 c.config.InterfaceName,
125 c.config.RemoteAddress,
131 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
133 idsCache: govpn.NewMACCache(),
136 termSignal: termSignal,
137 Error: make(chan error, 1),
139 confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf.Peer}
140 client.idsCache.Update(&confs)