Update controller and client
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
This commit is contained in:
parent
566f6b0cf8
commit
f6985e6661
20
README.md
20
README.md
@ -1,3 +1,21 @@
|
||||
# Golang pproxit to TCP and UDP Connections
|
||||
|
||||
Same to playit.gg, to make more simples to connections and implementations and host self server proxy
|
||||
Same to playit.gg, to make more simples to connections and implementations and host self server proxy
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] Agent Connect
|
||||
- [x] Auth
|
||||
- [ ] Send shutdow agent
|
||||
- [x] Recive packets
|
||||
- [x] Send packets
|
||||
- [ ] Controller
|
||||
- [x] Listener tunnels
|
||||
- [x] Redirect packets to agent
|
||||
- Require Patchs to fix agent retransmitter packets
|
||||
- [ ] TCP
|
||||
- [ ] TX Data
|
||||
- [ ] RX Data
|
||||
- [ ] UDP
|
||||
- [ ] TX Data
|
||||
- [ ] RX Data
|
@ -1,6 +1,8 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -60,8 +62,22 @@ func (client *Client) Setup() error {
|
||||
var auth = proto.AgentAuth(client.Token)
|
||||
for {
|
||||
client.Send(proto.Request{AgentAuth: &auth})
|
||||
res, err := proto.ReaderResponse(client.Conn)
|
||||
|
||||
buff := make([]byte, 1024)
|
||||
n, err := client.Conn.Read(buff)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := proto.ReaderResponse(bytes.NewBuffer(buff[:n]))
|
||||
if err != nil {
|
||||
if opt, isOpt := err.(*net.OpError); isOpt {
|
||||
if opt.Timeout() {
|
||||
<-time.After(time.Second * 3)
|
||||
client.Send(proto.Request{AgentAuth: &auth})
|
||||
continue
|
||||
}
|
||||
}
|
||||
// return err
|
||||
break
|
||||
} else if res.Unauthorized {
|
||||
@ -106,8 +122,17 @@ func (tun *Client) GetTargetWrite(Proto uint8, To netip.AddrPort) io.Writer {
|
||||
}
|
||||
|
||||
func (client *Client) handlers() {
|
||||
var lastPing int64 = 0
|
||||
for {
|
||||
if time.Now().UnixMilli() - lastPing > 3_000 {
|
||||
var now = time.Now()
|
||||
go client.Send(proto.Request{Ping: &now})
|
||||
}
|
||||
|
||||
res, err := proto.ReaderResponse(client.Conn)
|
||||
d, _ := json.Marshal(res)
|
||||
fmt.Println(string(d))
|
||||
|
||||
if err != nil {
|
||||
if err == proto.ErrInvalidBody {
|
||||
continue
|
||||
@ -179,6 +204,8 @@ func (client *Client) handlers() {
|
||||
if tun, ok := client.clientsUDP[data.Client.Client.String()]; ok {
|
||||
go tun.Write(data.Data)
|
||||
}
|
||||
} else if res.Pong != nil {
|
||||
fmt.Println(res.Pong.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
@ -39,6 +40,22 @@ type Ping struct {
|
||||
AgentTime time.Time `json:"agent" xorm:"datetime notnull"`
|
||||
}
|
||||
|
||||
type AddrBlocked struct {
|
||||
ID int64 `json:"-" xorm:"pk"` // Tunnel ID
|
||||
TunID int64 `json:"-"`
|
||||
Enabled bool
|
||||
Address string
|
||||
}
|
||||
|
||||
type RTX struct {
|
||||
ID int64 `json:"-" xorm:"pk"` // Tunnel ID
|
||||
TunID int64 `json:"-"`
|
||||
Client netip.AddrPort
|
||||
TXSize int
|
||||
RXSize int
|
||||
Proto uint8
|
||||
}
|
||||
|
||||
func NewCall(DBConn string) (call *serverCalls, err error) {
|
||||
call = new(serverCalls)
|
||||
if call.XormEngine, err = xorm.NewEngine("sqlite", DBConn); err != nil {
|
||||
@ -49,7 +66,9 @@ func NewCall(DBConn string) (call *serverCalls, err error) {
|
||||
defer session.Close()
|
||||
session.CreateTable(User{})
|
||||
session.CreateTable(Tun{})
|
||||
session.CreateTable(AddrBlocked{})
|
||||
session.CreateTable(Ping{})
|
||||
session.CreateTable(RTX{})
|
||||
return
|
||||
}
|
||||
|
||||
@ -58,11 +77,58 @@ type TunCallbcks struct {
|
||||
XormEngine *xorm.Engine
|
||||
}
|
||||
|
||||
func (tun *TunCallbcks) BlockedAddr(AddrPort netip.Addr) bool { return false }
|
||||
func (tun *TunCallbcks) AgentPing(agent, server time.Time) {}
|
||||
func (tun *TunCallbcks) AgentShutdown(onTime time.Time) {}
|
||||
func (tun *TunCallbcks) RegisterRX(client netip.AddrPort, Size int, Proto uint8) {}
|
||||
func (tun *TunCallbcks) RegisterTX(client netip.AddrPort, Size int, Proto uint8) {}
|
||||
func (tun *TunCallbcks) AgentShutdown(onTime time.Time) {}
|
||||
|
||||
func (tun *TunCallbcks) BlockedAddr(AddrPort string) bool {
|
||||
var addr = AddrBlocked{Address: AddrPort, TunID: tun.tunID}
|
||||
ok, err := tun.XormEngine.Get(&addr)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return true
|
||||
} else if ok {
|
||||
return addr.Enabled
|
||||
}
|
||||
var addrs []AddrBlocked
|
||||
if err := tun.XormEngine.Find(&addrs); err != nil {
|
||||
fmt.Println(err)
|
||||
return true
|
||||
}
|
||||
for ind := range addrs {
|
||||
if addrs[ind].Enabled {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (tun *TunCallbcks) AgentPing(agent, server time.Time) {
|
||||
c, _ := tun.XormEngine.Count(Ping{})
|
||||
tun.XormEngine.InsertOne(&Ping{
|
||||
ID: c,
|
||||
TunID: tun.tunID,
|
||||
ServerTime: server,
|
||||
AgentTime: agent,
|
||||
})
|
||||
}
|
||||
|
||||
func (tun *TunCallbcks) RegisterRX(client netip.AddrPort, Size int, Proto uint8) {
|
||||
tun.XormEngine.InsertOne(&RTX{
|
||||
TunID: tun.tunID,
|
||||
Client: client,
|
||||
Proto: Proto,
|
||||
RXSize: Size,
|
||||
TXSize: 0,
|
||||
})
|
||||
}
|
||||
func (tun *TunCallbcks) RegisterTX(client netip.AddrPort, Size int, Proto uint8) {
|
||||
tun.XormEngine.InsertOne(&RTX{
|
||||
TunID: tun.tunID,
|
||||
Client: client,
|
||||
Proto: Proto,
|
||||
TXSize: Size,
|
||||
RXSize: 0,
|
||||
})
|
||||
}
|
||||
|
||||
func (caller *serverCalls) AgentAuthentication(Token [36]byte) (server.TunnelInfo, error) {
|
||||
var tun = Tun{Token: Token}
|
||||
|
@ -3,7 +3,6 @@ package udplisterner
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
@ -12,19 +11,19 @@ import (
|
||||
type PipeConn struct {
|
||||
root *net.UDPConn
|
||||
to, localAddr netip.AddrPort
|
||||
buff *bytes.Buffer
|
||||
closed bool
|
||||
closedChan chan struct{}
|
||||
buff *bytes.Buffer
|
||||
}
|
||||
|
||||
func NewConn(root *net.UDPConn, LocalAddr, to netip.AddrPort, buff *bytes.Buffer) *PipeConn {
|
||||
func NewConn(root *net.UDPConn, LocalAddr, to netip.AddrPort) *PipeConn {
|
||||
return &PipeConn{
|
||||
root: root,
|
||||
to: to,
|
||||
localAddr: LocalAddr,
|
||||
closedChan: make(chan struct{}),
|
||||
closed: false,
|
||||
buff: buff,
|
||||
buff: bytes.NewBuffer(make([]byte, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,22 +54,12 @@ func (conn PipeConn) Read(r []byte) (int, error) {
|
||||
if conn.closed {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
doned := false
|
||||
defer func(){
|
||||
doned = true
|
||||
}()
|
||||
time.AfterFunc(time.Second*15, func() {
|
||||
if doned {
|
||||
return
|
||||
count := 50
|
||||
for !conn.closed && conn.buff.Len() < len(r) {
|
||||
if count--; count == 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
doned = true
|
||||
conn.Close()
|
||||
})
|
||||
for conn.buff.Len() < len(r) && !conn.closed {
|
||||
log.Println("waiting")
|
||||
<-time.After(time.Second)
|
||||
<-time.After(time.Second * 5)
|
||||
}
|
||||
|
||||
return conn.buff.Read(r)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ Lidar com pacotes de varios tamanhos
|
||||
package udplisterner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"net"
|
||||
"net/netip"
|
||||
@ -73,14 +72,7 @@ func (list *Udplisterner) handle() {
|
||||
list.locker.Lock() // Locker map
|
||||
list.loger.Printf("locked")
|
||||
|
||||
list.currentClients[from.String()] = &PipeConn{
|
||||
root: list.conn,
|
||||
to: from,
|
||||
localAddr: from,
|
||||
closedChan: make(chan struct{}),
|
||||
closed: false,
|
||||
buff: bytes.NewBuffer(make([]byte, 0)),
|
||||
}
|
||||
list.currentClients[from.String()] = NewConn(list.conn, from, from)
|
||||
client = list.currentClients[from.String()]
|
||||
|
||||
list.locker.Unlock() // Unlocker map
|
||||
|
@ -37,11 +37,10 @@ func (agent *AgentAuth) Reader(r io.Reader) error {
|
||||
|
||||
// Send request to agent and wait response
|
||||
type Request struct {
|
||||
AgentAuth *AgentAuth // Send agent authentication to controller
|
||||
Ping *time.Time // Send ping time to controller in unix milliseconds
|
||||
ClientClose *Client // Close client in controller
|
||||
DataTX *ClientData // Recive data from agent
|
||||
ResizeBuffer *uint64 // Resize request buffer
|
||||
AgentAuth *AgentAuth `json:",omitempty"` // Send agent authentication to controller
|
||||
Ping *time.Time `json:",omitempty"` // Send ping time to controller in unix milliseconds
|
||||
ClientClose *Client `json:",omitempty"` // Close client in controller
|
||||
DataTX *ClientData `json:",omitempty"` // Recive data from agent
|
||||
}
|
||||
|
||||
func ReaderRequest(r io.Reader) (*Request, error) {
|
||||
|
@ -88,16 +88,16 @@ func (agent *AgentInfo) Reader(r io.Reader) (err error) {
|
||||
|
||||
// Reader data from Controller and process in agent
|
||||
type Response struct {
|
||||
Unauthorized bool // Controller reject connection
|
||||
BadRequest bool // Controller accepted packet so cannot process Request
|
||||
SendAuth bool // Send Agent token
|
||||
NotListened bool // Controller cannot Listen port
|
||||
Unauthorized bool `json:",omitempty"` // Controller reject connection
|
||||
BadRequest bool `json:",omitempty"` // Controller accepted packet so cannot process Request
|
||||
SendAuth bool `json:",omitempty"` // Send Agent token
|
||||
NotListened bool `json:",omitempty"` // Controller cannot Listen port
|
||||
|
||||
AgentInfo *AgentInfo // Agent Info
|
||||
Pong *time.Time // ping response
|
||||
AgentInfo *AgentInfo `json:",omitempty"` // Agent Info
|
||||
Pong *time.Time `json:",omitempty"` // ping response
|
||||
|
||||
CloseClient *Client // Controller end client
|
||||
DataRX *ClientData // Controller recive data from client
|
||||
CloseClient *Client `json:",omitempty"` // Controller end client
|
||||
DataRX *ClientData `json:",omitempty"` // Controller recive data from client
|
||||
}
|
||||
|
||||
func ReaderResponse(r io.Reader) (*Response, error) {
|
||||
|
@ -2,11 +2,11 @@ package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"sirherobrine23.org/Minecraft-Server/go-pproxit/internal/udplisterner/v2"
|
||||
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type TunnelCall interface {
|
||||
BlockedAddr(AddrPort netip.Addr) bool // Ignore request from this address
|
||||
BlockedAddr(AddrPort string) bool // Ignore request from this address
|
||||
AgentPing(agent, server time.Time) // Register ping to Agent
|
||||
AgentShutdown(onTime time.Time) // Agend end connection
|
||||
RegisterRX(client netip.AddrPort, Size int, Proto uint8) // Register Recived data from client
|
||||
@ -60,6 +60,8 @@ func (tun *Tunnel) Close() error {
|
||||
}
|
||||
|
||||
func (tun *Tunnel) send(res proto.Response) error {
|
||||
d, _ := json.Marshal(res)
|
||||
fmt.Printf("\nSending: %s\n", string(d))
|
||||
return proto.WriteResponse(tun.RootConn, res)
|
||||
}
|
||||
|
||||
@ -126,9 +128,19 @@ func (tun *Tunnel) Setup() {
|
||||
}
|
||||
|
||||
d, _ := json.Marshal(req)
|
||||
os.Stderr.Write(append(d, 0x000A))
|
||||
fmt.Printf("\nRequest: %s\n", string(d))
|
||||
|
||||
if ping := req.Ping; req.Ping != nil {
|
||||
if req.AgentAuth != nil {
|
||||
go tun.send(proto.Response{
|
||||
AgentInfo: &proto.AgentInfo{
|
||||
Protocol: tun.TunInfo.Proto,
|
||||
AddrPort: netip.MustParseAddrPort(tun.RootConn.RemoteAddr().String()),
|
||||
UDPPort: tun.TunInfo.UDPPort,
|
||||
TCPPort: tun.TunInfo.TCPPort,
|
||||
},
|
||||
})
|
||||
continue
|
||||
} else if ping := req.Ping; req.Ping != nil {
|
||||
var now = time.Now()
|
||||
tun.send(proto.Response{Pong: &now})
|
||||
go tun.TunInfo.Callbacks.AgentPing(*ping, now) // backgroud process
|
||||
@ -170,7 +182,7 @@ func (tun *Tunnel) TCP() (err error) {
|
||||
return
|
||||
}
|
||||
remote := netip.MustParseAddrPort(conn.RemoteAddr().String())
|
||||
if tun.TunInfo.Callbacks.BlockedAddr(remote.Addr()) {
|
||||
if tun.TunInfo.Callbacks.BlockedAddr(remote.Addr().String()) {
|
||||
conn.Close() // Close connection
|
||||
continue
|
||||
}
|
||||
@ -194,7 +206,7 @@ func (tun *Tunnel) UDP() (err error) {
|
||||
return
|
||||
}
|
||||
remote := netip.MustParseAddrPort(conn.RemoteAddr().String())
|
||||
if tun.TunInfo.Callbacks.BlockedAddr(remote.Addr()) {
|
||||
if tun.TunInfo.Callbacks.BlockedAddr(remote.Addr().String()) {
|
||||
conn.Close() // Close connection
|
||||
continue
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user