Compare commits

...
10 Commits
Author SHA1 Message Date
xjasonlyu 8fcd8fee85 Fix(fd): compile error 2021-11-01 14:01:21 +08:00
xjasonlyu e06cce1dd4 Chore: update go modules
$ go mod tidy -compat=1.17
2021-11-01 13:07:07 +08:00
xjasonlyu 867cfefa76 Chore(workflows/go): ignore docs 2021-10-19 21:13:14 +08:00
xjasonlyu 176324412b Chore: update README 2021-10-19 21:11:29 +08:00
xjasonlyu 66e9c6d4e9 Chore: update modules 2021-10-17 14:30:19 +08:00
xjasonlyu 3928350e57 Chore: minor reposition 2021-10-17 14:29:08 +08:00
xjasonlyu f6ba31f121 Feature: support fd device
Experimental support for file descriptor based device, may be used like `fd://3`
2021-10-17 14:21:37 +08:00
xjasonlyu bf3f4599cc Chore(tun): remove with options 2021-10-17 14:12:54 +08:00
xjasonlyu dc8e4f1ba1 Feature: new fd driver 2021-10-17 14:07:11 +08:00
xjasonlyu ad53cc7ffe Chore: correct a typo 2021-10-12 19:55:21 +08:00
14 changed files with 160 additions and 1015 deletions
+3
View File
@@ -6,6 +6,9 @@ on:
- '*'
tags-ignore:
- '*'
paths-ignore:
- '**.md'
- 'docs/**'
pull_request:
jobs:
+26 -16
View File
@@ -16,25 +16,30 @@ English | [简体中文](README_ZH.md)
## Features
- **Fully support:** IPv4/IPv6/ICMP/TCP/UDP
- **Proxy protocol:** HTTP/Socks4/Socks5/Shadowsocks
- **Game ready:** optimized UDP transmission
- **Pure Go:** no CGO required, stability improved
- **Router mode:** forwarding packets in LAN
- **TCP/IP stack:** powered by **[gVisor](https://github.com/google/gvisor)**
- **High performance:** >2.5Gbps throughput
## Requirements
| Target | Minimum | Recommended |
| :----- | :-----: | :---------: |
| System | Linux MacOS Freebsd OpenBSD Windows | Linux or MacOS |
| Memory | >20MB | >128MB |
| CPU | ANY | AMD64 or ARM64 |
- **Network Support**
- Dualstack: `IPv4/IPv6`
- Forwarder: `TCP/UDP`
- Ping Echo: `ICMP`
- **Platform Support**
- Linux
- MacOS
- Windows
- FreeBSD
- OpenBSD
- **Proxy Protocol**
- HTTP
- Socks4
- Socks5
- Shadowsocks
- **Extra Feature**
- Improved stability without CGO
- Optimized UDP transmission for game
- Performed with >2.5Gbps throughput
- TCP/IP stack powered by **[gVisor](https://github.com/google/gvisor)**
## Documentation
Documentations and quick start guides can be found at [Github Wiki](https://github.com/xjasonlyu/tun2socks/wiki).
Docs and quick start guides can be found at [Github Wiki](https://github.com/xjasonlyu/tun2socks/wiki).
## Community
@@ -51,3 +56,8 @@ Welcome and feel free to ask any questions at [Github Discussions](https://githu
[GPL-3.0](https://github.com/xjasonlyu/tun2socks/blob/main/LICENSE)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fxjasonlyu%2Ftun2socks.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fxjasonlyu%2Ftun2socks?ref=badge_large)
## Stargazers over time
[![Stargazers over time](https://starchart.cc/xjasonlyu/tun2socks.svg)](https://starchart.cc/xjasonlyu/tun2socks)
+3
View File
@@ -0,0 +1,3 @@
package fd
const Driver = "fd"
+41
View File
@@ -0,0 +1,41 @@
//go:build !windows
package fd
import (
"fmt"
"strconv"
"github.com/xjasonlyu/tun2socks/core/device"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
type FD struct {
stack.LinkEndpoint
fd int
mtu uint32
}
func Open(name string, mtu uint32) (device.Device, error) {
fd, err := strconv.Atoi(name)
if err != nil {
return nil, fmt.Errorf("cannot open fd: %s", name)
}
return open(fd, mtu)
}
func (f *FD) Type() string {
return Driver
}
func (f *FD) Name() string {
return strconv.Itoa(f.fd)
}
func (f *FD) Close() error {
return unix.Close(f.fd)
}
var _ device.Device = (*FD)(nil)
+11
View File
@@ -0,0 +1,11 @@
package fd
import (
"errors"
"github.com/xjasonlyu/tun2socks/core/device"
)
func Open(name string, mtu uint32) (device.Device, error) {
return nil, errors.New("not supported")
}
+24
View File
@@ -0,0 +1,24 @@
package fd
import (
"fmt"
"github.com/xjasonlyu/tun2socks/core/device"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
)
func open(fd int, mtu uint32) (device.Device, error) {
f := &FD{fd: fd, mtu: mtu}
ep, err := fdbased.New(&fdbased.Options{
MTU: mtu,
FDs: []int{fd},
EthernetHeader: false,
})
if err != nil {
return nil, fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return f, nil
}
+23
View File
@@ -0,0 +1,23 @@
//go:build !linux && !windows
package fd
import (
"fmt"
"os"
"github.com/xjasonlyu/tun2socks/core/device"
"github.com/xjasonlyu/tun2socks/core/device/rwbased"
)
func open(fd int, mtu uint32) (device.Device, error) {
f := &FD{fd: fd, mtu: mtu}
ep, err := rwbased.New(os.NewFile(uintptr(fd), f.Name()), mtu)
if err != nil {
return nil, fmt.Errorf("create endpoint: %w", err)
}
f.LinkEndpoint = ep
return f, nil
}
-15
View File
@@ -1,15 +0,0 @@
package tun
type Option func(*TUN)
func WithName(name string) Option {
return func(t *TUN) {
t.name = name
}
}
func WithMTU(mtu uint32) Option {
return func(t *TUN) {
t.mtu = mtu
}
}
+4 -8
View File
@@ -23,12 +23,8 @@ type TUN struct {
name string
}
func Open(opts ...Option) (device.Device, error) {
t := &TUN{}
for _, opt := range opts {
opt(t)
}
func Open(name string, mtu uint32) (device.Device, error) {
t := &TUN{name: name, mtu: mtu}
if len(t.name) >= unix.IFNAMSIZ {
return nil, fmt.Errorf("interface name too long: %s", t.name)
@@ -46,11 +42,11 @@ func Open(opts ...Option) (device.Device, error) {
}
}
mtu, err := rawfile.GetMTU(t.name)
_mtu, err := rawfile.GetMTU(t.name)
if err != nil {
return nil, fmt.Errorf("get mtu: %w", err)
}
t.mtu = mtu
t.mtu = _mtu
ep, err := fdbased.New(&fdbased.Options{
MTU: t.mtu,
+4 -8
View File
@@ -19,12 +19,8 @@ type TUN struct {
name string
}
func Open(opts ...Option) (device.Device, error) {
t := &TUN{}
for _, opt := range opts {
opt(t)
}
func Open(name string, mtu uint32) (device.Device, error) {
t := &TUN{name: name, mtu: mtu}
forcedMTU := defaultMTU
if t.mtu > 0 {
@@ -37,11 +33,11 @@ func Open(opts ...Option) (device.Device, error) {
}
t.nt = nt.(*tun.NativeTun)
mtu, err := nt.MTU()
_mtu, err := nt.MTU()
if err != nil {
return nil, fmt.Errorf("get mtu: %w", err)
}
t.mtu = uint32(mtu)
t.mtu = uint32(_mtu)
ep, err := rwbased.New(t, t.mtu)
if err != nil {
+4 -1
View File
@@ -7,6 +7,7 @@ import (
"strings"
"github.com/xjasonlyu/tun2socks/core/device"
"github.com/xjasonlyu/tun2socks/core/device/fd"
"github.com/xjasonlyu/tun2socks/core/device/tun"
"github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/proxy/proto"
@@ -26,8 +27,10 @@ func parseDevice(s string, mtu uint32) (device.Device, error) {
driver := strings.ToLower(u.Scheme)
switch driver {
case fd.Driver:
return fd.Open(name, mtu)
case tun.Driver:
return tun.Open(tun.WithName(name), tun.WithMTU(mtu))
return tun.Open(name, mtu)
default:
return nil, fmt.Errorf("unsupported driver: %s", driver)
}
+5 -5
View File
@@ -7,17 +7,17 @@ require (
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-chi/cors v1.2.0
github.com/go-chi/render v1.0.1
github.com/gofrs/uuid v4.0.0+incompatible
github.com/gofrs/uuid v4.1.0+incompatible
github.com/gorilla/websocket v1.4.2
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
go.uber.org/atomic v1.9.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 // indirect
golang.org/x/sys v0.0.0-20211003122950-b1ebd4e1001c
golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
golang.org/x/sys v0.0.0-20211031064116-611d5d643895
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
golang.zx2c4.com/wireguard v0.0.0-20210927201915-bb745b2ea326
gvisor.dev/gvisor v0.0.0-20211002000755-82b90506164f
golang.zx2c4.com/wireguard v0.0.0-20211030003956-52704c4b9288
gvisor.dev/gvisor v0.0.0-20211029210705-806fa5c3235c
)
require (
+11 -961
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -104,7 +104,7 @@ func (ss *Socks5) DialUDP(*M.Metadata) (_ net.PacketConn, err error) {
addr, err := socks5.ClientHandshake(c, targetAddr, socks5.CmdUDPAssociate, user)
if err != nil {
return nil, fmt.Errorf("client hanshake: %w", err)
return nil, fmt.Errorf("client handshake: %w", err)
}
pc, err := dialer.ListenPacket("udp", "")