WIP: Update struct decode and encode #2

Draft
Sirherobrine23 wants to merge 13 commits from struct-serealize into main
24 changed files with 941 additions and 1140 deletions
Showing only changes of commit f8c0dfcbd3 - Show all commits

View File

@ -1,21 +1,15 @@
# Golang pproxit to TCP and UDP Connections # 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, this implements only Server, for client Wrapper to same `net.Listener`
## TODO ## Server
- [ ] Agent Connect Controls all connections and connected clients, any request within the accepted posts will be forwarded to its current clients without any data filtering, only users that can be blocked
- [x] Auth
- [ ] Send shutdow agent ## Client
- [x] Recive packets
- [x] Send packets Connect to the controller and accept and manage local connections, quickly and dynamically, without having to specify the values
- [ ] Controller
- [x] Listener tunnels ### Auth
- [x] Redirect packets to agent
- Require Patchs to fix agent retransmitter packets As part of being simple, the controller can enable simple or even more complex authentication, and some data can also be recorded if there are some functions enabled by the Controller.
- [ ] TCP
- [ ] TX Data
- [ ] RX Data
- [ ] UDP
- [ ] TX Data
- [ ] RX Data

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"net" "net"
"net/netip" "net/netip"
"reflect"
"time" "time"
"sirherobrine23.com.br/Minecraft-Server/go-pproxit/internal/pipe" "sirherobrine23.com.br/Minecraft-Server/go-pproxit/internal/pipe"
@ -52,8 +53,7 @@ type Client struct {
Latency int64 // Latency response in ms from last Pong Latency int64 // Latency response in ms from last Pong
newListen chan remoteClient // new clients listener in Controller newListen chan remoteClient // new clients listener in Controller
cachErr chan error // Pipe errors to channel errListen chan error // new clients listener in Controller
closedErr bool
} }
func NewClient(Address string, AuthToken []byte) (*Client, error) { func NewClient(Address string, AuthToken []byte) (*Client, error) {
@ -82,7 +82,7 @@ func NewClient(Address string, AuthToken []byte) (*Client, error) {
} }
} }
clientStr.cachErr = make(chan error) clientStr.errListen = make(chan error)
clientStr.newListen = make(chan remoteClient) clientStr.newListen = make(chan remoteClient)
clientStr.TCPConns = make(map[string]*net.TCPConn) clientStr.TCPConns = make(map[string]*net.TCPConn)
clientStr.UDPConns = make(map[string]*net.UDPConn) clientStr.UDPConns = make(map[string]*net.UDPConn)
@ -90,25 +90,21 @@ func NewClient(Address string, AuthToken []byte) (*Client, error) {
return &clientStr, nil return &clientStr, nil
} }
// Wait for any error, if catch error close connection and return error // Wait for any error
func (client *Client) WaitCloseError() error { func (client *Client) WaitError() error {
if err, ok := <-client.cachErr; ok { if err, ok := <-client.errListen; ok {
if err != nil && client.Conn != nil {
client.Close()
}
return err return err
} }
return io.EOF return io.EOF
} }
// Wait for any error // Wait for any error, if catch error close connection and return error
func (client *Client) WaitError() error { func (client *Client) WaitCloseError() error {
if err, ok := <-client.cachErr; ok { err := client.WaitError()
return err if err != nil && client.Conn != nil {
} else if client.Conn == nil { client.Close()
return io.EOF
} }
return nil return err
} }
// Close Clients and Controller connection // Close Clients and Controller connection
@ -131,16 +127,18 @@ func (client *Client) Close() error {
} }
client.Conn = nil client.Conn = nil
} }
client.closedErr = true close(client.newListen)
close(client.cachErr) close(client.errListen)
return nil return nil
} }
// Send error to WaitError, if closed catcher and ignored
func (client *Client) sendErr(err error) { func (client *Client) sendErr(err error) {
if client.closedErr { if reflect.ValueOf(client.errListen).IsZero() {
return return
} }
client.cachErr <- err defer func() { recover() }()
client.errListen <- err
} }
// Send auth to controller // Send auth to controller

