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
> 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
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
```bash
@@ -16,8 +16,10 @@ wg [-c config.yaml] [-d|w] [-g] [-h] [-m mtu] [-p]
-d print debug logs
-g generate key pair
-h display this help
-l string
write log to file (default "-")
-m int
set the mtu of wg (default 32700)
set the mtu of wg (default 1432)
-p show my publickey
-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")
debug := flag.Bool("d", false, "print debug logs")
warn := flag.Bool("w", false, "only show logs above warn level")
logfile := flag.String("l", "-", "write log to file")
flag.Parse()
if *debug {
logrus.SetLevel(logrus.DebugLevel)
@@ -50,6 +51,14 @@ func main() {
fmt.Println("PrivateKey:", helper.BytesToString(prvk[:57]))
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) {
f := new(bytes.Buffer)
var r string

View File

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

View File

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

View File

@@ -3,6 +3,7 @@ package tunnel
import (
"crypto/rand"
"encoding/hex"
"io"
"testing"
curve "github.com/fumiama/go-x25519"
@@ -84,6 +85,17 @@ func TestTunnel(t *testing.T) {
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()
tunnpeer.Stop()
}