Skip to content

Commit

Permalink
dialer
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiwuchang committed Oct 9, 2024
1 parent 48c93c7 commit 190e9e4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.0.7

* udp dialer support network "udp" or "udp4" or "udp6"
* tcp dialer support network "tcp" or "tcp4" or "tcp6"

# v0.0.6

* udp listener support network "udp" or "udp4" or "udp6"
Expand Down
2 changes: 2 additions & 0 deletions bin/example/udp.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
listen: ':1053',
// remote target addr
to: '8.8.8.8:53',
// "udp" "udp4" "udp6"
toNetwork:"udp",
// udp max frame length, default 1024*2
size: 1500,
// udp timeout, default 3m
Expand Down
2 changes: 2 additions & 0 deletions config/udp_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type UDPForward struct {
Listen string `json:"listen"`
// remote target addr
To string `json:"to"`
// "udp" "udp4" "udp6"
ToNetwork string `json:"toNetwork"`
// udp max frame length, default 1024*2
Size int `json:"size"`
// udp timeout, default 3m
Expand Down
2 changes: 1 addition & 1 deletion dialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New(nk *network.Network, log *slog.Logger, pool *pool.Pool, opts *config.Di
case HttpTls:
dialer, e = newHttpDialer(nk, log, opts, u, true)
case Basic:
if opts.Network == `udp` {
if opts.Network == `udp` || opts.Network == `udp4` || opts.Network == `udp6` {
dialer, e = newUdpDialer(nk, log, opts, u, pool)
} else {
dialer, e = newBasicDialer(nk, log, opts, u, false)
Expand Down
6 changes: 3 additions & 3 deletions dialer/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newUdpDialer(
}

var (
network = `tcp`
network = `udp`
addr = u.Host
query url.Values
)
Expand Down Expand Up @@ -109,11 +109,11 @@ func (u *udpDialer) Tag() string {
return u.remoteAddr.Dialer
}
func (u *udpDialer) Connect(ctx context.Context) (conn *Conn, e error) {
addr, e := net.ResolveUDPAddr(`udp`, u.remoteAddr.Addr)
addr, e := net.ResolveUDPAddr(u.remoteAddr.Network, u.remoteAddr.Addr)
if e != nil {
return
}
c, e := net.DialUDP("udp", nil, addr)
c, e := net.DialUDP(u.remoteAddr.Network, nil, addr)
if e != nil {
return
}
Expand Down
2 changes: 2 additions & 0 deletions internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func (n *Network) Dialer(network string, addr string, cfg *tls.Config) (dialer D
n.pipeList.PushBack(dialer)
return
case `tcp`:
case `tcp4`:
case `tcp6`:
case `unix`:
if runtime.GOOS != `linux` {
e = ErrNetworkUnix
Expand Down
57 changes: 36 additions & 21 deletions internal/udp/udp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package udp

import (
"errors"
"log/slog"
"net"
"sync"
Expand All @@ -11,10 +12,12 @@ import (
)

type UDP struct {
tag string
listen string
c *net.UDPConn
to *net.UDPAddr
tag string
listen string
c *net.UDPConn
toNetwork string
to string

timeout time.Duration
size int
mutex sync.Mutex
Expand All @@ -31,9 +34,19 @@ func New(log *slog.Logger, opts *config.UDPForward) (u *UDP, e error) {
network := opts.Network
if network == `` {
network = `udp`
} else if network != `udp` && network != `udp4` && network != `udp6` {
e = errors.New(`network not supported: ` + network)
return
}
toNetwork := opts.ToNetwork
if toNetwork == `` {
toNetwork = `udp`
} else if toNetwork != `udp` && toNetwork != `udp4` && toNetwork != `udp6` {
e = errors.New(`network not supported: ` + toNetwork)
return
}
if tag == `` {
tag = network + ` ` + opts.Listen + ` -> ` + opts.To
tag = network + ` ` + opts.Listen + ` -> ` + toNetwork + ` ` + opts.To
}
log = log.With(
`tag`, tag,
Expand All @@ -49,11 +62,6 @@ func New(log *slog.Logger, opts *config.UDPForward) (u *UDP, e error) {
log.Error(`listen udp fial`, `error`, e)
return
}
to, e := net.ResolveUDPAddr(`udp`, opts.To)
if e != nil {
log.Error(`listen udp fial`, `error`, e)
return
}
var timeout time.Duration
if opts.Timeout == `` {
timeout = time.Minute * 3
Expand All @@ -70,23 +78,24 @@ func New(log *slog.Logger, opts *config.UDPForward) (u *UDP, e error) {
}
log.Info(`udp forward`, `timeout`, timeout, `size`, size)
u = &UDP{
tag: tag,
c: c,
listen: opts.Listen,
to: to,
timeout: timeout,
size: size,
keys: make(map[string]*remoteConn),
done: make(chan struct{}),
log: log,
tag: tag,
c: c,
listen: opts.Listen,
to: opts.To,
toNetwork: opts.ToNetwork,
timeout: timeout,
size: size,
keys: make(map[string]*remoteConn),
done: make(chan struct{}),
log: log,
}
return
}
func (u *UDP) Info() any {
return map[string]any{
`tag`: u.tag,
`listen`: u.listen,
`to`: u.to.String(),
`to`: u.to,
`timeout`: u.timeout.String(),
`size`: u.size,
}
Expand All @@ -100,6 +109,7 @@ func (u *UDP) Serve() (e error) {
c *remoteConn
ok bool
conn *net.UDPConn
to *net.UDPAddr
)
for {
n, addr, e = u.c.ReadFromUDP(b)
Expand All @@ -115,7 +125,12 @@ func (u *UDP) Serve() (e error) {
continue
}
} else {
conn, e = net.DialUDP(`udp`, nil, u.to)
to, e = net.ResolveUDPAddr(u.toNetwork, u.to)
if e != nil {
u.log.Warn("ResolveUDPAddr fail", `error`, e)
continue
}
conn, e = net.DialUDP(u.toNetwork, nil, to)
if e != nil {
u.log.Warn("DialUDP fail", `error`, e)
continue
Expand Down
2 changes: 1 addition & 1 deletion script/conf.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Target="streamf"
Docker="king011/streamf"
Dir=$(cd "$(dirname $BASH_SOURCE)/.." && pwd)
Version="v0.0.6"
Version="v0.0.7"
Platforms=(
darwin/amd64
windows/amd64
Expand Down

0 comments on commit 190e9e4

Please sign in to comment.