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
30 UpPath, DownPath string
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)
40 if len(c.RemoteAddress) == 0 {
41 return errors.New("Missing RemoteAddress")
43 if len(c.InterfaceName) == 0 {
44 return errors.New("Missing InterfaceName")
49 func (c *Configuration) isProxy() bool {
50 return len(c.ProxyAddress) > 0
54 idsCache *govpn.MACCache
56 knownPeers govpn.KnownPeers
57 statsPort net.Listener
58 timeouted chan struct{}
59 rehandshaking chan struct{}
60 termination chan struct{}
62 termSignal chan os.Signal
65 // Error receive any error of all routines
69 func (c *Client) MainCycle() {
71 c.tap, err = govpn.TAPListen(c.config.InterfaceName, c.config.MTU)
73 c.Error <- fmt.Errorf("Can not listen on TUN/TAP interface: %s", err.Error())
77 if len(c.config.StatsAddress) > 0 {
78 c.statsPort, err = net.Listen("tcp", c.config.StatsAddress)
80 c.Error <- fmt.Errorf("Can't listen on stats port: %s", err.Error())
83 c.knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
84 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
89 c.timeouted = make(chan struct{})
90 c.rehandshaking = make(chan struct{})
91 c.termination = make(chan struct{})
92 switch c.config.Protocol {
96 if c.config.isProxy() {
104 govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
105 c.termination <- struct{}{}
106 // send a non-error to let know everything went fine
110 if c.config.NoReconnect {
113 govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout)
114 time.Sleep(c.config.Peer.Timeout)
115 case <-c.rehandshaking:
118 close(c.rehandshaking)
121 if _, err = govpn.ScriptCall(c.config.DownPath, c.config.InterfaceName, c.config.RemoteAddress); err != nil {
126 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
128 idsCache: govpn.NewMACCache(),
131 termSignal: termSignal,
132 Error: make(chan error, 1),
134 confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf.Peer}
135 client.idsCache.Update(&confs)