]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/cmd/govpn-client/main.go
Go language does not like underscores in names
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-client / main.go
index df426ba8820c45bac23d84d83a21e814cf6e5540..b17063297ca8e51b2900d0a56a10926f03843e5b 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2017 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
@@ -22,11 +22,10 @@ package main
 import (
        "flag"
        "fmt"
-       "log"
-       "os"
-       "os/signal"
        "time"
 
+       "github.com/Sirupsen/logrus"
+
        "cypherpunks.ru/govpn"
        "cypherpunks.ru/govpn/client"
 )
@@ -54,8 +53,10 @@ func main() {
                syslog      = flag.Bool("syslog", false, "Enable logging to syslog")
                version     = flag.Bool("version", false, "Print version information")
                warranty    = flag.Bool("warranty", false, "Print warranty information")
-               protocol client.Protocol
-               err error
+               logLevel    = flag.String("loglevel", "warning", "Logging level")
+               protocol    govpn.Protocol
+               err         error
+               fields      = logrus.Fields{"func": "main"}
        )
 
        flag.Parse()
@@ -67,52 +68,59 @@ func main() {
                fmt.Println(govpn.VersionGet())
                return
        }
-       log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
+
+       logger, err := govpn.NewLogger(*logLevel, *syslog)
+       if err != nil {
+               logrus.WithFields(fields).WithError(err).Fatal("Can not initialize logging")
+       }
 
        if *egdPath != "" {
-               log.Println("Using", *egdPath, "EGD")
+               logger.WithField("egd_path", *egdPath).WithFields(fields).Debug("Init EGD")
                govpn.EGDInit(*egdPath)
        }
 
-       switch *proto {
-       case "udp":
-               protocol = client.ProtocolUDP
-       case "tcp":
-               protocol = client.ProtocolTCP
-       default:
-               log.Fatalln("Unknown protocol specified")
+       if protocol, err = govpn.NewProtocolFromString(*proto); err != nil {
+               logger.WithError(
+                       err,
+               ).WithFields(
+                       fields,
+               ).WithField(
+                       "proto", *proto,
+               ).Fatal("Invalid protocol")
        }
 
-       if *proxyAddr != "" && protocol == client.ProtocolUDP {
-               log.Println("Proxy is supported for TCP only, switch")
-               protocol = client.ProtocolTCP
+       if *proxyAddr != "" && protocol == govpn.ProtocolUDP {
+               logrus.WithFields(fields).WithFields(logrus.Fields{
+                       "proxy": *proxyAddr,
+                       "proto": *proto,
+               }).Fatal("HTTP proxy is supported only in TCP mode")
        }
 
        if *verifierRaw == "" {
-               log.Fatalln("-verifier is required")
+               logger.Fatalln("-verifier is required")
        }
        verifier, err := govpn.VerifierFromString(*verifierRaw)
        if err != nil {
-               log.Fatalln("Invalid -verifier:", err)
+               logger.WithError(err).Fatal("Invalid -verifier")
        }
        key, err := govpn.KeyRead(*keyPath)
        if err != nil {
-               log.Fatalln("Invalid -key:", err)
+               logger.WithError(err).Fatal("Invalid -key")
+       }
+       priv, err := verifier.PasswordApply(key)
+       if err != nil {
+               logger.WithError(err).Fatal("Can not PasswordApply")
        }
-       priv := verifier.PasswordApply(key)
        if *encless {
-               if protocol != client.ProtocolTCP {
-                       log.Fatalln("Currently encryptionless mode works only with TCP")
-               }
-               if !*noisy {
-                       log.Println("-encless is on, force -noisy mode")
-                       *noisy = true
+               if protocol != govpn.ProtocolTCP {
+                       logger.Fatal("Currently encryptionless mode works only with TCP")
                }
+               *noisy = true
        }
        conf := client.Configuration{
                PrivateKey: priv,
                Peer: &govpn.PeerConf{
-                       Id:       verifier.Id,
+                       ID:       verifier.ID,
                        Iface:    *ifaceName,
                        MTU:      *mtu,
                        Timeout:  time.Second * time.Duration(*timeoutP),
@@ -122,35 +130,30 @@ func main() {
                        Encless:  *encless,
                        Verifier: verifier,
                        DSAPriv:  priv,
+                       Up:       govpn.RunScriptAction(upPath),
+                       Down:     govpn.RunScriptAction(downPath),
                },
-               Protocol: protocol,
-               InterfaceName: *ifaceName,
-               ProxyAddress: *proxyAddr,
+               Protocol:            protocol,
+               ProxyAddress:        *proxyAddr,
                ProxyAuthentication: *proxyAuth,
-               RemoteAddress: *remoteAddr,
-               UpPath: *upPath,
-               DownPath: *downPath,
-               StatsAddress: *stats,
-               NoReconnect: *noreconnect,
-               MTU: *mtu,
+               RemoteAddress:       *remoteAddr,
+               NoReconnect:         *noreconnect,
        }
        if err = conf.Validate(); err != nil {
-               log.Fatalln("Invalid settings:", err)
+               logger.WithError(err).Fatal("Invalid settings")
        }
 
-       log.Println(govpn.VersionGet())
+       c, err := client.NewClient(conf, logger, govpn.CatchSignalShutdown())
+       if err != nil {
+               logger.WithError(err).Fatal("Can not initialize client")
+       }
 
-       if *syslog {
-               govpn.SyslogEnable()
+       if *stats != "" {
+               go govpn.StatsProcessor(*stats, c.KnownPeers())
        }
 
-       termSignal := make(chan os.Signal, 1)
-       signal.Notify(termSignal, os.Interrupt, os.Kill)
-       c := client.NewClient(conf, verifier, termSignal)
        go c.MainCycle()
-       if err := <-c.Error; err != nil {
-               log.Fatalln(err)
-       } else {
-               log.Println("Closed VPN tunnel")
+       if err = <-c.Error; err != nil {
+               logger.WithError(err).Fatal("Fatal error")
        }
 }