Skip to content

Commit

Permalink
Update server listener
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
  • Loading branch information
Sirherobrine23 committed Jun 11, 2024
1 parent 964544e commit 24764dd
Show file tree
Hide file tree
Showing 12 changed files with 492 additions and 79 deletions.
28 changes: 26 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"net/netip"
"reflect"
"time"

"sirherobrine23.org/Minecraft-Server/go-pproxit/internal/pipe"
Expand Down Expand Up @@ -67,6 +68,13 @@ func (client Client) Recive() (res *proto.Response, err error) {
recBuff := make([]byte, client.ResponseBuffer+proto.PacketSize)
var n int
if n, err = client.Conn.Read(recBuff); err != nil {
if opErr, isOp := err.(*net.OpError); isOp {
log.Println()
err = opErr.Err
if reflect.TypeOf(opErr.Err).String() == "poll.errNetClosing" {
return nil, io.EOF
}
}
return
}

Expand All @@ -89,7 +97,9 @@ func (client Client) Send(req proto.Request) error {
return nil
}

// Send token to controller to connect to tunnel
func (client *Client) auth() (info *proto.AgentInfo, err error) {
attemps := 0
var res *proto.Response
for {
if err = client.Send(proto.Request{AgentAuth: &client.Token}); err != nil {
Expand All @@ -103,6 +113,10 @@ func (client *Client) auth() (info *proto.AgentInfo, err error) {
if res.BadRequest || res.SendAuth {
// Wait seconds to resend token
<-time.After(time.Second * 3)
if attemps++; attemps >= 25 {
err = ErrAgentUnathorized // Cannot auth
return
}
continue // Reload auth
} else if res.Unauthorized {
// Close tunnel and break loop-de-loop 🦔
Expand All @@ -115,19 +129,29 @@ func (client *Client) auth() (info *proto.AgentInfo, err error) {
return res.AgentInfo, nil
}

// Dial and Auth agent before require call in new gorotine client.Backgroud()
// Dial to controller and auto accept new responses from controller
func (client *Client) Dial() (info *proto.AgentInfo, err error) {
if client.Conn, err = net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(client.ControlAddr)); err != nil {
return
}
go client.backgroud()
return client.auth()
}

// Watcher response from controller
func (client *Client) Backgroud() (err error) {
func (client *Client) backgroud() (err error) {
go func(){
for {
var current = time.Now()
client.Send(proto.Request{Ping: &current})
<-time.After(time.Second * 5)
}
}()
for {
log.Println("waiting response from controller")
var res *proto.Response
if res, err = client.Recive(); err != nil {
log.Println(err.Error())
if err == io.EOF {
break
}
Expand Down
31 changes: 11 additions & 20 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,21 @@ var CmdClient = cli.Command{
}
fmt.Printf("Connected, Remote port: %d\n", info.LitenerPort)
fmt.Printf(" Remote address: %s\n", info.AddrPort.String())
go client.Backgroud()

localConnect := ctx.String("dial")
for {
var conn, dial net.Conn
select {
case tcp := <-client.NewTCPClient:
go func() {
conn, err := net.Dial("tcp", localConnect)
if err != nil {
return
}
go io.Copy(conn, tcp)
go io.Copy(tcp, conn)
}()
case udp := <-client.NewUDPClient:
go func () {
conn, err := net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(netip.MustParseAddrPort(localConnect)))
if err != nil {
return
}
go io.Copy(conn, udp)
go io.Copy(udp, conn)
}()
case conn = <-client.NewTCPClient:
if dial, err = net.Dial("tcp", localConnect); err != nil {
continue
}
case conn = <-client.NewUDPClient:
if dial, err = net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(netip.MustParseAddrPort(localConnect))); err != nil {
continue
}
}
go io.Copy(conn, dial)
go io.Copy(dial, conn)
}
},
}
50 changes: 39 additions & 11 deletions cmd/server/servercall.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ type serverCalls struct {
XormEngine *xorm.Engine
}

type Tun struct {
ID int64 `xorm:"pk"` // Tunnel ID
User int64 // Agent ID
Token [36]byte // Tunnel Token
Proto uint8 // Proto accept
PortListen uint16 // Port listen agent
}

type User struct {
ID int64 `xorm:"pk"` // Client ID
Username string `xorm:"varchar(32) notnull unique 'user'"` // Username
Expand All @@ -30,18 +22,35 @@ type User struct {
UpdateAt time.Time `xorm:"updated"` // Update date
}

type Tun struct {
ID int64 `xorm:"pk"` // Tunnel ID
User int64 `xorm:"notnull"` // Agent ID
Token [36]byte `xorm:"blob notnull unique"` // Tunnel Token
Proto uint8 `xorm:"default 3"` // Proto accept
PortListen uint16 // Port listen agent
}

type Ping struct {
ID int64 `json:"-" xorm:"pk"` // Tunnel ID
TunID int64 `json:"-"`
ServerTime time.Time `json:"server" xorm:"datetime notnull"`
AgentTime time.Time `json:"agent" xorm:"datetime notnull"`
}

func NewCall(DBConn string) (call *serverCalls, err error) {
call = new(serverCalls)
if call.XormEngine, err = xorm.NewEngine("sqlite", DBConn); err != nil {
return
}
call.XormEngine.SetMapper(names.SameMapper{})
call.XormEngine.CreateTables(Tun{}, User{})
session := call.XormEngine.NewSession()
defer session.Close()
session.CreateTable(User{})
session.CreateTable(Tun{})
session.CreateTable(Ping{})
return
}

func (call serverCalls) AgentShutdown(Token [36]byte) (err error) { return } // Ignore

func (call serverCalls) AgentInfo(Token [36]byte) (server.TunnelInfo, error) {
var tun = Tun{Token: Token}
if ok, err := call.XormEngine.Get(&tun); err != nil || !ok {
Expand All @@ -55,3 +64,22 @@ func (call serverCalls) AgentInfo(Token [36]byte) (server.TunnelInfo, error) {
Proto: tun.Proto,
}, nil
}

func (call serverCalls) RegisterPing(serverTime, clientTime time.Time, Token [36]byte) error {
var tun = Tun{Token: Token}
if ok, err := call.XormEngine.Get(&tun); err != nil {
return err
} else if !ok {
return server.ErrNoAgent
}

ping := new(Ping)
ping.TunID = tun.ID
ping.ServerTime = serverTime
ping.AgentTime = clientTime
_, err := call.XormEngine.InsertOne(ping)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.19.0 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.50.9 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 24764dd

Please sign in to comment.