10 "github.com/agl/ed25519"
11 "cypherpunks.ru/govpn"
17 ProtocolUDP Protocol = iota
21 type Configuration struct {
22 PrivateKey *[ed25519.PrivateKeySize]byte
27 ProxyAuthentication string
29 UpPath, DownPath string
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)
39 if len(c.RemoteAddress) == 0 {
40 return errors.New("Missing RemoteAddress")
42 if len(c.InterfaceName) == 0 {
43 return errors.New("Missing InterfaceName")
48 func (c *Configuration) isProxy() bool {
49 return len(c.ProxyAddress) > 0
53 idsCache *govpn.MACCache
55 knownPeers govpn.KnownPeers
56 statsPort net.Listener
57 timeouted chan struct{}
58 rehandshaking chan struct{}
59 termination chan struct{}
61 termSignal chan os.Signal
64 // Error receive any error of all routines
68 func (c *Client) MainCycle() {
70 c.tap, err = govpn.TAPListen(c.config.InterfaceName, c.config.MTU)
72 c.Error <- fmt.Errorf("Can not listen on TUN/TAP interface: %s", err.Error())
76 if len(c.config.StatsAddress) > 0 {
77 c.statsPort, err = net.Listen("tcp", c.config.StatsAddress)
79 c.Error <- fmt.Errorf("Can't listen on stats port: %s", err.Error())
82 c.knownPeers = govpn.KnownPeers(make(map[string]**govpn.Peer))
83 go govpn.StatsProcessor(c.statsPort, &c.knownPeers)
88 c.timeouted = make(chan struct{})
89 c.rehandshaking = make(chan struct{})
90 c.termination = make(chan struct{})
91 switch c.config.Protocol {
95 if c.config.isProxy() {
103 govpn.BothPrintf(`[finish remote="%s"]`, c.config.RemoteAddress)
104 c.termination <- struct{}{}
105 // send a non-error to let know everything went fine
109 if c.config.NoReconnect {
112 govpn.BothPrintf(`[sleep seconds="%d"]`, c.config.Peer.Timeout)
113 time.Sleep(c.config.Peer.Timeout)
114 case <-c.rehandshaking:
117 close(c.rehandshaking)
120 if _, err = govpn.ScriptCall(c.config.DownPath, c.config.InterfaceName, c.config.RemoteAddress); err != nil {
125 func NewClient(conf Configuration, verifier *govpn.Verifier, termSignal chan os.Signal) *Client {
127 idsCache: govpn.NewMACCache(),
130 termSignal: termSignal,
131 Error: make(chan error, 1),
133 confs := map[govpn.PeerId]*govpn.PeerConf{*verifier.Id: conf.Peer}
134 client.idsCache.Update(&confs)