ios vpn app

This commit is contained in:
kulikov0 committed 2026-09-03 14:35:47 +02:00
1 parent 96f16399d7
commit 1b506b9ee7
48 files changed
+4934 -45

No files matched your search

+1 -1
View File
@@ -15,6 +15,7 @@ require (
github.com/xtaci/kcp-go/v5 v5.6.72
golang.org/x/crypto v0.51.0
golang.org/x/mobile v0.0.0-20260508232728-bebd421c7fa8
golang.org/x/sys v0.45.0
)
require (
@@ -50,7 +51,6 @@ require (
golang.org/x/mod v0.36.0 // indirect
golang.org/x/net v0.55.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/text v0.37.0 // indirect
golang.org/x/time v0.14.0 // indirect
golang.org/x/tools v0.45.0 // indirect
-10
View File
@@ -3,7 +3,6 @@ package ios
import (
"encoding/json"
"fmt"
"net"
"sync"
"whitelist-bypass/relay/common"
@@ -29,7 +28,6 @@ var activeHeadless struct {
sync.Mutex
joiner joinerHandle
callback HeadlessCallback
socksLn net.Listener
bridge *tunnel.RelayBridge
stopped bool
platform string
@@ -120,9 +118,6 @@ func makeHelpers(callback HeadlessCallback) (func(string, ...any), joiner.Resolv
return logFn, resolveFn, statusEmitter
}
func init() {
}
func SetDebug(enabled bool) { common.Debug = enabled }
func StartWBStreamHeadless(socksPort int, socksUser, socksPass string, callback HeadlessCallback) {
@@ -266,10 +261,8 @@ func StopHeadless() {
activeHeadless.Lock()
activeHeadless.stopped = true
currentJoiner := activeHeadless.joiner
socksLn := activeHeadless.socksLn
bridge := activeHeadless.bridge
activeHeadless.joiner = nil
activeHeadless.socksLn = nil
activeHeadless.bridge = nil
activeHeadless.callback = nil
activeHeadless.platform = ""
@@ -282,7 +275,4 @@ func StopHeadless() {
if bridge != nil {
bridge.Close()
}
if socksLn != nil {
socksLn.Close()
}
}
+5
View File
@@ -0,0 +1,5 @@
package ios
type PacketFlowSink interface {
WritePacket(packet []byte, afFamily int)
}
+157
View File
@@ -0,0 +1,157 @@
//go:build ios || darwin
package ios
import (
"fmt"
"io"
"sync"
"github.com/xjasonlyu/tun2socks/v2/core"
"github.com/xjasonlyu/tun2socks/v2/core/device/iobased"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/tunnel"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"whitelist-bypass/relay/common"
)
const (
afInet = 2
afInet6 = 30
inboundQ = 512
)
func iosLog(format string, args ...any) {
activeHeadless.Lock()
cb := activeHeadless.callback
activeHeadless.Unlock()
if cb != nil {
cb.OnLog(fmt.Sprintf(format, args...))
}
}
type packetBridge struct {
sink PacketFlowSink
inbound chan []byte
done chan struct{}
closeOnce sync.Once
}
func newPacketBridge(sink PacketFlowSink) *packetBridge {
return &packetBridge{
sink: sink,
inbound: make(chan []byte, inboundQ),
done: make(chan struct{}),
}
}
func (b *packetBridge) Read(p []byte) (int, error) {
select {
case data := <-b.inbound:
return copy(p, data), nil
case <-b.done:
return 0, io.EOF
}
}
func (b *packetBridge) Write(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
af := afInet
if p[0]>>4 == 6 {
af = afInet6
}
cp := make([]byte, len(p))
copy(cp, p)
b.sink.WritePacket(cp, af)
return len(p), nil
}
func (b *packetBridge) input(data []byte) {
if len(data) == 0 {
return
}
cp := make([]byte, len(data))
copy(cp, data)
select {
case b.inbound <- cp:
case <-b.done:
default:
}
}
func (b *packetBridge) close() {
b.closeOnce.Do(func() { close(b.done) })
}
var (
tunMu sync.Mutex
tunStack *stack.Stack
tunBridge *packetBridge
)
func StartTun2SocksPacketFlow(sink PacketFlowSink, mtu int, socksPort int, socksUser, socksPass string) error {
tunMu.Lock()
defer tunMu.Unlock()
if tunStack != nil {
return nil
}
if sink == nil {
return fmt.Errorf("tun2socks: nil packet sink")
}
addr := fmt.Sprintf("%s:%d", common.SocksLocalhostIP, socksPort)
dialer, err := proxy.NewSocks5(addr, socksUser, socksPass)
if err != nil {
return fmt.Errorf("tun2socks: socks5 dialer: %w", err)
}
tunnel.T().SetDialer(dialer)
bridge := newPacketBridge(sink)
ep, err := iobased.New(bridge, uint32(mtu), 0)
if err != nil {
bridge.close()
return fmt.Errorf("tun2socks: endpoint: %w", err)
}
st, err := core.CreateStack(&core.Config{
LinkEndpoint: ep,
TransportHandler: tunnel.T(),
})
if err != nil {
bridge.close()
return fmt.Errorf("tun2socks: create stack: %w", err)
}
tunStack = st
tunBridge = bridge
iosLog("tun2socks: packetFlow stack up, socks=%s mtu=%d", addr, mtu)
return nil
}
func InputPacket(data []byte) {
tunMu.Lock()
b := tunBridge
tunMu.Unlock()
if b != nil {
b.input(data)
}
}
func StopTun2Socks() {
tunMu.Lock()
defer tunMu.Unlock()
if tunStack == nil {
return
}
if tunBridge != nil {
tunBridge.close()
tunBridge = nil
}
tunStack.Close()
tunStack = nil
iosLog("tun2socks: packetFlow stack stopped")
}
+13
View File
@@ -0,0 +1,13 @@
//go:build !ios && !darwin
package ios
import "fmt"
func StartTun2SocksPacketFlow(sink PacketFlowSink, mtu int, socksPort int, socksUser, socksPass string) error {
return fmt.Errorf("tun2socks: iOS only")
}
func InputPacket(data []byte) {}
func StopTun2Socks() {}