28
go.mod
View File

@ -1,31 +1,3 @@
module sirherobrine23.com.br/Minecraft-Server/go-pproxit module sirherobrine23.com.br/Minecraft-Server/go-pproxit
go 1.22 go 1.22
require (
modernc.org/sqlite v1.33.1
xorm.io/xorm v1.3.9
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/goccy/go-json v0.8.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
golang.org/x/sys v0.22.0 // indirect
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
modernc.org/libc v1.55.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
)

105
go.sum
View File

@ -1,105 +0,0 @@
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.8.1 h1:4/Wjm0JIJaTDm8K1KcGrLHJoa8EsJ13YWeX+6Kfq6uI=
github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
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/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=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ=
modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ=
modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y=
modernc.org/ccgo/v4 v4.19.2/go.mod h1:ysS3mxiMV38XGRTTcgo0DQTeTmAO4oCmJl1nX9VFI3s=
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v2 v2.4.1 h1:9cNzOqPyMJBvrUipmynX0ZohMhcxPtMccYgGOJdOiBw=
modernc.org/gc/v2 v2.4.1/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
modernc.org/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U=
modernc.org/libc v1.55.3/go.mod h1:qFXepLhz+JjFThQ4kzwzOjA/y/artDeg+pcYnY+Q83w=
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU=
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4=
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0=
modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc=
modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss=
modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM=
modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k=
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 h1:bvLlAPW1ZMTWA32LuZMBEGHAUOcATZjzHcotf3SWweM=
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE=
xorm.io/xorm v1.3.9 h1:TUovzS0ko+IQ1XnNLfs5dqK1cJl1H5uHpWbWqAQ04nU=
xorm.io/xorm v1.3.9/go.mod h1:LsCCffeeYp63ssk0pKumP6l96WZcHix7ChpurcLNuMw=

View File

