V0.2.0 (#37)
* refactor go files, add headless telemost support * js crap -> ts crap * docker-build (#32) Co-authored-by: Mimocro <suzumagefag@gmail.com> * add headless telemost for creator app * UX improvements (#39) * Add search in split tunneling app list * Add captcha detection * Add autoclick settings * headless vk joiner * add headless telemost joiner * refactor go modules * redesign settings dialog * headless option in settings * headless joiner support in MainActivity * display headless vk status in statusBar * update ux for headless in creator app * fix double log render for headless mode in creator * unicode for cyrillic * add socks auth * put socksPort inf prefs, verify that port is free on start * proxy only mode * mask addresses in go network errors * update docker build, add file write logic for headless * increment release version, add make-release script --------- Co-authored-by: kulikov <user@Mac.lan> Co-authored-by: Mimocro <67226986+Mimocro@users.noreply.github.com> Co-authored-by: Mimocro <suzumagefag@gmail.com> Co-authored-by: senya <76913432+senya-sh@users.noreply.github.com>
This commit is contained in:
122 files changed
+9938
-3284
No files matched your search
@@ -0,0 +1,346 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"whitelist-bypass/relay/common"
|
||||
)
|
||||
|
||||
type udpClient struct {
|
||||
udpConn *net.UDPConn
|
||||
clientAddr *net.UDPAddr
|
||||
socksHdr []byte
|
||||
}
|
||||
|
||||
type RelayBridge struct {
|
||||
tunnel DataTunnel
|
||||
conns sync.Map
|
||||
udpClients sync.Map
|
||||
nextID atomic.Uint32
|
||||
logFn func(string, ...any)
|
||||
mode string
|
||||
readBuf int
|
||||
ready chan struct{}
|
||||
once sync.Once
|
||||
socksUser string
|
||||
socksPass string
|
||||
}
|
||||
|
||||
func NewRelayBridgeWithAuth(tunnel DataTunnel, mode string, readBuf int, logFn func(string, ...any), socksUser, socksPass string) *RelayBridge {
|
||||
rb := NewRelayBridge(tunnel, mode, readBuf, logFn)
|
||||
rb.socksUser = socksUser
|
||||
rb.socksPass = socksPass
|
||||
return rb
|
||||
}
|
||||
|
||||
func NewRelayBridge(tunnel DataTunnel, mode string, readBuf int, logFn func(string, ...any)) *RelayBridge {
|
||||
rb := &RelayBridge{
|
||||
tunnel: tunnel,
|
||||
logFn: logFn,
|
||||
mode: mode,
|
||||
readBuf: readBuf,
|
||||
ready: make(chan struct{}),
|
||||
}
|
||||
tunnel.SetOnData(rb.handleTunnelData)
|
||||
tunnel.SetOnClose(rb.closeAll)
|
||||
return rb
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) closeAll() {
|
||||
rb.logFn("relay: closing all connections")
|
||||
rb.conns.Range(func(key, value any) bool {
|
||||
switch v := value.(type) {
|
||||
case net.Conn:
|
||||
v.Close()
|
||||
case *socksConn:
|
||||
v.conn.Close()
|
||||
}
|
||||
rb.conns.Delete(key)
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) MarkReady() {
|
||||
rb.once.Do(func() { close(rb.ready) })
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) send(connID uint32, msgType byte, payload []byte) {
|
||||
frame := EncodeFrame(connID, msgType, payload)
|
||||
rb.tunnel.SendData(frame)
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) handleTunnelData(data []byte) {
|
||||
DecodeFrames(data, func(connID uint32, msgType byte, payload []byte) {
|
||||
switch rb.mode {
|
||||
case "joiner":
|
||||
rb.handleJoinerMessage(connID, msgType, payload)
|
||||
case "creator":
|
||||
rb.handleCreatorMessage(connID, msgType, payload)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) handleJoinerMessage(connID uint32, msgType byte, payload []byte) {
|
||||
if msgType == MsgUDPReply {
|
||||
uval, ok := rb.udpClients.Load(connID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
uc := uval.(*udpClient)
|
||||
reply := make([]byte, len(uc.socksHdr)+len(payload))
|
||||
copy(reply, uc.socksHdr)
|
||||
copy(reply[len(uc.socksHdr):], payload)
|
||||
uc.udpConn.WriteToUDP(reply, uc.clientAddr)
|
||||
rb.udpClients.Delete(connID)
|
||||
return
|
||||
}
|
||||
val, ok := rb.conns.Load(connID)
|
||||
if !ok {
|
||||
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()
|
||||
rb.conns.Delete(connID)
|
||||
}
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) handleCreatorMessage(connID uint32, msgType byte, payload []byte) {
|
||||
switch msgType {
|
||||
case MsgConnect:
|
||||
go rb.connectTCP(connID, string(payload))
|
||||
case MsgUDP:
|
||||
go rb.handleUDP(connID, payload)
|
||||
case MsgData:
|
||||
if val, ok := rb.conns.Load(connID); ok {
|
||||
if c, ok := val.(net.Conn); ok {
|
||||
c.Write(payload)
|
||||
}
|
||||
}
|
||||
case MsgClose:
|
||||
if val, ok := rb.conns.LoadAndDelete(connID); ok {
|
||||
if c, ok := val.(net.Conn); ok {
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) handleUDP(connID uint32, payload []byte) {
|
||||
if len(payload) < 2 {
|
||||
return
|
||||
}
|
||||
addrLen := int(payload[0])
|
||||
if addrLen == 0 || len(payload) < 1+addrLen {
|
||||
return
|
||||
}
|
||||
if bytes.IndexByte(payload[1:1+addrLen], 0) != -1 {
|
||||
return
|
||||
}
|
||||
addr := string(payload[1 : 1+addrLen])
|
||||
data := payload[1+addrLen:]
|
||||
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn, err := net.DialUDP("udp", nil, udpAddr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
conn.Write(data)
|
||||
buf := make([]byte, common.UDPBufSize)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rb.send(connID, MsgUDPReply, buf[:n])
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) connectTCP(connID uint32, addr string) {
|
||||
rb.logFn("relay: CONNECT %d -> %s", connID, common.MaskAddr(addr))
|
||||
conn, err := net.DialTimeout("tcp", addr, 10e9)
|
||||
if err != nil {
|
||||
rb.logFn("relay: CONNECT %d failed: %s", connID, common.MaskError(err))
|
||||
rb.send(connID, MsgConnectErr, []byte(common.MaskError(err)))
|
||||
return
|
||||
}
|
||||
rb.conns.Store(connID, conn)
|
||||
rb.send(connID, MsgConnectOK, nil)
|
||||
rb.logFn("relay: CONNECTED %d -> %s", connID, common.MaskAddr(addr))
|
||||
|
||||
buf := make([]byte, rb.readBuf)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if n > 0 {
|
||||
rb.send(connID, MsgData, buf[:n])
|
||||
}
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
rb.logFn("relay: conn %d read error: %s", connID, common.MaskError(err))
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
rb.send(connID, MsgClose, nil)
|
||||
rb.conns.Delete(connID)
|
||||
}
|
||||
|
||||
type socksConn struct {
|
||||
id uint32
|
||||
conn net.Conn
|
||||
rb *RelayBridge
|
||||
rdy chan error
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) ListenSOCKS(addr string) error {
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rb.logFn("relay: SOCKS5 on %s", addr)
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
rb.logFn("relay: accept error: %v", err)
|
||||
continue
|
||||
}
|
||||
go rb.handleSOCKS(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
|
||||
<-rb.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, rb.socksUser, rb.socksPass) {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
n, err = conn.Read(buf)
|
||||
if err != nil || n < 7 || buf[0] != common.Ver {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
cmd := buf[1]
|
||||
if cmd == common.CmdUDP {
|
||||
rb.handleUDPAssociate(conn)
|
||||
return
|
||||
}
|
||||
if cmd != common.CmdTCP {
|
||||
conn.Write(common.CmdErr)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
host, _, err := common.ParseAddress(buf, n)
|
||||
if err != nil {
|
||||
conn.Write(common.AddrErr)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
id := rb.nextID.Add(1)
|
||||
sc := &socksConn{id: id, conn: conn, rb: rb, rdy: make(chan error, 1)}
|
||||
rb.conns.Store(id, sc)
|
||||
rb.logFn("relay: SOCKS CONNECT %d -> %s", id, common.MaskAddr(host))
|
||||
rb.send(id, MsgConnect, []byte(host))
|
||||
|
||||
if err := <-sc.rdy; err != nil {
|
||||
rb.logFn("relay: SOCKS CONNECT %d failed: %s", id, common.MaskError(err))
|
||||
conn.Write(common.ConnFail)
|
||||
conn.Close()
|
||||
rb.conns.Delete(id)
|
||||
return
|
||||
}
|
||||
conn.Write(common.OK)
|
||||
rb.logFn("relay: SOCKS CONNECTED %d -> %s", id, common.MaskAddr(host))
|
||||
|
||||
go func() {
|
||||
readBuf := make([]byte, rb.readBuf)
|
||||
for {
|
||||
rn, rerr := conn.Read(readBuf)
|
||||
if rn > 0 {
|
||||
rb.send(id, MsgData, readBuf[:rn])
|
||||
}
|
||||
if rerr != nil {
|
||||
rb.send(id, MsgClose, nil)
|
||||
rb.conns.Delete(id)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) 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)
|
||||
|
||||
go func() {
|
||||
buf := make([]byte, 1)
|
||||
tcpConn.Read(buf)
|
||||
udpConn.Close()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer udpConn.Close()
|
||||
defer tcpConn.Close()
|
||||
buf := make([]byte, common.UDPBufSize)
|
||||
for {
|
||||
n, addr, err := udpConn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if n < 10 {
|
||||
continue
|
||||
}
|
||||
frag := buf[2]
|
||||
if frag != 0 {
|
||||
continue
|
||||
}
|
||||
dstAddr, headerLen, addrErr := common.ParseAddress(buf, n)
|
||||
if addrErr != nil {
|
||||
continue
|
||||
}
|
||||
id := rb.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])
|
||||
rb.udpClients.Store(id, &udpClient{udpConn: udpConn, clientAddr: addr, socksHdr: buf[:headerLen]})
|
||||
rb.send(id, MsgUDP, payload)
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in new issue
Block a user