]> Cypherpunks.ru repositories - govpn.git/blobdiff - cmd/govpn-client/main.go
Ability to generate Constant Packet Rate traffic
[govpn.git] / cmd / govpn-client / main.go
index e358d43f8b82dad67ad9c6de2bfc4afeffbf3422..9d99f080d42ea4f1ebbd11fe0ce05234ae9b710a 100644 (file)
@@ -25,6 +25,7 @@ import (
        "net"
        "os"
        "os/signal"
+       "time"
 
        "govpn"
 )
@@ -36,9 +37,12 @@ var (
        keyPath    = flag.String("key", "", "Path to authentication key file")
        upPath     = flag.String("up", "", "Path to up-script")
        downPath   = flag.String("down", "", "Path to down-script")
-       mtu        = flag.Int("mtu", 1500, "MTU")
+       stats      = flag.String("stats", "", "Enable stats retrieving on host:port")
+       mtu        = flag.Int("mtu", 1452, "MTU for outgoing packets")
        nonceDiff  = flag.Int("noncediff", 1, "Allow nonce difference")
        timeoutP   = flag.Int("timeout", 60, "Timeout seconds")
+       noisy      = flag.Bool("noise", false, "Enable noise appending")
+       cpr        = flag.Int("cpr", 0, "Enable constant KiB/s out traffic rate")
 )
 
 func main() {
@@ -48,10 +52,13 @@ func main() {
        log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
 
        govpn.MTU = *mtu
-       govpn.Timeout = timeout
+       govpn.Timeout = time.Second * time.Duration(timeout)
        govpn.Noncediff = *nonceDiff
+       govpn.NoiseEnable = *noisy
+       govpn.CPRInit(*cpr)
 
        id := govpn.IDDecode(*IDRaw)
+       govpn.PeersInitDummy(id)
        key := govpn.KeyRead(*keyPath)
        if id == nil {
                panic("ID is not specified")
@@ -80,19 +87,31 @@ func main() {
        firstUpCall := true
        var peer *govpn.Peer
        var ethPkt []byte
-       var udpPkt *govpn.UDPPkt
+       var udpPkt govpn.UDPPkt
        var udpPktData []byte
+       knownPeers := govpn.KnownPeers(map[string]**govpn.Peer{remote.String(): &peer})
+
+       log.Println(govpn.VersionGet())
+       log.Println("Max MTU on TAP interface:", govpn.TAPMaxMTU())
+       if *stats != "" {
+               log.Println("Stats are going to listen on", *stats)
+               statsPort, err := net.Listen("tcp", *stats)
+               if err != nil {
+                       panic(err)
+               }
+               go govpn.StatsProcessor(statsPort, &knownPeers)
+       }
 
        termSignal := make(chan os.Signal, 1)
        signal.Notify(termSignal, os.Interrupt, os.Kill)
 
-       log.Println("Client version", govpn.Version)
        log.Println("Starting handshake")
        handshake := govpn.HandshakeStart(conn, remote, id, key)
 
 MainCycle:
        for {
-               if peer != nil && peer.Bytes > govpn.MaxBytesPerKey {
+               if peer != nil && (peer.BytesIn+peer.BytesOut) > govpn.MaxBytesPerKey {
+                       peer.Zero()
                        peer = nil
                        handshake = govpn.HandshakeStart(conn, remote, id, key)
                        log.Println("Rehandshaking")
@@ -102,7 +121,9 @@ MainCycle:
                        break MainCycle
                case ethPkt = <-ethSink:
                        if peer == nil {
-                               ethReady <- struct{}{}
+                               if len(ethPkt) > 0 {
+                                       ethReady <- struct{}{}
+                               }
                                continue
                        }
                        peer.EthProcess(ethPkt, conn, ethReady)
@@ -111,25 +132,31 @@ MainCycle:
                        if timeouts >= timeout {
                                break MainCycle
                        }
-                       if udpPkt == nil {
+                       if udpPkt.Addr == nil {
                                udpReady <- struct{}{}
                                continue
                        }
 
                        udpPktData = udpBuf[:udpPkt.Size]
-                       if govpn.IsValidHandshakePkt(udpPktData) {
+                       if peer == nil {
                                if udpPkt.Addr.String() != remote.String() {
                                        udpReady <- struct{}{}
                                        log.Println("Unknown handshake message")
                                        continue
                                }
-                               if p := handshake.Client(conn, key, udpPktData); p != nil {
+                               if govpn.IDsCache.Find(udpPktData) == nil {
+                                       log.Println("Invalid identity in handshake packet")
+                                       udpReady <- struct{}{}
+                                       continue
+                               }
+                               if p := handshake.Client(id, conn, key, udpPktData); p != nil {
                                        log.Println("Handshake completed")
                                        if firstUpCall {
                                                go govpn.ScriptCall(*upPath, *ifaceName)
                                                firstUpCall = false
                                        }
                                        peer = p
+                                       handshake.Zero()
                                        handshake = nil
                                }
                                udpReady <- struct{}{}