diff --git a/README.md b/README.md index 114a255..e9fd2db 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/main.go b/main.go index 464c024..7cec9f4 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/main_win.go b/main_win.go index ca6bade..b0645c6 100644 --- a/main_win.go +++ b/main_win.go @@ -26,7 +26,7 @@ const ( colorReset = "\x1b[0m" ) -// LogFormat specialize for zbp +// LogFormat ... type LogFormat struct{} // Format implements logrus.Formatter diff --git a/upper/services/tunnel/tunnel.go b/upper/services/tunnel/tunnel.go index 497cc77..d510e39 100644 --- a/upper/services/tunnel/tunnel.go +++ b/upper/services/tunnel/tunnel.go @@ -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() { diff --git a/upper/services/tunnel/tunnel_test.go b/upper/services/tunnel/tunnel_test.go index 0a3451c..9721aad 100644 --- a/upper/services/tunnel/tunnel_test.go +++ b/upper/services/tunnel/tunnel_test.go @@ -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() }