]> Cypherpunks.ru repositories - govpn.git/commitdiff
Initial syslog support
authorSergey Matveev <stargrave@stargrave.org>
Sun, 8 May 2016 19:52:47 +0000 (22:52 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 8 May 2016 19:52:47 +0000 (22:52 +0300)
12 files changed:
VERSION
src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
src/cypherpunks.ru/govpn/cmd/govpn-client/proxy.go
src/cypherpunks.ru/govpn/cmd/govpn-client/tcp.go
src/cypherpunks.ru/govpn/cmd/govpn-client/udp.go
src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
src/cypherpunks.ru/govpn/cmd/govpn-server/conf.go
src/cypherpunks.ru/govpn/cmd/govpn-server/main.go
src/cypherpunks.ru/govpn/cmd/govpn-server/proxy.go
src/cypherpunks.ru/govpn/cmd/govpn-server/tcp.go
src/cypherpunks.ru/govpn/cmd/govpn-server/udp.go
src/cypherpunks.ru/govpn/logger.go [new file with mode: 0644]

diff --git a/VERSION b/VERSION
index 760606e1ffff82566e95fa1bfa840ca7d1024dd5..3659ea2fa3a7771c8641de0cdbf8c0a02e47e49c 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-5.7
+5.8
index 58d5ce7d8597022df5a0a9e9197cf71f1dce0cdf..c2ddb3e1b9a1d5e1773f8855cd42e4a3bd3903f1 100644 (file)
@@ -49,6 +49,7 @@ var (
        encless     = flag.Bool("encless", false, "Encryptionless mode")
        cpr         = flag.Int("cpr", 0, "Enable constant KiB/sec out traffic rate")
        egdPath     = flag.String("egd", "", "Optional path to EGD socket")
+       syslog      = flag.Bool("syslog", false, "Enable logging to syslog")
        warranty    = flag.Bool("warranty", false, "Print warranty information")
 
        conf        *govpn.PeerConf
@@ -126,6 +127,10 @@ func main() {
                go govpn.StatsProcessor(statsPort, &knownPeers)
        }
 
+       if *syslog {
+               govpn.SyslogEnable()
+       }
+
        termSignal := make(chan os.Signal, 1)
        signal.Notify(termSignal, os.Interrupt, os.Kill)
 
@@ -151,7 +156,8 @@ MainCycle:
                }
                select {
                case <-termSignal:
-                       log.Fatalln("Finishing")
+                       log.Println("Finishing")
+                       govpn.Println("Finishing")
                        termination <- struct{}{}
                        break MainCycle
                case <-timeouted:
index a4259f39c2840d5f57b61084ca9f1dc8738f6f35..430f1ef971b0c2aaf099dd5fb87937b6e5f6b878 100644 (file)
@@ -24,6 +24,8 @@ import (
        "log"
        "net"
        "net/http"
+
+       "cypherpunks.ru/govpn"
 )
 
 func proxyTCP(timeouted, rehandshaking, termination chan struct{}) {
@@ -50,6 +52,6 @@ func proxyTCP(timeouted, rehandshaking, termination chan struct{}) {
        if err != nil || resp.StatusCode != http.StatusOK {
                log.Fatalln("Unexpected response from proxy")
        }
-       log.Println("Connected to proxy:", *proxyAddr)
+       govpn.Println("Connected to proxy:", *proxyAddr)
        go handleTCP(conn, timeouted, rehandshaking, termination)
 }
index 4b11eac1091f8c5cca3bd458d95230fc1ba89b7e..8c232e446aba7fe890dc5c2d7e95eef1ed7f263a 100644 (file)
@@ -37,7 +37,7 @@ func startTCP(timeouted, rehandshaking, termination chan struct{}) {
        if err != nil {
                log.Fatalln("Can not connect to address:", err)
        }
-       log.Println("Connected to TCP:" + *remoteAddr)
+       govpn.Println("Connected to TCP:" + *remoteAddr)
        handleTCP(conn, timeouted, rehandshaking, termination)
 }
 
@@ -57,7 +57,7 @@ HandshakeCycle:
                default:
                }
                if prev == len(buf) {
-                       log.Println("Timeouted waiting for the packet")
+                       govpn.Println("Timeouted waiting for the packet")
                        timeouted <- struct{}{}
                        break HandshakeCycle
                }
@@ -65,7 +65,7 @@ HandshakeCycle:
                conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
                n, err = conn.Read(buf[prev:])
                if err != nil {
-                       log.Println("Connection timeouted")
+                       govpn.Println("Connection timeouted")
                        timeouted <- struct{}{}
                        break HandshakeCycle
                }
@@ -80,7 +80,7 @@ HandshakeCycle:
                if peer == nil {
                        continue
                }
-               log.Println("Handshake completed")
+               govpn.Println("Handshake completed")
                knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
                if firstUpCall {
                        go govpn.ScriptCall(*upPath, *ifaceName, *remoteAddr)
@@ -126,14 +126,14 @@ TransportCycle:
                default:
                }
                if prev == len(buf) {
-                       log.Println("Timeouted waiting for the packet")
+                       govpn.Println("Timeouted waiting for the packet")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
                conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
                n, err = conn.Read(buf[prev:])
                if err != nil {
-                       log.Println("Connection timeouted")
+                       govpn.Println("Connection timeouted")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
@@ -147,12 +147,12 @@ TransportCycle:
                        continue
                }
                if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
-                       log.Println("Unauthenticated packet, dropping connection")
+                       govpn.Println("Unauthenticated packet, dropping connection")
                        timeouted <- struct{}{}
                        break TransportCycle
                }
                if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
-                       log.Println("Need rehandshake")
+                       govpn.Println("Need rehandshake")
                        rehandshaking <- struct{}{}
                        break TransportCycle
                }
index acacbd15473b435abbada466ec37f8030e66ff85..1b3396454be9172ef3988dd72fc7bf84131a671f 100644 (file)
@@ -36,7 +36,7 @@ func startUDP(timeouted, rehandshaking, termination chan struct{}) {
        if err != nil {
                log.Fatalln("Can not listen on UDP:", err)
        }
-       log.Println("Connected to UDP:" + *remoteAddr)
+       govpn.Println("Connected to UDP:" + *remoteAddr)
 
        hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
        buf := make([]byte, *mtu*2)
@@ -55,7 +55,7 @@ MainCycle:
                conn.SetReadDeadline(time.Now().Add(time.Second))
                n, err = conn.Read(buf)
                if timeouts == timeout {
-                       log.Println("Timeouted")
+                       govpn.Println("Timeouted")
                        timeouted <- struct{}{}
                        break
                }
@@ -67,18 +67,18 @@ MainCycle:
                        if peer.PktProcess(buf[:n], tap, true) {
                                timeouts = 0
                        } else {
-                               log.Println("Unauthenticated packet")
+                               govpn.Println("Unauthenticated packet")
                                timeouts++
                        }
                        if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
-                               log.Println("Need rehandshake")
+                               govpn.Println("Need rehandshake")
                                rehandshaking <- struct{}{}
                                break MainCycle
                        }
                        continue
                }
                if idsCache.Find(buf[:n]) == nil {
-                       log.Println("Invalid identity in handshake packet")
+                       govpn.Println("Invalid identity in handshake packet")
                        continue
                }
                timeouts = 0
