WIP: prepare for testing
This commit is contained in:
parent
df828e2e9e
commit
59a91d29fd
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
cmd/netbounce/netbounce
|
cmd/netbounce/netbounce
|
||||||
|
/netbounce
|
||||||
|
|||||||
@ -16,7 +16,10 @@ package abstract
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type ConnectionType int
|
type ConnectionType int
|
||||||
|
|
||||||
@ -42,4 +45,5 @@ type TCPConnectionConfig interface {
|
|||||||
type UDPConnectionConfig interface {
|
type UDPConnectionConfig interface {
|
||||||
ConnectionConfig
|
ConnectionConfig
|
||||||
Timeout() time.Duration
|
Timeout() time.Duration
|
||||||
|
BackendAddr() net.Addr
|
||||||
}
|
}
|
||||||
|
|||||||
@ -119,7 +119,7 @@ func (b *backendUDP) createRelSend(clientAddr string, buf []byte) (udpRel, error
|
|||||||
return rel, fmt.Errorf("create udp relation and send message: dial udp: %w", err)
|
return rel, fmt.Errorf("create udp relation and send message: dial udp: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n, err = udpConn.Write(buf); err != nil && n == 0 {
|
if n, err = udpConn.WriteTo(buf, b.cfg.BackendAddr()); err != nil && n == 0 {
|
||||||
_ = udpConn.Close()
|
_ = udpConn.Close()
|
||||||
return rel, fmt.Errorf("create udp relation and send message: write udp: %w", err)
|
return rel, fmt.Errorf("create udp relation and send message: write udp: %w", err)
|
||||||
}
|
}
|
||||||
@ -130,6 +130,23 @@ func (b *backendUDP) createRelSend(clientAddr string, buf []byte) (udpRel, error
|
|||||||
return rel, nil
|
return rel, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backendUDP) relSend(rel udpRel, buf []byte) error {
|
||||||
|
var (
|
||||||
|
n int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if n, err = rel.backend.WriteTo(buf, b.cfg.BackendAddr()); err != nil && n == 0 {
|
||||||
|
return fmt.Errorf("relSend: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf) != n {
|
||||||
|
log.Error().Msg("relSend mismatch size")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backendUDP) handle(wg *sync.WaitGroup, ctx context.Context, msg udpMessage) {
|
func (b *backendUDP) handle(wg *sync.WaitGroup, ctx context.Context, msg udpMessage) {
|
||||||
var (
|
var (
|
||||||
rel udpRel
|
rel udpRel
|
||||||
@ -139,9 +156,15 @@ func (b *backendUDP) handle(wg *sync.WaitGroup, ctx context.Context, msg udpMess
|
|||||||
|
|
||||||
if rel, ok = b.findRel(msg.addr); !ok {
|
if rel, ok = b.findRel(msg.addr); !ok {
|
||||||
if rel, err = b.createRelSend(msg.addr, msg.buf.Bytes()); err != nil {
|
if rel, err = b.createRelSend(msg.addr, msg.buf.Bytes()); err != nil {
|
||||||
|
msg.buf.Reset()
|
||||||
|
b.bufPool.Put(msg.buf)
|
||||||
log.Error().Err(err).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("establish relation with udp backend")
|
log.Error().Err(err).Str(DIRECTION, CLIENT_TO_BACKEND).Msg("establish relation with udp backend")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg.buf.Reset()
|
||||||
|
b.bufPool.Put(msg.buf)
|
||||||
|
|
||||||
rel.ctx, rel.ctxCancel = context.WithCancel(ctx)
|
rel.ctx, rel.ctxCancel = context.WithCancel(ctx)
|
||||||
b.addUpdateRel(msg.addr, rel)
|
b.addUpdateRel(msg.addr, rel)
|
||||||
|
|
||||||
@ -150,9 +173,18 @@ func (b *backendUDP) handle(wg *sync.WaitGroup, ctx context.Context, msg udpMess
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = rel
|
if err = b.relSend(rel, msg.buf.Bytes()); err != nil {
|
||||||
//TODO: handle existing valid client
|
msg.buf.Reset()
|
||||||
|
b.bufPool.Put(msg.buf)
|
||||||
|
log.Error().Err(err).Msg("handle: send for existing relation")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.buf.Reset()
|
||||||
|
b.bufPool.Put(msg.buf)
|
||||||
|
|
||||||
|
rel.expiry = time.Now().Add(b.cfg.Timeout())
|
||||||
|
b.addUpdateRel(rel.clientAddr, rel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backendUDP) Send(ctx context.Context, addr string, p []byte) error {
|
func (b *backendUDP) Send(ctx context.Context, addr string, p []byte) error {
|
||||||
|
|||||||
1
cmd/test-client/main.go
Normal file
1
cmd/test-client/main.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package main
|
||||||
1
cmd/test-server/main.go
Normal file
1
cmd/test-server/main.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package main
|
||||||
0
compose.yml
Normal file
0
compose.yml
Normal file
@ -18,6 +18,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gitea.suyono.dev/suyono/netbounce/abstract"
|
"gitea.suyono.dev/suyono/netbounce/abstract"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,5 +60,5 @@ func (t tcpModule) AsTCP() abstract.TCPConnectionConfig {
|
|||||||
|
|
||||||
func (t tcpModule) AsUDP() abstract.UDPConnectionConfig {
|
func (t tcpModule) AsUDP() abstract.UDPConnectionConfig {
|
||||||
panic(fmt.Errorf("not UDP"))
|
panic(fmt.Errorf("not UDP"))
|
||||||
return nil
|
// return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,10 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.suyono.dev/suyono/netbounce/abstract"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.suyono.dev/suyono/netbounce/abstract"
|
||||||
)
|
)
|
||||||
|
|
||||||
type udpConfig struct {
|
type udpConfig struct {
|
||||||
@ -30,6 +32,10 @@ type udpConfig struct {
|
|||||||
Timeout time.Duration `mapstructure:"timeout,30m"`
|
Timeout time.Duration `mapstructure:"timeout,30m"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type udpBackendAddr struct {
|
||||||
|
config udpConfig
|
||||||
|
}
|
||||||
|
|
||||||
type udpModule struct {
|
type udpModule struct {
|
||||||
config udpConfig
|
config udpConfig
|
||||||
}
|
}
|
||||||
@ -40,7 +46,7 @@ func (u udpModule) AsUDP() abstract.UDPConnectionConfig {
|
|||||||
|
|
||||||
func (u udpModule) AsTCP() abstract.TCPConnectionConfig {
|
func (u udpModule) AsTCP() abstract.TCPConnectionConfig {
|
||||||
panic(fmt.Errorf("not UDP"))
|
panic(fmt.Errorf("not UDP"))
|
||||||
return nil
|
// return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u udpModule) Name() string {
|
func (u udpModule) Name() string {
|
||||||
@ -62,3 +68,15 @@ func (u udpModule) Backend() string {
|
|||||||
func (u udpModule) Timeout() time.Duration {
|
func (u udpModule) Timeout() time.Duration {
|
||||||
return u.config.Timeout
|
return u.config.Timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u udpModule) BackendAddr() net.Addr {
|
||||||
|
return udpBackendAddr(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a udpBackendAddr) Network() string {
|
||||||
|
return "udp"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a udpBackendAddr) String() string {
|
||||||
|
return a.config.Backend
|
||||||
|
}
|
||||||
|
|||||||
1
docker/Dockerfile
Normal file
1
docker/Dockerfile
Normal file
@ -0,0 +1 @@
|
|||||||
|
FROM suyono/wingmate:v0.2.0-bookworm AS wingmate
|
||||||
0
docker/Dockerfile-test-client
Normal file
0
docker/Dockerfile-test-client
Normal file
0
docker/Dockerfile-test-server
Normal file
0
docker/Dockerfile-test-server
Normal file
Loading…
x
Reference in New Issue
Block a user