This commit is contained in:
Alex Tsai 2022-08-06 00:53:03 +08:00
parent 91af002f9e
commit b3cdf7b421
10 changed files with 23 additions and 35 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/net-byte/vtun/tun" "github.com/net-byte/vtun/tun"
"github.com/net-byte/vtun/udp" "github.com/net-byte/vtun/udp"
"github.com/net-byte/vtun/ws" "github.com/net-byte/vtun/ws"
"github.com/net-byte/water"
) )
var _banner = ` var _banner = `
@ -28,6 +29,7 @@ var _srcUrl = "https://github.com/net-byte/vtun"
type Vtun struct { type Vtun struct {
Config *config.Config Config *config.Config
Version string Version string
Iface *water.Interface
} }
// InitConfig initializes the config // InitConfig initializes the config
@ -43,36 +45,37 @@ func (app *Vtun) InitConfig() {
// StartApp starts the app // StartApp starts the app
func (app *Vtun) StartApp() { func (app *Vtun) StartApp() {
app.Iface = tun.CreateTun(*app.Config)
switch app.Config.Protocol { switch app.Config.Protocol {
case "udp": case "udp":
if app.Config.ServerMode { if app.Config.ServerMode {
udp.StartServer(*app.Config) udp.StartServer(app.Iface, *app.Config)
} else { } else {
udp.StartClient(*app.Config) udp.StartClient(app.Iface, *app.Config)
} }
case "ws", "wss": case "ws", "wss":
if app.Config.ServerMode { if app.Config.ServerMode {
ws.StartServer(*app.Config) ws.StartServer(app.Iface, *app.Config)
} else { } else {
ws.StartClient(*app.Config) ws.StartClient(app.Iface, *app.Config)
} }
case "tls": case "tls":
if app.Config.ServerMode { if app.Config.ServerMode {
tls.StartServer(*app.Config) tls.StartServer(app.Iface, *app.Config)
} else { } else {
tls.StartClient(*app.Config) tls.StartClient(app.Iface, *app.Config)
} }
case "grpc": case "grpc":
if app.Config.ServerMode { if app.Config.ServerMode {
grpc.StartServer(*app.Config) grpc.StartServer(app.Iface, *app.Config)
} else { } else {
grpc.StartClient(*app.Config) grpc.StartClient(app.Iface, *app.Config)
} }
default: default:
if app.Config.ServerMode { if app.Config.ServerMode {
udp.StartServer(*app.Config) udp.StartServer(app.Iface, *app.Config)
} else { } else {
udp.StartClient(*app.Config) udp.StartClient(app.Iface, *app.Config)
} }
} }
} }
@ -80,5 +83,6 @@ func (app *Vtun) StartApp() {
// StopApp stops the app // StopApp stops the app
func (app *Vtun) StopApp() { func (app *Vtun) StopApp() {
tun.ResetTun(*app.Config) tun.ResetTun(*app.Config)
app.Iface.Close()
log.Println("vtun stopped") log.Println("vtun stopped")
} }

View File

@ -15,14 +15,12 @@ import (
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartClient starts the grpc client // StartClient starts the grpc client
func StartClient(config config.Config) { func StartClient(iface *water.Interface, config config.Config) {
log.Printf("vtun grpc client started on %v", config.LocalAddr) log.Printf("vtun grpc client started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
go tunToGrpc(config, iface) go tunToGrpc(config, iface)
tlsconfig := &tls.Config{ tlsconfig := &tls.Config{
InsecureSkipVerify: config.TLSInsecureSkipVerify, InsecureSkipVerify: config.TLSInsecureSkipVerify,

View File

@ -15,7 +15,6 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
@ -33,9 +32,8 @@ func (s *StreamService) Tunnel(srv proto.GrpcServe_TunnelServer) error {
} }
// StartServer starts the grpc server // StartServer starts the grpc server
func StartServer(config config.Config) { func StartServer(iface *water.Interface, config config.Config) {
log.Printf("vtun grpc server started on %v", config.LocalAddr) log.Printf("vtun grpc server started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
ln, err := net.Listen("tcp", config.LocalAddr) ln, err := net.Listen("tcp", config.LocalAddr)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)

View File

@ -10,7 +10,7 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
) )
var _version = "v1.6.4" var _version = "v1.6.5"
func main() { func main() {
config := config.Config{} config := config.Config{}

View File

@ -12,14 +12,12 @@ import (
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartClient starts the tls client // StartClient starts the tls client
func StartClient(config config.Config) { func StartClient(iface *water.Interface, config config.Config) {
log.Printf("vtun tls client started on %v", config.LocalAddr) log.Printf("vtun tls client started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
go tunToTLS(config, iface) go tunToTLS(config, iface)
tlsconfig := &tls.Config{ tlsconfig := &tls.Config{
InsecureSkipVerify: config.TLSInsecureSkipVerify, InsecureSkipVerify: config.TLSInsecureSkipVerify,

View File

@ -13,14 +13,12 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartServer starts the tls server // StartServer starts the tls server
func StartServer(config config.Config) { func StartServer(iface *water.Interface, config config.Config) {
log.Printf("vtun tls server started on %v", config.LocalAddr) log.Printf("vtun tls server started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
cert, err := tls.LoadX509KeyPair(config.TLSCertificateFilePath, config.TLSCertificateKeyFilePath) cert, err := tls.LoadX509KeyPair(config.TLSCertificateFilePath, config.TLSCertificateKeyFilePath)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)

View File

@ -8,14 +8,12 @@ import (
"github.com/net-byte/vtun/common/cipher" "github.com/net-byte/vtun/common/cipher"
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartClient starts the udp client // StartClient starts the udp client
func StartClient(config config.Config) { func StartClient(iface *water.Interface, config config.Config) {
log.Printf("vtun udp client started on %v", config.LocalAddr) log.Printf("vtun udp client started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
serverAddr, err := net.ResolveUDPAddr("udp", config.ServerAddr) serverAddr, err := net.ResolveUDPAddr("udp", config.ServerAddr)
if err != nil { if err != nil {
log.Fatalln("failed to resolve server addr:", err) log.Fatalln("failed to resolve server addr:", err)

View File

@ -10,15 +10,13 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
) )
// StartServer starts the udp server // StartServer starts the udp server
func StartServer(config config.Config) { func StartServer(iface *water.Interface, config config.Config) {
log.Printf("vtun udp server started on %v", config.LocalAddr) log.Printf("vtun udp server started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
localAddr, err := net.ResolveUDPAddr("udp", config.LocalAddr) localAddr, err := net.ResolveUDPAddr("udp", config.LocalAddr)
if err != nil { if err != nil {
log.Fatalln("failed to get udp socket:", err) log.Fatalln("failed to get udp socket:", err)

View File

@ -12,14 +12,12 @@ import (
"github.com/net-byte/vtun/common/config" "github.com/net-byte/vtun/common/config"
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartClient starts the ws client // StartClient starts the ws client
func StartClient(config config.Config) { func StartClient(iface *water.Interface, config config.Config) {
log.Printf("vtun websocket client started on %v", config.LocalAddr) log.Printf("vtun websocket client started on %v", config.LocalAddr)
iface := tun.CreateTun(config)
go tunToWs(config, iface) go tunToWs(config, iface)
for { for {
conn := netutil.ConnectServer(config) conn := netutil.ConnectServer(config)

View File

@ -19,13 +19,11 @@ import (
"github.com/net-byte/vtun/common/counter" "github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil" "github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/register" "github.com/net-byte/vtun/register"
"github.com/net-byte/vtun/tun"
"github.com/net-byte/water" "github.com/net-byte/water"
) )
// StartServer starts the ws server // StartServer starts the ws server
func StartServer(config config.Config) { func StartServer(iface *water.Interface, config config.Config) {
iface := tun.CreateTun(config)
// server -> client // server -> client
go toClient(config, iface) go toClient(config, iface)
// client -> server // client -> server