@@ -86,7 +86,7 @@ MainCycle:
                if peer == nil {
                        continue
                }
-               log.Println("Handshake completed")
+               govpn.Println("Handshake completed")
                knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
                if firstUpCall {
                        go govpn.ScriptCall(*upPath, *ifaceName, *remoteAddr)
index f7435dddcd766a63337e23e9762fd20d5ec5cfdb..770ec2256b76d33d38716b267a1c9dc68a818267 100644 (file)
@@ -20,7 +20,6 @@ package main
 
 import (
        "bytes"
-       "log"
        "sync"
        "time"
 
@@ -71,7 +70,7 @@ func callUp(peerId *govpn.PeerId, remoteAddr string) (string, error) {
        if confs[*peerId].Up != "" {
                result, err := govpn.ScriptCall(confs[*peerId].Up, ifaceName, remoteAddr)
                if err != nil {
-                       log.Println("Script", confs[*peerId].Up, "call failed", err)
+                       govpn.Println("Script", confs[*peerId].Up, "call failed", err)
                        return "", err
                }
                if ifaceName == "" {
@@ -83,7 +82,7 @@ func callUp(peerId *govpn.PeerId, remoteAddr string) (string, error) {
                }
        }
        if ifaceName == "" {
-               log.Println("Can not obtain interface name for", *peerId)
+               govpn.Println("Can not obtain interface name for", *peerId)
        }
        return ifaceName, nil
 }
index e486ec97b12a92b37f03a9b2846612bf70903701..25fddd012927512f731bb3ead42d4bd2235f34d0 100644 (file)
@@ -62,7 +62,7 @@ func confRead() (*map[govpn.PeerId]*govpn.PeerConf, error) {
                        pc.MTU = govpn.MTUDefault
                }
                if pc.MTU > govpn.MTUMax {
-                       log.Println("MTU value", pc.MTU, "is too high, overriding to", govpn.MTUMax)
+                       govpn.Println("MTU value", pc.MTU, "is too high, overriding to", govpn.MTUMax)
                        pc.MTU = govpn.MTUMax
                }
                conf := govpn.PeerConf{
@@ -90,7 +90,7 @@ func confRead() (*map[govpn.PeerId]*govpn.PeerConf, error) {
 func confRefresh() error {
        newConfs, err := confRead()
        if err != nil {
-               log.Println("Unable to parse peers configuration:", err)
+               govpn.Println("Unable to parse peers configuration:", err)
                return err
        }
        confs = *newConfs
index 606caf0ede5ccbb32c0e0e267dbadfc9d6f59793..4b4206560e834877ba7a6400f588575b1547455e 100644 (file)
@@ -38,6 +38,7 @@ var (
        stats    = flag.String("stats", "", "Enable stats retrieving on host:port")
        proxy    = flag.String("proxy", "", "Enable HTTP proxy on host:port")
        egdPath  = flag.String("egd", "", "Optional path to EGD socket")
+       syslog   = flag.Bool("syslog", false, "Enable logging to syslog")
        warranty = flag.Bool("warranty", false, "Print warranty information")
 )
 
@@ -88,7 +89,12 @@ func main() {
        if *proxy != "" {
                go proxyStart()
        }
+
+       if *syslog {
+               govpn.SyslogEnable()
+       }
        log.Println("Server started")
+       govpn.Println("Server started")
 
        var needsDeletion bool
 MainCycle:
@@ -96,6 +102,7 @@ MainCycle:
                select {
                case <-termSignal:
                        log.Println("Terminating")
+                       govpn.Println("Terminating")
                        for _, ps := range peers {
                                govpn.ScriptCall(
                                        confs[*ps.peer.Id].Down,
@@ -109,7 +116,7 @@ MainCycle:
                        hsLock.Lock()
                        for addr, hs := range handshakes {
                                if hs.LastPing.Add(timeout).Before(now) {
-                                       log.Println("Deleting handshake state", addr)
+                                       govpn.Println("Deleting handshake state", addr)
                                        hs.Zero()
                                        delete(handshakes, addr)
                                }
@@ -122,7 +129,7 @@ MainCycle:
                                needsDeletion = ps.peer.LastPing.Add(timeout).Before(now)
                                ps.peer.BusyR.Unlock()
                                if needsDeletion {
-                                       log.Println("Deleting peer", ps.peer)
+                                       govpn.Println("Deleting peer", ps.peer)
                                        delete(peers, addr)
                                        delete(knownPeers, addr)
                                        delete(peersById, *ps.peer.Id)
index f1f5e09d37aeec0558de8033cd361e2e595e9b62..bcfd882c40e4e8c94b3db7b6fe8ab89ac3634c35 100644 (file)
@@ -21,6 +21,8 @@ package main
 import (
        "log"
        "net/http"
+
+       "cypherpunks.ru/govpn"
 )
 
 type proxyHandler struct{}
@@ -28,7 +30,7 @@ type proxyHandler struct{}
 func (p proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        conn, _, err := w.(http.Hijacker).Hijack()
        if err != nil {
-               log.Println("Hijacking failed:", err.Error())
+               govpn.Println("Hijacking failed:", err.Error())
                return
        }
        conn.Write([]byte("HTTP/1.0 200 OK\n\n"))
@@ -37,9 +39,11 @@ func (p proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 func proxyStart() {
        log.Println("HTTP proxy listening on:" + *proxy)
+       govpn.Println("HTTP proxy listening on:" + *proxy)
        s := &http.Server{
                Addr:    *proxy,
                Handler: proxyHandler{},
        }
        log.Println("HTTP proxy result:", s.ListenAndServe())
+       govpn.Println("HTTP proxy result:", s.ListenAndServe())
 }
index 6ca6967f60af4c892b50d59242ddf273a17d7154..f8a76e23dbf0b9a60330a161bf01b7831d83d8d9 100644 (file)
@@ -37,11 +37,12 @@ func startTCP() {
                log.Fatalln("Can not listen on TCP:", err)
        }
        log.Println("Listening on TCP:" + *bindAddr)
+       govpn.Println("Listening on TCP:" + *bindAddr)
        go func() {
                for {
                        conn, err := listener.AcceptTCP()
                        if err != nil {
-                               log.Println("Error accepting TCP:", err)
+                               govpn.Println("Error accepting TCP:", err)
                                continue
                        }
                        go handleTCP(conn)
@@ -78,7 +79,7 @@ func handleTCP(conn net.Conn) {
                if hs == nil {
                        conf = confs[*peerId]
                        if conf == nil {
-                               log.Println("Can not get peer configuration:", peerId.String())
+                               govpn.Println("Can not get peer configuration:", peerId.String())
                                break
                        }
                        hs = govpn.NewHandshake(addr, conn, conf)
@@ -89,7 +90,7 @@ func handleTCP(conn net.Conn) {
                        continue
                }
                hs.Zero()
-               log.Println("Peer handshake finished:", addr, peer.Id.String())
+               govpn.Println("Peer handshake finished:", addr, peer.Id.String())
                peersByIdLock.RLock()
                addrPrev, exists := peersById[*peer.Id]
                peersByIdLock.RUnlock()
@@ -113,7 +114,7 @@ func handleTCP(conn net.Conn) {
                        peersLock.Unlock()
                        peersByIdLock.Unlock()
                        kpLock.Unlock()
-                       log.Println("Rehandshake processed:", peer.Id.String())
+                       govpn.Println("Rehandshake processed:", peer.Id.String())
                } else {
                        ifaceName, err := callUp(peer.Id, peer.Addr)
                        if err != nil {
@@ -122,7 +123,7 @@ func handleTCP(conn net.Conn) {
                        }
                        tap, err = govpn.TAPListen(ifaceName, peer.MTU)
                        if err != nil {
-                               log.Println("Unable to create TAP:", err)
+                               govpn.Println("Unable to create TAP:", err)
                                peer = nil
                                break
                        }
@@ -141,7 +142,7 @@ func handleTCP(conn net.Conn) {
                        peersLock.Unlock()
                        peersByIdLock.Unlock()
                        kpLock.Unlock()
-                       log.Println("Peer created:", peer.Id.String())
+                       govpn.Println("Peer created:", peer.Id.String())
                }
                break
        }
@@ -176,7 +177,7 @@ func handleTCP(conn net.Conn) {
                        continue
                }
                if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
-                       log.Println(
+                       govpn.Println(
                                "Unauthenticated packet, dropping connection",
                                addr, peer.Id.String(),
                        )
index d9197cfbbe0e52c8721ffb27bf4f7b818b1abe0c..f654181fa1bc583a0f03e2196d5b9325c0955ca6 100644 (file)
@@ -49,6 +49,7 @@ func startUDP() {
                log.Fatalln("Can not listen on UDP:", err)
        }
        log.Println("Listening on UDP:" + *bindAddr)
+       govpn.Println("Listening on UDP:" + *bindAddr)
 
        udpBufs <- make([]byte, govpn.MTUMax)
        go func() {
@@ -68,7 +69,7 @@ func startUDP() {
                        buf = <-udpBufs
                        n, raddr, err = conn.ReadFromUDP(buf)
                        if err != nil {
-                               log.Println("Unexpected error when receiving", err)
+                               govpn.Println("Unexpected error when receiving", err)
                                break
                        }
                        addr = raddr.String()
@@ -96,7 +97,7 @@ func startUDP() {
                                goto Finished
                        }
 
-                       log.Println("Peer handshake finished:", addr, peer.Id.String())
+                       govpn.Println("Peer handshake finished:", addr, peer.Id.String())
                        hs.Zero()
                        hsLock.Lock()
                        delete(handshakes, addr)
@@ -132,7 +133,7 @@ func startUDP() {
                                peersLock.Unlock()
                                peersByIdLock.Unlock()
                                kpLock.Unlock()
-                               log.Println("Rehandshake processed:", peer.Id.String())
+                               govpn.Println("Rehandshake processed:", peer.Id.String())
                        } else {
                                go func(addr string, peer *govpn.Peer) {
                                        ifaceName, err := callUp(peer.Id, peer.Addr)
@@ -141,7 +142,7 @@ func startUDP() {
                                        }
                                        tap, err := govpn.TAPListen(ifaceName, peer.MTU)
                                        if err != nil {
-                                               log.Println("Unable to create TAP:", err)
+                                               govpn.Println("Unable to create TAP:", err)
                                                return
                                        }
                                        ps = &PeerState{
@@ -163,19 +164,19 @@ func startUDP() {
                                        peersLock.Unlock()
                                        peersByIdLock.Unlock()
                                        kpLock.Unlock()
-                                       log.Println("Peer created:", peer.Id.String())
+                                       govpn.Println("Peer created:", peer.Id.String())
                                }(addr, peer)
                        }
                        goto Finished
                CheckID:
                        peerId = idsCache.Find(buf[:n])
                        if peerId == nil {
-                               log.Println("Unknown identity from:", addr)
+                               govpn.Println("Unknown identity from:", addr)
                                goto Finished
                        }
                        conf = confs[*peerId]
                        if conf == nil {
-                               log.Println("Unable to get peer configuration:", peerId.String())
+                               govpn.Println("Unable to get peer configuration:", peerId.String())
                                goto Finished
                        }
                        hs = govpn.NewHandshake(
diff --git a/src/cypherpunks.ru/govpn/logger.go b/src/cypherpunks.ru/govpn/logger.go
new file mode 100644 (file)
index 0000000..f646338
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+GoVPN -- simple secure free software virtual private network daemon
+Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package govpn
+
+import (
+       "log"
+       "log/syslog"
+)
+
+var (
+       sysloger *log.Logger
+)
+
+// Enable logging to syslog, instead of default stdout log.
+func SyslogEnable() {
+       var err error
+       sysloger, err = syslog.NewLogger(syslog.LOG_INFO, 0)
+       if err != nil {
+               log.Fatalln(err)
+       }
+}
+
+// Call either syslog-related logger.Println if SyslogEnabled,
+// default log.Println otherwise.
+func Println(v ...interface{}) {
+       if sysloger == nil {
+               log.Println(v...)
+       } else {
+               sysloger.Println(v...)
+       }
+}