@ -8,7 +8,7 @@ import (
) )
func TestClientServer(t *testing.T) { func TestClientServer(t *testing.T) {
calls, err := NewCall("./pproxit.db") calls, err := NewCall()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@ -18,6 +18,7 @@ package structcode
import ( import (
"encoding" "encoding"
"errors"
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
@ -34,11 +35,20 @@ var (
typeofBinMarshal = reflect.TypeFor[encoding.BinaryMarshaler]() typeofBinMarshal = reflect.TypeFor[encoding.BinaryMarshaler]()
) )
func NewEncode(w io.Writer, target any) error { func NewEncode(w io.Writer, target any) (err error) {
if target == nil { if target == nil {
return nil return nil
} }
defer func() {
if ierr := recover(); ierr != nil {
switch v := ierr.(type) {
case error:
err = v
case string:
err = errors.New(v)
}
}
}()
reflectValue := reflect.ValueOf(target) reflectValue := reflect.ValueOf(target)
if reflectValue.Type().Kind() == reflect.Pointer { if reflectValue.Type().Kind() == reflect.Pointer {
reflectValue = reflectValue.Elem() reflectValue = reflectValue.Elem()
@ -46,7 +56,17 @@ func NewEncode(w io.Writer, target any) error {
return encodeRecursive(w, reflectValue) return encodeRecursive(w, reflectValue)
} }
func NewDecode(r io.Reader, target any) error { func NewDecode(r io.Reader, target any) (err error) {
defer func() {
if ierr := recover(); ierr != nil {
switch v := ierr.(type) {
case error:
err = v
case string:
err = errors.New(v)
}
}
}()
if target == nil { if target == nil {
return fmt.Errorf("set target, not nil") return fmt.Errorf("set target, not nil")
} else if reflect.TypeOf(target).Kind() != reflect.Pointer { } else if reflect.TypeOf(target).Kind() != reflect.Pointer {

View File

@ -65,10 +65,10 @@ func (controller *Server) handlerConn(conn net.Conn) {
fmt.Fprintf(os.Stderr, "Auth decode error: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "Auth decode error: %s\n", err.Error())
} }
return return
} else if req.AgentAuth == nil { }
structcode.NewEncode(conn, proto.Response{SendAuth: true})
continue
} else if tunnelInfo, err = controller.ControlCalls.AgentAuthentication(*req.AgentAuth); err != nil { if tunnelInfo, err = controller.ControlCalls.AgentAuthentication(*req.AgentAuth); err != nil {
if err == ErrAuthAgentFail { if err == ErrAuthAgentFail {
structcode.NewEncode(conn, proto.Response{Unauthorized: true}) structcode.NewEncode(conn, proto.Response{Unauthorized: true})
return return

View File

@ -1,57 +1,53 @@
package gopproxit_test package gopproxit_test
import ( import (
"fmt"
"net/netip" "net/netip"
"sync" "sync"
"time" "time"
_ "modernc.org/sqlite"
"sirherobrine23.com.br/Minecraft-Server/go-pproxit/proto" "sirherobrine23.com.br/Minecraft-Server/go-pproxit/proto"
"sirherobrine23.com.br/Minecraft-Server/go-pproxit/server" "sirherobrine23.com.br/Minecraft-Server/go-pproxit/server"
"xorm.io/xorm"
"xorm.io/xorm/names"
) )
type serverCalls struct { type serverCalls struct {
XormEngine *xorm.Engine
Locker sync.Locker Locker sync.Locker
Tables map[string]map[int64]any
} }
type User struct { type User struct {
ID int64 `xorm:"pk"` // Client ID ID int64 // Client ID
Username string `xorm:"varchar(32) notnull unique 'user'"` // Username Username string // Username
FullName string `xorm:"text notnull notnull 'name'"` // Real name for user FullName string // Real name for user
AccountStatus int8 `xorm:"BIT notnull 'status'"` // Account Status AccountStatus int8 // Account Status
CreateAt time.Time `xorm:"created"` // Create date CreateAt time.Time // Create date
UpdateAt time.Time `xorm:"updated"` // Update date UpdateAt time.Time // Update date
} }
type Tun struct { type Tun struct {
ID int64 `xorm:"pk"` // Tunnel ID ID int64 // Tunnel ID
User int64 `xorm:"notnull"` // Agent ID User int64 // Agent ID
Token []byte `xorm:"blob notnull unique"` // Tunnel Token Token []byte // Tunnel Token
Proto proto.Protoc `xorm:"default 3"` // Proto accept Proto proto.Protoc // Proto accept
TPCListen uint16 // Port listen TCP agent TPCListen uint16 // Port listen TCP agent
UDPListen uint16 // Port listen UDP agent UDPListen uint16 // Port listen UDP agent
} }
type Ping struct { type Ping struct {
ID int64 `json:"-" xorm:"pk"` // Tunnel ID ID int64 `json:"-"` // Tunnel ID
TunID int64 `json:"-"` TunID int64 `json:"-"`
ServerTime time.Time `json:"server" xorm:"datetime notnull"` ServerTime time.Time `json:"server"`
AgentTime time.Time `json:"agent" xorm:"datetime notnull"` AgentTime time.Time `json:"agent"`
} }
type AddrBlocked struct { type AddrBlocked struct {
ID int64 `json:"-" xorm:"pk"` // Tunnel ID ID int64 `json:"-"` // Tunnel ID
TunID int64 `json:"-"` TunID int64 `json:"-"`
Enabled bool Enabled bool
Address string Address string
} }
type RTX struct { type RTX struct {
ID int64 `json:"-" xorm:"pk"` // Tunnel ID ID int64 `json:"-"` // Tunnel ID
TunID int64 `json:"-"` TunID int64 `json:"-"`
Client netip.AddrPort Client netip.AddrPort
TXSize int TXSize int
@ -59,116 +55,53 @@ type RTX struct {
Proto proto.Protoc Proto proto.Protoc
} }
func NewCall(DBConn string) (call *serverCalls, err error) { func NewCall() (call *serverCalls, err error) {
call = new(serverCalls) call = new(serverCalls)
if call.XormEngine, err = xorm.NewEngine("sqlite", DBConn); err != nil {
return
}
call.Locker = &sync.Mutex{} call.Locker = &sync.Mutex{}
call.XormEngine.SetMapper(names.SameMapper{}) call.Tables = make(map[string]map[int64]any)
session := call.XormEngine.NewSession() call.Tables["User"] = make(map[int64]any)
defer session.Close() call.Tables["Tun"] = make(map[int64]any)
session.CreateTable(User{}) call.Tables["AddrBlocked"] = make(map[int64]any)
session.CreateTable(Tun{}) call.Tables["Ping"] = make(map[int64]any)
session.CreateTable(AddrBlocked{}) call.Tables["RTX"] = make(map[int64]any)
session.CreateTable(Ping{})
session.CreateTable(RTX{})
return return
} }
type TunCallbcks struct { type TunCallbcks struct {
tunID int64 tunID int64
XormEngine *xorm.Engine
Locker sync.Locker Locker sync.Locker
} }
func (tun *TunCallbcks) BlockedAddr(AddrPort string) bool { return false }
func (*TunCallbcks) AgentShutdown(onTime time.Time) {} func (*TunCallbcks) AgentShutdown(onTime time.Time) {}
func (tun *TunCallbcks) BlockedAddr(AddrPort string) bool { func (tun *TunCallbcks) AgentPing(agent, server time.Time) {}
tun.Locker.Lock() func (tun *TunCallbcks) RegisterRX(client netip.AddrPort, Size int, Proto proto.Protoc) {}
defer tun.Locker.Unlock() func (tun *TunCallbcks) RegisterTX(client netip.AddrPort, Size int, Proto proto.Protoc) {}
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) {
tun.Locker.Lock()
defer tun.Locker.Unlock()
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 proto.Protoc) {
tun.Locker.Lock()
defer tun.Locker.Unlock()
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 proto.Protoc) {
tun.Locker.Lock()
defer tun.Locker.Unlock()
tun.XormEngine.InsertOne(&RTX{
TunID: tun.tunID,
Client: client,
Proto: Proto,
TXSize: Size,
RXSize: 0,
})
}
func (caller *serverCalls) AgentAuthentication(Token []byte) (server.TunnelInfo, error) { func (caller *serverCalls) AgentAuthentication(Token []byte) (server.TunnelInfo, error) {
var tun = Tun{Token: Token} for _, tunInfo := range caller.Tables["Tun"] {
if ok, err := caller.XormEngine.Get(&tun); err != nil || !ok {
if !ok {
return server.TunnelInfo{}, server.ErrAuthAgentFail
}
return server.TunnelInfo{}, err
}
return server.TunnelInfo{ return server.TunnelInfo{
Proto: tun.Proto, Proto: tunInfo.(Tun).Proto,
TCPPort: tun.TPCListen, TCPPort: tunInfo.(Tun).TPCListen,
UDPPort: tun.UDPListen, UDPPort: tunInfo.(Tun).UDPListen,
Callbacks: &TunCallbcks{tunID: tun.ID, XormEngine: caller.XormEngine, Locker: caller.Locker}, Callbacks: &TunCallbcks{
Locker: caller.Locker,
tunID: tunInfo.(Tun).ID,
},
}, nil }, nil
} }
return server.TunnelInfo{}, server.ErrAuthAgentFail
}
func (caller *serverCalls) RegisterRandomUser() []byte { func (caller *serverCalls) RegisterRandomUser() []byte {
token := []byte{0, 0, 12, 14, 22, 89, 255, 81} token := []byte{0, 0, 12, 14, 22, 89, 255, 81}
caller.XormEngine.Insert( caller.Tables["User"][0] = User{ID: 0, AccountStatus: 1, FullName: "Radon user", Username: "random"}
&User{ID: 0, AccountStatus: 1, FullName: "Radon user", Username: "random"}, caller.Tables["Tun"][0] = Tun{
&Tun{
User: 0, User: 0,
Token: token, Token: token,
Proto: proto.ProtoBoth, Proto: proto.ProtoBoth,
TPCListen: 5522, TPCListen: 5522,
UDPListen: 5522, UDPListen: 5522,
}, }
)
return token return token
} }