2023-01-08 09:26:04 +00:00
|
|
|
package udpproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2024-03-08 02:29:40 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-01-08 09:26:04 +00:00
|
|
|
)
|
|
|
|
|
2024-10-19 21:19:03 +01:00
|
|
|
// Option is a functional option type that allows us to configure the Client.
|
|
|
|
type Option func(*Client)
|
|
|
|
|
|
|
|
// WithStaleTimeout is a functional option to set the stale session timeout
|
|
|
|
func WithStaleTimeout(timeout time.Duration) Option {
|
|
|
|
return func(c *Client) {
|
|
|
|
if timeout < time.Minute {
|
|
|
|
log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 5 minutes")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.timeout = timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 09:26:04 +00:00
|
|
|
type Client struct {
|
|
|
|
laddr *net.UDPAddr
|
|
|
|
raddr *net.UDPAddr
|
|
|
|
|
|
|
|
proxyConn *net.UDPConn
|
|
|
|
|
|
|
|
mutex sync.RWMutex
|
2024-03-26 10:47:53 +00:00
|
|
|
sessions map[string]*session
|
2024-10-19 21:19:03 +01:00
|
|
|
|
|
|
|
timeout time.Duration
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 21:19:03 +01:00
|
|
|
func New(port, target string, options ...Option) (*Client, error) {
|
2023-01-08 09:26:04 +00:00
|
|
|
laddr, err := net.ResolveUDPAddr("udp", port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raddr, err := net.ResolveUDPAddr("udp", target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-19 21:19:03 +01:00
|
|
|
c := &Client{
|
2023-01-08 09:26:04 +00:00
|
|
|
laddr: laddr,
|
|
|
|
raddr: raddr,
|
|
|
|
mutex: sync.RWMutex{},
|
2024-03-26 10:47:53 +00:00
|
|
|
sessions: map[string]*session{},
|
2024-10-19 21:19:03 +01:00
|
|
|
timeout: 5 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ListenAndServe() error {
|
|
|
|
var err error
|
|
|
|
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go c.pruneSessions()
|
|
|
|
|
2024-09-28 00:38:55 +01:00
|
|
|
buf := make([]byte, 2048)
|
2023-01-08 09:26:04 +00:00
|
|
|
for {
|
2023-01-08 10:59:43 +00:00
|
|
|
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
2023-01-08 09:26:04 +00:00
|
|
|
if err != nil {
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err)
|
2024-01-27 14:48:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 15:36:04 +01:00
|
|
|
session, ok := c.sessions[caddr.String()]
|
|
|
|
if !ok {
|
2024-03-20 17:05:02 +00:00
|
|
|
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
2023-01-08 09:26:04 +00:00
|
|
|
if err != nil {
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err)
|
2023-01-08 09:26:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.sessions[caddr.String()] = session
|
|
|
|
}
|
|
|
|
|
2023-01-08 11:04:44 +00:00
|
|
|
go session.proxyTo(buf[:n])
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) pruneSessions() {
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
|
|
|
|
|
|
// the locks here could be abusive and i dont even know if this is a real
|
|
|
|
// problem but we definitely need to clean up stale sessions
|
|
|
|
for range ticker.C {
|
|
|
|
for _, session := range c.sessions {
|
|
|
|
c.mutex.RLock()
|
2024-10-19 21:19:03 +01:00
|
|
|
if time.Since(session.updateTime) > c.timeout {
|
2023-01-08 09:26:04 +00:00
|
|
|
delete(c.sessions, session.caddr.String())
|
2024-10-19 21:19:03 +01:00
|
|
|
log.Tracef("session for %s deleted", session.caddr)
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
c.mutex.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|