import (
"flag"
"fmt"
- "log"
- "os"
- "os/signal"
"time"
+ "github.com/Sirupsen/logrus"
+
"cypherpunks.ru/govpn"
"cypherpunks.ru/govpn/client"
)
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("log_level", "warning", "Log level")
+ protocol govpn.Protocol
+ err error
+ fields = logrus.Fields{"func": "main"}
)
flag.Parse()
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("Couldn't 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't 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),
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't 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")
}
}