diff --git a/cmd/client.go b/cmd/client.go index 2bc1f52..5cc6fe3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -13,7 +13,6 @@ import ( "net" "net/http" "os" - "strings" "time" "github.com/lucas-clemente/quic-go" @@ -28,7 +27,6 @@ import ( "github.com/tobyxdd/hysteria/pkg/socks5" "github.com/tobyxdd/hysteria/pkg/tproxy" "github.com/tobyxdd/hysteria/pkg/transport" - "github.com/tobyxdd/hysteria/pkg/tun" ) func client(config *clientConfig) { @@ -248,51 +246,7 @@ func client(config *clientConfig) { } if len(config.TUN.Name) != 0 { - go func() { - timeout := time.Duration(config.TUN.Timeout) * time.Second - if timeout == 0 { - timeout = 300 * time.Second - } - tunServer, err := tun.NewServer(client, time.Duration(config.TUN.Timeout)*time.Second, - config.TUN.Name, config.TUN.MTU) - if err != nil { - logrus.WithField("error", err).Fatal("Failed to initialize TUN server") - } - tunServer.RequestFunc = func(addr net.Addr, reqAddr string) { - logrus.WithFields(logrus.Fields{ - "src": addr.String(), - "dst": reqAddr, - }).Debugf("TUN %s request", strings.ToUpper(addr.Network())) - } - tunServer.ErrorFunc = func(addr net.Addr, reqAddr string, err error) { - if err != nil { - if err == io.EOF { - logrus.WithFields(logrus.Fields{ - "src": addr.String(), - "dst": reqAddr, - }).Debugf("TUN %s EOF", strings.ToUpper(addr.Network())) - } else if err == core.ErrClosed && strings.HasPrefix(addr.Network(), "udp") { - logrus.WithFields(logrus.Fields{ - "src": addr.String(), - "dst": reqAddr, - }).Debugf("TUN %s closed for timeout", strings.ToUpper(addr.Network())) - } else if nErr, ok := err.(net.Error); ok && nErr.Timeout() && strings.HasPrefix(addr.Network(), "tcp") { - logrus.WithFields(logrus.Fields{ - "src": addr.String(), - "dst": reqAddr, - }).Debugf("TUN %s closed for timeout", strings.ToUpper(addr.Network())) - } else { - logrus.WithFields(logrus.Fields{ - "error": err, - "src": addr.String(), - "dst": reqAddr, - }).Infof("TUN %s error", strings.ToUpper(addr.Network())) - } - } - } - logrus.WithField("interface", config.TUN.Name).Info("TUN up and running") - errChan <- tunServer.ListenAndServe() - }() + go startTUN(config, client, errChan) } if len(config.TCPRelay.Listen) > 0 { diff --git a/cmd/client_gpl.go b/cmd/client_gpl.go new file mode 100644 index 0000000..5b633ff --- /dev/null +++ b/cmd/client_gpl.go @@ -0,0 +1,77 @@ +//go:build gpl +// +build gpl + +package main + +import ( + "github.com/sirupsen/logrus" + "github.com/tobyxdd/hysteria/pkg/core" + "github.com/tobyxdd/hysteria/pkg/tun" + "io" + "net" + "strings" + "time" +) + +const license = `Hysteria is a feature-packed proxy & relay utility optimized for lossy, unstable connections. +Copyright (C) 2022 Toby + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +` + +func startTUN(config *clientConfig, client *core.Client, errChan chan error) { + timeout := time.Duration(config.TUN.Timeout) * time.Second + if timeout == 0 { + timeout = 300 * time.Second + } + tunServer, err := tun.NewServer(client, time.Duration(config.TUN.Timeout)*time.Second, + config.TUN.Name, config.TUN.MTU) + if err != nil { + logrus.WithField("error", err).Fatal("Failed to initialize TUN server") + } + tunServer.RequestFunc = func(addr net.Addr, reqAddr string) { + logrus.WithFields(logrus.Fields{ + "src": addr.String(), + "dst": reqAddr, + }).Debugf("TUN %s request", strings.ToUpper(addr.Network())) + } + tunServer.ErrorFunc = func(addr net.Addr, reqAddr string, err error) { + if err != nil { + if err == io.EOF { + logrus.WithFields(logrus.Fields{ + "src": addr.String(), + "dst": reqAddr, + }).Debugf("TUN %s EOF", strings.ToUpper(addr.Network())) + } else if err == core.ErrClosed && strings.HasPrefix(addr.Network(), "udp") { + logrus.WithFields(logrus.Fields{ + "src": addr.String(), + "dst": reqAddr, + }).Debugf("TUN %s closed for timeout", strings.ToUpper(addr.Network())) + } else if nErr, ok := err.(net.Error); ok && nErr.Timeout() && strings.HasPrefix(addr.Network(), "tcp") { + logrus.WithFields(logrus.Fields{ + "src": addr.String(), + "dst": reqAddr, + }).Debugf("TUN %s closed for timeout", strings.ToUpper(addr.Network())) + } else { + logrus.WithFields(logrus.Fields{ + "error": err, + "src": addr.String(), + "dst": reqAddr, + }).Infof("TUN %s error", strings.ToUpper(addr.Network())) + } + } + } + logrus.WithField("interface", config.TUN.Name).Info("TUN up and running") + errChan <- tunServer.ListenAndServe() +} diff --git a/cmd/client_nongpl.go b/cmd/client_nongpl.go new file mode 100644 index 0000000..ca1efb5 --- /dev/null +++ b/cmd/client_nongpl.go @@ -0,0 +1,36 @@ +//go:build !gpl +// +build !gpl + +package main + +import ( + "github.com/sirupsen/logrus" + "github.com/tobyxdd/hysteria/pkg/core" +) + +const license = `The MIT License (MIT) + +Copyright (c) 2021 Toby + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +` + +func startTUN(config *clientConfig, client *core.Client, errChan chan error) { + logrus.Fatalln("TUN mode is only available in the GPL build, please rebuild hysteria with -tags gpl") +} diff --git a/cmd/main.go b/cmd/main.go index b5d815b..abc3e4f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -65,6 +65,12 @@ var rootCmd = &cobra.Command{ }) } + // license + if viper.GetBool("license") { + fmt.Printf("%s\n", license) + os.Exit(0) + } + // check update if !viper.GetBool("no-check") { go checkUpdate() @@ -154,6 +160,7 @@ func init() { rootCmd.PersistentFlags().String("log-timestamp", time.RFC3339, "log timestamp format") rootCmd.PersistentFlags().String("log-format", "txt", "log output format(txt/json)") rootCmd.PersistentFlags().Bool("no-check", false, "disable update check") + rootCmd.PersistentFlags().Bool("license", false, "show license and exit") // add to root cmd rootCmd.AddCommand(clientCmd, serverCmd, completionCmd) @@ -165,6 +172,7 @@ func init() { _ = viper.BindPFlag("log-timestamp", rootCmd.PersistentFlags().Lookup("log-timestamp")) _ = viper.BindPFlag("log-format", rootCmd.PersistentFlags().Lookup("log-format")) _ = viper.BindPFlag("no-check", rootCmd.PersistentFlags().Lookup("no-check")) + _ = viper.BindPFlag("license", rootCmd.PersistentFlags().Lookup("license")) // bind env _ = viper.BindEnv("config", "HYSTERIA_CONFIG") diff --git a/pkg/tun/server.go b/pkg/tun/server.go index 67a9fab..b3ed73d 100644 --- a/pkg/tun/server.go +++ b/pkg/tun/server.go @@ -1,3 +1,6 @@ +//go:build gpl +// +build gpl + package tun import ( diff --git a/pkg/tun/tcp.go b/pkg/tun/tcp.go index 25e3b11..df61dcc 100644 --- a/pkg/tun/tcp.go +++ b/pkg/tun/tcp.go @@ -1,3 +1,6 @@ +//go:build gpl +// +build gpl + package tun import ( diff --git a/pkg/tun/udp.go b/pkg/tun/udp.go index 7dfb532..167fbf3 100644 --- a/pkg/tun/udp.go +++ b/pkg/tun/udp.go @@ -1,3 +1,6 @@ +//go:build gpl +// +build gpl + package tun import (