add log to file option -l

This commit is contained in:
源文雨
2022-04-20 19:02:32 +08:00
parent 9f90cc456c
commit 6586b5dd10
5 changed files with 28 additions and 5 deletions

View File

@@ -7,7 +7,7 @@
## Usage ## Usage
> If you are running in windows, remember to select the `wintun.dll` of your arch in `lower/wintun` and place it alongside the compiled exe > If you are running in windows, remember to select the `wintun.dll` of your arch in `lower/wintun` and place it alongside the compiled exe
```bash ```bash
wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p] wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p] [-l log.txt]
``` ```
#### Instructions #### Instructions
```bash ```bash
@@ -16,8 +16,10 @@ wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p]
-d print debug logs -d print debug logs
-g generate key pair -g generate key pair
-h display this help -h display this help
-l string
write log to file (default "-")
-m int -m int
set the mtu of wg (default 32700) set the mtu of wg (default 1432)
-p show my publickey -p show my publickey
-w only show logs above warn level -w only show logs above warn level
``` ```

View File

@@ -24,6 +24,7 @@ func main() {
mtu := flag.Int("m", 1500-68, "set the mtu of wg") mtu := flag.Int("m", 1500-68, "set the mtu of wg")
debug := flag.Bool("d", false, "print debug logs") debug := flag.Bool("d", false, "print debug logs")
warn := flag.Bool("w", false, "only show logs above warn level") warn := flag.Bool("w", false, "only show logs above warn level")
logfile := flag.String("l", "-", "write log to file")
flag.Parse() flag.Parse()
if *debug { if *debug {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
@@ -50,6 +51,14 @@ func main() {
fmt.Println("PrivateKey:", helper.BytesToString(prvk[:57])) fmt.Println("PrivateKey:", helper.BytesToString(prvk[:57]))
os.Exit(0) os.Exit(0)
} }
if *logfile != "-" {
f, err := os.Create(*logfile)
if err != nil {
panic(err)
}
defer f.Close()
logrus.SetOutput(f)
}
if helper.IsNotExist(*file) { if helper.IsNotExist(*file) {
f := new(bytes.Buffer) f := new(bytes.Buffer)
var r string var r string

View File

@@ -26,7 +26,7 @@ const (
colorReset = "\x1b[0m" colorReset = "\x1b[0m"
) )
// LogFormat specialize for zbp // LogFormat ...
type LogFormat struct{} type LogFormat struct{}
// Format implements logrus.Formatter // Format implements logrus.Formatter

View File

@@ -1,7 +1,7 @@
package tunnel package tunnel
import ( import (
"errors" "io"
"net" "net"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -72,7 +72,7 @@ func (s *Tunnel) Read(p []byte) (int, error) {
return copy(p, d[:len(p)]), nil return copy(p, d[:len(p)]), nil
} }
} }
return 0, errors.New("reading reaches nil") return 0, io.EOF
} }
func (s *Tunnel) Stop() { func (s *Tunnel) Stop() {

View File

@@ -3,6 +3,7 @@ package tunnel
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"io"
"testing" "testing"
curve "github.com/fumiama/go-x25519" curve "github.com/fumiama/go-x25519"
@@ -84,6 +85,17 @@ func TestTunnel(t *testing.T) {
t.Fatal("error: recv 4096 bytes data") t.Fatal("error: recv 4096 bytes data")
} }
sendb = make([]byte, 65535)
rand.Read(sendb)
n, _ := tunnme.Write(sendb)
t.Log("write", n, "bytes")
buf = make([]byte, 65535)
n, _ = io.ReadFull(&tunnpeer, buf)
t.Log("read", n, "bytes")
if string(sendb) != string(buf) {
t.Fatal("error: recv 65535 bytes data")
}
tunnme.Stop() tunnme.Stop()
tunnpeer.Stop() tunnpeer.Stop()
} }