mirror of
https://github.com/net-byte/vtun
synced 2024-03-14 10:50:03 +08:00
fix
This commit is contained in:
parent
53a1452f91
commit
ad123eb093
@ -4,9 +4,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/net-byte/vtun/common/cache"
|
||||||
"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/tun"
|
"github.com/net-byte/vtun/tun"
|
||||||
@ -17,39 +17,40 @@ import (
|
|||||||
func StartClient(config config.Config) {
|
func StartClient(config config.Config) {
|
||||||
log.Printf("vtun tcp client started on %v", config.LocalAddr)
|
log.Printf("vtun tcp client started on %v", config.LocalAddr)
|
||||||
iface := tun.CreateTun(config)
|
iface := tun.CreateTun(config)
|
||||||
|
go tunToTcp(config, iface)
|
||||||
for {
|
for {
|
||||||
if conn, err := net.DialTimeout("tcp", config.ServerAddr, time.Duration(config.Timeout)*time.Second); conn != nil && err == nil {
|
if conn, err := net.DialTimeout("tcp", config.ServerAddr, time.Duration(config.Timeout)*time.Second); conn != nil && err == nil {
|
||||||
var wg sync.WaitGroup
|
cache.GetCache().Set("tcpconn", conn, 24*time.Hour)
|
||||||
wg.Add(2)
|
tcpToTun(config, conn, iface)
|
||||||
go tcpToTun(&wg, config, conn, iface)
|
cache.GetCache().Delete("tcpconn")
|
||||||
go tunToTcp(&wg, config, conn, iface)
|
|
||||||
wg.Wait()
|
|
||||||
conn.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tunToTcp(wg *sync.WaitGroup, config config.Config, tcpconn net.Conn, iface *water.Interface) {
|
func tunToTcp(config config.Config, iface *water.Interface) {
|
||||||
defer wg.Done()
|
|
||||||
packet := make([]byte, config.MTU)
|
packet := make([]byte, config.MTU)
|
||||||
for {
|
for {
|
||||||
n, err := iface.Read(packet)
|
n, err := iface.Read(packet)
|
||||||
if err != nil || n == 0 {
|
if err != nil || n == 0 {
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
b := packet[:n]
|
if v, ok := cache.GetCache().Get("tcpconn"); ok {
|
||||||
if config.Obfs {
|
b := packet[:n]
|
||||||
b = cipher.XOR(b)
|
if config.Obfs {
|
||||||
}
|
packet = cipher.XOR(packet)
|
||||||
tcpconn.SetWriteDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
}
|
||||||
_, err = tcpconn.Write(b)
|
tcpconn := v.(net.Conn)
|
||||||
if err != nil {
|
tcpconn.SetWriteDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||||
break
|
_, err = tcpconn.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func tcpToTun(wg *sync.WaitGroup, config config.Config, tcpconn net.Conn, iface *water.Interface) {
|
|
||||||
defer wg.Done()
|
func tcpToTun(config config.Config, tcpconn net.Conn, iface *water.Interface) {
|
||||||
|
defer tcpconn.Close()
|
||||||
packet := make([]byte, config.MTU)
|
packet := make([]byte, config.MTU)
|
||||||
for {
|
for {
|
||||||
tcpconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
tcpconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||||
@ -61,6 +62,9 @@ func tcpToTun(wg *sync.WaitGroup, config config.Config, tcpconn net.Conn, iface
|
|||||||
if config.Obfs {
|
if config.Obfs {
|
||||||
b = cipher.XOR(b)
|
b = cipher.XOR(b)
|
||||||
}
|
}
|
||||||
iface.Write(b)
|
_, err = iface.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package ws
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gobwas/ws/wsutil"
|
"github.com/gobwas/ws/wsutil"
|
||||||
|
"github.com/net-byte/vtun/common/cache"
|
||||||
"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/netutil"
|
"github.com/net-byte/vtun/common/netutil"
|
||||||
@ -18,20 +18,18 @@ import (
|
|||||||
func StartClient(config config.Config) {
|
func StartClient(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)
|
iface := tun.CreateTun(config)
|
||||||
|
go tunToWs(config, iface)
|
||||||
for {
|
for {
|
||||||
if conn := netutil.ConnectServer(config); conn != nil {
|
if conn := netutil.ConnectServer(config); conn != nil {
|
||||||
var wg sync.WaitGroup
|
cache.GetCache().Set("wsconn", conn, 24*time.Hour)
|
||||||
wg.Add(2)
|
wsToTun(config, conn, iface)
|
||||||
go wsToTun(&wg, config, conn, iface)
|
cache.GetCache().Delete("wsconn")
|
||||||
go tunToWs(&wg, config, conn, iface)
|
|
||||||
wg.Wait()
|
|
||||||
conn.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func wsToTun(wg *sync.WaitGroup, config config.Config, wsconn net.Conn, iface *water.Interface) {
|
func wsToTun(config config.Config, wsconn net.Conn, iface *water.Interface) {
|
||||||
defer wg.Done()
|
defer wsconn.Close()
|
||||||
for {
|
for {
|
||||||
wsconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
wsconn.SetReadDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||||
packet, err := wsutil.ReadServerBinary(wsconn)
|
packet, err := wsutil.ReadServerBinary(wsconn)
|
||||||
@ -41,25 +39,30 @@ func wsToTun(wg *sync.WaitGroup, config config.Config, wsconn net.Conn, iface *w
|
|||||||
if config.Obfs {
|
if config.Obfs {
|
||||||
packet = cipher.XOR(packet)
|
packet = cipher.XOR(packet)
|
||||||
}
|
}
|
||||||
iface.Write(packet)
|
_, err = iface.Write(packet)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tunToWs(wg *sync.WaitGroup, config config.Config, wsconn net.Conn, iface *water.Interface) {
|
func tunToWs(config config.Config, iface *water.Interface) {
|
||||||
defer wg.Done()
|
|
||||||
packet := make([]byte, config.MTU)
|
packet := make([]byte, config.MTU)
|
||||||
for {
|
for {
|
||||||
n, err := iface.Read(packet)
|
n, err := iface.Read(packet)
|
||||||
if err != nil || n == 0 {
|
if err != nil || n == 0 {
|
||||||
break
|
continue
|
||||||
}
|
}
|
||||||
b := packet[:n]
|
if v, ok := cache.GetCache().Get("wsconn"); ok {
|
||||||
if config.Obfs {
|
b := packet[:n]
|
||||||
packet = cipher.XOR(packet)
|
if config.Obfs {
|
||||||
}
|
packet = cipher.XOR(packet)
|
||||||
wsconn.SetWriteDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
}
|
||||||
if err = wsutil.WriteClientBinary(wsconn, b); err != nil {
|
wsconn := v.(net.Conn)
|
||||||
break
|
wsconn.SetWriteDeadline(time.Now().Add(time.Duration(config.Timeout) * time.Second))
|
||||||
|
if err = wsutil.WriteClientBinary(wsconn, b); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user