mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
unix: support udp forwarding (#194)
This commit is contained in:
1 parent
63112a1509
commit
142865535e
10 files changed
+259
-111
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package unix
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
proxy.RegisterDialer("unix", NewUnixDialer)
|
||||
}
|
||||
|
||||
// NewUnixDialer returns a unix domain socket dialer.
|
||||
func NewUnixDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
|
||||
return NewUnix(s, d, nil)
|
||||
}
|
||||
|
||||
// Addr returns forwarder's address.
|
||||
func (s *Unix) Addr() string {
|
||||
if s.addr == "" {
|
||||
return s.dialer.Addr()
|
||||
}
|
||||
return s.addr
|
||||
}
|
||||
|
||||
// Dial connects to the address addr on the network net via the proxy.
|
||||
// NOTE: must be the first dialer in a chain
|
||||
func (s *Unix) Dial(network, addr string) (net.Conn, error) {
|
||||
return net.Dial("unix", s.addr)
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
// NOTE: must be the first dialer in a chain
|
||||
func (s *Unix) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
|
||||
laddru := s.addru + "_" + addr
|
||||
os.Remove(laddru)
|
||||
|
||||
luaddru, err := net.ResolveUnixAddr("unixgram", laddru)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
pc, err := net.ListenUnixgram("unixgram", luaddru)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return &PktConn{pc, laddru, luaddru, s.uaddru}, s.uaddru, nil
|
||||
}
|
||||
|
||||
// PktConn .
|
||||
type PktConn struct {
|
||||
*net.UnixConn
|
||||
addr string
|
||||
uaddr *net.UnixAddr
|
||||
writeAddr *net.UnixAddr
|
||||
}
|
||||
|
||||
// ReadFrom overrides the original function from net.PacketConn.
|
||||
func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
n, _, err := pc.UnixConn.ReadFrom(b)
|
||||
return n, pc.uaddr, err
|
||||
}
|
||||
|
||||
// WriteTo overrides the original function from net.PacketConn.
|
||||
func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
return pc.UnixConn.WriteTo(b, pc.writeAddr)
|
||||
}
|
||||
|
||||
// Close overrides the original function from net.PacketConn.
|
||||
func (pc *PktConn) Close() error {
|
||||
pc.UnixConn.Close()
|
||||
os.Remove(pc.addr)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user