v0.2.1 (#53)
* ios proxy app, tune vp8 tunnel * refactor go modules, move headless joiner android to a go captcha * restore emojis in vk bot keyboard * headless support in vk bot * remove telemost dc hooks, update cretor-app ui and bot * remove telemost support from go code, add fallback to video if mode == dc in joiners * fix headless creators in windows * update vk call creation autoclick * force desktop useragent for creator app * kick disconnected user in headless vk creator immediately when connection is lost * update README.md * update SETUP.md * update release version * add fallback for link creation in vk bot
This commit is contained in:
1 parent
9e1f2a98d9
commit
1b2d6bc5d3
89 files changed
+3503
-2158
No files matched your search
@@ -0,0 +1,470 @@
|
||||
package androidbind
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"whitelist-bypass/relay/common"
|
||||
)
|
||||
|
||||
const (
|
||||
msgConnect byte = 0x01
|
||||
msgConnectOK byte = 0x02
|
||||
msgConnectErr byte = 0x03
|
||||
msgData byte = 0x04
|
||||
msgClose byte = 0x05
|
||||
msgUDP byte = 0x06
|
||||
msgUDPReply byte = 0x07
|
||||
)
|
||||
|
||||
const readBufSize = 65536
|
||||
|
||||
|
||||
var framePool = sync.Pool{
|
||||
New: func() any {
|
||||
buf := make([]byte, 5+readBufSize)
|
||||
return &buf
|
||||
},
|
||||
}
|
||||
|
||||
func encodeFrameInto(buf []byte, connID uint32, msgType byte, payload []byte) int {
|
||||
binary.BigEndian.PutUint32(buf[0:4], connID)
|
||||
buf[4] = msgType
|
||||
copy(buf[5:], payload)
|
||||
return 5 + len(payload)
|
||||
}
|
||||
|
||||
func decodeFrame(data []byte) (connID uint32, msgType byte, payload []byte) {
|
||||
if len(data) < 5 {
|
||||
return 0, 0, nil
|
||||
}
|
||||
connID = binary.BigEndian.Uint32(data[0:4])
|
||||
msgType = data[4]
|
||||
payload = data[5:]
|
||||
return
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
ReadBufferSize: readBufSize,
|
||||
WriteBufferSize: readBufSize,
|
||||
}
|
||||
|
||||
type LogCallback interface {
|
||||
OnLog(msg string)
|
||||
}
|
||||
|
||||
var logCb LogCallback
|
||||
|
||||
func logMsg(format string, args ...any) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
if logCb != nil {
|
||||
logCb.OnLog(msg)
|
||||
} else {
|
||||
log.Print(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type wsWriter struct {
|
||||
ws *websocket.Conn
|
||||
ch chan []byte
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func newWSWriter(ws *websocket.Conn) *wsWriter {
|
||||
w := &wsWriter{
|
||||
ws: ws,
|
||||
ch: make(chan []byte, 1024),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go w.loop()
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *wsWriter) loop() {
|
||||
defer close(w.done)
|
||||
for msg := range w.ch {
|
||||
if err := w.ws.WriteMessage(websocket.BinaryMessage, msg); err != nil {
|
||||
logMsg("ws write error: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wsWriter) send(msg []byte) {
|
||||
cp := make([]byte, len(msg))
|
||||
copy(cp, msg)
|
||||
select {
|
||||
case w.ch <- cp:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wsWriter) close() {
|
||||
close(w.ch)
|
||||
<-w.done
|
||||
}
|
||||
|
||||
|
||||
var activeJoiner struct {
|
||||
sync.Mutex
|
||||
j *joinerRelay
|
||||
ws *http.Server
|
||||
socksLn net.Listener
|
||||
wsPort int
|
||||
socksPort int
|
||||
}
|
||||
|
||||
func ActiveWsPort() int { return activeJoiner.wsPort }
|
||||
func ActiveSocksPort() int { return activeJoiner.socksPort }
|
||||
|
||||
func StopJoiner() {
|
||||
activeJoiner.Lock()
|
||||
defer activeJoiner.Unlock()
|
||||
if activeJoiner.socksLn != nil {
|
||||
activeJoiner.socksLn.Close()
|
||||
activeJoiner.socksLn = nil
|
||||
}
|
||||
if activeJoiner.ws != nil {
|
||||
activeJoiner.ws.Close()
|
||||
activeJoiner.ws = nil
|
||||
}
|
||||
if activeJoiner.j != nil {
|
||||
activeJoiner.j.closeAll()
|
||||
activeJoiner.j = nil
|
||||
}
|
||||
logMsg("dc-joiner: stopped")
|
||||
}
|
||||
|
||||
func StartJoiner(wsPort, socksPort int, socksUser, socksPass string, cb LogCallback) error {
|
||||
StopJoiner()
|
||||
logCb = cb
|
||||
j := &joinerRelay{
|
||||
conns: sync.Map{},
|
||||
ready: make(chan struct{}),
|
||||
socksUser: socksUser,
|
||||
socksPass: socksPass,
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/ws", j.handleWS)
|
||||
|
||||
wsAddr := fmt.Sprintf("127.0.0.1:%d", wsPort)
|
||||
wsLn, err := net.Listen("tcp", wsAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dc-joiner: ws listen %s: %w", wsAddr, err)
|
||||
}
|
||||
wsSrv := &http.Server{Handler: mux}
|
||||
go func() {
|
||||
logMsg("dc-joiner: WebSocket on %s", wsAddr)
|
||||
if err := wsSrv.Serve(wsLn); err != nil && err != http.ErrServerClosed {
|
||||
logMsg("dc-joiner: ws server error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
socksAddr := fmt.Sprintf("127.0.0.1:%d", socksPort)
|
||||
socksLn, err := net.Listen("tcp", socksAddr)
|
||||
if err != nil {
|
||||
wsSrv.Close()
|
||||
return fmt.Errorf("dc-joiner: socks listen %s: %w", socksAddr, err)
|
||||
}
|
||||
logMsg("dc-joiner: SOCKS5 on %s", socksAddr)
|
||||
|
||||
activeJoiner.Lock()
|
||||
activeJoiner.j = j
|
||||
activeJoiner.ws = wsSrv
|
||||
activeJoiner.socksLn = socksLn
|
||||
activeJoiner.wsPort = wsPort
|
||||
activeJoiner.socksPort = socksPort
|
||||
activeJoiner.Unlock()
|
||||
|
||||
return j.listenSOCKS(socksLn)
|
||||
}
|
||||
|
||||
type joinerRelay struct {
|
||||
writer *wsWriter
|
||||
conns sync.Map
|
||||
udpClients sync.Map
|
||||
nextID atomic.Uint32
|
||||
ready chan struct{}
|
||||
once sync.Once
|
||||
socksUser string
|
||||
socksPass string
|
||||
}
|
||||
|
||||
func (j *joinerRelay) closeAll() {
|
||||
j.conns.Range(func(key, val any) bool {
|
||||
val.(*socksConn).conn.Close()
|
||||
j.conns.Delete(key)
|
||||
return true
|
||||
})
|
||||
if j.writer != nil {
|
||||
j.writer.close()
|
||||
}
|
||||
}
|
||||
|
||||
type socksConn struct {
|
||||
id uint32
|
||||
conn net.Conn
|
||||
j *joinerRelay
|
||||
rdy chan error
|
||||
}
|
||||
|
||||
type udpClient struct {
|
||||
udpConn *net.UDPConn
|
||||
clientAddr *net.UDPAddr
|
||||
socksHeader []byte
|
||||
}
|
||||
|
||||
func (j *joinerRelay) handleUDPAssociate(tcpConn net.Conn) {
|
||||
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
tcpConn.Write(common.GenFail)
|
||||
tcpConn.Close()
|
||||
return
|
||||
}
|
||||
udpConn, err := net.ListenUDP("udp", udpAddr)
|
||||
if err != nil {
|
||||
tcpConn.Write(common.GenFail)
|
||||
tcpConn.Close()
|
||||
return
|
||||
}
|
||||
localAddr := udpConn.LocalAddr().(*net.UDPAddr)
|
||||
reply := []byte{common.Ver, 0x00, 0x00, common.AtypIPv4, 127, 0, 0, 1, 0, 0}
|
||||
binary.BigEndian.PutUint16(reply[8:10], uint16(localAddr.Port))
|
||||
tcpConn.Write(reply)
|
||||
logMsg("dc-joiner: UDP ASSOCIATE on port %d", localAddr.Port)
|
||||
|
||||
go func() {
|
||||
buf := make([]byte, 1)
|
||||
tcpConn.Read(buf)
|
||||
udpConn.Close()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer udpConn.Close()
|
||||
defer tcpConn.Close()
|
||||
buf := make([]byte, readBufSize)
|
||||
var clientAddr *net.UDPAddr
|
||||
for {
|
||||
n, addr, err := udpConn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if n < 10 {
|
||||
continue
|
||||
}
|
||||
clientAddr = addr
|
||||
frag := buf[2]
|
||||
if frag != 0 {
|
||||
continue
|
||||
}
|
||||
atyp := buf[3]
|
||||
var dstAddr string
|
||||
var headerLen int
|
||||
switch atyp {
|
||||
case common.AtypIPv4:
|
||||
if n < 10 {
|
||||
continue
|
||||
}
|
||||
dstAddr = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
|
||||
binary.BigEndian.Uint16(buf[8:10]))
|
||||
headerLen = 10
|
||||
case common.AtypDomain:
|
||||
dlen := int(buf[4])
|
||||
if n < 5+dlen+2 {
|
||||
continue
|
||||
}
|
||||
dstAddr = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
|
||||
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
|
||||
headerLen = 5 + dlen + 2
|
||||
case common.AtypIPv6:
|
||||
if n < 22 {
|
||||
continue
|
||||
}
|
||||
ip := net.IP(buf[4:20])
|
||||
dstAddr = fmt.Sprintf("[%s]:%d", ip.String(),
|
||||
binary.BigEndian.Uint16(buf[20:22]))
|
||||
headerLen = 22
|
||||
default:
|
||||
continue
|
||||
}
|
||||
id := j.nextID.Add(1)
|
||||
payload := make([]byte, len(dstAddr)+1+n-headerLen)
|
||||
payload[0] = byte(len(dstAddr))
|
||||
copy(payload[1:], dstAddr)
|
||||
copy(payload[1+len(dstAddr):], buf[headerLen:n])
|
||||
j.udpClients.Store(id, &udpClient{udpConn: udpConn, clientAddr: clientAddr, socksHeader: buf[:headerLen]})
|
||||
j.send(id, msgUDP, payload)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (j *joinerRelay) handleWS(w http.ResponseWriter, r *http.Request) {
|
||||
ws, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
logMsg("dc-joiner: ws upgrade error: %v", err)
|
||||
return
|
||||
}
|
||||
j.writer = newWSWriter(ws)
|
||||
j.once.Do(func() { close(j.ready) })
|
||||
logMsg("dc-joiner: browser connected via WebSocket")
|
||||
for {
|
||||
_, msg, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
logMsg("dc-joiner: ws read error: %v", err)
|
||||
return
|
||||
}
|
||||
connID, msgType, payload := decodeFrame(msg)
|
||||
j.handleMessage(connID, msgType, payload)
|
||||
}
|
||||
}
|
||||
|
||||
func (j *joinerRelay) handleMessage(connID uint32, msgType byte, payload []byte) {
|
||||
val, ok := j.conns.Load(connID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if msgType == msgUDPReply {
|
||||
uval, ok := j.udpClients.Load(connID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
uc := uval.(*udpClient)
|
||||
reply := make([]byte, len(uc.socksHeader)+len(payload))
|
||||
copy(reply, uc.socksHeader)
|
||||
copy(reply[len(uc.socksHeader):], payload)
|
||||
uc.udpConn.WriteToUDP(reply, uc.clientAddr)
|
||||
j.udpClients.Delete(connID)
|
||||
return
|
||||
}
|
||||
sc := val.(*socksConn)
|
||||
switch msgType {
|
||||
case msgConnectOK:
|
||||
sc.rdy <- nil
|
||||
case msgConnectErr:
|
||||
sc.rdy <- fmt.Errorf("%s", payload)
|
||||
case msgData:
|
||||
sc.conn.Write(payload)
|
||||
case msgClose:
|
||||
sc.conn.Close()
|
||||
j.conns.Delete(connID)
|
||||
}
|
||||
}
|
||||
|
||||
func (j *joinerRelay) send(connID uint32, msgType byte, payload []byte) {
|
||||
w := j.writer
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
bufp := framePool.Get().(*[]byte)
|
||||
buf := *bufp
|
||||
n := encodeFrameInto(buf, connID, msgType, payload)
|
||||
w.send(buf[:n])
|
||||
framePool.Put(bufp)
|
||||
}
|
||||
|
||||
func (j *joinerRelay) listenSOCKS(ln net.Listener) error {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go j.handleSOCKS(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (j *joinerRelay) handleSOCKS(conn net.Conn) {
|
||||
<-j.ready
|
||||
buf := make([]byte, common.HandshakeBuf)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil || n < 2 || buf[0] != common.Ver {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if !common.NegotiateAuth(conn, buf, n, j.socksUser, j.socksPass) {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
n, err = conn.Read(buf)
|
||||
if err != nil || n < 7 || buf[0] != common.Ver {
|
||||
conn.Write(common.GenFail)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
cmd := buf[1]
|
||||
if cmd == common.CmdUDP {
|
||||
j.handleUDPAssociate(conn)
|
||||
return
|
||||
}
|
||||
if cmd != common.CmdTCP {
|
||||
conn.Write(common.CmdErr)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
var host string
|
||||
switch buf[3] {
|
||||
case common.AtypIPv4:
|
||||
if n < 10 {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
host = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
|
||||
binary.BigEndian.Uint16(buf[8:10]))
|
||||
case common.AtypDomain:
|
||||
dlen := int(buf[4])
|
||||
if n < 5+dlen+2 {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
host = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
|
||||
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
|
||||
case common.AtypIPv6:
|
||||
if n < 22 {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
ip := net.IP(buf[4:20])
|
||||
host = fmt.Sprintf("[%s]:%d", ip.String(),
|
||||
binary.BigEndian.Uint16(buf[20:22]))
|
||||
default:
|
||||
conn.Write(common.AddrErr)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
id := j.nextID.Add(1)
|
||||
sc := &socksConn{id: id, conn: conn, j: j, rdy: make(chan error, 1)}
|
||||
j.conns.Store(id, sc)
|
||||
logMsg("dc-joiner: CONNECT %d -> %s", id, common.MaskAddr(host))
|
||||
j.send(id, msgConnect, []byte(host))
|
||||
if err := <-sc.rdy; err != nil {
|
||||
logMsg("dc-joiner: CONNECT %d failed: %s", id, common.MaskError(err))
|
||||
conn.Write(common.ConnFail)
|
||||
conn.Close()
|
||||
j.conns.Delete(id)
|
||||
return
|
||||
}
|
||||
conn.Write(common.OK)
|
||||
logMsg("dc-joiner: CONNECTED %d -> %s", id, common.MaskAddr(host))
|
||||
go func() {
|
||||
buf := make([]byte, readBufSize)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if n > 0 {
|
||||
j.send(id, msgData, buf[:n])
|
||||
}
|
||||
if err != nil {
|
||||
j.send(id, msgClose, nil)
|
||||
j.conns.Delete(id)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//go:build android
|
||||
|
||||
package androidbind
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/xjasonlyu/tun2socks/v2/engine"
|
||||
)
|
||||
|
||||
var (
|
||||
tunReady sync.WaitGroup
|
||||
tunOrigFd int = -1
|
||||
)
|
||||
|
||||
func StartTun2Socks(fd, mtu, socksPort int, socksUser, socksPass string) error {
|
||||
// dup the fd: tun2socks will close the dup on Stop(),
|
||||
// we keep the original open to prevent the kernel from
|
||||
// recycling the fd number while gvisor goroutines drain
|
||||
dupFd, err := syscall.Dup(fd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dup tun fd: %w", err)
|
||||
}
|
||||
tunOrigFd = fd
|
||||
|
||||
var proxy string
|
||||
if socksUser != "" {
|
||||
proxy = fmt.Sprintf("socks5://%s:%s@127.0.0.1:%d", socksUser, socksPass, socksPort)
|
||||
} else {
|
||||
proxy = fmt.Sprintf("socks5://127.0.0.1:%d", socksPort)
|
||||
}
|
||||
logMsg("tun2socks: starting fd=%d (dup=%d) mtu=%d proxy=%s", fd, dupFd, mtu, proxy)
|
||||
os.Setenv("TUN2SOCKS_LOG_LEVEL", "info")
|
||||
key := &engine.Key{
|
||||
Proxy: proxy,
|
||||
Device: fmt.Sprintf("fd://%d", dupFd),
|
||||
MTU: mtu,
|
||||
}
|
||||
tunReady.Add(1)
|
||||
engine.Insert(key)
|
||||
engine.Start()
|
||||
tunReady.Done()
|
||||
logMsg("tun2socks: running")
|
||||
return nil
|
||||
}
|
||||
|
||||
func StopTun2Socks() {
|
||||
tunReady.Wait()
|
||||
engine.Stop()
|
||||
// now safe to close the original - gvisor goroutines are done
|
||||
if tunOrigFd >= 0 {
|
||||
syscall.Close(tunOrigFd)
|
||||
tunOrigFd = -1
|
||||
}
|
||||
logMsg("tun2socks: stopped")
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//go:build !android
|
||||
|
||||
package androidbind
|
||||
|
||||
import "fmt"
|
||||
|
||||
func StartTun2Socks(fd, mtu, socksPort int, socksUser, socksPass string) error {
|
||||
return fmt.Errorf("tun2socks is only available on Android")
|
||||
}
|
||||
|
||||
func StopTun2Socks() {}
|
||||
Reference in new issue
Block a user